1111import static org .tron .core .services .jsonrpc .JsonRpcApiUtil .triggerCallContract ;
1212
1313import com .alibaba .fastjson .JSON ;
14+ import com .google .common .cache .Cache ;
15+ import com .google .common .cache .CacheBuilder ;
1416import com .google .protobuf .ByteString ;
1517import com .google .protobuf .GeneratedMessageV3 ;
1618import java .io .Closeable ;
2527import java .util .concurrent .ConcurrentHashMap ;
2628import java .util .concurrent .ExecutionException ;
2729import java .util .concurrent .ExecutorService ;
30+ import java .util .concurrent .TimeUnit ;
2831import java .util .regex .Pattern ;
2932import lombok .Getter ;
3033import lombok .extern .slf4j .Slf4j ;
31- import org .apache .commons .collections4 .CollectionUtils ;
3234import org .apache .commons .lang3 .StringUtils ;
3335import org .bouncycastle .util .encoders .Hex ;
3436import org .springframework .beans .factory .annotation .Autowired ;
@@ -110,6 +112,10 @@ public enum RequestSource {
110112
111113 private static final String FILTER_NOT_FOUND = "filter not found" ;
112114 public static final int EXPIRE_SECONDS = 5 * 60 ;
115+ private static final Cache <Integer , LogFilterElement > logElementCache = CacheBuilder .newBuilder ()
116+ .maximumSize (100_000L )
117+ .expireAfterWrite (EXPIRE_SECONDS , TimeUnit .SECONDS )
118+ .recordStats ().build (); //LRU cache
113119 /**
114120 * for log filter in Full Json-RPC
115121 */
@@ -165,7 +171,7 @@ public enum RequestSource {
165171
166172 @ Autowired
167173 public TronJsonRpcImpl (@ Autowired NodeInfoService nodeInfoService , @ Autowired Wallet wallet ,
168- @ Autowired Manager manager ) {
174+ @ Autowired Manager manager ) {
169175 this .nodeInfoService = nodeInfoService ;
170176 this .wallet = wallet ;
171177 this .manager = manager ;
@@ -191,6 +197,9 @@ public static void handleBLockFilter(BlockFilterCapsule blockFilterCapsule) {
191197 }
192198 }
193199
200+ /**
201+ * append LogsFilterCapsule's LogFilterElement list to each filter if matched
202+ */
194203 public static void handleLogsFilter (LogsFilterCapsule logsFilterCapsule ) {
195204 Iterator <Entry <String , LogFilterAndResult >> it ;
196205
@@ -226,8 +235,15 @@ public static void handleLogsFilter(LogsFilterCapsule logsFilterCapsule) {
226235 LogMatch .matchBlock (logFilter , logsFilterCapsule .getBlockNumber (),
227236 logsFilterCapsule .getBlockHash (), logsFilterCapsule .getTxInfoList (),
228237 logsFilterCapsule .isRemoved ());
229- if (CollectionUtils .isNotEmpty (elements )) {
230- logFilterAndResult .getResult ().addAll (elements );
238+
239+ for (LogFilterElement element : elements ) {
240+ LogFilterElement newElement ;
241+ try {
242+ newElement = logElementCache .get (element .hashCode (), () -> element );
243+ } catch (ExecutionException e ) {
244+ continue ;
245+ }
246+ logFilterAndResult .getResult ().add (newElement );
231247 }
232248 }
233249 }
@@ -797,7 +813,7 @@ public TransactionReceipt getTransactionReceipt(String txId)
797813 long blockNum = blockCapsule .getNum ();
798814 TransactionInfoList transactionInfoList = wallet .getTransactionInfoByBlockNum (blockNum );
799815 long energyFee = wallet .getEnergyFee (blockCapsule .getTimeStamp ());
800-
816+
801817 // Find transaction context
802818 TransactionReceipt .TransactionContext context
803819 = findTransactionContext (transactionInfoList ,
@@ -806,7 +822,7 @@ public TransactionReceipt getTransactionReceipt(String txId)
806822 if (context == null ) {
807823 return null ; // Transaction not found in block
808824 }
809-
825+
810826 return new TransactionReceipt (blockCapsule , transactionInfo , context , energyFee );
811827 }
812828
0 commit comments