Skip to content

Commit 89c47c5

Browse files
committed
feat(block): improve performance for transaction packing
1. remove unused code 2. optimize cycle operation
1 parent 2d88f6e commit 89c47c5

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
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: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,16 +1456,17 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
14561456
}
14571457
}
14581458

1459-
TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule(blockCapsule);
1460-
14611459
Set<String> accountSet = new HashSet<>();
14621460
AtomicInteger shieldedTransCounts = new AtomicInteger(0);
1461+
List<TransactionCapsule> toBePacked = new ArrayList<>();
1462+
long currentSize = blockCapsule.getInstance().getSerializedSize();
1463+
boolean isSort = Args.getInstance().isOpenTransactionSort();
14631464
while (pendingTransactions.size() > 0 || rePushTransactions.size() > 0) {
14641465
boolean fromPending = false;
14651466
TransactionCapsule trx;
14661467
if (pendingTransactions.size() > 0) {
14671468
trx = pendingTransactions.peek();
1468-
if (Args.getInstance().isOpenTransactionSort()) {
1469+
if (isSort) {
14691470
TransactionCapsule trxRepush = rePushTransactions.peek();
14701471
if (trxRepush == null || trx.getOrder() >= trxRepush.getOrder()) {
14711472
fromPending = true;
@@ -1501,24 +1502,24 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15011502
}
15021503

15031504
// check the block size
1504-
if ((blockCapsule.getInstance().getSerializedSize() + trx.getSerializedSize() + 3)
1505-
> ChainConstant.BLOCK_SIZE) {
1505+
if ((currentSize = currentSize + trx.getSerializedSize() + 3) > ChainConstant.BLOCK_SIZE) {
15061506
postponedTrxCount++;
1507-
continue;
1507+
break;
15081508
}
15091509
//shielded transaction
1510-
if (isShieldedTransaction(trx.getInstance())
1510+
Transaction transaction = trx.getInstance();
1511+
if (isShieldedTransaction(transaction)
15111512
&& shieldedTransCounts.incrementAndGet() > SHIELDED_TRANS_IN_BLOCK_COUNTS) {
15121513
continue;
15131514
}
15141515
//multi sign transaction
1515-
Contract contract = trx.getInstance().getRawData().getContract(0);
1516+
Contract contract = transaction.getRawData().getContract(0);
15161517
byte[] owner = TransactionCapsule.getOwner(contract);
15171518
String ownerAddress = ByteArray.toHexString(owner);
15181519
if (accountSet.contains(ownerAddress)) {
15191520
continue;
15201521
} else {
1521-
if (isMultiSignTransaction(trx.getInstance())) {
1522+
if (isMultiSignTransaction(transaction)) {
15221523
accountSet.add(ownerAddress);
15231524
}
15241525
}
@@ -1528,19 +1529,16 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15281529
// apply transaction
15291530
try (ISession tmpSession = revokingStore.buildSession()) {
15301531
accountStateCallBack.preExeTrans();
1531-
TransactionInfo result = processTransaction(trx, blockCapsule);
1532+
processTransaction(trx, blockCapsule);
15321533
accountStateCallBack.exeTransFinish();
15331534
tmpSession.merge();
1534-
blockCapsule.addTransaction(trx);
1535-
if (Objects.nonNull(result)) {
1536-
transactionRetCapsule.addTransactionInfo(result);
1537-
}
1535+
toBePacked.add(trx);
15381536
} catch (Exception e) {
15391537
logger.error("Process trx {} failed when generating block: {}", trx.getTransactionId(),
15401538
e.getMessage());
15411539
}
15421540
}
1543-
1541+
blockCapsule.addAllTransactions(toBePacked);
15441542
accountStateCallBack.executeGenerateFinish();
15451543

15461544
session.reset();
@@ -1641,18 +1639,21 @@ private void processBlock(BlockCapsule block, List<TransactionCapsule> txs)
16411639
try {
16421640
merkleContainer.resetCurrentMerkleTree();
16431641
accountStateCallBack.preExecute(block);
1642+
List<TransactionInfo> results = new ArrayList<>();
1643+
long num = block.getNum();
16441644
for (TransactionCapsule transactionCapsule : block.getTransactions()) {
1645-
transactionCapsule.setBlockNum(block.getNum());
1645+
transactionCapsule.setBlockNum(num);
16461646
if (block.generatedByMyself) {
16471647
transactionCapsule.setVerified(true);
16481648
}
16491649
accountStateCallBack.preExeTrans();
16501650
TransactionInfo result = processTransaction(transactionCapsule, block);
16511651
accountStateCallBack.exeTransFinish();
16521652
if (Objects.nonNull(result)) {
1653-
transactionRetCapsule.addTransactionInfo(result);
1653+
results.add(result);
16541654
}
16551655
}
1656+
transactionRetCapsule.addAllTransactionInfos(results);
16561657
accountStateCallBack.executePushFinish();
16571658
} finally {
16581659
accountStateCallBack.exceptionFinish();

0 commit comments

Comments
 (0)