Skip to content

Commit

Permalink
Add UncleHash tests, fill in empty fields for woTestData
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Nov 21, 2024
1 parent 12ed62b commit 9e13bf7
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions core/types/wo_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"fmt"
"math/big"
"testing"

Expand All @@ -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))
Expand All @@ -28,16 +29,20 @@ func woTestData() (*WorkObject, common.Hash) {
return wo, wo.Hash()
}

var (
expectedWoHash = common.HexToHash("0xd8e3ef0d1804c06495b219308535844169ccfdbd8565770077ea12f928fc8000")
expectedUncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
)

func TestWoHash(t *testing.T) {
_, actualHash := woTestData()
expectedHash := common.HexToHash("0x1bfdf14daa1844a372da6f2a2024d2fcc2bf92feb01d11b4e02f88d17ee9e9f8")
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")
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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("0x67c4f50242b43f752a32574a28633ac08d316fdbd786ccdc675e90887643adc2"),
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)
Expand Down

0 comments on commit 9e13bf7

Please sign in to comment.