|
|
@ -91,16 +91,18 @@ func (e *Envelope) Seal(options *MessageParams) error { |
|
|
|
target = e.powToFirstBit(options.PoW) |
|
|
|
target = e.powToFirstBit(options.PoW) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
buf := make([]byte, 64) |
|
|
|
rlp := e.rlpWithoutNonce() |
|
|
|
h := crypto.Keccak256(e.rlpWithoutNonce()) |
|
|
|
buf := make([]byte, len(rlp)+8) |
|
|
|
copy(buf[:32], h) |
|
|
|
copy(buf, rlp) |
|
|
|
|
|
|
|
asAnInt := new(big.Int) |
|
|
|
|
|
|
|
|
|
|
|
finish := time.Now().Add(time.Duration(options.WorkTime) * time.Second).UnixNano() |
|
|
|
finish := time.Now().Add(time.Duration(options.WorkTime) * time.Second).UnixNano() |
|
|
|
for nonce := uint64(0); time.Now().UnixNano() < finish; { |
|
|
|
for nonce := uint64(0); time.Now().UnixNano() < finish; { |
|
|
|
for i := 0; i < 1024; i++ { |
|
|
|
for i := 0; i < 1024; i++ { |
|
|
|
binary.BigEndian.PutUint64(buf[56:], nonce) |
|
|
|
binary.BigEndian.PutUint64(buf[len(rlp):], nonce) |
|
|
|
d := new(big.Int).SetBytes(crypto.Keccak256(buf)) |
|
|
|
h := crypto.Keccak256(buf) |
|
|
|
leadingZeros := 256 - d.BitLen() |
|
|
|
asAnInt.SetBytes(h) |
|
|
|
|
|
|
|
leadingZeros := 256 - asAnInt.BitLen() |
|
|
|
if leadingZeros > bestLeadingZeros { |
|
|
|
if leadingZeros > bestLeadingZeros { |
|
|
|
e.Nonce, bestLeadingZeros = nonce, leadingZeros |
|
|
|
e.Nonce, bestLeadingZeros = nonce, leadingZeros |
|
|
|
if target > 0 && bestLeadingZeros >= target { |
|
|
|
if target > 0 && bestLeadingZeros >= target { |
|
|
@ -128,11 +130,10 @@ func (e *Envelope) PoW() float64 { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (e *Envelope) calculatePoW(diff uint32) { |
|
|
|
func (e *Envelope) calculatePoW(diff uint32) { |
|
|
|
buf := make([]byte, 64) |
|
|
|
|
|
|
|
rlp := e.rlpWithoutNonce() |
|
|
|
rlp := e.rlpWithoutNonce() |
|
|
|
h := crypto.Keccak256(rlp) |
|
|
|
buf := make([]byte, len(rlp)+8) |
|
|
|
copy(buf[:32], h) |
|
|
|
copy(buf, rlp) |
|
|
|
binary.BigEndian.PutUint64(buf[56:], e.Nonce) |
|
|
|
binary.BigEndian.PutUint64(buf[len(rlp):], e.Nonce) |
|
|
|
powHash := new(big.Int).SetBytes(crypto.Keccak256(buf)) |
|
|
|
powHash := new(big.Int).SetBytes(crypto.Keccak256(buf)) |
|
|
|
leadingZeroes := 256 - powHash.BitLen() |
|
|
|
leadingZeroes := 256 - powHash.BitLen() |
|
|
|
x := gmath.Pow(2, float64(leadingZeroes)) |
|
|
|
x := gmath.Pow(2, float64(leadingZeroes)) |
|
|
|