Skip to content

Commit 217469d

Browse files
committed
feat(block): improve performance for transaction packing
1. remove unused code 2. optimize cycle operation
1 parent 82c40b4 commit 217469d

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ public void addTransaction(TransactionCapsule pendingTrx) {
143143
getTransactions().add(pendingTrx);
144144
}
145145

146+
public void addAllTransactions(List<TransactionCapsule> pendingTrxs) {
147+
List<Transaction> list = pendingTrxs.stream().map(TransactionCapsule::getInstance).collect(
148+
Collectors.toList());
149+
this.block = this.block.toBuilder().addAllTransactions(list).build();
150+
getTransactions().addAll(pendingTrxs);
151+
}
152+
146153
public List<TransactionCapsule> getTransactions() {
147154
return transactions;
148155
}

chainbase/src/main/java/org/tron/core/capsule/TransactionRetCapsule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.capsule;
22

33
import com.google.protobuf.InvalidProtocolBufferException;
4+
import java.util.List;
45
import java.util.Objects;
56
import lombok.extern.slf4j.Slf4j;
67
import org.tron.core.exception.BadItemException;
@@ -29,7 +30,7 @@ public TransactionRetCapsule() {
2930

3031
public TransactionRetCapsule(byte[] data) throws BadItemException {
3132
try {
32-
this.transactionRet = transactionRet.parseFrom(data);
33+
this.transactionRet = TransactionRet.parseFrom(data);
3334
} catch (InvalidProtocolBufferException e) {
3435
throw new BadItemException("TransactionInfoCapsule proto data parse exception");
3536
}
@@ -39,6 +40,10 @@ public void addTransactionInfo(TransactionInfo result) {
3940
this.transactionRet = this.transactionRet.toBuilder().addTransactioninfo(result).build();
4041
}
4142

43+
public void addAllTransactionInfos(List<TransactionInfo> results) {
44+
this.transactionRet = this.transactionRet.toBuilder().addAllTransactioninfo(results).build();
45+
}
46+
4247
@Override
4348
public byte[] getData() {
4449
if (Objects.isNull(transactionRet)) {

framework/src/main/java/org/tron/core/db/Manager.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,16 +1470,17 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
14701470
}
14711471
}
14721472

1473-
TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule(blockCapsule);
1474-
14751473
Set<String> accountSet = new HashSet<>();
14761474
AtomicInteger shieldedTransCounts = new AtomicInteger(0);
1475+
List<TransactionCapsule> toBePacked = new ArrayList<>();
1476+
long currentSize = blockCapsule.getInstance().getSerializedSize();
1477+
boolean isSort = Args.getInstance().isOpenTransactionSort();
14771478
while (pendingTransactions.size() > 0 || rePushTransactions.size() > 0) {
14781479
boolean fromPending = false;
14791480
TransactionCapsule trx;
14801481
if (pendingTransactions.size() > 0) {
14811482
trx = pendingTransactions.peek();
1482-
if (Args.getInstance().isOpenTransactionSort()) {
1483+
if (isSort) {
14831484
TransactionCapsule trxRepush = rePushTransactions.peek();
14841485
if (trxRepush == null || trx.getOrder() >= trxRepush.getOrder()) {
14851486
fromPending = true;
@@ -1516,13 +1517,14 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15161517
}
15171518

15181519
// check the block size
1519-
if ((blockCapsule.getInstance().getSerializedSize() + trx.getSerializedSize() + 3)
1520+
if ((currentSize + trx.getSerializedSize() + 3)
15201521
> ChainConstant.BLOCK_SIZE) {
15211522
postponedTrxCount++;
1522-
continue;
1523+
continue; // try pack more small trx
15231524
}
15241525
//shielded transaction
1525-
if (isShieldedTransaction(trx.getInstance())
1526+
Transaction transaction = trx.getInstance();
1527+
if (isShieldedTransaction(transaction)
15261528
&& shieldedTransCounts.incrementAndGet() > SHIELDED_TRANS_IN_BLOCK_COUNTS) {
15271529
continue;
15281530
}
@@ -1532,7 +1534,7 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15321534
if (accountSet.contains(ownerAddress)) {
15331535
continue;
15341536
} else {
1535-
if (isMultiSignTransaction(trx.getInstance())) {
1537+
if (isMultiSignTransaction(transaction)) {
15361538
accountSet.add(ownerAddress);
15371539
}
15381540
}
@@ -1542,27 +1544,25 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15421544
// apply transaction
15431545
try (ISession tmpSession = revokingStore.buildSession()) {
15441546
accountStateCallBack.preExeTrans();
1545-
TransactionInfo result = processTransaction(trx, blockCapsule);
1547+
processTransaction(trx, blockCapsule);
15461548
accountStateCallBack.exeTransFinish();
15471549
tmpSession.merge();
1548-
blockCapsule.addTransaction(trx);
1549-
if (Objects.nonNull(result)) {
1550-
transactionRetCapsule.addTransactionInfo(result);
1551-
}
1550+
toBePacked.add(trx);
1551+
currentSize += trx.getSerializedSize() + 2; // proto tag num is 1 , so tag size is 2
15521552
} catch (Exception e) {
15531553
logger.error("Process trx {} failed when generating block {}, {}.", trx.getTransactionId(),
15541554
blockCapsule.getNum(), e.getMessage());
15551555
}
15561556
}
1557-
1557+
blockCapsule.addAllTransactions(toBePacked);
15581558
accountStateCallBack.executeGenerateFinish();
15591559

15601560
session.reset();
15611561

1562-
logger.info("Generate block {} success, trxs: {}, pendingCount: {}, rePushCount: {},"
1563-
+ " postponedCount: {}.",
1562+
logger.info("Generate block {} success, trxs:{}, pendingCount: {}, rePushCount: {},"
1563+
+ " postponedCount: {}, blockSize: {} B",
15641564
blockCapsule.getNum(), blockCapsule.getTransactions().size(),
1565-
pendingTransactions.size(), rePushTransactions.size(), postponedTrxCount);
1565+
pendingTransactions.size(), rePushTransactions.size(), postponedTrxCount, currentSize);
15661566

15671567
blockCapsule.setMerkleRoot();
15681568
blockCapsule.sign(miner.getPrivateKey());
@@ -1654,18 +1654,21 @@ private void processBlock(BlockCapsule block, List<TransactionCapsule> txs)
16541654
try {
16551655
merkleContainer.resetCurrentMerkleTree();
16561656
accountStateCallBack.preExecute(block);
1657+
List<TransactionInfo> results = new ArrayList<>();
1658+
long num = block.getNum();
16571659
for (TransactionCapsule transactionCapsule : block.getTransactions()) {
1658-
transactionCapsule.setBlockNum(block.getNum());
1660+
transactionCapsule.setBlockNum(num);
16591661
if (block.generatedByMyself) {
16601662
transactionCapsule.setVerified(true);
16611663
}
16621664
accountStateCallBack.preExeTrans();
16631665
TransactionInfo result = processTransaction(transactionCapsule, block);
16641666
accountStateCallBack.exeTransFinish();
16651667
if (Objects.nonNull(result)) {
1666-
transactionRetCapsule.addTransactionInfo(result);
1668+
results.add(result);
16671669
}
16681670
}
1671+
transactionRetCapsule.addAllTransactionInfos(results);
16691672
accountStateCallBack.executePushFinish();
16701673
} finally {
16711674
accountStateCallBack.exceptionFinish();

0 commit comments

Comments
 (0)