Skip to content

Commit f40f188

Browse files
authored
fix(jsonrpc): enforce maxBlockRange on eth_getFilterLogs (#6842)
* fix(jsonrpc): enforce maxBlockRange on eth_getFilterLogs * style(jsonrpc): fix styles and comments
1 parent 0871079 commit f40f188

3 files changed

Lines changed: 49 additions & 8 deletions

File tree

‎framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,8 @@ public LogFilterElement[] getLogs(FilterRequest fr) throws JsonRpcInvalidParamsE
15481548
}
15491549

15501550
@Override
1551-
public LogFilterElement[] getFilterLogs(String filterId) throws ExecutionException,
1551+
public LogFilterElement[] getFilterLogs(String filterId) throws
1552+
JsonRpcInvalidParamsException, ExecutionException,
15521553
InterruptedException, BadItemException, ItemNotFoundException,
15531554
JsonRpcMethodNotFoundException, JsonRpcTooManyResultException {
15541555
disableInPBFT("eth_getFilterLogs");
@@ -1568,6 +1569,10 @@ public LogFilterElement[] getFilterLogs(String filterId) throws ExecutionExcepti
15681569
LogFilterWrapper logFilterWrapper = eventFilter2Result.get(filterId).getLogFilterWrapper();
15691570
long currentMaxBlockNum = wallet.getNowBlock().getBlockHeader().getRawData().getNumber();
15701571

1572+
// re-check the block range against the current head: the filter was created without the cap
1573+
// (eth_newFilter), so enforce it here to prevent an unbounded scan.
1574+
logFilterWrapper.validateBlockRange(currentMaxBlockNum);
1575+
15711576
return getLogsByLogFilterWrapper(logFilterWrapper, currentMaxBlockNum);
15721577
}
15731578

‎framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,23 @@ public LogFilterWrapper(FilterRequest fr, long currentMaxBlockNum, Wallet wallet
9898
throw new JsonRpcInvalidParamsException("please verify: fromBlock <= toBlock");
9999
}
100100
}
101-
102-
// till now, it needs to check block range for eth_getLogs
103-
int maxBlockRange = Args.getInstance().getJsonRpcMaxBlockRange();
104-
if (checkBlockRange && maxBlockRange > 0
105-
&& min(toBlockSrc, currentMaxBlockNum) - fromBlockSrc > maxBlockRange) {
106-
throw new JsonRpcInvalidParamsException("exceed max block range: " + maxBlockRange);
107-
}
108101
}
109102

110103
this.fromBlock = fromBlockSrc;
111104
this.toBlock = toBlockSrc;
105+
106+
// eth_getLogs enforces the block range at construction time. eth_newFilter creates the
107+
// wrapper with checkBlockRange=false (no creation-time gate); eth_getFilterLogs re-runs this
108+
// check against the current head before scanning so the cap cannot be bypassed.
109+
if (checkBlockRange) {
110+
validateBlockRange(currentMaxBlockNum);
111+
}
112+
}
113+
114+
public void validateBlockRange(long currentMaxBlockNum) throws JsonRpcInvalidParamsException {
115+
int maxBlockRange = Args.getInstance().getJsonRpcMaxBlockRange();
116+
if (maxBlockRange > 0 && min(toBlock, currentMaxBlockNum) - fromBlock > maxBlockRange) {
117+
throw new JsonRpcInvalidParamsException("exceed max block range: " + maxBlockRange);
118+
}
112119
}
113120
}

‎framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,35 @@ public void testLogFilterTagHandling() {
13281328
}
13291329
}
13301330

1331+
@Test
1332+
public void testGetFilterLogsEnforcesBlockRange() throws Exception {
1333+
int oldMaxBlockRange = Args.getInstance().getJsonRpcMaxBlockRange();
1334+
Args.getInstance().setJsonRpcMaxBlockRange(5_000);
1335+
try {
1336+
// toBlock=0xf4240 (1_000_000) is clamped to head (LATEST_BLOCK_NUM = 10_000), so the span
1337+
// is min(1_000_000, 10_000) - 0 = 10_000 > 5_000 cap.
1338+
String wideFilterId = tronJsonRpc.newFilter(
1339+
new FilterRequest("0x0", "0xf4240", null, null, null));
1340+
Assert.assertNotNull(wideFilterId);
1341+
1342+
// eth_getFilterLogs must reject it at query time (previously bypassed -> unbounded scan).
1343+
JsonRpcInvalidParamsException ex = Assert.assertThrows(
1344+
JsonRpcInvalidParamsException.class,
1345+
() -> tronJsonRpc.getFilterLogs(wideFilterId));
1346+
Assert.assertEquals("exceed max block range: 5000", ex.getMessage());
1347+
1348+
// A within-cap filter (span 0) is still served normally - no false rejection.
1349+
String headHex = ByteArray.toJsonHex(blockCapsule1.getNum());
1350+
int expectedLogs = blockCapsule1.getTransactions().size() * 2;
1351+
String okFilterId = tronJsonRpc.newFilter(
1352+
new FilterRequest(headHex, headHex, null, null, null));
1353+
LogFilterElement[] okResult = tronJsonRpc.getFilterLogs(okFilterId);
1354+
Assert.assertEquals(expectedLogs, okResult.length);
1355+
} finally {
1356+
Args.getInstance().setJsonRpcMaxBlockRange(oldMaxBlockRange);
1357+
}
1358+
}
1359+
13311360
@Test
13321361
public void testGetBlockReceipts() {
13331362

0 commit comments

Comments
 (0)