From 3e04fc6b547a1520bc7044165162c4f1fa8a0032 Mon Sep 17 00:00:00 2001 From: Hussam Date: Tue, 19 Nov 2024 16:35:52 -0600 Subject: [PATCH] Commit to MixHash in header, update tests --- core/types/block.go | 6 +++++- core/types/wo.go | 12 +++++++----- core/types/wo_test.go | 22 ++++++++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 4a3172481b..cb4ffc43c5 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -35,6 +35,10 @@ import ( "github.com/dominant-strategies/go-quai/rlp" ) +const ( + NonceLength = 8 +) + var ( EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") EmptyUncleHash = RlpHash([]*Header(nil)) @@ -48,7 +52,7 @@ var ( // A BlockNonce is a 64-bit hash which proves (combined with the // mix-hash) that a sufficient amount of computation has been carried // out on a block. -type BlockNonce [8]byte +type BlockNonce [NonceLength]byte // EncodeNonce converts the given integer to a block nonce. func EncodeNonce(i uint64) BlockNonce { diff --git a/core/types/wo.go b/core/types/wo.go index 1f8ee03429..bded301183 100644 --- a/core/types/wo.go +++ b/core/types/wo.go @@ -794,6 +794,7 @@ func (wo *WorkObject) WithBody(header *Header, txs []*Transaction, etxs []*Trans func EmptyWorkObjectBody() *WorkObjectBody { woBody := &WorkObjectBody{} + woBody.SetHeader(EmptyHeader()) woBody.SetTransactions([]*Transaction{}) woBody.SetOutboundEtxs([]*Transaction{}) return woBody @@ -1082,9 +1083,10 @@ func (wh *WorkObjectHeader) Hash() (hash common.Hash) { hasherMu.Lock() defer hasherMu.Unlock() hasher.Reset() - var hData [40]byte - copy(hData[:], wh.Nonce().Bytes()) - copy(hData[len(wh.nonce):], sealHash) + var hData [common.HashLength + common.HashLength + NonceLength]byte + copy(hData[:], wh.MixHash().Bytes()) + copy(hData[common.HashLength:], sealHash) + copy(hData[common.HashLength+common.HashLength:], wh.Nonce().Bytes()) sum := blake3.Sum256(hData[:]) hash.SetBytes(sum[:]) return hash @@ -1106,7 +1108,7 @@ func (wh *WorkObjectHeader) SealHash() (hash common.Hash) { func (wh *WorkObjectHeader) SealEncode() *ProtoWorkObjectHeader { // Omit MixHash and PowHash - hash := common.ProtoHash{Value: wh.HeaderHash().Bytes()} + headerHash := common.ProtoHash{Value: wh.HeaderHash().Bytes()} parentHash := common.ProtoHash{Value: wh.ParentHash().Bytes()} txHash := common.ProtoHash{Value: wh.TxHash().Bytes()} number := wh.Number().Bytes() @@ -1118,7 +1120,7 @@ func (wh *WorkObjectHeader) SealEncode() *ProtoWorkObjectHeader { coinbase := common.ProtoAddress{Value: wh.PrimaryCoinbase().Bytes()} return &ProtoWorkObjectHeader{ - HeaderHash: &hash, + HeaderHash: &headerHash, ParentHash: &parentHash, Number: number, Difficulty: difficulty, diff --git a/core/types/wo_test.go b/core/types/wo_test.go index 6884814957..39618ba4ef 100644 --- a/core/types/wo_test.go +++ b/core/types/wo_test.go @@ -12,7 +12,7 @@ func woTestData() (*WorkObject, common.Hash) { wo := &WorkObject{ woHeader: &WorkObjectHeader{ headerHash: common.HexToHash("0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0"), - parentHash: common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1"), + parentHash: common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef12"), number: big.NewInt(1), difficulty: big.NewInt(123456789), primeTerminusNumber: big.NewInt(42), @@ -24,16 +24,24 @@ func woTestData() (*WorkObject, common.Hash) { nonce: EncodeNonce(uint64(1)), lock: 0, }, + woBody: EmptyWorkObjectBody(), } return wo, wo.Hash() } func TestWoHash(t *testing.T) { _, actualHash := woTestData() - expectedHash := common.HexToHash("0x572273f26629ca2a02c329cd21dc3def6e1d1934465ea11fca393c7a9ce3c89b") + expectedHash := common.HexToHash("0x5699348bb74873668408253dc3c518e1570eec73516dced58cbddcf294d2f477") require.Equal(t, expectedHash, actualHash, "Hash not equal to expected hash") } +func TestWoSealHash(t *testing.T) { + testWo, _ := woTestData() + actualHash := testWo.SealHash() + expectedHash := common.HexToHash("0x8ea659d6d9e1da464ae1a505cc19b1fe5f3df1e7013168227be1c290b3edaf01") + require.Equal(t, expectedHash, actualHash, "Seal hash not equal to expected hash") +} + func FuzzHeaderHash(f *testing.F) { fuzzHash(f, func(woh *WorkObjectHeader) common.Hash { return woh.headerHash }, @@ -60,8 +68,14 @@ func FuzzNumberHash(f *testing.F) { func FuzzTxHash(f *testing.F) { fuzzHash(f, - func(woh *WorkObjectHeader) common.Hash { return woh.txHash }, - func(woh *WorkObjectHeader, hash common.Hash) { woh.txHash = hash }) + func(woh *WorkObjectHeader) common.Hash { return woh.TxHash() }, + func(woh *WorkObjectHeader, hash common.Hash) { woh.SetTxHash(hash)}) +} + +func FuzzMixHash(f *testing.F) { + fuzzHash(f, + func(woh *WorkObjectHeader) common.Hash { return woh.MixHash() }, + func(woh *WorkObjectHeader, hash common.Hash) { woh.SetMixHash(hash) }) } func TestLocationHash(t *testing.T) {