From 08fc7226190e2e351d555b3070c148513d85ac22 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet Date: Mon, 25 Mar 2019 19:06:06 +0100 Subject: [PATCH] whisper: fix unit tests --- whisper/whisperv6/envelope.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/whisper/whisperv6/envelope.go b/whisper/whisperv6/envelope.go index 5b4df31092..5d9d350811 100644 --- a/whisper/whisperv6/envelope.go +++ b/whisper/whisperv6/envelope.go @@ -27,7 +27,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/rlp" @@ -82,7 +81,7 @@ func (e *Envelope) Seal(options *MessageParams) error { return nil } - var target, bestBit int + var target, bestLeadingZeros int if options.PoW < 0 { // target is not set - the function should run for a period // of time specified in WorkTime param. Since we can predict @@ -101,10 +100,10 @@ func (e *Envelope) Seal(options *MessageParams) error { for i := 0; i < 1024; i++ { binary.BigEndian.PutUint64(buf[56:], nonce) d := new(big.Int).SetBytes(crypto.Keccak256(buf)) - firstBit := math.FirstBitSet(d) - if firstBit > bestBit { - e.Nonce, bestBit = nonce, firstBit - if target > 0 && bestBit >= target { + leadingZeros := 256 - d.BitLen() + if leadingZeros > bestLeadingZeros { + e.Nonce, bestLeadingZeros = nonce, leadingZeros + if target > 0 && bestLeadingZeros >= target { return nil } } @@ -112,7 +111,7 @@ func (e *Envelope) Seal(options *MessageParams) error { } } - if target > 0 && bestBit < target { + if target > 0 && bestLeadingZeros < target { return fmt.Errorf("failed to reach the PoW target, specified pow time (%d seconds) was insufficient", options.WorkTime) }