Skip to content

Commit fb7d52a

Browse files
authored
client: expand getLogs to accept array of addresses (#1783)
* expand getLogs to accept array of addresses * bump getLogs block range limit from 2000 to 2500 for teku
1 parent 010322d commit fb7d52a

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

packages/client/lib/execution/receipt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class ReceiptsManager extends MetaDBManager {
8282
/**
8383
* Block range limit for getLogs
8484
*/
85-
GET_LOGS_BLOCK_RANGE_LIMIT = 2000
85+
GET_LOGS_BLOCK_RANGE_LIMIT = 2500
8686

8787
/**
8888
* Saves receipts to db. Also saves tx hash indexes if within txLookupLimit,
@@ -159,7 +159,7 @@ export class ReceiptsManager extends MetaDBManager {
159159
async getLogs(
160160
from: Block,
161161
to: Block,
162-
address?: Buffer,
162+
addresses?: Buffer[],
163163
topics: (Buffer | Buffer[] | null)[] = []
164164
): Promise<GetLogsReturn> {
165165
const returnedLogs: GetLogsReturn = []
@@ -181,8 +181,8 @@ export class ReceiptsManager extends MetaDBManager {
181181
}))
182182
)
183183
}
184-
if (address) {
185-
logs = logs.filter((l) => l.log[0].equals(address))
184+
if (addresses && addresses.length > 0) {
185+
logs = logs.filter((l) => addresses.some((a) => a.equals(l.log[0])))
186186
}
187187
if (topics.length > 0) {
188188
// From https://eth.wiki/json-rpc/API:

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ export class Eth {
390390
validators.object({
391391
fromBlock: validators.optional(validators.blockOption),
392392
toBlock: validators.optional(validators.blockOption),
393-
address: validators.optional(validators.address),
393+
address: validators.optional(
394+
validators.either(validators.array(validators.address), validators.address)
395+
),
394396
topics: validators.optional(
395397
validators.array(
396398
validators.optional(
@@ -845,12 +847,15 @@ export class Eth {
845847
return toBuffer(t)
846848
}
847849
})
848-
const logs = await this.receiptsManager.getLogs(
849-
from,
850-
to,
851-
address ? toBuffer(address) : undefined,
852-
formattedTopics
853-
)
850+
let addrs
851+
if (address) {
852+
if (Array.isArray(address)) {
853+
addrs = address.map((a) => toBuffer(a))
854+
} else {
855+
addrs = [toBuffer(address)]
856+
}
857+
}
858+
const logs = await this.receiptsManager.getLogs(from, to, addrs, formattedTopics)
854859
return await Promise.all(
855860
logs.map(({ log, block, tx, txIndex, logIndex }) =>
856861
jsonRpcLog(log, block, tx, txIndex, logIndex)

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,30 @@ tape(`${method}: call with valid arguments`, async (t) => {
110110
}
111111
await baseRequest(t, server, req, 200, expectRes, false)
112112

113-
// test filtering by address
113+
// test filtering by single address
114114
req = params(method, [{ address: contractAddr1.toString() }])
115115
expectRes = (res: any) => {
116-
const msg = 'should return the correct logs (filter by address)'
117-
if (res.body.result.length === 10 && res.body.result[0].address === contractAddr1.toString()) {
116+
const msg = 'should return the correct logs (filter by single address)'
117+
if (
118+
res.body.result.length === 10 &&
119+
res.body.result.every((r: any) => r.address === contractAddr1.toString())
120+
) {
121+
t.pass(msg)
122+
} else {
123+
t.fail(msg)
124+
}
125+
}
126+
await baseRequest(t, server, req, 200, expectRes, false)
127+
128+
// test filtering by multiple addresses
129+
const addresses = [contractAddr1.toString(), contractAddr2.toString()]
130+
req = params(method, [{ address: addresses }])
131+
expectRes = (res: any) => {
132+
const msg = 'should return the correct logs (filter by multiple addresses)'
133+
if (
134+
res.body.result.length === 20 &&
135+
res.body.result.every((r: any) => addresses.includes(r.address))
136+
) {
118137
t.pass(msg)
119138
} else {
120139
t.fail(msg)

0 commit comments

Comments
 (0)