From 1bb314f767785fe617c3c5efeca1a64127339506 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 4 Aug 2020 10:47:20 -0400 Subject: include extra block data into hash calculation --- core/types/block.go | 4 ++++ examples/block/main.go | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 6fa710a..b3dbcd3 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -85,6 +85,7 @@ type Header struct { Extra []byte `json:"extraData" gencodec:"required"` MixDigest common.Hash `json:"mixHash"` Nonce BlockNonce `json:"nonce"` + ExtDataHash common.Hash `json:"extDataHash" gencodec:"required"` } // field type overrides for gencodec @@ -288,12 +289,15 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error { b.header, b.uncles, b.transactions = eb.Header, eb.Uncles, eb.Txs b.extdata = nil } + b.hash = atomic.Value{} b.size.Store(common.StorageSize(rlp.ListSize(size))) return nil } func (b *Block) SetExtraData(data []byte) { b.extdata = data + b.header.ExtDataHash = rlpHash(data) + b.hash = atomic.Value{} } func (b *Block) ExtraData() []byte { diff --git a/examples/block/main.go b/examples/block/main.go index e5349e0..45d7586 100644 --- a/examples/block/main.go +++ b/examples/block/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "crypto/rand" "fmt" "github.com/ava-labs/coreth" "github.com/ava-labs/coreth/core" @@ -11,6 +10,7 @@ import ( "github.com/ava-labs/coreth/params" "github.com/ava-labs/go-ethereum/common" "github.com/ava-labs/go-ethereum/common/hexutil" + "github.com/ava-labs/go-ethereum/crypto" "github.com/ava-labs/go-ethereum/rlp" "math/big" ) @@ -43,7 +43,9 @@ func main() { // configure the genesis block genBalance := big.NewInt(100000000000000000) - genKey, _ := coreth.NewKey(rand.Reader) + hk, _ := crypto.HexToECDSA( + "abd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1") + genKey := coreth.NewKeyFromECDSA(hk) config.Genesis = &core.Genesis{ Config: chainConfig, @@ -108,11 +110,11 @@ func main() { checkError(err) buff.WriteString("somesuffix") fmt.Println(buff.Len()) - fmt.Println(common.ToHex(buff.Bytes())) + fmt.Println(blk2.Hash().Hex()) err = rlp.Decode(buff, blk2) - fmt.Println(buff.Len(), (string)(blk2.ExtraData())) checkError(err) + fmt.Println(buff.Len(), (string)(blk2.ExtraData()), blk2.Hash().Hex()) decoded1 := new(string) err = rlp.DecodeBytes(blk2.ExtraData(), decoded1) checkError(err) @@ -138,11 +140,12 @@ func main() { blk2.SetExtraData(extra) err = blk2.EncodeRLPTest(buff, 0xfffffffe) checkError(err) + fmt.Println(blk2.Hash().Hex()) err = rlp.Decode(buff, blk2) checkError(err) decoded2 := new(MyData) err = rlp.DecodeBytes(blk2.ExtraData(), decoded2) checkError(err) - fmt.Println(buff.Len(), decoded2) + fmt.Println(buff.Len(), decoded2, blk2.Hash().Hex()) buff.Reset() } -- cgit v1.2.3-70-g09d2