diff --git a/core/types/wo_test.go b/core/types/wo_test.go index c3a4ed2b0..741f777a8 100644 --- a/core/types/wo_test.go +++ b/core/types/wo_test.go @@ -1,6 +1,7 @@ package types import ( + "fmt" "math/big" "testing" @@ -11,8 +12,8 @@ import ( func woTestData() (*WorkObject, common.Hash) { wo := &WorkObject{} wo.SetWorkObjectHeader(&WorkObjectHeader{}) - wo.woHeader.SetHeaderHash(common.HexToHash("0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0")) - wo.woHeader.SetParentHash(common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef12")) + wo.woHeader.SetHeaderHash(EmptyHeader().Hash()) + wo.woHeader.SetParentHash(EmptyHeader().Hash()) wo.woHeader.SetNumber(big.NewInt(1)) wo.woHeader.SetDifficulty(big.NewInt(123456789)) wo.woHeader.SetPrimeTerminusNumber(big.NewInt(42)) @@ -28,16 +29,20 @@ func woTestData() (*WorkObject, common.Hash) { return wo, wo.Hash() } +var ( + expectedWoHash = common.HexToHash("0x7a107825db4e54d262248b6d619679a94e8235a2ae01fd06b4e3819179b81d3a") + expectedUncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347") +) + func TestWoHash(t *testing.T) { _, actualHash := woTestData() - expectedHash := common.HexToHash("0x6c0e69beaec348d6323fc1d8bfdf2343ce5f085dcfd327fedd2333129bf452fc") - require.Equal(t, expectedHash, actualHash, "Hash not equal to expected hash") + require.Equal(t, expectedWoHash, actualHash, "Hash not equal to expected hash") } func TestWoSealHash(t *testing.T) { testWo, _ := woTestData() actualHash := testWo.SealHash() - expectedHash := common.HexToHash("0x8ea659d6d9e1da464ae1a505cc19b1fe5f3df1e7013168227be1c290b3edaf01") + expectedHash := common.HexToHash("0x83fd7f1dbd2f62d320c2dd974e2d1b3ce8108594a7c9e3aa99dfd993ca106090") require.Equal(t, expectedHash, actualHash, "Seal hash not equal to expected hash") } @@ -68,7 +73,7 @@ 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.SetTxHash(hash)}) + func(woh *WorkObjectHeader, hash common.Hash) { woh.SetTxHash(hash) }) } func FuzzMixHash(f *testing.F) { @@ -117,6 +122,61 @@ func FuzzNonceHash(f *testing.F) { func(woh *WorkObjectHeader, nonce uint64) { woh.nonce = EncodeNonce(nonce) }) } +func TestCalcUncleHash(t *testing.T) { + tests := []struct { + uncleNum int + expectedUncleHash common.Hash + expectedWoHash common.Hash + shouldPass bool + }{ + { + uncleNum: 0, + expectedUncleHash: expectedUncleHash, + expectedWoHash: expectedWoHash, + shouldPass: true, + }, + { + uncleNum: 1, + expectedUncleHash: expectedUncleHash, + expectedWoHash: expectedWoHash, + shouldPass: false, + }, + { + uncleNum: 5, + expectedUncleHash: common.HexToHash("0x3c9dd26495f9a6ddf36e1443bee2ff0a3bb59b0722a765b145d01ab1f78ccd44"), + expectedWoHash: common.HexToHash("0x0cf899993d6dd984fdc862a159d2740562c266712d5505168480bade496816b2"), + shouldPass: true, + }, + } + + // Run test cases + for _, tt := range tests { + t.Run(fmt.Sprintf("uncleNum=%d", tt.uncleNum), func(t *testing.T) { + assertUncleHash(t, tt.uncleNum, tt.expectedUncleHash, tt.expectedWoHash, tt.shouldPass) + }) + } +} + +func assertUncleHash(t *testing.T, uncleNum int, expectedUncleHash common.Hash, expectedWoHash common.Hash, shouldPass bool) { + wo, _ := woTestData() + wo.Body().uncles = make([]*WorkObjectHeader, uncleNum) + for i := 0; i < uncleNum; i++ { + uncle, _ := woTestData() + wo.Body().uncles[i] = CopyWorkObjectHeader(uncle.WorkObjectHeader()) + } + + wo.Body().Header().SetUncleHash(CalcUncleHash(wo.Body().uncles)) + wo.woHeader.SetHeaderHash(wo.Body().header.Hash()) + + if shouldPass { + require.Equal(t, expectedUncleHash, wo.Header().UncleHash(), "Uncle hashes do not create the expected root hash") + require.Equal(t, expectedWoHash, wo.Hash(), "Uncle hashes do not create the expected WorkObject hash") + } else { + require.NotEqual(t, expectedUncleHash, wo.Header().UncleHash(), "Uncle hashes do not create the expected root hash") + require.NotEqual(t, expectedWoHash, wo.Hash(), "Uncle hashes do not create the expected WorkObject hash") + } +} + func fuzzHash(f *testing.F, getField func(*WorkObjectHeader) common.Hash, setField func(*WorkObjectHeader, common.Hash)) { wo, _ := woTestData() f.Add(testByte)