Skip to content

Commit 151c0e4

Browse files
committed
restrict number of block filter
1 parent 00ab69c commit 151c0e4

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.tron.core.exception.jsonrpc;
2+
3+
public class JsonRpcTooManyFilterException extends JsonRpcException {
4+
5+
public JsonRpcTooManyFilterException() {
6+
super();
7+
}
8+
9+
public JsonRpcTooManyFilterException(String message) {
10+
super(message);
11+
}
12+
13+
public JsonRpcTooManyFilterException(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
}

framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
2424
import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException;
2525
import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException;
26+
import org.tron.core.exception.jsonrpc.JsonRpcTooManyFilterException;
2627
import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException;
2728
import org.tron.core.services.jsonrpc.types.BlockResult;
2829
import org.tron.core.services.jsonrpc.types.BuildArguments;
2930
import org.tron.core.services.jsonrpc.types.CallArguments;
3031
import org.tron.core.services.jsonrpc.types.TransactionReceipt;
3132
import org.tron.core.services.jsonrpc.types.TransactionResult;
3233

34+
/**
35+
* Error code refers to https://www.quicknode.com/docs/ethereum/error-references
36+
*/
3337
@Component
3438
public interface TronJsonRpc {
3539

@@ -293,9 +297,10 @@ String newFilter(FilterRequest fr) throws JsonRpcInvalidParamsException,
293297

294298
@JsonRpcMethod("eth_newBlockFilter")
295299
@JsonRpcErrors({
300+
@JsonRpcError(exception = JsonRpcTooManyFilterException.class, code = -32005, data = "{}"),
296301
@JsonRpcError(exception = JsonRpcMethodNotFoundException.class, code = -32601, data = "{}"),
297302
})
298-
String newBlockFilter() throws JsonRpcMethodNotFoundException;
303+
String newBlockFilter() throws JsonRpcTooManyFilterException, JsonRpcMethodNotFoundException;
299304

300305
@JsonRpcMethod("eth_uninstallFilter")
301306
@JsonRpcErrors({

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
6666
import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException;
6767
import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException;
68+
import org.tron.core.exception.jsonrpc.JsonRpcTooManyFilterException;
6869
import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException;
6970
import org.tron.core.services.NodeInfoService;
7071
import org.tron.core.services.http.JsonFormat;
@@ -112,6 +113,7 @@ public enum RequestSource {
112113

113114
private static final String FILTER_NOT_FOUND = "filter not found";
114115
public static final int EXPIRE_SECONDS = 5 * 60;
116+
private static final int MAX_FILTER_NUM = 100000;
115117
private static final Cache<LogFilterElement, LogFilterElement> logElementCache =
116118
CacheBuilder.newBuilder()
117119
.maximumSize(300_000L) // 300s * tps(1000) * 1 log/tx ≈ 300_000
@@ -1424,7 +1426,8 @@ public String newFilter(FilterRequest fr) throws JsonRpcInvalidParamsException,
14241426
}
14251427

14261428
@Override
1427-
public String newBlockFilter() throws JsonRpcMethodNotFoundException {
1429+
public String newBlockFilter() throws JsonRpcMethodNotFoundException,
1430+
JsonRpcTooManyFilterException {
14281431
disableInPBFT("eth_newBlockFilter");
14291432

14301433
Map<String, BlockFilterAndResult> blockFilter2Result;
@@ -1433,6 +1436,9 @@ public String newBlockFilter() throws JsonRpcMethodNotFoundException {
14331436
} else {
14341437
blockFilter2Result = blockFilter2ResultSolidity;
14351438
}
1439+
if (blockFilter2Result.size() >= MAX_FILTER_NUM) {
1440+
throw new JsonRpcTooManyFilterException("Block filter's num exceeds " + MAX_FILTER_NUM);
1441+
}
14361442

14371443
BlockFilterAndResult filterAndResult = new BlockFilterAndResult();
14381444
String filterID = generateFilterId();

0 commit comments

Comments
 (0)