diff --git a/statediff/indexer/database/file/indexer_test.go b/statediff/indexer/database/file/indexer_test.go index fb5453fe6..2b030210d 100644 --- a/statediff/indexer/database/file/indexer_test.go +++ b/statediff/indexer/database/file/indexer_test.go @@ -625,23 +625,34 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 1) - stateNode := stateNodes[0] - var data []byte - dc, err := cid.Decode(stateNode.CID) - if err != nil { - t.Fatal(err) + test_helpers.ExpectEqual(t, len(stateNodes), 2) + for idx, stateNode := range stateNodes { + var data []byte + dc, err := cid.Decode(stateNode.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 = sqlxdb.Get(&data, ipfsPgGet, prefixedKey) + if err != nil { + t.Fatal(err) + } + + if idx == 0 { + test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) + test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.RemovedLeafKey).Hex()) + test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x02'}) + test_helpers.ExpectEqual(t, data, []byte{}) + } + if idx == 1 { + test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) + test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.Contract2LeafKey).Hex()) + test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x07'}) + test_helpers.ExpectEqual(t, data, []byte{}) + } } - mhKey := dshelp.MultihashToDsKey(dc.Hash()) - prefixedKey := blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) - 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{}) }) t.Run("Publish and index storage IPLDs in a single tx", func(t *testing.T) { @@ -694,26 +705,45 @@ 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'}, - }) - dc, err = cid.Decode(storageNodes[0].CID) - if err != nil { - t.Fatal(err) + test_helpers.ExpectEqual(t, len(storageNodes), 3) + expectedStorageNodes := []models.StorageNodeWithStateKeyModel{ + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{'\x03'}, + }, + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.Storage2LeafKey).Hex(), + StateKey: common.BytesToHash(mocks.Contract2LeafKey).Hex(), + Path: []byte{'\x0e'}, + }, + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.Storage3LeafKey).Hex(), + StateKey: common.BytesToHash(mocks.Contract2LeafKey).Hex(), + Path: []byte{'\x0f'}, + }, } - mhKey = dshelp.MultihashToDsKey(dc.Hash()) - prefixedKey = blockstore.BlockPrefix.String() + mhKey.String() - test_helpers.ExpectEqual(t, prefixedKey, shared.RemovedNodeMhKey) - err = sqlxdb.Get(&data, ipfsPgGet, prefixedKey) - if err != nil { - t.Fatal(err) + for idx, storageNode := range storageNodes { + test_helpers.ExpectEqual(t, storageNode, expectedStorageNodes[idx]) + dc, err = cid.Decode(storageNode.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 = sqlxdb.Get(&data, ipfsPgGet, prefixedKey) + if err != nil { + t.Fatal(err) + } + test_helpers.ExpectEqual(t, data, []byte{}) } - test_helpers.ExpectEqual(t, data, []byte{}) }) } diff --git a/statediff/indexer/database/sql/pgx_indexer_test.go b/statediff/indexer/database/sql/pgx_indexer_test.go index fef559486..94d0e2c4c 100644 --- a/statediff/indexer/database/sql/pgx_indexer_test.go +++ b/statediff/indexer/database/sql/pgx_indexer_test.go @@ -473,23 +473,34 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 1) - stateNode := stateNodes[0] - var data []byte - dc, err := cid.Decode(stateNode.CID) - if err != nil { - t.Fatal(err) + test_helpers.ExpectEqual(t, len(stateNodes), 2) + for idx, stateNode := range stateNodes { + var data []byte + dc, err := cid.Decode(stateNode.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) + if err != nil { + t.Fatal(err) + } + + if idx == 0 { + test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) + test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.RemovedLeafKey).Hex()) + test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x02'}) + test_helpers.ExpectEqual(t, data, []byte{}) + } + if idx == 1 { + test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) + test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.Contract2LeafKey).Hex()) + test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x07'}) + test_helpers.ExpectEqual(t, data, []byte{}) + } } - 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) - 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{}) }) t.Run("Publish and index storage IPLDs in a single tx", func(t *testing.T) { @@ -541,26 +552,45 @@ 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'}, - }) - dc, err = cid.Decode(storageNodes[0].CID) - if err != nil { - t.Fatal(err) + test_helpers.ExpectEqual(t, len(storageNodes), 3) + expectedStorageNodes := []models.StorageNodeWithStateKeyModel{ + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{'\x03'}, + }, + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.Storage2LeafKey).Hex(), + StateKey: common.BytesToHash(mocks.Contract2LeafKey).Hex(), + Path: []byte{'\x0e'}, + }, + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.Storage3LeafKey).Hex(), + StateKey: common.BytesToHash(mocks.Contract2LeafKey).Hex(), + Path: []byte{'\x0f'}, + }, } - 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) - if err != nil { - t.Fatal(err) + for idx, storageNode := range storageNodes { + test_helpers.ExpectEqual(t, storageNode, expectedStorageNodes[idx]) + dc, err = cid.Decode(storageNode.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) + if err != nil { + t.Fatal(err) + } + test_helpers.ExpectEqual(t, data, []byte{}) } - test_helpers.ExpectEqual(t, data, []byte{}) }) } diff --git a/statediff/indexer/database/sql/sqlx_indexer_test.go b/statediff/indexer/database/sql/sqlx_indexer_test.go index 73309ea7f..50b855f64 100644 --- a/statediff/indexer/database/sql/sqlx_indexer_test.go +++ b/statediff/indexer/database/sql/sqlx_indexer_test.go @@ -466,23 +466,34 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - test_helpers.ExpectEqual(t, len(stateNodes), 1) - stateNode := stateNodes[0] - var data []byte - dc, err := cid.Decode(stateNode.CID) - if err != nil { - t.Fatal(err) + test_helpers.ExpectEqual(t, len(stateNodes), 2) + for idx, stateNode := range stateNodes { + var data []byte + dc, err := cid.Decode(stateNode.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) + if err != nil { + t.Fatal(err) + } + + if idx == 0 { + test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) + test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.RemovedLeafKey).Hex()) + test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x02'}) + test_helpers.ExpectEqual(t, data, []byte{}) + } + if idx == 1 { + test_helpers.ExpectEqual(t, stateNode.CID, shared.RemovedNodeStateCID) + test_helpers.ExpectEqual(t, stateNode.StateKey, common.BytesToHash(mocks.Contract2LeafKey).Hex()) + test_helpers.ExpectEqual(t, stateNode.Path, []byte{'\x07'}) + test_helpers.ExpectEqual(t, data, []byte{}) + } } - 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) - 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{}) }) t.Run("Publish and index storage IPLDs in a single tx", func(t *testing.T) { @@ -534,26 +545,45 @@ 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'}, - }) - dc, err = cid.Decode(storageNodes[0].CID) - if err != nil { - t.Fatal(err) + test_helpers.ExpectEqual(t, len(storageNodes), 3) + expectedStorageNodes := []models.StorageNodeWithStateKeyModel{ + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.RemovedLeafKey).Hex(), + StateKey: common.BytesToHash(mocks.ContractLeafKey).Hex(), + Path: []byte{'\x03'}, + }, + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.Storage2LeafKey).Hex(), + StateKey: common.BytesToHash(mocks.Contract2LeafKey).Hex(), + Path: []byte{'\x0e'}, + }, + { + CID: shared.RemovedNodeStorageCID, + NodeType: 3, + StorageKey: common.BytesToHash(mocks.Storage3LeafKey).Hex(), + StateKey: common.BytesToHash(mocks.Contract2LeafKey).Hex(), + Path: []byte{'\x0f'}, + }, } - 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) - if err != nil { - t.Fatal(err) + for idx, storageNode := range storageNodes { + test_helpers.ExpectEqual(t, storageNode, expectedStorageNodes[idx]) + dc, err = cid.Decode(storageNode.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) + if err != nil { + t.Fatal(err) + } + test_helpers.ExpectEqual(t, data, []byte{}) } - test_helpers.ExpectEqual(t, data, []byte{}) }) } diff --git a/statediff/indexer/mocks/test_data.go b/statediff/indexer/mocks/test_data.go index e5d72e5ba..87e80e465 100644 --- a/statediff/indexer/mocks/test_data.go +++ b/statediff/indexer/mocks/test_data.go @@ -57,6 +57,7 @@ var ( Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593") ContractAddress = crypto.CreateAddress(SenderAddr, MockTransactions[2].Nonce()) + ContractAddress2 = crypto.CreateAddress(SenderAddr, MockTransactions[3].Nonce()) MockContractByteCode = []byte{0, 1, 2, 3, 4, 5} mockTopic11 = common.HexToHash("0x04") mockTopic12 = common.HexToHash("0x06") @@ -143,6 +144,12 @@ var ( ContractAccount, }) + Contract2LeafKey = test_helpers.AddressToLeafKey(ContractAddress2) + storage2Location = common.HexToHash("2") + Storage2LeafKey = crypto.Keccak256Hash(storage2Location[:]).Bytes() + storage3Location = common.HexToHash("3") + Storage3LeafKey = crypto.Keccak256Hash(storage3Location[:]).Bytes() + nonce0 = uint64(0) AccountRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" AccountCodeHash = common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") @@ -194,6 +201,26 @@ var ( LeafKey: RemovedLeafKey, NodeValue: []byte{}, }, + { + Path: []byte{'\x07'}, + NodeType: sdtypes.Removed, + LeafKey: Contract2LeafKey, + NodeValue: []byte{}, + StorageNodes: []sdtypes.StorageNode{ + { + Path: []byte{'\x0e'}, + NodeType: sdtypes.Removed, + LeafKey: Storage2LeafKey, + NodeValue: []byte{}, + }, + { + Path: []byte{'\x0f'}, + NodeType: sdtypes.Removed, + LeafKey: Storage3LeafKey, + NodeValue: []byte{}, + }, + }, + }, } )