From bdd771aed9035132f696eb3fb1bdef566525991d Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 2 Aug 2023 23:32:27 +0800 Subject: [PATCH] refactor builder tests --- builder_test.go | 17 ++++++++--------- mainnet_tests/builder_test.go | 3 +-- test_helpers/builder.go | 21 +++++++++++++-------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/builder_test.go b/builder_test.go index bdce724..e17be82 100644 --- a/builder_test.go +++ b/builder_test.go @@ -27,7 +27,6 @@ import ( "github.com/ethereum/go-ethereum/rlp" statediff "github.com/cerc-io/plugeth-statediff" - "github.com/cerc-io/plugeth-statediff/adapt" "github.com/cerc-io/plugeth-statediff/indexer/ipld" "github.com/cerc-io/plugeth-statediff/indexer/shared" "github.com/cerc-io/plugeth-statediff/test_helpers" @@ -796,7 +795,7 @@ func TestBuilder(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block0: bankAccountAtBlock0LeafNode, block1: block1BranchRootNode, @@ -1010,7 +1009,7 @@ func TestBuilderWithWatchedAddressList(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), tests, params, test_helpers.CheckedRoots{ + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block0: bankAccountAtBlock0LeafNode, block1: block1BranchRootNode, block2: block2BranchRootNode, @@ -1260,7 +1259,7 @@ func TestBuilderWithRemovedAccountAndStorage(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), tests, params, test_helpers.CheckedRoots{ + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block4: block4BranchRootNode, block5: block5BranchRootNode, block6: block6BranchRootNode, @@ -1394,7 +1393,7 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), tests, params, test_helpers.CheckedRoots{ + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block4: block4BranchRootNode, block5: block5BranchRootNode, block6: block6BranchRootNode, @@ -1597,7 +1596,7 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), tests, params, test_helpers.CheckedRoots{ + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block4: block4BranchRootNode, block5: block5BranchRootNode, block6: block6BranchRootNode, @@ -1824,7 +1823,7 @@ func TestBuilderWithMovedAccount(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), tests, params, test_helpers.CheckedRoots{ + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block1: block01BranchRootNode, block2: bankAccountAtBlock02LeafNode, }) @@ -2350,7 +2349,7 @@ func TestBuilderWithInternalizedLeafNode(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), tests, params, test_helpers.CheckedRoots{ + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block1: block1bBranchRootNode, block2: block2bBranchRootNode, block3: block3bBranchRootNode, @@ -2551,7 +2550,7 @@ func TestBuilderWithInternalizedLeafNodeAndWatchedAddress(t *testing.T) { }, } - test_helpers.RunBuilderTests(t, statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), tests, params, test_helpers.CheckedRoots{ + test_helpers.RunBuilderTests(t, chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block1: block1bBranchRootNode, block2: block2bBranchRootNode, block3: block3bBranchRootNode, diff --git a/mainnet_tests/builder_test.go b/mainnet_tests/builder_test.go index f31ca90..6299338 100644 --- a/mainnet_tests/builder_test.go +++ b/mainnet_tests/builder_test.go @@ -35,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/rlp" statediff "github.com/cerc-io/plugeth-statediff" - "github.com/cerc-io/plugeth-statediff/adapt" "github.com/cerc-io/plugeth-statediff/indexer/ipld" "github.com/cerc-io/plugeth-statediff/test_helpers" sdtypes "github.com/cerc-io/plugeth-statediff/types" @@ -659,7 +658,7 @@ func TestBuilderOnMainnetBlocks(t *testing.T) { } test_helpers.RunBuilderTests(t, - statediff.NewBuilder(adapt.GethStateView(chain.StateCache())), + chain.StateCache(), tests, params, test_helpers.CheckedRoots{ block1: block1RootBranchNode, block2: block2RootBranchNode, diff --git a/test_helpers/builder.go b/test_helpers/builder.go index 12be63b..f0d62be 100644 --- a/test_helpers/builder.go +++ b/test_helpers/builder.go @@ -6,7 +6,9 @@ import ( "testing" statediff "github.com/cerc-io/plugeth-statediff" + "github.com/cerc-io/plugeth-statediff/adapt" sdtypes "github.com/cerc-io/plugeth-statediff/types" + "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/require" @@ -22,20 +24,23 @@ type CheckedRoots = map[*types.Block][]byte func RunBuilderTests( t *testing.T, - builder statediff.Builder, + sdb state.Database, tests []TestCase, params statediff.Params, roots CheckedRoots, ) { + builder := statediff.NewBuilder(adapt.GethStateView(sdb)) for _, test := range tests { - diff, err := builder.BuildStateDiffObject(test.Args, params) - if err != nil { - t.Error(err) - } + t.Run(test.Name, func(t *testing.T) { + diff, err := builder.BuildStateDiffObject(test.Args, params) + if err != nil { + t.Error(err) + } - normalize(test.Expected) - normalize(&diff) - require.Equal(t, *test.Expected, diff, test.Name) + normalize(test.Expected) + normalize(&diff) + require.Equal(t, *test.Expected, diff) + }) } // Let's also confirm that our root state nodes form the state root hash in the headers for block, node := range roots {