Skip to content

Commit bf3498e

Browse files
s1narjl493456442
authored andcommitted
eth/filters: add timestamp to derived logs (ethereum#31887)
The block timestamp field is now added to the logs returned by eth_getLogs.
1 parent aee3a29 commit bf3498e

File tree

16 files changed

+99
-77
lines changed

16 files changed

+99
-77
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
303303
}
304304

305305
// Set the receipt logs and create the bloom filter.
306-
receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash)
306+
receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash, vmContext.Time)
307307
receipt.Bloom = types.CreateBloom(receipt)
308308

309309
// These three are non-consensus fields:

core/rawdb/accessors_chain_test.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -680,26 +680,28 @@ func makeTestReceipts(n int, nPerBlock int) []types.Receipts {
680680
}
681681

682682
type fullLogRLP struct {
683-
Address common.Address
684-
Topics []common.Hash
685-
Data []byte
686-
BlockNumber uint64
687-
TxHash common.Hash
688-
TxIndex uint
689-
BlockHash common.Hash
690-
Index uint
683+
Address common.Address
684+
Topics []common.Hash
685+
Data []byte
686+
BlockNumber uint64
687+
BlockTimestamp uint64
688+
TxHash common.Hash
689+
TxIndex uint
690+
BlockHash common.Hash
691+
Index uint
691692
}
692693

693694
func newFullLogRLP(l *types.Log) *fullLogRLP {
694695
return &fullLogRLP{
695-
Address: l.Address,
696-
Topics: l.Topics,
697-
Data: l.Data,
698-
BlockNumber: l.BlockNumber,
699-
TxHash: l.TxHash,
700-
TxIndex: l.TxIndex,
701-
BlockHash: l.BlockHash,
702-
Index: l.Index,
696+
Address: l.Address,
697+
Topics: l.Topics,
698+
Data: l.Data,
699+
BlockNumber: l.BlockNumber,
700+
BlockTimestamp: l.BlockTimestamp,
701+
TxHash: l.TxHash,
702+
TxIndex: l.TxIndex,
703+
BlockHash: l.BlockHash,
704+
Index: l.Index,
703705
}
704706
}
705707

@@ -834,7 +836,7 @@ func TestDeriveLogFields(t *testing.T) {
834836
// Derive log metadata fields
835837
number := big.NewInt(1)
836838
hash := common.BytesToHash([]byte{0x03, 0x14})
837-
types.Receipts(receipts).DeriveFields(params.TestChainConfig, hash, number.Uint64(), 0, big.NewInt(0), big.NewInt(0), txs)
839+
types.Receipts(receipts).DeriveFields(params.TestChainConfig, hash, number.Uint64(), 12, big.NewInt(0), big.NewInt(0), txs)
838840

839841
// Iterate over all the computed fields and check that they're correct
840842
logIndex := uint(0)
@@ -846,6 +848,9 @@ func TestDeriveLogFields(t *testing.T) {
846848
if receipts[i].Logs[j].BlockHash != hash {
847849
t.Errorf("receipts[%d].Logs[%d].BlockHash = %s, want %s", i, j, receipts[i].Logs[j].BlockHash.String(), hash.String())
848850
}
851+
if receipts[i].Logs[j].BlockTimestamp != 12 {
852+
t.Errorf("receipts[%d].Logs[%d].BlockTimestamp = %d, want %d", i, j, receipts[i].Logs[j].BlockTimestamp, 12)
853+
}
849854
if receipts[i].Logs[j].TxHash != txs[i].Hash() {
850855
t.Errorf("receipts[%d].Logs[%d].TxHash = %s, want %s", i, j, receipts[i].Logs[j].TxHash.String(), txs[i].Hash().String())
851856
}

core/state/statedb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,12 @@ func (s *StateDB) AddLog(log *types.Log) {
252252

253253
// GetLogs returns the logs matching the specified transaction hash, and annotates
254254
// them with the given blockNumber and blockHash.
255-
func (s *StateDB) GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log {
255+
func (s *StateDB) GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash, blockTime uint64) []*types.Log {
256256
logs := s.logs[hash]
257257
for _, l := range logs {
258258
l.BlockNumber = blockNumber
259259
l.BlockHash = blockHash
260+
l.BlockTimestamp = blockTime
260261
}
261262
return logs
262263
}

core/state/statedb_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
669669
return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d",
670670
state.GetRefund(), checkstate.GetRefund())
671671
}
672-
if !reflect.DeepEqual(state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{})) {
672+
if !reflect.DeepEqual(state.GetLogs(common.Hash{}, 0, common.Hash{}, 0), checkstate.GetLogs(common.Hash{}, 0, common.Hash{}, 0)) {
673673
return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v",
674-
state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{}))
674+
state.GetLogs(common.Hash{}, 0, common.Hash{}, 0), checkstate.GetLogs(common.Hash{}, 0, common.Hash{}, 0))
675675
}
676676
if !maps.Equal(state.journal.dirties, checkstate.journal.dirties) {
677677
getKeys := func(dirty map[common.Address]int) string {

core/state_processor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
9898
}
9999
statedb.SetTxContext(tx.Hash(), i)
100100

101-
receipt, err := ApplyTransactionWithEVM(msg, gp, statedb, blockNumber, blockHash, tx, usedGas, evm)
101+
receipt, err := ApplyTransactionWithEVM(msg, gp, statedb, blockNumber, blockHash, context.Time, tx, usedGas, evm)
102102
if err != nil {
103103
return nil, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
104104
}
@@ -137,7 +137,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
137137
// ApplyTransactionWithEVM attempts to apply a transaction to the given state database
138138
// and uses the input parameters for its environment similar to ApplyTransaction. However,
139139
// this method takes an already created EVM instance as input.
140-
func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (receipt *types.Receipt, err error) {
140+
func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, blockTime uint64, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (receipt *types.Receipt, err error) {
141141
if hooks := evm.Config.Tracer; hooks != nil {
142142
if hooks.OnTxStart != nil {
143143
hooks.OnTxStart(evm.GetVMContext(), tx, msg.From)
@@ -166,11 +166,11 @@ func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB,
166166
statedb.AccessEvents().Merge(evm.AccessEvents)
167167
}
168168

169-
return MakeReceipt(evm, result, statedb, blockNumber, blockHash, tx, *usedGas, root), nil
169+
return MakeReceipt(evm, result, statedb, blockNumber, blockHash, blockTime, tx, *usedGas, root), nil
170170
}
171171

172172
// MakeReceipt generates the receipt object for a transaction given its execution result.
173-
func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas uint64, root []byte) *types.Receipt {
173+
func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, blockTime uint64, tx *types.Transaction, usedGas uint64, root []byte) *types.Receipt {
174174
// Create a new receipt for the transaction, storing the intermediate root and gas used
175175
// by the tx.
176176
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: usedGas}
@@ -193,7 +193,7 @@ func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, b
193193
}
194194

195195
// Set the receipt logs and create the bloom filter.
196-
receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash)
196+
receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash, blockTime)
197197
receipt.Bloom = types.CreateBloom(receipt)
198198
receipt.BlockHash = blockHash
199199
receipt.BlockNumber = blockNumber
@@ -211,7 +211,7 @@ func ApplyTransaction(evm *vm.EVM, gp *GasPool, statedb *state.StateDB, header *
211211
return nil, err
212212
}
213213
// Create a new context to be used in the EVM environment
214-
return ApplyTransactionWithEVM(msg, gp, statedb, header.Number, header.Hash(), tx, usedGas, evm)
214+
return ApplyTransactionWithEVM(msg, gp, statedb, header.Number, header.Hash(), header.Time, tx, usedGas, evm)
215215
}
216216

217217
// ProcessBeaconBlockRoot applies the EIP-4788 system call to the beacon block root

core/types/gen_log_json.go

Lines changed: 24 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type Log struct {
4545
TxIndex uint `json:"transactionIndex" rlp:"-"`
4646
// hash of the block in which the transaction was included
4747
BlockHash common.Hash `json:"blockHash" rlp:"-"`
48+
// timestamp of the block in which the transaction was included
49+
BlockTimestamp uint64 `json:"blockTimestamp" rlp:"-"`
4850
// index of the log in the block
4951
Index uint `json:"logIndex" rlp:"-"`
5052

core/types/receipt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, nu
367367
for j := 0; j < len(rs[i].Logs); j++ {
368368
rs[i].Logs[j].BlockNumber = number
369369
rs[i].Logs[j].BlockHash = hash
370+
rs[i].Logs[j].BlockTimestamp = time
370371
rs[i].Logs[j].TxHash = rs[i].TxHash
371372
rs[i].Logs[j].TxIndex = uint(i)
372373
rs[i].Logs[j].Index = logIndex

core/types/receipt_test.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,23 @@ func getTestReceipts() Receipts {
173173
Address: common.BytesToAddress([]byte{0x11}),
174174
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
175175
// derived fields:
176-
BlockNumber: blockNumber.Uint64(),
177-
TxHash: txs[0].Hash(),
178-
TxIndex: 0,
179-
BlockHash: blockHash,
180-
Index: 0,
176+
BlockNumber: blockNumber.Uint64(),
177+
TxHash: txs[0].Hash(),
178+
TxIndex: 0,
179+
BlockHash: blockHash,
180+
BlockTimestamp: blockTime,
181+
Index: 0,
181182
},
182183
{
183184
Address: common.BytesToAddress([]byte{0x01, 0x11}),
184185
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
185186
// derived fields:
186-
BlockNumber: blockNumber.Uint64(),
187-
TxHash: txs[0].Hash(),
188-
TxIndex: 0,
189-
BlockHash: blockHash,
190-
Index: 1,
187+
BlockNumber: blockNumber.Uint64(),
188+
TxHash: txs[0].Hash(),
189+
TxIndex: 0,
190+
BlockHash: blockHash,
191+
BlockTimestamp: blockTime,
192+
Index: 1,
191193
},
192194
},
193195
// derived fields:
@@ -207,21 +209,23 @@ func getTestReceipts() Receipts {
207209
Address: common.BytesToAddress([]byte{0x22}),
208210
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
209211
// derived fields:
210-
BlockNumber: blockNumber.Uint64(),
211-
TxHash: txs[1].Hash(),
212-
TxIndex: 1,
213-
BlockHash: blockHash,
214-
Index: 2,
212+
BlockNumber: blockNumber.Uint64(),
213+
TxHash: txs[1].Hash(),
214+
TxIndex: 1,
215+
BlockHash: blockHash,
216+
BlockTimestamp: blockTime,
217+
Index: 2,
215218
},
216219
{
217220
Address: common.BytesToAddress([]byte{0x02, 0x22}),
218221
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
219222
// derived fields:
220-
BlockNumber: blockNumber.Uint64(),
221-
TxHash: txs[1].Hash(),
222-
TxIndex: 1,
223-
BlockHash: blockHash,
224-
Index: 3,
223+
BlockNumber: blockNumber.Uint64(),
224+
TxHash: txs[1].Hash(),
225+
TxIndex: 1,
226+
BlockHash: blockHash,
227+
BlockTimestamp: blockTime,
228+
Index: 3,
225229
},
226230
},
227231
// derived fields:

eth/filters/filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*typ
463463
// such as tx index, block hash, etc.
464464
// Notably tx hash is NOT filled in because it needs
465465
// access to block body data.
466-
cached, err := f.sys.cachedLogElem(ctx, hash, header.Number.Uint64())
466+
cached, err := f.sys.cachedLogElem(ctx, hash, header.Number.Uint64(), header.Time)
467467
if err != nil {
468468
return nil, err
469469
}

0 commit comments

Comments
 (0)