diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 6c7a871dee..c0f78ce655 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -20,6 +20,7 @@ package clique import ( "bytes" "errors" + "io" "math/big" "math/rand" "sync" @@ -665,30 +666,6 @@ func (c *Clique) SealHash(header *types.Header) common.Hash { return SealHash(header) } -// SealHash returns the hash of a block prior to it being sealed. -func SealHash(header *types.Header) (hash common.Hash) { - hasher := sha3.NewLegacyKeccak256() - rlp.Encode(hasher, []interface{}{ - header.ParentHash, - header.UncleHash, - header.Coinbase, - header.Root, - header.TxHash, - header.ReceiptHash, - header.Bloom, - header.Difficulty, - header.Number, - header.GasLimit, - header.GasUsed, - header.Time, - header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short - header.MixDigest, - header.Nonce, - }) - hasher.Sum(hash[:0]) - return hash -} - // Close implements consensus.Engine. It's a noop for clique as there are no background threads. func (c *Clique) Close() error { return nil @@ -705,6 +682,14 @@ func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API { }} } +// SealHash returns the hash of a block prior to it being sealed. +func SealHash(header *types.Header) (hash common.Hash) { + hasher := sha3.NewLegacyKeccak256() + encodeSigHeader(hasher, header) + hasher.Sum(hash[:0]) + return hash +} + // CliqueRLP returns the rlp bytes which needs to be signed for the proof-of-authority // sealing. The RLP to sign consists of the entire header apart from the 65 byte signature // contained at the end of the extra data. @@ -713,7 +698,13 @@ func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API { // panics. This is done to avoid accidentally using both forms (signature present // or not), which could be abused to produce different hashes for the same header. func CliqueRLP(header *types.Header) []byte { - data, _ := rlp.EncodeToBytes([]interface{}{ + b := new(bytes.Buffer) + encodeSigHeader(b, header) + return b.Bytes() +} + +func encodeSigHeader(w io.Writer, header *types.Header) { + err := rlp.Encode(w, []interface{}{ header.ParentHash, header.UncleHash, header.Coinbase, @@ -730,5 +721,7 @@ func CliqueRLP(header *types.Header) []byte { header.MixDigest, header.Nonce, }) - return data + if err != nil { + panic("can't encode: " + err.Error()) + } }