aboutsummaryrefslogtreecommitdiffstats
path: root/whisper
diff options
context:
space:
mode:
authorGuillaume Ballet <gballet@gmail.com>2019-06-25 18:01:34 +0800
committerGitHub <noreply@github.com>2019-06-25 18:01:34 +0800
commit54fd263b400d1233fb69759c1b6fb5db9e628914 (patch)
tree145469be11ec9cf5a56bf715fe7babf98050d379 /whisper
parent2ca89ea47902adcc0f7dab40e897c23265740fd7 (diff)
downloadgo-tangerine-54fd263b400d1233fb69759c1b6fb5db9e628914.tar
go-tangerine-54fd263b400d1233fb69759c1b6fb5db9e628914.tar.gz
go-tangerine-54fd263b400d1233fb69759c1b6fb5db9e628914.tar.bz2
go-tangerine-54fd263b400d1233fb69759c1b6fb5db9e628914.tar.lz
go-tangerine-54fd263b400d1233fb69759c1b6fb5db9e628914.tar.xz
go-tangerine-54fd263b400d1233fb69759c1b6fb5db9e628914.tar.zst
go-tangerine-54fd263b400d1233fb69759c1b6fb5db9e628914.zip
whisper: PoW calculations as specified in EIP-627 (#19753)
* whisper: PoW calculations as specified in EIP-627 * Fix unit tests
Diffstat (limited to 'whisper')
-rw-r--r--whisper/whisperv6/envelope.go21
-rw-r--r--whisper/whisperv6/envelope_test.go6
2 files changed, 14 insertions, 13 deletions
diff --git a/whisper/whisperv6/envelope.go b/whisper/whisperv6/envelope.go
index 5d9d35081..5b6925edb 100644
--- a/whisper/whisperv6/envelope.go
+++ b/whisper/whisperv6/envelope.go
@@ -91,16 +91,18 @@ func (e *Envelope) Seal(options *MessageParams) error {
target = e.powToFirstBit(options.PoW)
}
- buf := make([]byte, 64)
- h := crypto.Keccak256(e.rlpWithoutNonce())
- copy(buf[:32], h)
+ rlp := e.rlpWithoutNonce()
+ buf := make([]byte, len(rlp)+8)
+ copy(buf, rlp)
+ asAnInt := new(big.Int)
finish := time.Now().Add(time.Duration(options.WorkTime) * time.Second).UnixNano()
for nonce := uint64(0); time.Now().UnixNano() < finish; {
for i := 0; i < 1024; i++ {
- binary.BigEndian.PutUint64(buf[56:], nonce)
- d := new(big.Int).SetBytes(crypto.Keccak256(buf))
- leadingZeros := 256 - d.BitLen()
+ binary.BigEndian.PutUint64(buf[len(rlp):], nonce)
+ h := crypto.Keccak256(buf)
+ asAnInt.SetBytes(h)
+ leadingZeros := 256 - asAnInt.BitLen()
if leadingZeros > bestLeadingZeros {
e.Nonce, bestLeadingZeros = nonce, leadingZeros
if target > 0 && bestLeadingZeros >= target {
@@ -128,11 +130,10 @@ func (e *Envelope) PoW() float64 {
}
func (e *Envelope) calculatePoW(diff uint32) {
- buf := make([]byte, 64)
rlp := e.rlpWithoutNonce()
- h := crypto.Keccak256(rlp)
- copy(buf[:32], h)
- binary.BigEndian.PutUint64(buf[56:], e.Nonce)
+ buf := make([]byte, len(rlp)+8)
+ copy(buf, rlp)
+ binary.BigEndian.PutUint64(buf[len(rlp):], e.Nonce)
powHash := new(big.Int).SetBytes(crypto.Keccak256(buf))
leadingZeroes := 256 - powHash.BitLen()
x := gmath.Pow(2, float64(leadingZeroes))
diff --git a/whisper/whisperv6/envelope_test.go b/whisper/whisperv6/envelope_test.go
index d6e38e683..c0bb4373b 100644
--- a/whisper/whisperv6/envelope_test.go
+++ b/whisper/whisperv6/envelope_test.go
@@ -43,12 +43,12 @@ func TestPoWCalculationsWith8LeadingZeros(t *testing.T) {
e := Envelope{
TTL: 1,
Data: []byte{0xde, 0xad, 0xbe, 0xef},
- Nonce: 48159,
+ Nonce: 276,
}
e.calculatePoW(0)
- if e.pow != 40329.846153846156 {
- t.Fatalf("invalid PoW calculation. Expected 0.07692307692307693, got %v", e.pow)
+ if e.pow != 19.692307692307693 {
+ t.Fatalf("invalid PoW calculation. Expected 19.692307692307693, got %v", e.pow)
}
}