consensus/clique: unduplicate header encoding

This commit is contained in:
Felix Lange 2019-02-05 00:11:34 +01:00
parent 632d73fb60
commit 4fa91b7a6a

View file

@ -20,6 +20,7 @@ package clique
import ( import (
"bytes" "bytes"
"errors" "errors"
"io"
"math/big" "math/big"
"math/rand" "math/rand"
"sync" "sync"
@ -665,30 +666,6 @@ func (c *Clique) SealHash(header *types.Header) common.Hash {
return SealHash(header) 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. // Close implements consensus.Engine. It's a noop for clique as there are no background threads.
func (c *Clique) Close() error { func (c *Clique) Close() error {
return nil 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 // 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 // sealing. The RLP to sign consists of the entire header apart from the 65 byte signature
// contained at the end of the extra data. // 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 // 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. // or not), which could be abused to produce different hashes for the same header.
func CliqueRLP(header *types.Header) []byte { 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.ParentHash,
header.UncleHash, header.UncleHash,
header.Coinbase, header.Coinbase,
@ -730,5 +721,7 @@ func CliqueRLP(header *types.Header) []byte {
header.MixDigest, header.MixDigest,
header.Nonce, header.Nonce,
}) })
return data if err != nil {
panic("can't encode: " + err.Error())
}
} }