Skip to content

Commit aeeac14

Browse files
committed
Populate BlockTimestamp field on Logs
1 parent 549ffc6 commit aeeac14

File tree

4 files changed

+128
-48
lines changed

4 files changed

+128
-48
lines changed

storage/index_test.go

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package storage_test
22

33
import (
4-
"fmt"
54
"testing"
65

76
pebble2 "github.com/cockroachdb/pebble"
@@ -58,6 +57,7 @@ func TestReceipts(t *testing.T) {
5857
require.NoError(t, err)
5958

6059
suite.Run(t, &ReceiptTestSuite{
60+
BlocksIndexer: bl,
6161
ReceiptIndexer: pebble.NewReceipts(db),
6262
DB: db,
6363
})
@@ -277,16 +277,23 @@ func (b *BlockTestSuite) TestHeights() {
277277

278278
type ReceiptTestSuite struct {
279279
suite.Suite
280+
BlocksIndexer storage.BlockIndexer
280281
ReceiptIndexer storage.ReceiptIndexer
281282
DB *pebble.Storage
282283
}
283284

284285
func (s *ReceiptTestSuite) TestStoreReceipt() {
285286

286287
s.Run("store receipt successfully", func() {
287-
receipt := mocks.NewReceipt(1, common.HexToHash("0xf1"))
288288
batch := s.DB.NewBatch()
289-
err := s.ReceiptIndexer.Store([]*models.Receipt{receipt}, batch)
289+
flowID := flow.Identifier{0x01}
290+
block := mocks.NewBlock(1)
291+
292+
err := s.BlocksIndexer.Store(block.Height+1, flowID, block, batch)
293+
s.Require().NoError(err)
294+
295+
receipt := mocks.NewReceipt(block)
296+
err = s.ReceiptIndexer.Store([]*models.Receipt{receipt}, batch)
290297
s.Require().NoError(err)
291298

292299
err = batch.Commit(pebble2.Sync)
@@ -295,10 +302,17 @@ func (s *ReceiptTestSuite) TestStoreReceipt() {
295302

296303
s.Run("store multiple receipts at same height", func() {
297304
const height = 5
305+
batch := s.DB.NewBatch()
306+
flowID := flow.Identifier{0x01}
307+
block := mocks.NewBlock(height)
308+
309+
err := s.BlocksIndexer.Store(height+1, flowID, block, batch)
310+
s.Require().NoError(err)
311+
298312
receipts := []*models.Receipt{
299-
mocks.NewReceipt(height, common.HexToHash("0x1")),
300-
mocks.NewReceipt(height, common.HexToHash("0x2")),
301-
mocks.NewReceipt(height, common.HexToHash("0x3")),
313+
mocks.NewReceipt(block),
314+
mocks.NewReceipt(block),
315+
mocks.NewReceipt(block),
302316
}
303317
// Log index field holds the index position in the entire block
304318
logIndex := uint(0)
@@ -309,8 +323,7 @@ func (s *ReceiptTestSuite) TestStoreReceipt() {
309323
}
310324
}
311325

312-
batch := s.DB.NewBatch()
313-
err := s.ReceiptIndexer.Store(receipts, batch)
326+
err = s.ReceiptIndexer.Store(receipts, batch)
314327
s.Require().NoError(err)
315328

316329
err = batch.Commit(pebble2.Sync)
@@ -325,22 +338,39 @@ func (s *ReceiptTestSuite) TestStoreReceipt() {
325338
})
326339

327340
s.Run("fail to store multiple receipts with different heights", func() {
341+
batch := s.DB.NewBatch()
342+
flowID := flow.Identifier{0x01}
343+
block1 := mocks.NewBlock(1)
344+
345+
err := s.BlocksIndexer.Store(block1.Height+1, flowID, block1, batch)
346+
s.Require().NoError(err)
347+
348+
block2 := mocks.NewBlock(2)
349+
350+
err = s.BlocksIndexer.Store(block2.Height+1, flowID, block2, batch)
351+
s.Require().NoError(err)
352+
328353
receipts := []*models.Receipt{
329-
mocks.NewReceipt(1, common.HexToHash("0x1")),
330-
mocks.NewReceipt(2, common.HexToHash("0x2")),
354+
mocks.NewReceipt(block1),
355+
mocks.NewReceipt(block2),
331356
}
332357

333-
batch := s.DB.NewBatch()
334-
err := s.ReceiptIndexer.Store(receipts, batch)
358+
err = s.ReceiptIndexer.Store(receipts, batch)
335359
s.Require().EqualError(err, "can't store receipts for multiple heights")
336360
})
337361
}
338362

339363
func (s *ReceiptTestSuite) TestGetReceiptByTransactionID() {
340364
s.Run("existing transaction ID", func() {
341-
receipt := mocks.NewReceipt(2, common.HexToHash("0xf2"))
342365
batch := s.DB.NewBatch()
343-
err := s.ReceiptIndexer.Store([]*models.Receipt{receipt}, batch)
366+
flowID := flow.Identifier{0x01}
367+
block := mocks.NewBlock(2)
368+
369+
err := s.BlocksIndexer.Store(block.Height+1, flowID, block, batch)
370+
s.Require().NoError(err)
371+
372+
receipt := mocks.NewReceipt(block)
373+
err = s.ReceiptIndexer.Store([]*models.Receipt{receipt}, batch)
344374
s.Require().NoError(err)
345375

346376
err = batch.Commit(pebble2.Sync)
@@ -361,9 +391,15 @@ func (s *ReceiptTestSuite) TestGetReceiptByTransactionID() {
361391

362392
func (s *ReceiptTestSuite) TestGetReceiptByBlockHeight() {
363393
s.Run("existing block height", func() {
364-
receipt := mocks.NewReceipt(3, common.HexToHash("0x1"))
365394
batch := s.DB.NewBatch()
366-
err := s.ReceiptIndexer.Store([]*models.Receipt{receipt}, batch)
395+
flowID := flow.Identifier{0x01}
396+
block := mocks.NewBlock(3)
397+
398+
err := s.BlocksIndexer.Store(block.Height+1, flowID, block, batch)
399+
s.Require().NoError(err)
400+
401+
receipt := mocks.NewReceipt(block)
402+
err = s.ReceiptIndexer.Store([]*models.Receipt{receipt}, batch)
367403
s.Require().NoError(err)
368404

369405
err = batch.Commit(pebble2.Sync)
@@ -372,7 +408,13 @@ func (s *ReceiptTestSuite) TestGetReceiptByBlockHeight() {
372408
batch = s.DB.NewBatch()
373409

374410
// add one more receipt that shouldn't be retrieved
375-
r := mocks.NewReceipt(4, common.HexToHash("0x2"))
411+
flowID = flow.Identifier{0x04}
412+
block4 := mocks.NewBlock(4)
413+
414+
err = s.BlocksIndexer.Store(block4.Height+1, flowID, block4, batch)
415+
s.Require().NoError(err)
416+
417+
r := mocks.NewReceipt(block4)
376418
s.Require().NoError(s.ReceiptIndexer.Store([]*models.Receipt{r}, batch))
377419

378420
err = batch.Commit(pebble2.Sync)
@@ -399,11 +441,18 @@ func (s *ReceiptTestSuite) TestBloomsForBlockRange() {
399441
testHeights := make([]uint64, 0)
400442

401443
for i := start; i < end; i++ {
402-
r := mocks.NewReceipt(i, common.HexToHash(fmt.Sprintf("0xf1%d", i)))
444+
batch := s.DB.NewBatch()
445+
flowID := flow.Identifier{0x0i}
446+
block := mocks.NewBlock(i)
447+
448+
err := s.BlocksIndexer.Store(block.Height+1, flowID, block, batch)
449+
s.Require().NoError(err)
450+
451+
r := mocks.NewReceipt(block)
403452
testBlooms = append(testBlooms, &r.Bloom)
404453
testHeights = append(testHeights, i)
405-
batch := s.DB.NewBatch()
406-
err := s.ReceiptIndexer.Store([]*models.Receipt{r}, batch)
454+
batch = s.DB.NewBatch()
455+
err = s.ReceiptIndexer.Store([]*models.Receipt{r}, batch)
407456
s.Require().NoError(err)
408457

409458
err = batch.Commit(pebble2.Sync)
@@ -441,13 +490,20 @@ func (s *ReceiptTestSuite) TestBloomsForBlockRange() {
441490
testHeights := make([]uint64, 0)
442491

443492
for i := start; i < end; i++ {
444-
r1 := mocks.NewReceipt(i, common.HexToHash(fmt.Sprintf("0x%d", i)))
445-
r2 := mocks.NewReceipt(i, common.HexToHash(fmt.Sprintf("0x%d", i)))
493+
batch := s.DB.NewBatch()
494+
flowID := flow.Identifier{0x0i}
495+
block := mocks.NewBlock(i)
496+
497+
err := s.BlocksIndexer.Store(block.Height+1, flowID, block, batch)
498+
s.Require().NoError(err)
499+
500+
r1 := mocks.NewReceipt(block)
501+
r2 := mocks.NewReceipt(block)
446502
receipts := []*models.Receipt{r1, r2}
447503

448-
batch := s.DB.NewBatch()
504+
batch = s.DB.NewBatch()
449505
s.Require().NoError(s.ReceiptIndexer.Store(receipts, batch))
450-
err := batch.Commit(pebble2.Sync)
506+
err = batch.Commit(pebble2.Sync)
451507
s.Require().NoError(err)
452508

453509
testBlooms = append(testBlooms, &r1.Bloom, &r2.Bloom)
@@ -494,13 +550,20 @@ func (s *ReceiptTestSuite) TestBloomsForBlockRange() {
494550

495551
var expectedBloom *types.Bloom
496552
for i := start; i < end; i++ {
497-
r1 := mocks.NewReceipt(i, common.HexToHash(fmt.Sprintf("0x%d", i)))
553+
batch := s.DB.NewBatch()
554+
flowID := flow.Identifier{0x0i}
555+
block := mocks.NewBlock(i)
556+
557+
err := s.BlocksIndexer.Store(block.Height+1, flowID, block, batch)
558+
s.Require().NoError(err)
559+
560+
r1 := mocks.NewReceipt(block)
498561
receipts := []*models.Receipt{r1}
499562

500-
batch := s.DB.NewBatch()
563+
batch = s.DB.NewBatch()
501564
s.Require().NoError(s.ReceiptIndexer.Store(receipts, batch))
502565

503-
err := batch.Commit(pebble2.Sync)
566+
err = batch.Commit(pebble2.Sync)
504567
s.Require().NoError(err)
505568

506569
if i == specific {

storage/mocks/mocks.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,44 @@ func NewBlock(height uint64) *models.Block {
3434
}
3535
}
3636

37-
func NewReceipt(height uint64, ID common.Hash) *models.Receipt {
38-
txHash := common.HexToHash(fmt.Sprintf("0xff%d", height))
37+
func NewReceipt(block *models.Block) *models.Receipt {
38+
txHash := common.HexToHash(fmt.Sprintf("0xff%d", block.Height))
39+
blockHash, _ := block.Hash()
40+
3941
return &models.Receipt{
4042
PostState: common.Hash{2}.Bytes(),
4143
CumulativeGasUsed: 3,
4244
Logs: []*gethTypes.Log{
4345
{
44-
Address: common.BytesToAddress([]byte{0x22}),
45-
Topics: []common.Hash{common.HexToHash("alfa"), common.HexToHash("bravo")},
46-
BlockNumber: height,
47-
TxHash: txHash,
48-
TxIndex: 1,
49-
BlockHash: ID,
50-
Index: 0,
51-
Data: fmt.Appendf(nil, "data-1%d", height),
46+
Address: common.BytesToAddress([]byte{0x22}),
47+
Topics: []common.Hash{common.HexToHash("alfa"), common.HexToHash("bravo")},
48+
BlockNumber: block.Height,
49+
TxHash: txHash,
50+
TxIndex: 1,
51+
BlockHash: blockHash,
52+
BlockTimestamp: block.Timestamp,
53+
Index: 0,
54+
Data: fmt.Appendf(nil, "data-1%d", block.Height),
5255
},
5356
{
54-
Address: common.BytesToAddress([]byte{0x02, 0x22}),
55-
Topics: []common.Hash{common.HexToHash("charlie"), common.HexToHash("delta")},
56-
BlockNumber: height,
57-
TxHash: txHash,
58-
TxIndex: 1,
59-
BlockHash: ID,
60-
Index: 1,
61-
Data: fmt.Appendf(nil, "data-2%d", height),
57+
Address: common.BytesToAddress([]byte{0x02, 0x22}),
58+
Topics: []common.Hash{common.HexToHash("charlie"), common.HexToHash("delta")},
59+
BlockNumber: block.Height,
60+
TxHash: txHash,
61+
TxIndex: 1,
62+
BlockHash: blockHash,
63+
BlockTimestamp: block.Timestamp,
64+
Index: 1,
65+
Data: fmt.Appendf(nil, "data-2%d", block.Height),
6266
},
6367
},
6468
TxHash: txHash,
6569
GasUsed: 2,
6670
EffectiveGasPrice: big.NewInt(22),
67-
BlockHash: ID,
68-
BlockNumber: big.NewInt(int64(height)),
71+
BlockHash: blockHash,
72+
BlockNumber: big.NewInt(int64(block.Height)),
6973
TransactionIndex: 1,
70-
Bloom: gethTypes.Bloom{byte(height), byte(rand.Int())},
74+
Bloom: gethTypes.Bloom{byte(block.Height), byte(rand.Int())},
7175
}
7276
}
7377

storage/pebble/receipts.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,24 @@ func (r *Receipts) getByBlockHeight(height []byte) ([]*models.Receipt, error) {
130130
return nil, err
131131
}
132132

133+
blockBytes, err := r.store.get(blockHeightKey, height)
134+
if err != nil {
135+
return nil, fmt.Errorf("failed to get block: %w", err)
136+
}
137+
138+
block, err := models.NewBlockFromBytes(blockBytes)
139+
if err != nil {
140+
return nil, err
141+
}
142+
133143
// Log index field holds the index position in the entire block
134144
logIndex := uint(0)
135145
for _, rcp := range receipts {
136146
// dynamically populate the values since they are not stored to save space
137147
for _, l := range rcp.Logs {
138148
l.BlockNumber = rcp.BlockNumber.Uint64()
139149
l.BlockHash = rcp.BlockHash
150+
l.BlockTimestamp = block.Timestamp
140151
l.TxHash = rcp.TxHash
141152
l.TxIndex = rcp.TransactionIndex
142153
l.Index = logIndex

tests/web3js/eth_logs_filtering_test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ it('emit logs and retrieve them using different filters', async () => {
146146
transactionHash: '0x0c2b2477ab81c9132c5c4fd4f50935bc5807fbf4cf3bf3b69173491b68d2ca8b',
147147
transactionIndex: '0x0',
148148
blockHash: latestBlock.hash,
149+
blockTimestamp: web3.utils.toHex(latestBlock.timestamp),
149150
logIndex: '0x0',
150151
removed: false
151152
}
@@ -182,6 +183,7 @@ it('emit logs and retrieve them using different filters', async () => {
182183
transactionHash: '0x0c2b2477ab81c9132c5c4fd4f50935bc5807fbf4cf3bf3b69173491b68d2ca8b',
183184
transactionIndex: '0x0',
184185
blockHash: latestBlock.hash,
186+
blockTimestamp: web3.utils.toHex(latestBlock.timestamp),
185187
logIndex: '0x0',
186188
removed: false
187189
}

0 commit comments

Comments
 (0)