Skip to content

Commit fa23379

Browse files
Merge pull request #6227 from zeusoo001/event-907
feat(event): optimize transaction trigger acquisition
2 parents e649699 + d56502f commit fa23379

File tree

11 files changed

+267
-21
lines changed

11 files changed

+267
-21
lines changed

framework/src/main/java/org/tron/common/application/ApplicationImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public void startup() {
5252
@Override
5353
public void shutdown() {
5454
this.shutdownServices();
55-
eventService.close();
5655
if (!Args.getInstance().isSolidityNode() && (!Args.getInstance().p2pDisable)) {
5756
tronNetService.close();
5857
}
5958
consensusService.stop();
59+
eventService.close();
6060
dbManager.close();
6161
shutdown.countDown();
6262
}

framework/src/main/java/org/tron/common/logsfilter/capsule/TransactionLogTriggerCapsule.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import com.google.protobuf.Any;
77
import com.google.protobuf.ByteString;
88
import java.util.ArrayList;
9+
import java.util.HashMap;
910
import java.util.List;
11+
import java.util.Map;
1012
import java.util.Objects;
1113
import lombok.Getter;
1214
import lombok.Setter;
@@ -49,9 +51,21 @@ public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule
4951
this(trxCapsule, blockCapsule, 0, 0, 0, null, 0);
5052
}
5153

54+
public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule,
55+
BlockCapsule blockCapsule, TransactionInfo transactionInfo) {
56+
this(trxCapsule, blockCapsule, 0, 0, 0, transactionInfo, 0, true);
57+
}
58+
5259
public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule blockCapsule,
5360
int txIndex, long preCumulativeEnergyUsed, long preCumulativeLogCount,
5461
TransactionInfo transactionInfo, long energyUnitPrice) {
62+
this(trxCapsule, blockCapsule, txIndex, preCumulativeEnergyUsed, preCumulativeLogCount,
63+
transactionInfo, energyUnitPrice, false);
64+
}
65+
66+
public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule blockCapsule,
67+
int txIndex, long preCumulativeEnergyUsed, long preCumulativeLogCount,
68+
TransactionInfo transactionInfo, long energyUnitPrice, boolean flag) {
5569
transactionLogTrigger = new TransactionLogTrigger();
5670

5771
String blockHash = "";
@@ -281,6 +295,7 @@ public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule
281295

282296
if (Objects.nonNull(contractAddress) && contractAddress.size() > 0) {
283297
if (Objects.nonNull(transactionInfo)
298+
&& EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible()
284299
&& contractType != null && contractType != CreateSmartContract) {
285300
transactionLogTrigger.setContractAddress(null);
286301
} else {
@@ -294,8 +309,38 @@ public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule
294309
getInternalTransactionList(programResult.getInternalTransactions()));
295310
}
296311

312+
if (Objects.isNull(trxTrace) && Objects.nonNull(transactionInfo) && flag) {
313+
Protocol.ResourceReceipt receipt = transactionInfo.getReceipt();
314+
energyUsageTotal = receipt.getEnergyUsageTotal();
315+
transactionLogTrigger.setEnergyFee(receipt.getEnergyFee());
316+
transactionLogTrigger.setOriginEnergyUsage(receipt.getOriginEnergyUsage());
317+
transactionLogTrigger.setEnergyUsageTotal(energyUsageTotal);
318+
transactionLogTrigger.setNetUsage(receipt.getNetUsage());
319+
transactionLogTrigger.setNetFee(receipt.getNetFee());
320+
transactionLogTrigger.setEnergyUsage(receipt.getEnergyUsage());
321+
322+
if (transactionInfo.getContractResultCount() > 0) {
323+
ByteString contractResult = transactionInfo.getContractResult(0);
324+
if (Objects.nonNull(contractResult) && contractResult.size() > 0) {
325+
transactionLogTrigger.setContractResult(Hex.toHexString(contractResult.toByteArray()));
326+
}
327+
}
328+
329+
ByteString contractAddress = transactionInfo.getContractAddress();
330+
if (Objects.nonNull(contractAddress) && contractAddress.size() > 0) {
331+
if (EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible()
332+
&& contractType != null && contractType != CreateSmartContract) {
333+
transactionLogTrigger.setContractAddress(null);
334+
} else {
335+
transactionLogTrigger
336+
.setContractAddress(StringUtil.encode58Check((contractAddress.toByteArray())));
337+
}
338+
}
339+
}
340+
297341
// process transactionInfo list, only enabled when ethCompatible is true
298-
if (Objects.nonNull(transactionInfo)) {
342+
if (Objects.nonNull(transactionInfo)
343+
&& EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible()) {
299344
transactionLogTrigger.setTransactionIndex(txIndex);
300345
transactionLogTrigger.setCumulativeEnergyUsed(preCumulativeEnergyUsed + energyUsageTotal);
301346
transactionLogTrigger.setPreCumulativeLogCount(preCumulativeLogCount);

framework/src/main/java/org/tron/core/services/event/BlockEventGet.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class BlockEventGet {
5454

5555
public BlockEvent getBlockEvent(long blockNum) throws Exception {
5656
BlockCapsule block = manager.getChainBaseManager().getBlockByNum(blockNum);
57+
block.getTransactions().forEach(t -> t.setBlockNum(block.getNum()));
5758
long solidNum = manager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
5859
BlockEvent blockEvent = new BlockEvent();
5960
blockEvent.setBlockId(block.getBlockId());
@@ -322,12 +323,7 @@ public List<TransactionLogTriggerCapsule> getTransactionLogTrigger(BlockCapsule
322323
long solidNum) {
323324
List<TransactionLogTriggerCapsule> transactionLogTriggerCapsules = new ArrayList<>();
324325
if (!EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible()) {
325-
for (TransactionCapsule t : block.getTransactions()) {
326-
TransactionLogTriggerCapsule trx = new TransactionLogTriggerCapsule(t, block);
327-
trx.setLatestSolidifiedBlockNumber(solidNum);
328-
transactionLogTriggerCapsules.add(trx);
329-
}
330-
return transactionLogTriggerCapsules;
326+
return getTransactionTriggers(block, solidNum);
331327
}
332328
List<TransactionCapsule> transactionCapsuleList = block.getTransactions();
333329
GrpcAPI.TransactionInfoList transactionInfoList = GrpcAPI
@@ -364,7 +360,7 @@ public List<TransactionLogTriggerCapsule> getTransactionLogTrigger(BlockCapsule
364360
TransactionCapsule transactionCapsule = transactionCapsuleList.get(i);
365361
transactionCapsule.setBlockNum(block.getNum());
366362
TransactionLogTriggerCapsule trx = new TransactionLogTriggerCapsule(transactionCapsule, block,
367-
i, cumulativeEnergyUsed, cumulativeLogCount, transactionInfo, energyUnitPrice);
363+
i, cumulativeEnergyUsed, cumulativeLogCount, transactionInfo, energyUnitPrice, true);
368364
trx.setLatestSolidifiedBlockNumber(solidNum);
369365
cumulativeEnergyUsed += trx.getTransactionLogTrigger().getEnergyUsageTotal();
370366
cumulativeLogCount += transactionInfo.getLogCount();
@@ -389,4 +385,45 @@ public long getEnergyPrice(long blockTime) {
389385
}
390386
return energyPrice;
391387
}
388+
389+
public List<TransactionLogTriggerCapsule> getTransactionTriggers(BlockCapsule block,
390+
long solidNum) {
391+
List<TransactionLogTriggerCapsule> list = new ArrayList<>();
392+
if (block.getTransactions().size() == 0) {
393+
return list;
394+
}
395+
396+
GrpcAPI.TransactionInfoList transactionInfoList = GrpcAPI
397+
.TransactionInfoList.newBuilder().build();
398+
GrpcAPI.TransactionInfoList.Builder transactionInfoListBuilder = GrpcAPI
399+
.TransactionInfoList.newBuilder();
400+
try {
401+
TransactionRetCapsule result = manager.getChainBaseManager().getTransactionRetStore()
402+
.getTransactionInfoByBlockNum(ByteArray.fromLong(block.getNum()));
403+
if (!Objects.isNull(result) && !Objects.isNull(result.getInstance())) {
404+
result.getInstance().getTransactioninfoList()
405+
.forEach(transactionInfoListBuilder::addTransactionInfo);
406+
transactionInfoList = transactionInfoListBuilder.build();
407+
}
408+
} catch (Exception e) {
409+
logger.warn("Get TransactionInfo failed, blockNum {}, {}.", block.getNum(), e.getMessage());
410+
}
411+
412+
if (block.getTransactions().size() != transactionInfoList.getTransactionInfoCount()) {
413+
for (TransactionCapsule t : block.getTransactions()) {
414+
TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule(t, block);
415+
triggerCapsule.setLatestSolidifiedBlockNumber(solidNum);
416+
list.add(triggerCapsule);
417+
}
418+
} else {
419+
for (int i = 0; i < transactionInfoList.getTransactionInfoCount(); i++) {
420+
TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule(
421+
block.getTransactions().get(i), block, transactionInfoList.getTransactionInfo(i));
422+
triggerCapsule.setLatestSolidifiedBlockNumber(solidNum);
423+
list.add(triggerCapsule);
424+
}
425+
}
426+
427+
return list;
428+
}
392429
}

framework/src/main/java/org/tron/core/services/event/BlockEventLoad.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ public void init() {
4242
}
4343

4444
public void close() {
45-
executor.shutdown();
46-
logger.info("Event load service close.");
45+
try {
46+
load();
47+
executor.shutdown();
48+
logger.info("Event load service close.");
49+
} catch (Exception e) {
50+
logger.warn("Stop event load service fail. {}", e.getMessage());
51+
}
4752
}
4853

49-
public void load() throws Exception {
54+
public synchronized void load() throws Exception {
5055
long cacheHeadNum = BlockEventCache.getHead().getBlockId().getNum();
5156
long tmpNum = manager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
5257
if (cacheHeadNum >= tmpNum) {

framework/src/main/java/org/tron/core/services/event/EventService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class EventService {
1818
@Autowired
1919
private HistoryEventService historyEventService;
2020

21+
@Autowired
22+
private SolidEventService solidEventService;
23+
2124
@Autowired
2225
private Manager manager;
2326

@@ -54,8 +57,9 @@ public void init() {
5457
}
5558

5659
public void close() {
57-
realtimeEventService.close();
58-
blockEventLoad.close();
5960
historyEventService.close();
61+
blockEventLoad.close();
62+
realtimeEventService.close();
63+
solidEventService.close();
6064
}
6165
}

framework/src/main/java/org/tron/core/services/event/HistoryEventService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void init() {
3939

4040
isRunning = true;
4141

42-
new Thread(() -> syncEvent()).start();
42+
new Thread(() -> syncEvent(), "history-event").start();
4343

4444
logger.info("History event service start.");
4545
}

framework/src/main/java/org/tron/core/services/event/RealtimeEventService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ public void init() {
4545
}
4646

4747
public void close() {
48-
executor.shutdown();
49-
logger.info("Realtime event service close.");
48+
try {
49+
work();
50+
executor.shutdown();
51+
logger.info("Realtime event service close.");
52+
} catch (Exception e) {
53+
logger.warn("Close realtime event service fail. {}", e.getMessage());
54+
}
5055
}
5156

5257
public void add(Event event) {
@@ -57,7 +62,7 @@ public void add(Event event) {
5762
queue.offer(event);
5863
}
5964

60-
public void work() {
65+
public synchronized void work() {
6166
while (queue.size() > 0) {
6267
Event event = queue.poll();
6368
flush(event.getBlockEvent(), event.isRemove());

framework/src/main/java/org/tron/core/services/event/SolidEventService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ public void init() {
3737
}
3838

3939
public void close() {
40-
executor.shutdown();
41-
logger.info("Solid event service close.");
40+
try {
41+
work();
42+
executor.shutdown();
43+
logger.info("Solid event service close.");
44+
} catch (Exception e) {
45+
logger.warn("Close solid event service fail. {}", e.getMessage());
46+
}
4247
}
4348

44-
public void work() {
49+
public synchronized void work() {
4550
BlockCapsule.BlockId solidId = BlockEventCache.getSolidId();
4651
if (solidId.getNum() <= BlockEventCache.getSolidNum()) {
4752
return;

framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
55

66
import com.google.protobuf.ByteString;
7+
import org.bouncycastle.util.encoders.Hex;
78
import org.junit.Assert;
89
import org.junit.Before;
910
import org.junit.Test;
1011
import org.tron.common.logsfilter.capsule.TransactionLogTriggerCapsule;
1112
import org.tron.common.utils.Sha256Hash;
13+
import org.tron.common.utils.StringUtil;
1214
import org.tron.core.capsule.BlockCapsule;
1315
import org.tron.core.capsule.TransactionCapsule;
1416
import org.tron.p2p.utils.ByteArray;
@@ -242,6 +244,46 @@ public void testConstructorWithCreateSmartContract() {
242244
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
243245
}
244246

247+
@Test
248+
public void testConstructorWithCreateTransactionInfo() {
249+
Protocol.TransactionInfo.Builder infoBuild = Protocol.TransactionInfo.newBuilder();
250+
251+
Protocol.ResourceReceipt.Builder resourceBuild = Protocol.ResourceReceipt.newBuilder();
252+
resourceBuild.setEnergyFee(1);
253+
resourceBuild.setEnergyUsageTotal(2);
254+
resourceBuild.setEnergyUsage(3);
255+
resourceBuild.setOriginEnergyUsage(4);
256+
resourceBuild.setNetFee(5);
257+
resourceBuild.setNetUsage(6);
258+
259+
infoBuild
260+
.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_ADDRESS)))
261+
.addContractResult(ByteString.copyFrom(ByteArray.fromHexString("112233")))
262+
.setReceipt(resourceBuild.build());
263+
264+
SmartContractOuterClass.CreateSmartContract.Builder builder2 =
265+
SmartContractOuterClass.CreateSmartContract.newBuilder()
266+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
267+
TransactionCapsule tc = new TransactionCapsule(builder2.build(),
268+
Protocol.Transaction.Contract.ContractType.CreateSmartContract);
245269

270+
BlockCapsule bc = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
271+
System.currentTimeMillis(), Sha256Hash.ZERO_HASH.getByteString());
272+
273+
274+
TransactionLogTriggerCapsule trigger =
275+
new TransactionLogTriggerCapsule(tc, bc, infoBuild.build());
276+
277+
Assert.assertEquals(1, trigger.getTransactionLogTrigger().getEnergyFee());
278+
Assert.assertEquals(2, trigger.getTransactionLogTrigger().getEnergyUsageTotal());
279+
Assert.assertEquals(3, trigger.getTransactionLogTrigger().getEnergyUsage());
280+
Assert.assertEquals(4, trigger.getTransactionLogTrigger().getOriginEnergyUsage());
281+
Assert.assertEquals(5, trigger.getTransactionLogTrigger().getNetFee());
282+
Assert.assertEquals(6, trigger.getTransactionLogTrigger().getNetUsage());
283+
284+
Assert.assertEquals(StringUtil.encode58Check(Hex.decode(CONTRACT_ADDRESS)),
285+
trigger.getTransactionLogTrigger().getContractAddress());
286+
Assert.assertEquals("112233", trigger.getTransactionLogTrigger().getContractResult());
287+
}
246288

247289
}

0 commit comments

Comments
 (0)