-
Notifications
You must be signed in to change notification settings - Fork 0
fix(api): Optimize memory allocation for the filter interface of JSON-RPC #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: pr-6459-base
Are you sure you want to change the base?
Changes from all commits
114bbec
c0c02f7
46c258d
1ff79c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -423,6 +423,7 @@ public List<String> getCheckpointList() { | |
|
|
||
| private void deleteCheckpoint() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟢 The comment `// only occurs in mock test. TODO fix test` suggests that there is an underlying issue with the tests that should be addressed. While this is a pre-existing issue, since this line is being touched in this PR, it's a good opportunity to highlight that the tests should be fixed to avoid having special conditions in the production code just for tests.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The |
||
| if(checkTmpStore == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 It's good practice to address |
||
| // only occurs in mock test. TODO fix test | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Adding comments about mock tests in production code is generally discouraged. If this null check is required for safety in production, the comment might be misleading. If it is only for tests, the tests should ideally be fixed to set up the environment correctly rather than patching production code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 While this check prevents the test failure, leaving a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium - The |
||
| return; | ||
| } | ||
| try { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||
| package org.tron.core.exception.jsonrpc; | ||||||
|
|
||||||
| public class JsonRpcExceedLimitException extends JsonRpcException { | ||||||
|
|
||||||
| public JsonRpcExceedLimitException() { | ||||||
| super(); | ||||||
| } | ||||||
|
|
||||||
| public JsonRpcExceedLimitException(String message) { | ||||||
| super(message); | ||||||
| } | ||||||
|
|
||||||
| public JsonRpcExceedLimitException(String message, Throwable cause) { | ||||||
| super(message, cause); | ||||||
| } | ||||||
| } | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Nitpick: Please add a newline at the end of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 This new exception class is not used anywhere in the codebase. In Consider either using this new exception there (and updating the interface and error mappings accordingly) or removing this class to avoid dead code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟢 It's a good practice to add a newline at the end of the file. Most Java style guides recommend it.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 It's a good practice to have a newline at the end of the file.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Consider adding a newline at the end of the file for better POSIX compatibility and to align with common Java formatting practices. |
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -546,6 +546,7 @@ public boolean isBusy() { | |
| } | ||
| int queueSize = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Similar to the |
||
| if (eventListeners == null || eventListeners.isEmpty()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Similar to the |
||
| //only occurs in mock test. TODO fix test | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Similar to the comment in |
||
| return false; | ||
| } | ||
| for (IPluginEventListener listener : eventListeners) { | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 The changes in this file, moving the P2P service start to the end, seem unrelated to the PR's goal of optimizing memory allocation for the JSON-RPC filter interface. Could you please clarify why these changes are included in this PR or move them to a separate one? |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -87,9 +87,6 @@ private static void setP2pConfig(P2pConfig config) { | |||||||||||||||||
| public void start() { | ||||||||||||||||||
| try { | ||||||||||||||||||
| init = true; | ||||||||||||||||||
| setP2pConfig(getConfig()); | ||||||||||||||||||
| p2pService.start(p2pConfig); | ||||||||||||||||||
| p2pService.register(p2pEventHandler); | ||||||||||||||||||
| advService.init(); | ||||||||||||||||||
| syncService.init(); | ||||||||||||||||||
| peerStatusCheck.init(); | ||||||||||||||||||
|
|
@@ -101,6 +98,12 @@ public void start() { | |||||||||||||||||
| PeerManager.init(); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The reordering of the P2P service start to the end of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 The P2P service start has been reordered to the end of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 Moving the P2P service start to the end of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 The P2P service initialization in
Suggested change
|
||||||||||||||||||
| relayService.init(); | ||||||||||||||||||
| effectiveCheckService.init(); | ||||||||||||||||||
|
|
||||||||||||||||||
|
Comment on lines
98
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 The changes in this file, particularly moving the P2P service initialization, seem unrelated to the primary goal of this pull request, which is optimizing JSON-RPC filter memory allocation. Could you clarify the reason for this change or consider moving it to a separate PR to maintain focus and clarity?
|
||||||||||||||||||
| // Move P2P service start to the end | ||||||||||||||||||
| setP2pConfig(getConfig()); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 This change alters the startup order of services and seems unrelated to the PR's objective of optimizing memory allocation for JSON-RPC. While postponing P2P start might be a valid fix, it should ideally be in a separate PR or clearly documented in the PR description to ensure it gets proper visibility and testing regarding side effects. |
||||||||||||||||||
| p2pService.start(p2pConfig); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High - The reordering of |
||||||||||||||||||
| p2pService.register(p2pEventHandler); | ||||||||||||||||||
|
Comment on lines
+102
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 This change modifies the service startup sequence by moving Changing the startup order can have significant side effects if other services (like Please revert this change unless it is strictly necessary for the memory optimization, in which case, please provide a detailed explanation of the dependency. |
||||||||||||||||||
|
|
||||||||||||||||||
| logger.info("Net service start successfully"); | ||||||||||||||||||
|
Comment on lines
+104
to
107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🔴 The change in the P2P service initialization order is significant and seems unrelated to the primary goal of this PR, which is to optimize memory for JSON-RPC filters. Altering the startup sequence of core services can introduce subtle bugs, race conditions, or dependency issues that are hard to detect.
Please revert this change and move it to a separate PR where it can be reviewed with the appropriate context and justification. Bundling unrelated changes makes the review process difficult and increases the risk of introducing regressions. |
||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||
| throw new TronError(e, TronError.ErrCode.TRON_NET_SERVICE_INIT); | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.googlecode.jsonrpc4j.JsonRpcMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.ExecutionException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.AllArgsConstructor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.Getter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,6 +19,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.common.utils.ByteArray; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.exception.BadItemException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.exception.ItemNotFoundException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcExceedLimitException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcInternalException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -28,7 +30,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.services.jsonrpc.types.CallArguments; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.services.jsonrpc.types.TransactionReceipt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 The import
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.services.jsonrpc.types.TransactionResult; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.tron.core.net.peer.Item; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Error code refers to https://www.quicknode.com/docs/ethereum/error-references | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public interface TronJsonRpc { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -292,9 +298,10 @@ String newFilter(FilterRequest fr) throws JsonRpcInvalidParamsException, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @JsonRpcMethod("eth_newBlockFilter") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @JsonRpcErrors({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32005, data = "{}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @JsonRpcError(exception = JsonRpcMethodNotFoundException.class, code = -32601, data = "{}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String newBlockFilter() throws JsonRpcMethodNotFoundException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String newBlockFilter() throws JsonRpcInvalidParamsException, JsonRpcMethodNotFoundException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @JsonRpcMethod("eth_uninstallFilter") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @JsonRpcErrors({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -472,5 +479,41 @@ public LogFilterElement(String blockHash, Long blockNum, String txId, Integer tx | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.removed = removed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public boolean equals(Object o) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this == o) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (o == null || this.getClass() != o.getClass()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LogFilterElement item = (LogFilterElement) o; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Objects.equals(blockHash, item.blockHash)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Objects.equals(transactionHash, item.transactionHash)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Objects.equals(transactionIndex, item.transactionIndex)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Objects.equals(logIndex, item.logIndex)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return removed == item.removed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public int hashCode() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int result = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = 31 * result + (blockHash == null ? 0 : blockHash.hashCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = 31 * result + (transactionHash == null ? 0 : transactionHash.hashCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = 31 * result + (transactionIndex == null ? 0 : transactionIndex.hashCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+485
to
+512
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 These
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = 31 * result + (logIndex == null ? 0 : logIndex.hashCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = result + (removed ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+508
to
+514
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟢 For better readability and conciseness, you can use `Objects.hash()` to implement the `hashCode` method.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+514
to
+515
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟢 The `hashCode()` implementation can be made more concise and readable by using `java.util.Objects.hash()`.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+509
to
+516
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The custom
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+482
to
+517
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |||||||||||||||||||
| import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.triggerCallContract; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import com.alibaba.fastjson.JSON; | ||||||||||||||||||||
| import com.google.common.cache.Cache; | ||||||||||||||||||||
| import com.google.common.cache.CacheBuilder; | ||||||||||||||||||||
| import com.google.protobuf.ByteString; | ||||||||||||||||||||
| import com.google.protobuf.GeneratedMessageV3; | ||||||||||||||||||||
| import java.io.Closeable; | ||||||||||||||||||||
|
|
@@ -25,10 +27,10 @@ | |||||||||||||||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||||||||||||||||
| import java.util.concurrent.ExecutionException; | ||||||||||||||||||||
| import java.util.concurrent.ExecutorService; | ||||||||||||||||||||
| import java.util.concurrent.TimeUnit; | ||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||
| import lombok.Getter; | ||||||||||||||||||||
| import lombok.extern.slf4j.Slf4j; | ||||||||||||||||||||
| import org.apache.commons.collections4.CollectionUtils; | ||||||||||||||||||||
| import org.apache.commons.lang3.StringUtils; | ||||||||||||||||||||
| import org.bouncycastle.util.encoders.Hex; | ||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||||||||||||||||||
|
|
@@ -51,6 +53,7 @@ | |||||||||||||||||||
| import org.tron.core.Wallet; | ||||||||||||||||||||
| import org.tron.core.capsule.BlockCapsule; | ||||||||||||||||||||
| import org.tron.core.capsule.TransactionCapsule; | ||||||||||||||||||||
| import org.tron.core.config.args.Args; | ||||||||||||||||||||
| import org.tron.core.db.Manager; | ||||||||||||||||||||
| import org.tron.core.db2.core.Chainbase; | ||||||||||||||||||||
| import org.tron.core.exception.BadItemException; | ||||||||||||||||||||
|
|
@@ -59,6 +62,7 @@ | |||||||||||||||||||
| import org.tron.core.exception.HeaderNotFound; | ||||||||||||||||||||
| import org.tron.core.exception.ItemNotFoundException; | ||||||||||||||||||||
| import org.tron.core.exception.VMIllegalException; | ||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcExceedLimitException; | ||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcInternalException; | ||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; | ||||||||||||||||||||
| import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; | ||||||||||||||||||||
|
|
@@ -110,6 +114,17 @@ public enum RequestSource { | |||||||||||||||||||
|
|
||||||||||||||||||||
| private static final String FILTER_NOT_FOUND = "filter not found"; | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Adding a comment like |
||||||||||||||||||||
| public static final int EXPIRE_SECONDS = 5 * 60; | ||||||||||||||||||||
| private static final int maxBlockFilterNum = Args.getInstance().getJsonRpcMaxBlockFilterNum(); | ||||||||||||||||||||
| private static final Cache<LogFilterElement, LogFilterElement> logElementCache = | ||||||||||||||||||||
| CacheBuilder.newBuilder() | ||||||||||||||||||||
| .maximumSize(300_000L) // 300s * tps(1000) * 1 log/tx ≈ 300_000 | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 The cache maximum sizes are hardcoded. Consider making these configurable via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Adding a comment like |
||||||||||||||||||||
| .expireAfterWrite(EXPIRE_SECONDS, TimeUnit.SECONDS) | ||||||||||||||||||||
| .recordStats().build(); //LRU cache | ||||||||||||||||||||
| private static final Cache<String, String> blockHashCache = | ||||||||||||||||||||
| CacheBuilder.newBuilder() | ||||||||||||||||||||
| .maximumSize(60_000L) // 300s * 200 block/s when syncing | ||||||||||||||||||||
| .expireAfterWrite(EXPIRE_SECONDS, TimeUnit.SECONDS) | ||||||||||||||||||||
| .recordStats().build(); //LRU cache | ||||||||||||||||||||
| /** | ||||||||||||||||||||
| * for log filter in Full Json-RPC | ||||||||||||||||||||
| */ | ||||||||||||||||||||
|
|
@@ -181,16 +196,31 @@ public static void handleBLockFilter(BlockFilterCapsule blockFilterCapsule) { | |||||||||||||||||||
| it = getBlockFilter2ResultFull().entrySet().iterator(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (!it.hasNext()) { | ||||||||||||||||||||
| return; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| final String originalBlockHash = ByteArray.toJsonHex(blockFilterCapsule.getBlockHash()); | ||||||||||||||||||||
| String cachedBlockHash; | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| // compare with hashcode() first, then with equals(). If not exist, put it. | ||||||||||||||||||||
| cachedBlockHash = blockHashCache.get(originalBlockHash, () -> originalBlockHash); | ||||||||||||||||||||
| } catch (ExecutionException e) { | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 Catching `ExecutionException` and only logging it with a comment "never happen" can hide potential issues. The cause of the `ExecutionException` should be properly handled or re-thrown as a runtime exception to ensure that any unexpected errors during cache loading are propagated.
Suggested change
|
||||||||||||||||||||
| logger.error("Getting/loading blockHash from cache failed", e); //never happen | ||||||||||||||||||||
| cachedBlockHash = originalBlockHash; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟢 The comment `//never happen` isn't very descriptive. It would be better to explain *why* it will never happen. For example, you could explain that the callable for the cache does not throw a checked exception.
Suggested change
|
||||||||||||||||||||
| while (it.hasNext()) { | ||||||||||||||||||||
| Entry<String, BlockFilterAndResult> entry = it.next(); | ||||||||||||||||||||
| if (entry.getValue().isExpire()) { | ||||||||||||||||||||
| it.remove(); | ||||||||||||||||||||
| continue; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| entry.getValue().getResult().add(ByteArray.toJsonHex(blockFilterCapsule.getBlockHash())); | ||||||||||||||||||||
| entry.getValue().getResult().add(cachedBlockHash); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * append LogsFilterCapsule's LogFilterElement list to each filter if matched | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| public static void handleLogsFilter(LogsFilterCapsule logsFilterCapsule) { | ||||||||||||||||||||
| Iterator<Entry<String, LogFilterAndResult>> it; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -226,8 +256,17 @@ public static void handleLogsFilter(LogsFilterCapsule logsFilterCapsule) { | |||||||||||||||||||
| LogMatch.matchBlock(logFilter, logsFilterCapsule.getBlockNumber(), | ||||||||||||||||||||
| logsFilterCapsule.getBlockHash(), logsFilterCapsule.getTxInfoList(), | ||||||||||||||||||||
| logsFilterCapsule.isRemoved()); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 The
Suggested change
|
||||||||||||||||||||
| if (CollectionUtils.isNotEmpty(elements)) { | ||||||||||||||||||||
| logFilterAndResult.getResult().addAll(elements); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for (LogFilterElement element : elements) { | ||||||||||||||||||||
| LogFilterElement cachedElement; | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| // compare with hashcode() first, then with equals(). If not exist, put it. | ||||||||||||||||||||
| cachedElement = logElementCache.get(element, () -> element); | ||||||||||||||||||||
| } catch (ExecutionException e) { | ||||||||||||||||||||
| logger.error("Getting/loading LogFilterElement from cache fails", e); // never happen | ||||||||||||||||||||
| cachedElement = element; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| logFilterAndResult.getResult().add(cachedElement); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -797,7 +836,7 @@ public TransactionReceipt getTransactionReceipt(String txId) | |||||||||||||||||||
| long blockNum = blockCapsule.getNum(); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 This line contains redundant spaces. Consider removing them for consistency with coding style guidelines.
Suggested change
|
||||||||||||||||||||
| TransactionInfoList transactionInfoList = wallet.getTransactionInfoByBlockNum(blockNum); | ||||||||||||||||||||
| long energyFee = wallet.getEnergyFee(blockCapsule.getTimeStamp()); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Find transaction context | ||||||||||||||||||||
| TransactionReceipt.TransactionContext context | ||||||||||||||||||||
| = findTransactionContext(transactionInfoList, | ||||||||||||||||||||
|
|
@@ -806,7 +845,7 @@ public TransactionReceipt getTransactionReceipt(String txId) | |||||||||||||||||||
| if (context == null) { | ||||||||||||||||||||
| return null; // Transaction not found in block | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return new TransactionReceipt(blockCapsule, transactionInfo, context, energyFee); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -1391,7 +1430,8 @@ public String newFilter(FilterRequest fr) throws JsonRpcInvalidParamsException, | |||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium - The |
||||||||||||||||||||
| @Override | ||||||||||||||||||||
| public String newBlockFilter() throws JsonRpcMethodNotFoundException { | ||||||||||||||||||||
| public String newBlockFilter() throws JsonRpcMethodNotFoundException, | ||||||||||||||||||||
| JsonRpcInvalidParamsException { | ||||||||||||||||||||
| disableInPBFT("eth_newBlockFilter"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Map<String, BlockFilterAndResult> blockFilter2Result; | ||||||||||||||||||||
|
|
@@ -1400,6 +1440,10 @@ public String newBlockFilter() throws JsonRpcMethodNotFoundException { | |||||||||||||||||||
| } else { | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 The introduction of |
||||||||||||||||||||
| blockFilter2Result = blockFilter2ResultSolidity; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (blockFilter2Result.size() >= maxBlockFilterNum) { | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 You've introduced a new exception `JsonRpcExceedLimitException`, but you're throwing `JsonRpcInvalidParamsException` here. It would be more consistent and specific to use the new exception you've created.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 The newly created `JsonRpcExceedLimitException` should be used here instead of `JsonRpcInvalidParamsException` to provide a more specific error when the block filter limit is exceeded. This improves error handling clarity.
Suggested change
|
||||||||||||||||||||
| throw new JsonRpcInvalidParamsException( | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The class If you intend to use
|
||||||||||||||||||||
| "exceed max block filters: " + maxBlockFilterNum + ", try again later"); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟢 A new exception `JsonRpcExceedLimitException` was added in this PR, but it is not being used. Instead, a generic `JsonRpcInvalidParamsException` is thrown when the block filter limit is exceeded. To provide a more specific error to the API user, consider using the new exception here.
Suggested change
|
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+1446
to
1447
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 You've created a specific exception
Suggested change
|
||||||||||||||||||||
| BlockFilterAndResult filterAndResult = new BlockFilterAndResult(); | ||||||||||||||||||||
| String filterID = generateFilterId(); | ||||||||||||||||||||
|
|
@@ -1529,6 +1573,7 @@ public static Object[] getFilterResult(String filterId, Map<String, BlockFilterA | |||||||||||||||||||
|
|
||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠
Suggested change
|
||||||||||||||||||||
| @Override | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 It seems like `blockHashCache` is not invalidated in the `close()` method, which could lead to a memory leak. You should invalidate it along with `logElementCache`.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 For consistency and to ensure all cache resources are released upon closing, you should also invalidate
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High -
Suggested change
|
||||||||||||||||||||
| public void close() throws IOException { | ||||||||||||||||||||
| logElementCache.invalidateAll(); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 For consistency and to ensure all resources are released, consider invalidating the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 The newly introduced `blockHashCache` is not invalidated in the `close()` method, unlike `logElementCache`. While this might not be a critical issue if `TronJsonRpcImpl` is a singleton, it's a good practice to clean up all resources consistently. Please add `blockHashCache.invalidateAll()` to the `close()` method to prevent potential memory leaks in scenarios where the object might be re-initialized.
Suggested change
|
||||||||||||||||||||
| ExecutorServiceManager.shutdownAndAwaitTermination(sectionExecutor, esName); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 This TODO comment indicates a potential issue in mock tests. While not directly related to the current PR's functionality, it's good to keep track of these for future improvements.