From 1e64f486d8e0671da87caa84013f595c120a9118 Mon Sep 17 00:00:00 2001 From: i-norden Date: Thu, 24 Mar 2022 10:09:48 -0500 Subject: [PATCH] update unit tests --- .../database/file/indexer_legacy_test.go | 11 +- .../indexer/database/file/indexer_test.go | 152 ++++++++------- .../database/sql/indexer_shared_test.go | 2 +- .../database/sql/pgx_indexer_legacy_test.go | 11 +- .../indexer/database/sql/pgx_indexer_test.go | 176 ++++++++++-------- .../indexer/database/sql/postgres/config.go | 6 +- .../database/sql/sqlx_indexer_legacy_test.go | 11 +- .../indexer/database/sql/sqlx_indexer_test.go | 164 ++++++++-------- statediff/indexer/mocks/test_data.go | 6 +- .../indexer/test_helpers/test_helpers.go | 12 -- 10 files changed, 286 insertions(+), 265 deletions(-) diff --git a/statediff/indexer/database/file/indexer_legacy_test.go b/statediff/indexer/database/file/indexer_legacy_test.go index 56bca2683..ed4bcf000 100644 --- a/statediff/indexer/database/file/indexer_legacy_test.go +++ b/statediff/indexer/database/file/indexer_legacy_test.go @@ -33,7 +33,6 @@ import ( "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" "github.com/ethereum/go-ethereum/statediff/indexer/ipld" "github.com/ethereum/go-ethereum/statediff/indexer/mocks" - "github.com/ethereum/go-ethereum/statediff/indexer/test_helpers" ) var ( @@ -71,7 +70,7 @@ func setupLegacy(t *testing.T) { require.NoError(t, err) } - test_helpers.ExpectEqual(t, tx.(*file.BatchTx).BlockNumber, legacyData.BlockNumber.Uint64()) + require.Equal(t, legacyData.BlockNumber.String(), tx.(*file.BatchTx).BlockNumber) connStr := postgres.DefaultConfig.DbConnectionString() @@ -123,10 +122,10 @@ func TestFileIndexerLegacy(t *testing.T) { err = sqlxdb.QueryRowx(pgStr, legacyData.BlockNumber.Uint64()).StructScan(header) require.NoError(t, err) - test_helpers.ExpectEqual(t, header.CID, legacyHeaderCID.String()) - test_helpers.ExpectEqual(t, header.TD, legacyData.MockBlock.Difficulty().String()) - test_helpers.ExpectEqual(t, header.Reward, "5000000000000011250") - test_helpers.ExpectEqual(t, header.Coinbase, legacyData.MockBlock.Coinbase().String()) + require.Equal(t, legacyHeaderCID.String(), header.CID) + require.Equal(t, legacyData.MockBlock.Difficulty().String(), header.TD) + require.Equal(t, "5000000000000011250", header.Reward) + require.Equal(t, legacyData.MockBlock.Coinbase().String(), header.Coinbase) require.Nil(t, legacyData.MockHeader.BaseFee) }) } diff --git a/statediff/indexer/database/file/indexer_test.go b/statediff/indexer/database/file/indexer_test.go index ef849e8e8..bb03c1ec3 100644 --- a/statediff/indexer/database/file/indexer_test.go +++ b/statediff/indexer/database/file/indexer_test.go @@ -191,7 +191,7 @@ func setup(t *testing.T) { require.NoError(t, err) } - test_helpers.ExpectEqual(t, tx.(*file.BatchTx).BlockNumber, mocks.BlockNumber.Uint64()) + require.Equal(t, mocks.BlockNumber.String(), tx.(*file.BatchTx).BlockNumber) connStr := postgres.DefaultConfig.DbConnectionString() @@ -223,10 +223,10 @@ func TestFileIndexer(t *testing.T) { t.Fatal(err) } - test_helpers.ExpectEqual(t, header.CID, headerCID.String()) - test_helpers.ExpectEqual(t, header.TD, mocks.MockBlock.Difficulty().String()) - test_helpers.ExpectEqual(t, header.Reward, "2000000000000021250") - test_helpers.ExpectEqual(t, header.Coinbase, mocks.MockHeader.Coinbase.String()) + require.Equal(t, headerCID.String(), header.CID) + require.Equal(t, mocks.MockBlock.Difficulty().String(), header.TD) + require.Equal(t, "2000000000000021250", header.Reward) + require.Equal(t, mocks.MockHeader.Coinbase.String(), header.Coinbase) dc, err := cid.Decode(header.CID) if err != nil { t.Fatal(err) @@ -238,7 +238,7 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, mocks.MockHeaderRlp) + require.Equal(t, mocks.MockHeaderRlp, data) }) t.Run("Publish and index transaction IPLDs in a single tx", func(t *testing.T) { setup(t) @@ -253,7 +253,7 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(trxs), 5) + require.Equal(t, 5, len(trxs)) expectTrue(t, test_helpers.ListContainsString(trxs, trx1CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx2CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx3CID.String())) @@ -280,7 +280,7 @@ func TestFileIndexer(t *testing.T) { txTypeAndValueStr := `SELECT tx_type, value FROM eth.transaction_cids WHERE cid = $1` switch c { case trx1CID.String(): - test_helpers.ExpectEqual(t, data, tx1) + require.Equal(t, tx1, data) txRes := new(txResult) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { @@ -293,7 +293,7 @@ func TestFileIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[0].Value().String(), txRes.Value) } case trx2CID.String(): - test_helpers.ExpectEqual(t, data, tx2) + require.Equal(t, tx2, data) txRes := new(txResult) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { @@ -306,7 +306,7 @@ func TestFileIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[1].Value().String(), txRes.Value) } case trx3CID.String(): - test_helpers.ExpectEqual(t, data, tx3) + require.Equal(t, tx3, data) txRes := new(txResult) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { @@ -319,7 +319,7 @@ func TestFileIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[2].Value().String(), txRes.Value) } case trx4CID.String(): - test_helpers.ExpectEqual(t, data, tx4) + require.Equal(t, tx4, data) txRes := new(txResult) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { @@ -332,7 +332,9 @@ func TestFileIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value) } accessListElementModels := make([]models.AccessListElementModel, 0) - pgStr = `SELECT access_list_elements.* FROM eth.access_list_elements INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC` + pgStr = "SELECT cast(access_list_elements.block_number AS TEXT), access_list_elements.index, access_list_elements.tx_id, " + + "access_list_elements.address, access_list_elements.storage_keys FROM eth.access_list_elements " + + "INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC" err = sqlxdb.Select(&accessListElementModels, pgStr, c) if err != nil { t.Fatal(err) @@ -341,18 +343,20 @@ func TestFileIndexer(t *testing.T) { t.Fatalf("expected two access list entries, got %d", len(accessListElementModels)) } model1 := models.AccessListElementModel{ - Index: accessListElementModels[0].Index, - Address: accessListElementModels[0].Address, + BlockNumber: mocks.BlockNumber.String(), + Index: accessListElementModels[0].Index, + Address: accessListElementModels[0].Address, } model2 := models.AccessListElementModel{ + BlockNumber: mocks.BlockNumber.String(), Index: accessListElementModels[1].Index, Address: accessListElementModels[1].Address, StorageKeys: accessListElementModels[1].StorageKeys, } - test_helpers.ExpectEqual(t, model1, mocks.AccessListEntry1Model) - test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model) + require.Equal(t, mocks.AccessListEntry1Model, model1) + require.Equal(t, mocks.AccessListEntry2Model, model2) case trx5CID.String(): - test_helpers.ExpectEqual(t, data, tx5) + require.Equal(t, tx5, data) txRes := new(txResult) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { @@ -402,7 +406,7 @@ func TestFileIndexer(t *testing.T) { // expecting MockLog1 and MockLog2 for mockReceipt4 expectedLogs := mocks.MockReceipts[i].Logs - test_helpers.ExpectEqual(t, len(results), len(expectedLogs)) + require.Equal(t, len(expectedLogs), len(results)) var nodeElements []interface{} for idx, r := range results { @@ -413,12 +417,12 @@ func TestFileIndexer(t *testing.T) { logRaw, err := rlp.EncodeToBytes(expectedLogs[idx]) require.NoError(t, err) // 2nd element of the leaf node contains the encoded log data. - test_helpers.ExpectEqual(t, logRaw, nodeElements[1].([]byte)) + require.Equal(t, nodeElements[1].([]byte), logRaw) } else { logRaw, err := rlp.EncodeToBytes(expectedLogs[idx]) require.NoError(t, err) // raw log was IPLDized - test_helpers.ExpectEqual(t, logRaw, r.Data) + require.Equal(t, r.Data, logRaw) } } } @@ -439,7 +443,7 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(rcts), 5) + require.Equal(t, 5, len(rcts)) expectTrue(t, test_helpers.ListContainsString(rcts, rct1CID.String())) expectTrue(t, test_helpers.ListContainsString(rcts, rct2CID.String())) expectTrue(t, test_helpers.ListContainsString(rcts, rct3CID.String())) @@ -465,7 +469,7 @@ func TestFileIndexer(t *testing.T) { expectedRct, err := mocks.MockReceipts[idx].MarshalBinary() require.NoError(t, err) - test_helpers.ExpectEqual(t, expectedRct, nodeElements[1].([]byte)) + require.Equal(t, expectedRct, nodeElements[1].([]byte)) dc, err := cid.Decode(c) if err != nil { @@ -481,46 +485,46 @@ func TestFileIndexer(t *testing.T) { postStatePgStr := `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1` switch c { case rct1CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf1) + require.Equal(t, rctLeaf1, data) var postStatus uint64 pgStr = `SELECT post_status FROM eth.receipt_cids WHERE leaf_cid = $1` err = sqlxdb.Get(&postStatus, pgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postStatus, mocks.ExpectedPostStatus) + require.Equal(t, mocks.ExpectedPostStatus, postStatus) case rct2CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf2) + require.Equal(t, rctLeaf2, data) var postState string err = sqlxdb.Get(&postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState1) + require.Equal(t, mocks.ExpectedPostState1, postState) case rct3CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf3) + require.Equal(t, rctLeaf3, data) var postState string err = sqlxdb.Get(&postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState2) + require.Equal(t, mocks.ExpectedPostState2, postState) case rct4CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf4) + require.Equal(t, rctLeaf4, data) var postState string err = sqlxdb.Get(&postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState3) + require.Equal(t, mocks.ExpectedPostState3, postState) case rct5CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf5) + require.Equal(t, rctLeaf5, data) var postState string err = sqlxdb.Get(&postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState3) + require.Equal(t, mocks.ExpectedPostState3, postState) } } }) @@ -539,7 +543,7 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 2) + require.Equal(t, 2, len(stateNodes)) for _, stateNode := range stateNodes { var data []byte dc, err := cid.Decode(stateNode.CID) @@ -552,39 +556,41 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - pgStr = `SELECT * from eth.state_accounts WHERE header_id = $1 AND state_path = $2` + pgStr = `SELECT cast(block_number AS TEXT), header_id, state_path, cast(balance AS TEXT), nonce, code_hash, storage_root from eth.state_accounts WHERE header_id = $1 AND state_path = $2` var account models.StateAccountModel err = sqlxdb.Get(&account, pgStr, stateNode.HeaderID, stateNode.Path) if err != nil { t.Fatal(err) } if stateNode.CID == state1CID.String() { - test_helpers.ExpectEqual(t, stateNode.NodeType, 2) - test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.ContractLeafKey).Hex()) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x06'}) - test_helpers.ExpectEqual(t, data, mocks.ContractLeafNode) - test_helpers.ExpectEqual(t, account, models.StateAccountModel{ + require.Equal(t, 2, stateNode.NodeType) + require.Equal(t, common.BytesToHash(mocks.ContractLeafKey).Hex(), stateNode.StateKey) + require.Equal(t, []byte{'\x06'}, stateNode.Path) + require.Equal(t, mocks.ContractLeafNode, data) + require.Equal(t, models.StateAccountModel{ + BlockNumber: mocks.BlockNumber.String(), HeaderID: account.HeaderID, StatePath: stateNode.Path, Balance: "0", CodeHash: mocks.ContractCodeHash.Bytes(), StorageRoot: mocks.ContractRoot, Nonce: 1, - }) + }, account) } if stateNode.CID == state2CID.String() { - test_helpers.ExpectEqual(t, stateNode.NodeType, 2) - test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.AccountLeafKey).Hex()) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x0c'}) - test_helpers.ExpectEqual(t, data, mocks.AccountLeafNode) - test_helpers.ExpectEqual(t, account, models.StateAccountModel{ + require.Equal(t, 2, stateNode.NodeType) + require.Equal(t, common.BytesToHash(mocks.AccountLeafKey).Hex(), stateNode.StateKey) + require.Equal(t, []byte{'\x0c'}, stateNode.Path) + require.Equal(t, mocks.AccountLeafNode, data) + require.Equal(t, models.StateAccountModel{ + BlockNumber: mocks.BlockNumber.String(), HeaderID: account.HeaderID, StatePath: stateNode.Path, Balance: "1000", CodeHash: mocks.AccountCodeHash.Bytes(), StorageRoot: mocks.AccountRoot, Nonce: 0, - }) + }, account) } } @@ -597,7 +603,7 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 1) + require.Equal(t, 1, len(stateNodes)) stateNode := stateNodes[0] var data []byte dc, err := cid.Decode(stateNode.CID) @@ -606,14 +612,14 @@ func TestFileIndexer(t *testing.T) { } mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) + require.Equal(t, shared.RemovedNodeMhKey, prefixedKey) err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x02'}) - test_helpers.ExpectEqual(t, data, []byte{}) + require.Equal(t, shared.RemovedNodeStateCID, stateNode.CID) + require.Equal(t, []byte{'\x02'}, stateNode.Path) + require.Equal(t, []byte{}, data) }) t.Run("Publish and index storage IPLDs in a single tx", func(t *testing.T) { @@ -623,7 +629,7 @@ func TestFileIndexer(t *testing.T) { // check that storage nodes were properly indexed storageNodes := make([]models.StorageNodeWithStateKeyModel, 0) - pgStr := `SELECT storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path + pgStr := `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path FROM eth.storage_cids, eth.state_cids, eth.header_cids WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id) AND state_cids.header_id = header_cids.block_hash @@ -633,14 +639,15 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(storageNodes), 1) - test_helpers.ExpectEqual(t, storageNodes[0], models.StorageNodeWithStateKeyModel{ - CID: storageCID.String(), - NodeType: 2, - StorageKey: common.BytesToHash(mocks.StorageLeafKey).Hex(), - StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), - Path: []byte{}, - }) + require.Equal(t, 1, len(storageNodes)) + require.Equal(t, models.StorageNodeWithStateKeyModel{ + BlockNumber: mocks.BlockNumber.String(), + CID: storageCID.String(), + NodeType: 2, + StorageKey: common.BytesToHash(mocks.StorageLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{}, + }, storageNodes[0]) var data []byte dc, err := cid.Decode(storageNodes[0].CID) if err != nil { @@ -652,11 +659,11 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, mocks.StorageLeafNode) + require.Equal(t, mocks.StorageLeafNode, data) // check that Removed storage nodes were properly indexed storageNodes = make([]models.StorageNodeWithStateKeyModel, 0) - pgStr = `SELECT storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path + pgStr = `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path FROM eth.storage_cids, eth.state_cids, eth.header_cids WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id) AND state_cids.header_id = header_cids.block_hash @@ -666,25 +673,26 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(storageNodes), 1) - test_helpers.ExpectEqual(t, storageNodes[0], models.StorageNodeWithStateKeyModel{ - CID: shared.RemovedNodeStorageCID, - NodeType: 3, - StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), - StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), - Path: []byte{'\x03'}, - }) + require.Equal(t, 1, len(storageNodes)) + require.Equal(t, models.StorageNodeWithStateKeyModel{ + BlockNumber: mocks.BlockNumber.String(), + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{'\x03'}, + }, storageNodes[0]) dc, err = cid.Decode(storageNodes[0].CID) if err != nil { t.Fatal(err) } mhKey = dshelp.MultihashToDsKey(dc.Hash()) prefixedKey = blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) + require.Equal(t, shared.RemovedNodeMhKey, prefixedKey) err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, []byte{}) + require.Equal(t, []byte{}, data) }) } diff --git a/statediff/indexer/database/sql/indexer_shared_test.go b/statediff/indexer/database/sql/indexer_shared_test.go index 0351fb134..394ae0731 100644 --- a/statediff/indexer/database/sql/indexer_shared_test.go +++ b/statediff/indexer/database/sql/indexer_shared_test.go @@ -23,7 +23,7 @@ var ( err error ind interfaces.StateDiffIndexer ipfsPgGet = `SELECT data FROM public.blocks - WHERE key = $1` + WHERE key = $1 AND block_number = $2` tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte mockBlock *types.Block headerCID, trx1CID, trx2CID, trx3CID, trx4CID, trx5CID cid.Cid diff --git a/statediff/indexer/database/sql/pgx_indexer_legacy_test.go b/statediff/indexer/database/sql/pgx_indexer_legacy_test.go index 22e695c1d..bb3b36446 100644 --- a/statediff/indexer/database/sql/pgx_indexer_legacy_test.go +++ b/statediff/indexer/database/sql/pgx_indexer_legacy_test.go @@ -27,7 +27,6 @@ import ( "github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres" "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" "github.com/ethereum/go-ethereum/statediff/indexer/ipld" - "github.com/ethereum/go-ethereum/statediff/indexer/test_helpers" ) func setupLegacyPGX(t *testing.T) { @@ -56,7 +55,7 @@ func setupLegacyPGX(t *testing.T) { require.NoError(t, err) } - test_helpers.ExpectEqual(t, tx.(*sql.BatchTx).BlockNumber, legacyData.BlockNumber.Uint64()) + require.Equal(t, legacyData.BlockNumber.String(), tx.(*sql.BatchTx).BlockNumber) } func TestLegacyPGXIndexer(t *testing.T) { @@ -81,10 +80,10 @@ func TestLegacyPGXIndexer(t *testing.T) { &header.CID, &header.TD, &header.Reward, &header.BlockHash, &header.Coinbase) require.NoError(t, err) - test_helpers.ExpectEqual(t, header.CID, legacyHeaderCID.String()) - test_helpers.ExpectEqual(t, header.TD, legacyData.MockBlock.Difficulty().String()) - test_helpers.ExpectEqual(t, header.Reward, "5000000000000011250") - test_helpers.ExpectEqual(t, header.Coinbase, legacyData.MockHeader.Coinbase.String()) + require.Equal(t, legacyHeaderCID.String(), header.CID) + require.Equal(t, legacyData.MockBlock.Difficulty().String(), header.TD) + require.Equal(t, "5000000000000011250", header.Reward) + require.Equal(t, legacyData.MockHeader.Coinbase.String(), header.Coinbase) require.Nil(t, legacyData.MockHeader.BaseFee) }) } diff --git a/statediff/indexer/database/sql/pgx_indexer_test.go b/statediff/indexer/database/sql/pgx_indexer_test.go index ec5b94fd5..197dda04c 100644 --- a/statediff/indexer/database/sql/pgx_indexer_test.go +++ b/statediff/indexer/database/sql/pgx_indexer_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + "github.com/lib/pq" + "github.com/ipfs/go-cid" blockstore "github.com/ipfs/go-ipfs-blockstore" dshelp "github.com/ipfs/go-ipfs-ds-help" @@ -62,7 +64,7 @@ func setupPGX(t *testing.T) { require.NoError(t, err) } - test_helpers.ExpectEqual(t, tx.(*sql.BatchTx).BlockNumber, mocks.BlockNumber.Uint64()) + require.Equal(t, mocks.BlockNumber.String(), tx.(*sql.BatchTx).BlockNumber) } func TestPGXIndexer(t *testing.T) { @@ -91,10 +93,10 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, header.CID, headerCID.String()) - test_helpers.ExpectEqual(t, header.TD, mocks.MockBlock.Difficulty().String()) - test_helpers.ExpectEqual(t, header.Reward, "2000000000000021250") - test_helpers.ExpectEqual(t, header.Coinbase, mocks.MockHeader.Coinbase.String()) + require.Equal(t, headerCID.String(), header.CID) + require.Equal(t, mocks.MockBlock.Difficulty().String(), header.TD) + require.Equal(t, "2000000000000021250", header.Reward) + require.Equal(t, mocks.MockHeader.Coinbase.String(), header.Coinbase) dc, err := cid.Decode(header.CID) if err != nil { t.Fatal(err) @@ -102,11 +104,11 @@ func TestPGXIndexer(t *testing.T) { mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() var data []byte - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, mocks.MockHeaderRlp) + require.Equal(t, mocks.MockHeaderRlp, data) }) t.Run("Publish and index transaction IPLDs in a single tx", func(t *testing.T) { @@ -121,7 +123,7 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(trxs), 5) + require.Equal(t, 5, len(trxs)) expectTrue(t, test_helpers.ListContainsString(trxs, trx1CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx2CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx3CID.String())) @@ -141,14 +143,14 @@ func TestPGXIndexer(t *testing.T) { mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() var data []byte - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } txTypeAndValueStr := `SELECT tx_type, CAST(value as TEXT) FROM eth.transaction_cids WHERE cid = $1` switch c { case trx1CID.String(): - test_helpers.ExpectEqual(t, data, tx1) + require.Equal(t, tx1, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { @@ -161,7 +163,7 @@ func TestPGXIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[0].Value().String(), txRes.Value) } case trx2CID.String(): - test_helpers.ExpectEqual(t, data, tx2) + require.Equal(t, tx2, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { @@ -174,7 +176,7 @@ func TestPGXIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[1].Value().String(), txRes.Value) } case trx3CID.String(): - test_helpers.ExpectEqual(t, data, tx3) + require.Equal(t, tx3, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { @@ -187,7 +189,7 @@ func TestPGXIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[2].Value().String(), txRes.Value) } case trx4CID.String(): - test_helpers.ExpectEqual(t, data, tx4) + require.Equal(t, tx4, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { @@ -199,8 +201,18 @@ func TestPGXIndexer(t *testing.T) { if txRes.Value != transactions[3].Value().String() { t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value) } + // AccessListElementModel is the db model for eth.access_list_entry + type AccessListElementModel struct { + BlockNumber string `db:"block_number"` + Index int64 `db:"index"` + TxID string `db:"tx_id"` + Address string `db:"address"` + StorageKeys pq.StringArray `db:"storage_keys"` + } accessListElementModels := make([]models.AccessListElementModel, 0) - pgStr = `SELECT access_list_elements.* FROM eth.access_list_elements INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC` + pgStr = "SELECT cast(access_list_elements.block_number AS TEXT), access_list_elements.index, access_list_elements.tx_id, " + + "access_list_elements.address, access_list_elements.storage_keys FROM eth.access_list_elements " + + "INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC" err = db.Select(context.Background(), &accessListElementModels, pgStr, c) if err != nil { t.Fatal(err) @@ -209,18 +221,20 @@ func TestPGXIndexer(t *testing.T) { t.Fatalf("expected two access list entries, got %d", len(accessListElementModels)) } model1 := models.AccessListElementModel{ - Index: accessListElementModels[0].Index, - Address: accessListElementModels[0].Address, + BlockNumber: mocks.BlockNumber.String(), + Index: accessListElementModels[0].Index, + Address: accessListElementModels[0].Address, } model2 := models.AccessListElementModel{ + BlockNumber: mocks.BlockNumber.String(), Index: accessListElementModels[1].Index, Address: accessListElementModels[1].Address, StorageKeys: accessListElementModels[1].StorageKeys, } - test_helpers.ExpectEqual(t, model1, mocks.AccessListEntry1Model) - test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model) + require.Equal(t, mocks.AccessListEntry1Model, model1) + require.Equal(t, mocks.AccessListEntry2Model, model2) case trx5CID.String(): - test_helpers.ExpectEqual(t, data, tx5) + require.Equal(t, tx5, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { @@ -272,7 +286,7 @@ func TestPGXIndexer(t *testing.T) { require.NoError(t, err) expectedLogs := mocks.MockReceipts[i].Logs - test_helpers.ExpectEqual(t, len(results), len(expectedLogs)) + require.Equal(t, len(expectedLogs), len(results)) var nodeElements []interface{} for idx, r := range results { @@ -283,12 +297,12 @@ func TestPGXIndexer(t *testing.T) { logRaw, err := rlp.EncodeToBytes(expectedLogs[idx]) require.NoError(t, err) // 2nd element of the leaf node contains the encoded log data. - test_helpers.ExpectEqual(t, logRaw, nodeElements[1].([]byte)) + require.Equal(t, nodeElements[1].([]byte), logRaw) } else { logRaw, err := rlp.EncodeToBytes(expectedLogs[idx]) require.NoError(t, err) // raw log was IPLDized - test_helpers.ExpectEqual(t, logRaw, r.Data) + require.Equal(t, r.Data, logRaw) } } } @@ -309,7 +323,7 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(rcts), 5) + require.Equal(t, 5, len(rcts)) expectTrue(t, test_helpers.ListContainsString(rcts, rct1CID.String())) expectTrue(t, test_helpers.ListContainsString(rcts, rct2CID.String())) expectTrue(t, test_helpers.ListContainsString(rcts, rct3CID.String())) @@ -335,7 +349,7 @@ func TestPGXIndexer(t *testing.T) { expectedRct, err := mocks.MockReceipts[idx].MarshalBinary() require.NoError(t, err) - test_helpers.ExpectEqual(t, expectedRct, nodeElements[1].([]byte)) + require.Equal(t, nodeElements[1].([]byte), expectedRct) dc, err := cid.Decode(c) if err != nil { @@ -344,7 +358,7 @@ func TestPGXIndexer(t *testing.T) { mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() var data []byte - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } @@ -352,46 +366,46 @@ func TestPGXIndexer(t *testing.T) { postStatePgStr := `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1` switch c { case rct1CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf1) + require.Equal(t, rctLeaf1, data) var postStatus uint64 pgStr = `SELECT post_status FROM eth.receipt_cids WHERE leaf_cid = $1` err = db.Get(context.Background(), &postStatus, pgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postStatus, mocks.ExpectedPostStatus) + require.Equal(t, mocks.ExpectedPostStatus, postStatus) case rct2CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf2) + require.Equal(t, rctLeaf2, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState1) + require.Equal(t, mocks.ExpectedPostState1, postState) case rct3CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf3) + require.Equal(t, rctLeaf3, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState2) + require.Equal(t, mocks.ExpectedPostState2, postState) case rct4CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf4) + require.Equal(t, rctLeaf4, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState3) + require.Equal(t, mocks.ExpectedPostState3, postState) case rct5CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf5) + require.Equal(t, rctLeaf5, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState3) + require.Equal(t, mocks.ExpectedPostState3, postState) } } }) @@ -409,7 +423,7 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 2) + require.Equal(t, 2, len(stateNodes)) for _, stateNode := range stateNodes { var data []byte dc, err := cid.Decode(stateNode.CID) @@ -418,43 +432,45 @@ func TestPGXIndexer(t *testing.T) { } mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - pgStr = `SELECT header_id, state_path, cast(balance AS TEXT), nonce, code_hash, storage_root from eth.state_accounts WHERE header_id = $1 AND state_path = $2` + pgStr = `SELECT cast(block_number AS TEXT), header_id, state_path, cast(balance AS TEXT), nonce, code_hash, storage_root from eth.state_accounts WHERE header_id = $1 AND state_path = $2` var account models.StateAccountModel err = db.Get(context.Background(), &account, pgStr, stateNode.HeaderID, stateNode.Path) if err != nil { t.Fatal(err) } if stateNode.CID == state1CID.String() { - test_helpers.ExpectEqual(t, stateNode.NodeType, 2) - test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.ContractLeafKey).Hex()) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x06'}) - test_helpers.ExpectEqual(t, data, mocks.ContractLeafNode) - test_helpers.ExpectEqual(t, account, models.StateAccountModel{ + require.Equal(t, 2, stateNode.NodeType) + require.Equal(t, common.BytesToHash(mocks.ContractLeafKey).Hex(), stateNode.StateKey) + require.Equal(t, []byte{'\x06'}, stateNode.Path) + require.Equal(t, mocks.ContractLeafNode, data) + require.Equal(t, models.StateAccountModel{ + BlockNumber: mocks.BlockNumber.String(), HeaderID: account.HeaderID, StatePath: stateNode.Path, Balance: "0", CodeHash: mocks.ContractCodeHash.Bytes(), StorageRoot: mocks.ContractRoot, Nonce: 1, - }) + }, account) } if stateNode.CID == state2CID.String() { - test_helpers.ExpectEqual(t, stateNode.NodeType, 2) - test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.AccountLeafKey).Hex()) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x0c'}) - test_helpers.ExpectEqual(t, data, mocks.AccountLeafNode) - test_helpers.ExpectEqual(t, account, models.StateAccountModel{ + require.Equal(t, 2, stateNode.NodeType) + require.Equal(t, common.BytesToHash(mocks.AccountLeafKey).Hex(), stateNode.StateKey) + require.Equal(t, []byte{'\x0c'}, stateNode.Path) + require.Equal(t, mocks.AccountLeafNode, data) + require.Equal(t, models.StateAccountModel{ + BlockNumber: mocks.BlockNumber.String(), HeaderID: account.HeaderID, StatePath: stateNode.Path, Balance: "1000", CodeHash: mocks.AccountCodeHash.Bytes(), StorageRoot: mocks.AccountRoot, Nonce: 0, - }) + }, account) } } @@ -467,7 +483,7 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 1) + require.Equal(t, 1, len(stateNodes)) stateNode := stateNodes[0] var data []byte dc, err := cid.Decode(stateNode.CID) @@ -476,14 +492,14 @@ func TestPGXIndexer(t *testing.T) { } mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + require.Equal(t, shared.RemovedNodeMhKey, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x02'}) - test_helpers.ExpectEqual(t, data, []byte{}) + require.Equal(t, shared.RemovedNodeStateCID, stateNode.CID) + require.Equal(t, []byte{'\x02'}, stateNode.Path) + require.Equal(t, []byte{}, data) }) t.Run("Publish and index storage IPLDs in a single tx", func(t *testing.T) { @@ -492,7 +508,7 @@ func TestPGXIndexer(t *testing.T) { defer checkTxClosure(t, 1, 0, 1) // check that storage nodes were properly indexed storageNodes := make([]models.StorageNodeWithStateKeyModel, 0) - pgStr := `SELECT storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path + pgStr := `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path FROM eth.storage_cids, eth.state_cids, eth.header_cids WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id) AND state_cids.header_id = header_cids.block_hash @@ -502,14 +518,15 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(storageNodes), 1) - test_helpers.ExpectEqual(t, storageNodes[0], models.StorageNodeWithStateKeyModel{ - CID: storageCID.String(), - NodeType: 2, - StorageKey: common.BytesToHash(mocks.StorageLeafKey).Hex(), - StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), - Path: []byte{}, - }) + require.Equal(t, 1, len(storageNodes)) + require.Equal(t, models.StorageNodeWithStateKeyModel{ + BlockNumber: mocks.BlockNumber.String(), + CID: storageCID.String(), + NodeType: 2, + StorageKey: common.BytesToHash(mocks.StorageLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{}, + }, storageNodes[0]) var data []byte dc, err := cid.Decode(storageNodes[0].CID) if err != nil { @@ -517,15 +534,15 @@ func TestPGXIndexer(t *testing.T) { } mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, mocks.StorageLeafNode) + require.Equal(t, mocks.StorageLeafNode, data) // check that Removed storage nodes were properly indexed storageNodes = make([]models.StorageNodeWithStateKeyModel, 0) - pgStr = `SELECT storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path + pgStr = `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path FROM eth.storage_cids, eth.state_cids, eth.header_cids WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id) AND state_cids.header_id = header_cids.block_hash @@ -535,25 +552,26 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(storageNodes), 1) - test_helpers.ExpectEqual(t, storageNodes[0], models.StorageNodeWithStateKeyModel{ - CID: shared.RemovedNodeStorageCID, - NodeType: 3, - StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), - StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), - Path: []byte{'\x03'}, - }) + require.Equal(t, 1, len(storageNodes)) + require.Equal(t, models.StorageNodeWithStateKeyModel{ + BlockNumber: mocks.BlockNumber.String(), + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{'\x03'}, + }, storageNodes[0]) dc, err = cid.Decode(storageNodes[0].CID) if err != nil { t.Fatal(err) } mhKey = dshelp.MultihashToDsKey(dc.Hash()) prefixedKey = blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + require.Equal(t, shared.RemovedNodeMhKey, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, []byte{}) + require.Equal(t, []byte{}, data) }) } diff --git a/statediff/indexer/database/sql/postgres/config.go b/statediff/indexer/database/sql/postgres/config.go index 095b4dd24..4f4fd304d 100644 --- a/statediff/indexer/database/sql/postgres/config.go +++ b/statediff/indexer/database/sql/postgres/config.go @@ -49,9 +49,9 @@ func ResolveDriverType(str string) (DriverType, error) { var DefaultConfig = Config{ Hostname: "localhost", Port: 5432, - DatabaseName: "vulcanize_testing_v3", - Username: "vdbm", - Password: "password", + DatabaseName: "vulcanize_testing_v4", + Username: "postgres", + Password: "", } // Config holds params for a Postgres db diff --git a/statediff/indexer/database/sql/sqlx_indexer_legacy_test.go b/statediff/indexer/database/sql/sqlx_indexer_legacy_test.go index 4a6594eb3..daaa1550c 100644 --- a/statediff/indexer/database/sql/sqlx_indexer_legacy_test.go +++ b/statediff/indexer/database/sql/sqlx_indexer_legacy_test.go @@ -31,7 +31,6 @@ import ( "github.com/ethereum/go-ethereum/statediff/indexer/interfaces" "github.com/ethereum/go-ethereum/statediff/indexer/ipld" "github.com/ethereum/go-ethereum/statediff/indexer/mocks" - "github.com/ethereum/go-ethereum/statediff/indexer/test_helpers" ) var ( @@ -66,7 +65,7 @@ func setupLegacySQLX(t *testing.T) { require.NoError(t, err) } - test_helpers.ExpectEqual(t, tx.(*sql.BatchTx).BlockNumber, legacyData.BlockNumber.Uint64()) + require.Equal(t, legacyData.BlockNumber.String(), tx.(*sql.BatchTx).BlockNumber) } func TestLegacySQLXIndexer(t *testing.T) { @@ -89,10 +88,10 @@ func TestLegacySQLXIndexer(t *testing.T) { err = db.QueryRow(context.Background(), pgStr, legacyData.BlockNumber.Uint64()).(*sqlx.Row).StructScan(header) require.NoError(t, err) - test_helpers.ExpectEqual(t, header.CID, legacyHeaderCID.String()) - test_helpers.ExpectEqual(t, header.TD, legacyData.MockBlock.Difficulty().String()) - test_helpers.ExpectEqual(t, header.Reward, "5000000000000011250") - test_helpers.ExpectEqual(t, header.Coinbase, legacyData.MockHeader.Coinbase.String()) + require.Equal(t, legacyHeaderCID.String(), header.CID) + require.Equal(t, legacyData.MockBlock.Difficulty().String(), header.TD) + require.Equal(t, "5000000000000011250", header.Reward) + require.Equal(t, legacyData.MockHeader.Coinbase.String(), header.Coinbase) require.Nil(t, legacyData.MockHeader.BaseFee) }) } diff --git a/statediff/indexer/database/sql/sqlx_indexer_test.go b/statediff/indexer/database/sql/sqlx_indexer_test.go index 65c0a7615..d64cb90b7 100644 --- a/statediff/indexer/database/sql/sqlx_indexer_test.go +++ b/statediff/indexer/database/sql/sqlx_indexer_test.go @@ -63,7 +63,7 @@ func setupSQLX(t *testing.T) { require.NoError(t, err) } - test_helpers.ExpectEqual(t, tx.(*sql.BatchTx).BlockNumber, mocks.BlockNumber.Uint64()) + require.Equal(t, mocks.BlockNumber.String(), tx.(*sql.BatchTx).BlockNumber) } func TestSQLXIndexer(t *testing.T) { @@ -87,10 +87,10 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, header.CID, headerCID.String()) - test_helpers.ExpectEqual(t, header.TD, mocks.MockBlock.Difficulty().String()) - test_helpers.ExpectEqual(t, header.Reward, "2000000000000021250") - test_helpers.ExpectEqual(t, header.Coinbase, mocks.MockHeader.Coinbase.String()) + require.Equal(t, headerCID.String(), header.CID) + require.Equal(t, mocks.MockBlock.Difficulty().String(), header.TD) + require.Equal(t, "2000000000000021250", header.Reward) + require.Equal(t, mocks.MockHeader.Coinbase.String(), header.Coinbase) dc, err := cid.Decode(header.CID) if err != nil { t.Fatal(err) @@ -98,11 +98,11 @@ func TestSQLXIndexer(t *testing.T) { mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() var data []byte - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, mocks.MockHeaderRlp) + require.Equal(t, mocks.MockHeaderRlp, data) }) t.Run("Publish and index transaction IPLDs in a single tx", func(t *testing.T) { @@ -117,7 +117,7 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(trxs), 5) + require.Equal(t, 5, len(trxs)) expectTrue(t, test_helpers.ListContainsString(trxs, trx1CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx2CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx3CID.String())) @@ -137,14 +137,14 @@ func TestSQLXIndexer(t *testing.T) { mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() var data []byte - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } txTypeAndValueStr := `SELECT tx_type, value FROM eth.transaction_cids WHERE cid = $1` switch c { case trx1CID.String(): - test_helpers.ExpectEqual(t, data, tx1) + require.Equal(t, tx1, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { @@ -157,7 +157,7 @@ func TestSQLXIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[0].Value().String(), txRes.Value) } case trx2CID.String(): - test_helpers.ExpectEqual(t, data, tx2) + require.Equal(t, tx2, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { @@ -170,7 +170,7 @@ func TestSQLXIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[1].Value().String(), txRes.Value) } case trx3CID.String(): - test_helpers.ExpectEqual(t, data, tx3) + require.Equal(t, tx3, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { @@ -183,7 +183,7 @@ func TestSQLXIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[2].Value().String(), txRes.Value) } case trx4CID.String(): - test_helpers.ExpectEqual(t, data, tx4) + require.Equal(t, tx4, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { @@ -196,7 +196,9 @@ func TestSQLXIndexer(t *testing.T) { t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value) } accessListElementModels := make([]models.AccessListElementModel, 0) - pgStr = `SELECT access_list_elements.* FROM eth.access_list_elements INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC` + pgStr = "SELECT cast(access_list_elements.block_number AS TEXT), access_list_elements.index, access_list_elements.tx_id, " + + "access_list_elements.address, access_list_elements.storage_keys FROM eth.access_list_elements " + + "INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC" err = db.Select(context.Background(), &accessListElementModels, pgStr, c) if err != nil { t.Fatal(err) @@ -205,18 +207,20 @@ func TestSQLXIndexer(t *testing.T) { t.Fatalf("expected two access list entries, got %d", len(accessListElementModels)) } model1 := models.AccessListElementModel{ - Index: accessListElementModels[0].Index, - Address: accessListElementModels[0].Address, + BlockNumber: mocks.BlockNumber.String(), + Index: accessListElementModels[0].Index, + Address: accessListElementModels[0].Address, } model2 := models.AccessListElementModel{ + BlockNumber: mocks.BlockNumber.String(), Index: accessListElementModels[1].Index, Address: accessListElementModels[1].Address, StorageKeys: accessListElementModels[1].StorageKeys, } - test_helpers.ExpectEqual(t, model1, mocks.AccessListEntry1Model) - test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model) + require.Equal(t, mocks.AccessListEntry1Model, model1) + require.Equal(t, mocks.AccessListEntry2Model, model2) case trx5CID.String(): - test_helpers.ExpectEqual(t, data, tx5) + require.Equal(t, tx5, data) txRes := new(txResult) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { @@ -266,7 +270,7 @@ func TestSQLXIndexer(t *testing.T) { require.NoError(t, err) expectedLogs := mocks.MockReceipts[i].Logs - test_helpers.ExpectEqual(t, len(results), len(expectedLogs)) + require.Equal(t, len(expectedLogs), len(results)) var nodeElements []interface{} for idx, r := range results { @@ -277,12 +281,12 @@ func TestSQLXIndexer(t *testing.T) { logRaw, err := rlp.EncodeToBytes(expectedLogs[idx]) require.NoError(t, err) // 2nd element of the leaf node contains the encoded log data. - test_helpers.ExpectEqual(t, logRaw, nodeElements[1].([]byte)) + require.Equal(t, nodeElements[1].([]byte), logRaw) } else { logRaw, err := rlp.EncodeToBytes(expectedLogs[idx]) require.NoError(t, err) // raw log was IPLDized - test_helpers.ExpectEqual(t, logRaw, r.Data) + require.Equal(t, r.Data, logRaw) } } } @@ -303,7 +307,7 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(rcts), 5) + require.Equal(t, 5, len(rcts)) expectTrue(t, test_helpers.ListContainsString(rcts, rct1CID.String())) expectTrue(t, test_helpers.ListContainsString(rcts, rct2CID.String())) expectTrue(t, test_helpers.ListContainsString(rcts, rct3CID.String())) @@ -329,7 +333,7 @@ func TestSQLXIndexer(t *testing.T) { expectedRct, err := mocks.MockReceipts[idx].MarshalBinary() require.NoError(t, err) - test_helpers.ExpectEqual(t, expectedRct, nodeElements[1].([]byte)) + require.Equal(t, nodeElements[1].([]byte), expectedRct) dc, err := cid.Decode(c) if err != nil { @@ -338,53 +342,53 @@ func TestSQLXIndexer(t *testing.T) { mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() var data []byte - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } postStatePgStr := `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1` switch c { case rct1CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf1) + require.Equal(t, rctLeaf1, data) var postStatus uint64 pgStr = `SELECT post_status FROM eth.receipt_cids WHERE leaf_cid = $1` err = db.Get(context.Background(), &postStatus, pgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postStatus, mocks.ExpectedPostStatus) + require.Equal(t, mocks.ExpectedPostStatus, postStatus) case rct2CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf2) + require.Equal(t, rctLeaf2, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState1) + require.Equal(t, mocks.ExpectedPostState1, postState) case rct3CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf3) + require.Equal(t, rctLeaf3, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState2) + require.Equal(t, mocks.ExpectedPostState2, postState) case rct4CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf4) + require.Equal(t, rctLeaf4, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState3) + require.Equal(t, mocks.ExpectedPostState3, postState) case rct5CID.String(): - test_helpers.ExpectEqual(t, data, rctLeaf5) + require.Equal(t, rctLeaf5, data) var postState string err = db.Get(context.Background(), &postState, postStatePgStr, c) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, postState, mocks.ExpectedPostState3) + require.Equal(t, mocks.ExpectedPostState3, postState) } } }) @@ -402,7 +406,7 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 2) + require.Equal(t, 2, len(stateNodes)) for _, stateNode := range stateNodes { var data []byte dc, err := cid.Decode(stateNode.CID) @@ -411,7 +415,7 @@ func TestSQLXIndexer(t *testing.T) { } mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } @@ -422,32 +426,34 @@ func TestSQLXIndexer(t *testing.T) { t.Fatal(err) } if stateNode.CID == state1CID.String() { - test_helpers.ExpectEqual(t, stateNode.NodeType, 2) - test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.ContractLeafKey).Hex()) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x06'}) - test_helpers.ExpectEqual(t, data, mocks.ContractLeafNode) - test_helpers.ExpectEqual(t, account, models.StateAccountModel{ + require.Equal(t, 2, stateNode.NodeType) + require.Equal(t, common.BytesToHash(mocks.ContractLeafKey).Hex(), stateNode.StateKey) + require.Equal(t, []byte{'\x06'}, stateNode.Path) + require.Equal(t, mocks.ContractLeafNode, data) + require.Equal(t, models.StateAccountModel{ + BlockNumber: mocks.BlockNumber.String(), HeaderID: account.HeaderID, StatePath: stateNode.Path, Balance: "0", CodeHash: mocks.ContractCodeHash.Bytes(), StorageRoot: mocks.ContractRoot, Nonce: 1, - }) + }, account) } if stateNode.CID == state2CID.String() { - test_helpers.ExpectEqual(t, stateNode.NodeType, 2) - test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.AccountLeafKey).Hex()) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x0c'}) - test_helpers.ExpectEqual(t, data, mocks.AccountLeafNode) - test_helpers.ExpectEqual(t, account, models.StateAccountModel{ + require.Equal(t, 2, stateNode.NodeType) + require.Equal(t, common.BytesToHash(mocks.AccountLeafKey).Hex(), stateNode.StateKey) + require.Equal(t, []byte{'\x0c'}, stateNode.Path) + require.Equal(t, mocks.AccountLeafNode, data) + require.Equal(t, models.StateAccountModel{ + BlockNumber: mocks.BlockNumber.String(), HeaderID: account.HeaderID, StatePath: stateNode.Path, Balance: "1000", CodeHash: mocks.AccountCodeHash.Bytes(), StorageRoot: mocks.AccountRoot, Nonce: 0, - }) + }, account) } } @@ -460,7 +466,7 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 1) + require.Equal(t, 1, len(stateNodes)) stateNode := stateNodes[0] var data []byte dc, err := cid.Decode(stateNode.CID) @@ -469,14 +475,14 @@ func TestSQLXIndexer(t *testing.T) { } mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + require.Equal(t, shared.RemovedNodeMhKey, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) - test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x02'}) - test_helpers.ExpectEqual(t, data, []byte{}) + require.Equal(t, shared.RemovedNodeStateCID, stateNode.CID) + require.Equal(t, []byte{'\x02'}, stateNode.Path) + require.Equal(t, []byte{}, data) }) t.Run("Publish and index storage IPLDs in a single tx", func(t *testing.T) { @@ -485,7 +491,7 @@ func TestSQLXIndexer(t *testing.T) { defer checkTxClosure(t, 0, 0, 0) // check that storage nodes were properly indexed storageNodes := make([]models.StorageNodeWithStateKeyModel, 0) - pgStr := `SELECT storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path + pgStr := `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path FROM eth.storage_cids, eth.state_cids, eth.header_cids WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id) AND state_cids.header_id = header_cids.block_hash @@ -495,14 +501,15 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(storageNodes), 1) - test_helpers.ExpectEqual(t, storageNodes[0], models.StorageNodeWithStateKeyModel{ - CID: storageCID.String(), - NodeType: 2, - StorageKey: common.BytesToHash(mocks.StorageLeafKey).Hex(), - StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), - Path: []byte{}, - }) + require.Equal(t, 1, len(storageNodes)) + require.Equal(t, models.StorageNodeWithStateKeyModel{ + BlockNumber: mocks.BlockNumber.String(), + CID: storageCID.String(), + NodeType: 2, + StorageKey: common.BytesToHash(mocks.StorageLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{}, + }, storageNodes[0]) var data []byte dc, err := cid.Decode(storageNodes[0].CID) if err != nil { @@ -510,15 +517,15 @@ func TestSQLXIndexer(t *testing.T) { } mhKey := dshelp.MultihashToDsKey(dc.Hash()) prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, mocks.StorageLeafNode) + require.Equal(t, mocks.StorageLeafNode, data) // check that Removed storage nodes were properly indexed storageNodes = make([]models.StorageNodeWithStateKeyModel, 0) - pgStr = `SELECT storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path + pgStr = `SELECT cast(storage_cids.block_number AS TEXT), storage_cids.cid, state_cids.state_leaf_key, storage_cids.storage_leaf_key, storage_cids.node_type, storage_cids.storage_path FROM eth.storage_cids, eth.state_cids, eth.header_cids WHERE (storage_cids.state_path, storage_cids.header_id) = (state_cids.state_path, state_cids.header_id) AND state_cids.header_id = header_cids.block_hash @@ -528,25 +535,26 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(storageNodes), 1) - test_helpers.ExpectEqual(t, storageNodes[0], models.StorageNodeWithStateKeyModel{ - CID: shared.RemovedNodeStorageCID, - NodeType: 3, - StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), - StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), - Path: []byte{'\x03'}, - }) + require.Equal(t, 1, len(storageNodes)) + require.Equal(t, models.StorageNodeWithStateKeyModel{ + BlockNumber: mocks.BlockNumber.String(), + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{'\x03'}, + }, storageNodes[0]) dc, err = cid.Decode(storageNodes[0].CID) if err != nil { t.Fatal(err) } mhKey = dshelp.MultihashToDsKey(dc.Hash()) prefixedKey = blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) - err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey) + require.Equal(t, shared.RemovedNodeMhKey, prefixedKey) + err = db.Get(context.Background(), &data, ipfsPgGet, prefixedKey, mocks.BlockNumber.Uint64()) if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, data, []byte{}) + require.Equal(t, []byte{}, data) }) } diff --git a/statediff/indexer/mocks/test_data.go b/statediff/indexer/mocks/test_data.go index e5d72e5ba..dc7e7638f 100644 --- a/statediff/indexer/mocks/test_data.go +++ b/statediff/indexer/mocks/test_data.go @@ -106,10 +106,12 @@ var ( StorageKeys: []common.Hash{common.BytesToHash(StorageLeafKey), common.BytesToHash(MockStorageLeafKey)}, } AccessListEntry1Model = models.AccessListElementModel{ - Index: 0, - Address: Address.Hex(), + BlockNumber: BlockNumber.String(), + Index: 0, + Address: Address.Hex(), } AccessListEntry2Model = models.AccessListElementModel{ + BlockNumber: BlockNumber.String(), Index: 1, Address: AnotherAddress.Hex(), StorageKeys: []string{common.BytesToHash(StorageLeafKey).Hex(), common.BytesToHash(MockStorageLeafKey).Hex()}, diff --git a/statediff/indexer/test_helpers/test_helpers.go b/statediff/indexer/test_helpers/test_helpers.go index b519d80b5..6073db434 100644 --- a/statediff/indexer/test_helpers/test_helpers.go +++ b/statediff/indexer/test_helpers/test_helpers.go @@ -16,18 +16,6 @@ package test_helpers -import ( - "reflect" - "testing" -) - -// ExpectEqual asserts the provided interfaces are deep equal -func ExpectEqual(t *testing.T, got interface{}, want interface{}) { - if !reflect.DeepEqual(got, want) { - t.Fatalf("Expected: %v\nActual: %v", want, got) - } -} - // ListContainsString used to check if a list of strings contains a particular string func ListContainsString(sss []string, s string) bool { for _, str := range sss {