Skip to content

Commit d45c162

Browse files
committed
Add descriptive comments
1 parent 2b4f619 commit d45c162

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

statediff/indexer/database/sql/indexer_shared_test.go

+39-5
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,19 @@ func init() {
5454
os.Exit(0)
5555
}
5656

57+
// canonical block at LondonBlock height
5758
mockBlock = mocks.MockBlock
5859
txs, rcts := mocks.MockBlock.Transactions(), mocks.MockReceipts
5960

61+
// non-canonical block at LondonBlock height
6062
mockNonCanonicalBlock = mocks.MockNonCanonicalBlock
6163
nonCanonicalBlockRcts := mocks.MockNonCanonicalBlockReceipts
6264

65+
// non-canonical block at LondonBlock height + 1
6366
mockNonCanonicalBlock2 = mocks.MockNonCanonicalBlock2
6467
nonCanonicalBlock2Rcts := mocks.MockNonCanonicalBlock2Receipts
6568

69+
// encode mock receipts
6670
buf := new(bytes.Buffer)
6771
txs.EncodeIndex(0, buf)
6872
tx1 = make([]byte, buf.Len())
@@ -114,6 +118,7 @@ func init() {
114118
copy(rct5, buf.Bytes())
115119
buf.Reset()
116120

121+
// encode mock receipts for non-canonical blocks
117122
nonCanonicalBlockRcts.EncodeIndex(0, buf)
118123
nonCanonicalBlockRct1 = make([]byte, buf.Len())
119124
copy(nonCanonicalBlockRct1, buf.Bytes())
@@ -146,6 +151,7 @@ func init() {
146151
state2CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, mocks.AccountLeafNode, multihash.KECCAK_256)
147152
storageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, mocks.StorageLeafNode, multihash.KECCAK_256)
148153

154+
// create raw receipts
149155
rawRctLeafNodes, rctleafNodeCids := createRctTrie([][]byte{rct1, rct2, rct3, rct4, rct5})
150156

151157
rct1CID = rctleafNodeCids[0]
@@ -160,6 +166,7 @@ func init() {
160166
rctLeaf4 = rawRctLeafNodes[3]
161167
rctLeaf5 = rawRctLeafNodes[4]
162168

169+
// create raw receipts for non-canonical blocks
163170
nonCanonicalBlockRawRctLeafNodes, nonCanonicalBlockRctLeafNodeCids := createRctTrie([][]byte{nonCanonicalBlockRct1, nonCanonicalBlockRct2})
164171

165172
nonCanonicalBlockRct1CID = nonCanonicalBlockRctLeafNodeCids[0]
@@ -191,6 +198,8 @@ func init() {
191198
watchedAt3 = uint64(20)
192199
}
193200

201+
// createRctTrie creates a receipt trie from the given raw receipts
202+
// returns receipt leaf nodes and their CIDs
194203
func createRctTrie(rcts [][]byte) ([][]byte, []cid.Cid) {
195204
receiptTrie := ipld.NewRctTrie()
196205

@@ -213,6 +222,7 @@ func createRctTrie(rcts [][]byte) ([][]byte, []cid.Cid) {
213222
return orderedRctLeafNodes, rctleafNodeCids
214223
}
215224

225+
// createRctModel creates a models.ReceiptModel object from a given ethereum receipt
216226
func createRctModel(rct *types.Receipt, cid cid.Cid, blockNumber string) models.ReceiptModel {
217227
rctModel := models.ReceiptModel{
218228
BlockNumber: blockNumber,
@@ -256,6 +266,7 @@ func tearDown(t *testing.T) {
256266
require.NoError(t, err)
257267
}
258268

269+
// setupTestData indexes a single mock block along with it's state nodes
259270
func setupTestData(t *testing.T) {
260271
var tx interfaces.Batch
261272
tx, err = ind.PushBlock(
@@ -278,9 +289,12 @@ func setupTestData(t *testing.T) {
278289
require.Equal(t, mocks.BlockNumber.String(), tx.(*sql.BatchTx).BlockNumber)
279290
}
280291

292+
// setupTestData indexes a mock block and a non-canonical mock block at London height
293+
// and a non-canonical block at London height + 1
294+
// along with their state nodes
281295
func setupTestDataNonCanonical(t *testing.T) {
296+
// index a canonical block at London height
282297
var tx1 interfaces.Batch
283-
284298
tx1, err = ind.PushBlock(
285299
mockBlock,
286300
mocks.MockReceipts,
@@ -298,6 +312,8 @@ func setupTestDataNonCanonical(t *testing.T) {
298312
t.Fatal(err)
299313
}
300314

315+
// index a non-canonical block at London height
316+
// has transactions overlapping with that of the canonical block
301317
var tx2 interfaces.Batch
302318
tx2, err = ind.PushBlock(
303319
mockNonCanonicalBlock,
@@ -316,6 +332,8 @@ func setupTestDataNonCanonical(t *testing.T) {
316332
t.Fatal(err)
317333
}
318334

335+
// index a non-canonical block at London height + 1
336+
// has transactions overlapping with that of the canonical block
319337
var tx3 interfaces.Batch
320338
tx3, err = ind.PushBlock(
321339
mockNonCanonicalBlock2,
@@ -347,6 +365,9 @@ func testPublishAndIndexHeaderNonCanonical(t *testing.T) {
347365
t.Fatal(err)
348366
}
349367

368+
// expect three blocks to be indexed
369+
// a canonical and a non-canonical block at London height,
370+
// non-canonical block at London height + 1
350371
expectedRes := []models.HeaderModel{
351372
{
352373
BlockNumber: mockBlock.Number().String(),
@@ -426,6 +447,7 @@ func testPublishAndIndexTransactionsNonCanonical(t *testing.T) {
426447
t.Fatal(err)
427448
}
428449

450+
// expected transactions in the canonical block
429451
mockBlockTxs := mocks.MockBlock.Transactions()
430452
expectedBlockTxs := []models.TxModel{
431453
{
@@ -490,6 +512,7 @@ func testPublishAndIndexTransactionsNonCanonical(t *testing.T) {
490512
},
491513
}
492514

515+
// expected transactions in the canonical block at London height
493516
mockNonCanonicalBlockTxs := mockNonCanonicalBlock.Transactions()
494517
expectedNonCanonicalBlockTxs := []models.TxModel{
495518
{
@@ -518,6 +541,7 @@ func testPublishAndIndexTransactionsNonCanonical(t *testing.T) {
518541
},
519542
}
520543

544+
// expected transactions in the canonical block at London height + 1
521545
mockNonCanonicalBlock2Txs := mockNonCanonicalBlock2.Transactions()
522546
expectedNonCanonicalBlock2Txs := []models.TxModel{
523547
{
@@ -598,20 +622,23 @@ func testPublishAndIndexReceiptsNonCanonical(t *testing.T) {
598622
t.Fatal(err)
599623
}
600624

625+
// expected receipts in the canonical block
601626
rctCids := []cid.Cid{rct1CID, rct2CID, rct3CID, rct4CID, rct5CID}
602627
expectedBlockRctsMap := make(map[string]models.ReceiptModel, len(mocks.MockReceipts))
603628
for i, mockBlockRct := range mocks.MockReceipts {
604629
rctModel := createRctModel(mockBlockRct, rctCids[i], mockBlock.Number().String())
605630
expectedBlockRctsMap[rctCids[i].String()] = rctModel
606631
}
607632

633+
// expected receipts in the canonical block at London height + 1
608634
nonCanonicalBlockRctCids := []cid.Cid{nonCanonicalBlockRct1CID, nonCanonicalBlockRct2CID}
609635
expectedNonCanonicalBlockRctsMap := make(map[string]models.ReceiptModel, len(mocks.MockNonCanonicalBlockReceipts))
610636
for i, mockNonCanonicalBlockRct := range mocks.MockNonCanonicalBlockReceipts {
611637
rctModel := createRctModel(mockNonCanonicalBlockRct, nonCanonicalBlockRctCids[i], mockNonCanonicalBlock.Number().String())
612638
expectedNonCanonicalBlockRctsMap[nonCanonicalBlockRctCids[i].String()] = rctModel
613639
}
614640

641+
// expected receipts in the canonical block at London height + 1
615642
nonCanonicalBlock2RctCids := []cid.Cid{nonCanonicalBlock2Rct1CID, nonCanonicalBlock2Rct2CID}
616643
expectedNonCanonicalBlock2RctsMap := make(map[string]models.ReceiptModel, len(mocks.MockNonCanonicalBlock2Receipts))
617644
for i, mockNonCanonicalBlock2Rct := range mocks.MockNonCanonicalBlock2Receipts {
@@ -695,6 +722,7 @@ func testPublishAndIndexLogsNonCanonical(t *testing.T) {
695722
}
696723
mockRcts := make([]rctWithBlockHash, 0)
697724

725+
// logs in the canonical block
698726
for _, mockBlockRct := range mocks.MockReceipts {
699727
mockRcts = append(mockRcts, rctWithBlockHash{
700728
mockBlockRct,
@@ -703,6 +731,7 @@ func testPublishAndIndexLogsNonCanonical(t *testing.T) {
703731
})
704732
}
705733

734+
// logs in the canonical block at London height + 1
706735
for _, mockBlockRct := range mocks.MockNonCanonicalBlockReceipts {
707736
mockRcts = append(mockRcts, rctWithBlockHash{
708737
mockBlockRct,
@@ -711,6 +740,7 @@ func testPublishAndIndexLogsNonCanonical(t *testing.T) {
711740
})
712741
}
713742

743+
// logs in the canonical block at London height + 1
714744
for _, mockBlockRct := range mocks.MockNonCanonicalBlock2Receipts {
715745
mockRcts = append(mockRcts, rctWithBlockHash{
716746
mockBlockRct,
@@ -776,6 +806,7 @@ func testPublishAndIndexStateNonCanonical(t *testing.T) {
776806
removedNodeCID, _ := cid.Decode(shared.RemovedNodeStateCID)
777807
stateNodeCIDs := []cid.Cid{state1CID, state2CID, removedNodeCID, removedNodeCID}
778808

809+
// expected state nodes in the canonical and the non-canonical block at London height
779810
expectedStateNodes := make([]models.StateNodeModel, 0)
780811
for i, stateDiff := range mocks.StateDiffs {
781812
expectedStateNodes = append(expectedStateNodes, models.StateNodeModel{
@@ -795,6 +826,7 @@ func testPublishAndIndexStateNonCanonical(t *testing.T) {
795826
}
796827
})
797828

829+
// expected state nodes in the non-canonical block at London height + 1
798830
expectedNonCanonicalBlock2StateNodes := make([]models.StateNodeModel, 0)
799831
for i, stateDiff := range mocks.StateDiffs[:2] {
800832
expectedNonCanonicalBlock2StateNodes = append(expectedNonCanonicalBlock2StateNodes, models.StateNodeModel{
@@ -819,7 +851,7 @@ func testPublishAndIndexStateNonCanonical(t *testing.T) {
819851
require.Equal(t, expectedStateNode, stateNodes[i])
820852
}
821853

822-
// check state nodes for non-canonical block
854+
// check state nodes for non-canonical block at London height
823855
stateNodes = make([]models.StateNodeModel, 0)
824856
err = db.Select(context.Background(), &stateNodes, pgStr, mocks.BlockNumber.Uint64(), mockNonCanonicalBlock.Hash().String())
825857
if err != nil {
@@ -831,7 +863,7 @@ func testPublishAndIndexStateNonCanonical(t *testing.T) {
831863
require.Equal(t, expectedStateNode, stateNodes[i])
832864
}
833865

834-
// check state nodes for non-canonical block at another height
866+
// check state nodes for non-canonical block at London height + 1
835867
stateNodes = make([]models.StateNodeModel, 0)
836868
err = db.Select(context.Background(), &stateNodes, pgStr, mocks.Block2Number.Uint64(), mockNonCanonicalBlock2.Hash().String())
837869
if err != nil {
@@ -855,6 +887,7 @@ func testPublishAndIndexStorageNonCanonical(t *testing.T) {
855887
removedNodeCID, _ := cid.Decode(shared.RemovedNodeStorageCID)
856888
storageNodeCIDs := []cid.Cid{storageCID, removedNodeCID, removedNodeCID, removedNodeCID}
857889

890+
// expected state nodes in the canonical and the non-canonical block at London height
858891
expectedStorageNodes := make([]models.StorageNodeModel, 0)
859892
storageNodeIndex := 0
860893
for _, stateDiff := range mocks.StateDiffs {
@@ -879,6 +912,7 @@ func testPublishAndIndexStorageNonCanonical(t *testing.T) {
879912
}
880913
})
881914

915+
// expected state nodes in the non-canonical block at London height + 1
882916
expectedNonCanonicalBlock2StorageNodes := make([]models.StorageNodeModel, 0)
883917
storageNodeIndex = 0
884918
for _, stateDiff := range mocks.StateDiffs[:2] {
@@ -908,7 +942,7 @@ func testPublishAndIndexStorageNonCanonical(t *testing.T) {
908942
require.Equal(t, expectedStorageNode, storageNodes[i])
909943
}
910944

911-
// check storage nodes for non-canonical block
945+
// check storage nodes for non-canonical block at London height
912946
storageNodes = make([]models.StorageNodeModel, 0)
913947
err = db.Select(context.Background(), &storageNodes, pgStr, mocks.BlockNumber.Uint64(), mockNonCanonicalBlock.Hash().String())
914948
if err != nil {
@@ -920,7 +954,7 @@ func testPublishAndIndexStorageNonCanonical(t *testing.T) {
920954
require.Equal(t, expectedStorageNode, storageNodes[i])
921955
}
922956

923-
// check storage nodes for non-canonical block at another height
957+
// check storage nodes for non-canonical block at London height + 1
924958
storageNodes = make([]models.StorageNodeModel, 0)
925959
err = db.Select(context.Background(), &storageNodes, pgStr, mockNonCanonicalBlock2.NumberU64(), mockNonCanonicalBlock2.Hash().String())
926960
if err != nil {

statediff/indexer/database/sql/pgx_indexer_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func setupPGXNonCanonical(t *testing.T) {
5757
setupTestDataNonCanonical(t)
5858
}
5959

60+
// Test indexer for a canonical block
6061
func TestPGXIndexer(t *testing.T) {
6162
t.Run("Publish and index header IPLDs in a single tx", func(t *testing.T) {
6263
setupPGX(t)
@@ -591,6 +592,7 @@ func TestPGXIndexer(t *testing.T) {
591592
})
592593
}
593594

595+
// Test indexer for a canonical + a non-canonical block at London height + a non-canonical block at London height + 1
594596
func TestPGXIndexerNonCanonical(t *testing.T) {
595597
t.Run("Publish and index header", func(t *testing.T) {
596598
setupPGXNonCanonical(t)

statediff/indexer/database/sql/sqlx_indexer_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func setupSQLXNonCanonical(t *testing.T) {
5858
setupTestDataNonCanonical(t)
5959
}
6060

61+
// Test indexer for a canonical block
6162
func TestSQLXIndexer(t *testing.T) {
6263
t.Run("Publish and index header IPLDs in a single tx", func(t *testing.T) {
6364
setupSQLX(t)
@@ -584,6 +585,7 @@ func TestSQLXIndexer(t *testing.T) {
584585
})
585586
}
586587

588+
// Test indexer for a canonical + a non-canonical block at London height + a non-canonical block at London height + 1
587589
func TestSQLXIndexerNonCanonical(t *testing.T) {
588590
t.Run("Publish and index header", func(t *testing.T) {
589591
setupSQLXNonCanonical(t)

statediff/indexer/mocks/test_data.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ var (
4040
// TODO: Update this to `MainnetChainConfig` when `LondonBlock` is added
4141
TestConfig = params.RopstenChainConfig
4242
BlockNumber = TestConfig.LondonBlock
43-
MockHeader = types.Header{
43+
44+
// canonical block at London height
45+
// includes 5 transactions: 3 Legacy + 1 EIP-2930 + 1 EIP-1559
46+
MockHeader = types.Header{
4447
Time: 0,
4548
Number: new(big.Int).Set(BlockNumber),
4649
Root: common.HexToHash("0x0"),
@@ -55,12 +58,16 @@ var (
5558
MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts, new(trie.Trie))
5659
MockHeaderRlp, _ = rlp.EncodeToBytes(MockBlock.Header())
5760

61+
// non-canonical block at London height
62+
// includes 2nd and 5th transactions from the canonical block
5863
MockNonCanonicalHeader = MockHeader
5964
MockNonCanonicalBlockTransactions = types.Transactions{MockTransactions[1], MockTransactions[4]}
6065
MockNonCanonicalBlockReceipts = createNonCanonicalBlockReceipts(TestConfig, BlockNumber, MockNonCanonicalBlockTransactions)
6166
MockNonCanonicalBlock = types.NewBlock(&MockNonCanonicalHeader, MockNonCanonicalBlockTransactions, nil, MockNonCanonicalBlockReceipts, new(trie.Trie))
6267
MockNonCanonicalHeaderRlp, _ = rlp.EncodeToBytes(MockNonCanonicalBlock.Header())
6368

69+
// non-canonical block at London height + 1
70+
// includes 3rd and 5th transactions from the canonical block
6471
Block2Number = big.NewInt(BlockNumber.Int64() + 1)
6572
MockNonCanonicalHeader2 = types.Header{
6673
Time: 0,
@@ -465,6 +472,7 @@ func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big.
465472
return types.Transactions{signedTrx1, signedTrx2, signedTrx3, signedTrx4, signedTrx5}, types.Receipts{mockReceipt1, mockReceipt2, mockReceipt3, mockReceipt4, mockReceipt5}, senderAddr
466473
}
467474

475+
// createNonCanonicalBlockReceipts is a helper function to generate mock receipts with mock logs for non-canonical blocks
468476
func createNonCanonicalBlockReceipts(config *params.ChainConfig, blockNumber *big.Int, transactions types.Transactions) types.Receipts {
469477
transactionSigner := types.MakeSigner(config, blockNumber)
470478
mockCurve := elliptic.P256()

0 commit comments

Comments
 (0)