Skip to content

Commit e74a502

Browse files
SanLeo461acolytec3
andauthored
client: add blockTimestamp to eth_getLogs, add tests (#4074)
* client: add blockTimestamp to eth_getLogs, add tests * client: cleanup getLogs blocktimestamp tests --------- Co-authored-by: acolytec3 <[email protected]>
1 parent 539aaed commit e74a502

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

packages/client/src/rpc/modules/eth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ type JSONRPCLog = {
100100
transactionHash: string | null // DATA, 32 Bytes - hash of the transactions this log was created from. null when it's pending.
101101
blockHash: string | null // DATA, 32 Bytes - hash of the block where this log was in. null when it's pending.
102102
blockNumber: string | null // QUANTITY - the block number where this log was in. null when it's pending.
103+
blockTimestamp: string | null // QUANTITY - the block timestamp where this log was in. null when it's pending.
103104
address: string // DATA, 20 Bytes - address from which this log originated.
104105
data: string // DATA - contains one or more 32 Bytes non-indexed arguments of the log.
105106
topics: string[] // Array of DATA - Array of 0 to 4 32 Bytes DATA of indexed log arguments.
@@ -174,6 +175,7 @@ const toJSONRPCLog = async (
174175
transactionHash: tx !== undefined ? bytesToHex(tx.hash()) : null,
175176
blockHash: block ? bytesToHex(block.hash()) : null,
176177
blockNumber: block ? bigIntToHex(block.header.number) : null,
178+
blockTimestamp: block ? bigIntToHex(block.header.timestamp) : null,
177179
address: bytesToHex(log[0]),
178180
topics: log[1].map(bytesToHex),
179181
data: bytesToHex(log[2]),

packages/client/test/rpc/eth/getLogs.spec.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { createLegacyTx } from '@ethereumjs/tx'
2-
import { bytesToHex, createContractAddress, hexToBytes } from '@ethereumjs/util'
2+
import {
3+
bigIntToHex,
4+
bytesToHex,
5+
createContractAddress,
6+
hexToBigInt,
7+
hexToBytes,
8+
} from '@ethereumjs/util'
39
import { assert, describe, it } from 'vitest'
410

511
import { INVALID_PARAMS } from '../../../src/rpc/error-code.ts'
@@ -75,7 +81,7 @@ describe(method, async () => {
7581
{ common },
7682
).sign(dummy.privKey)
7783

78-
await runBlockWithTxs(chain, execution, [tx1, tx2, tx3, tx4])
84+
let block = await runBlockWithTxs(chain, execution, [tx1, tx2, tx3, tx4])
7985

8086
// compare the logs
8187
let res = await rpc.request(method, [{ fromBlock: 'earliest', toBlock: 'latest' }])
@@ -100,6 +106,12 @@ describe(method, async () => {
100106
assert.fail(`should return the correct logs (fromBlock/toBlock as 'earliest' and 'latest')`)
101107
}
102108

109+
assert.strictEqual(
110+
hexToBigInt(res.result[0].blockTimestamp),
111+
block.header.timestamp,
112+
`should return the correct blockTimestamp (fromBlock/toBlock as 'earliest' and 'latest')`,
113+
)
114+
103115
// get the logs using fromBlock/toBlock as numbers
104116
res = await rpc.request(method, [{ fromBlock: '0x0', toBlock: '0x1' }])
105117
assert.strictEqual(
@@ -206,6 +218,40 @@ describe(method, async () => {
206218
20,
207219
'should return the correct logs (filter by blockHash)',
208220
)
221+
222+
// test adding more logs and checking timestamps against multiple blocks
223+
const tx5 = createLegacyTx(
224+
{
225+
...txData,
226+
data,
227+
to: contractAddr1,
228+
nonce: 4,
229+
},
230+
{ common },
231+
).sign(dummy.privKey)
232+
const tx6 = createLegacyTx(
233+
{
234+
...txData,
235+
data,
236+
to: contractAddr2,
237+
nonce: 5,
238+
},
239+
{ common },
240+
).sign(dummy.privKey)
241+
242+
const block1Timestamp = bigIntToHex(block.header.timestamp)
243+
block = await runBlockWithTxs(chain, execution, [tx5, tx6])
244+
const block2Timestamp = bigIntToHex(block.header.timestamp)
245+
246+
res = await rpc.request(method, [{ fromBlock: 'earliest', toBlock: 'latest' }])
247+
const block1Logs: any[] = res.result.filter((log: any) => log.blockNumber === '0x1')
248+
const block2Logs: any[] = res.result.filter((log: any) => log.blockNumber === '0x2')
249+
250+
assert.isTrue(
251+
block1Logs.every((log) => log.blockTimestamp === block1Timestamp) &&
252+
block2Logs.every((log) => log.blockTimestamp === block2Timestamp),
253+
'should return the correct log timestamps across multiple blocks',
254+
)
209255
})
210256

211257
it('call with invalid params', async () => {

0 commit comments

Comments
 (0)