Skip to content

Commit

Permalink
core: add forkchoice tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manav2401 committed Oct 18, 2023
1 parent 6b2e666 commit 3478674
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions core/forkchoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"

"github.com/stretchr/testify/require"
)

// chainValidatorFake is a mock for the chain validator service
Expand All @@ -30,6 +32,62 @@ func newChainReaderFake(getTd func(hash common.Hash, number uint64) *big.Int) *c
return &chainReaderFake{getTd: getTd}
}

// nolint: tparallel
func TestForkChoice(t *testing.T) {
t.Parallel()

// Create mocks for forker
getTd := func(hash common.Hash, number uint64) *big.Int {
if number <= 2 {
return big.NewInt(int64(number))
}

return big.NewInt(0)
}
mockChainReader := newChainReaderFake(getTd)
mockForker := NewForkChoice(mockChainReader, nil, nil)

createHeader := func(number int64, extra []byte) *types.Header {
return &types.Header{
Number: big.NewInt(number),
Extra: extra,
}
}

// Create headers for different cases
headerA := createHeader(1, []byte("A"))
headerB := createHeader(2, []byte("B"))
headerC := createHeader(3, []byte("C"))
headerD := createHeader(4, []byte("D")) // 0x96b0f70c01f4d2b1ee2df5b0202c099776f24c9375ffc89d94b880007633961b (hash)
headerE := createHeader(4, []byte("E")) // 0xdc0acf54354ff86194baeaab983098a49a40218cffcc77a583726fc06c429685 (hash)

// Create test cases
testCases := []struct {
name string
current *types.Header
incoming *types.Header
want bool
}{
{"tdd(incoming) > tdd(current)", headerA, headerB, true},
{"tdd(current) > tdd(incoming)", headerB, headerA, false},
{"tdd(current) = tdd(incoming), number(incoming) > number(current)", headerC, headerD, false},
{"tdd(current) = tdd(incoming), number(current) > number(incoming)", headerD, headerC, true},
{"tdd(current) = tdd(incoming), number(current) = number(incoming), hash(current) > hash(incoming)", headerE, headerD, false},
{"tdd(current) = tdd(incoming), number(current) = number(incoming), hash(incoming) > hash(current)", headerD, headerE, true},
}
_ = testCases

// nolint: paralleltest
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
res, err := mockForker.ReorgNeeded(tc.current, tc.incoming)
require.Equal(t, tc.want, res, tc.name)
require.NoError(t, err, tc.name)
})
}
}

func TestPastChainInsert(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 3478674

Please sign in to comment.