github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/consensus/parliautil/parliautil.go (about)

     1  package parliautil
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/fff-chain/go-fff/common"
     6  	"github.com/fff-chain/go-fff/core/types"
     7  	"github.com/fff-chain/go-fff/rlp"
     8  	"golang.org/x/crypto/sha3"
     9  	"io"
    10  	"math/big"
    11  )
    12  
    13  // ===========================     utility function        ==========================
    14  // SealHash returns the hash of a block prior to it being sealed.
    15  func SealHash(header *types.Header, chainId *big.Int) (hash common.Hash) {
    16  	hasher := sha3.NewLegacyKeccak256()
    17  	encodeSigHeader(hasher, header, chainId)
    18  	hasher.Sum(hash[:0])
    19  	return hash
    20  }
    21  
    22  // ParliaRLP returns the rlp bytes which needs to be signed for the parlia
    23  // sealing. The RLP to sign consists of the entire header apart from the 65 byte signature
    24  // contained at the end of the extra data.
    25  //
    26  // Note, the method requires the extra data to be at least 65 bytes, otherwise it
    27  // panics. This is done to avoid accidentally using both forms (signature present
    28  // or not), which could be abused to produce different hashes for the same header.
    29  func ParliaRLP(header *types.Header, chainId *big.Int) []byte {
    30  	b := new(bytes.Buffer)
    31  	encodeSigHeader(b, header, chainId)
    32  	return b.Bytes()
    33  }
    34  
    35  func encodeSigHeader(w io.Writer, header *types.Header, chainId *big.Int) {
    36  	err := rlp.Encode(w, []interface{}{
    37  		chainId,
    38  		header.ParentHash,
    39  		header.UncleHash,
    40  		header.Coinbase,
    41  		header.Root,
    42  		header.TxHash,
    43  		header.ReceiptHash,
    44  		header.Bloom,
    45  		header.Difficulty,
    46  		header.Number,
    47  		header.GasLimit,
    48  		header.GasUsed,
    49  		header.Time,
    50  		header.Extra[:len(header.Extra)-65], // this will panic if extra is too short, should check before calling encodeSigHeader
    51  		header.MixDigest,
    52  		header.Nonce,
    53  	})
    54  	if err != nil {
    55  		panic("can't encode: " + err.Error())
    56  	}
    57  }