Skip to content

Commit a66493b

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

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
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: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,6 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
14341434
MetricKeys.Histogram.BLOCK_GENERATE_LATENCY, address);
14351435
Metrics.histogramObserve(MetricKeys.Histogram.MINER_LATENCY,
14361436
(System.currentTimeMillis() - blockTime) / Metrics.MILLISECONDS_PER_SECOND, address);
1437-
long postponedTrxCount = 0;
14381437
logger.info("Generate block {} begin", chainBaseManager.getHeadBlockNum() + 1);
14391438

14401439
BlockCapsule blockCapsule = new BlockCapsule(chainBaseManager.getHeadBlockNum() + 1,
@@ -1456,16 +1455,17 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
14561455
}
14571456
}
14581457

1459-
TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule(blockCapsule);
1460-
14611458
Set<String> accountSet = new HashSet<>();
14621459
AtomicInteger shieldedTransCounts = new AtomicInteger(0);
1460+
List<TransactionCapsule> toBePacked = new ArrayList<>();
1461+
long currentSize = blockCapsule.getInstance().getSerializedSize();
1462+
boolean isSort = Args.getInstance().isOpenTransactionSort();
14631463
while (pendingTransactions.size() > 0 || rePushTransactions.size() > 0) {
14641464
boolean fromPending = false;
14651465
TransactionCapsule trx;
14661466
if (pendingTransactions.size() > 0) {
14671467
trx = pendingTransactions.peek();
1468-
if (Args.getInstance().isOpenTransactionSort()) {
1468+
if (isSort) {
14691469
TransactionCapsule trxRepush = rePushTransactions.peek();
14701470
if (trxRepush == null || trx.getOrder() >= trxRepush.getOrder()) {
14711471
fromPending = true;
@@ -1501,24 +1501,23 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15011501
}
15021502

15031503
// check the block size
1504-
if ((blockCapsule.getInstance().getSerializedSize() + trx.getSerializedSize() + 3)
1505-
> ChainConstant.BLOCK_SIZE) {
1506-
postponedTrxCount++;
1507-
continue;
1504+
if ((currentSize += trx.getSerializedSize() + 2) > ChainConstant.BLOCK_SIZE) {
1505+
break;
15081506
}
15091507
//shielded transaction
1510-
if (isShieldedTransaction(trx.getInstance())
1508+
Transaction transaction = trx.getInstance();
1509+
if (isShieldedTransaction(transaction)
15111510
&& shieldedTransCounts.incrementAndGet() > SHIELDED_TRANS_IN_BLOCK_COUNTS) {
15121511
continue;
15131512
}
15141513
//multi sign transaction
1515-
Contract contract = trx.getInstance().getRawData().getContract(0);
1514+
Contract contract = transaction.getRawData().getContract(0);
15161515
byte[] owner = TransactionCapsule.getOwner(contract);
15171516
String ownerAddress = ByteArray.toHexString(owner);
15181517
if (accountSet.contains(ownerAddress)) {
15191518
continue;
15201519
} else {
1521-
if (isMultiSignTransaction(trx.getInstance())) {
1520+
if (isMultiSignTransaction(transaction)) {
15221521
accountSet.add(ownerAddress);
15231522
}
15241523
}
@@ -1528,27 +1527,24 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
15281527
// apply transaction
15291528
try (ISession tmpSession = revokingStore.buildSession()) {
15301529
accountStateCallBack.preExeTrans();
1531-
TransactionInfo result = processTransaction(trx, blockCapsule);
1530+
processTransaction(trx, blockCapsule);
15321531
accountStateCallBack.exeTransFinish();
15331532
tmpSession.merge();
1534-
blockCapsule.addTransaction(trx);
1535-
if (Objects.nonNull(result)) {
1536-
transactionRetCapsule.addTransactionInfo(result);
1537-
}
1533+
toBePacked.add(trx);
15381534
} catch (Exception e) {
15391535
logger.error("Process trx {} failed when generating block: {}", trx.getTransactionId(),
15401536
e.getMessage());
15411537
}
15421538
}
1543-
1539+
blockCapsule.addAllTransactions(toBePacked);
15441540
accountStateCallBack.executeGenerateFinish();
15451541

15461542
session.reset();
15471543

15481544
logger.info("Generate block {} success, trxs:{}, pendingCount: {}, rePushCount: {},"
1549-
+ " postponedCount: {}",
1545+
+ " blockSize: {} B",
15501546
blockCapsule.getNum(), blockCapsule.getTransactions().size(),
1551-
pendingTransactions.size(), rePushTransactions.size(), postponedTrxCount);
1547+
pendingTransactions.size(), rePushTransactions.size(), currentSize);
15521548

15531549
blockCapsule.setMerkleRoot();
15541550
blockCapsule.sign(miner.getPrivateKey());
@@ -1641,18 +1637,21 @@ private void processBlock(BlockCapsule block, List<TransactionCapsule> txs)
16411637
try {
16421638
merkleContainer.resetCurrentMerkleTree();
16431639
accountStateCallBack.preExecute(block);
1640+
List<TransactionInfo> results = new ArrayList<>();
1641+
long num = block.getNum();
16441642
for (TransactionCapsule transactionCapsule : block.getTransactions()) {
1645-
transactionCapsule.setBlockNum(block.getNum());
1643+
transactionCapsule.setBlockNum(num);
16461644
if (block.generatedByMyself) {
16471645
transactionCapsule.setVerified(true);
16481646
}
16491647
accountStateCallBack.preExeTrans();
16501648
TransactionInfo result = processTransaction(transactionCapsule, block);
16511649
accountStateCallBack.exeTransFinish();
16521650
if (Objects.nonNull(result)) {
1653-
transactionRetCapsule.addTransactionInfo(result);
1651+
results.add(result);
16541652
}
16551653
}
1654+
transactionRetCapsule.addAllTransactionInfos(results);
16561655
accountStateCallBack.executePushFinish();
16571656
} finally {
16581657
accountStateCallBack.exceptionFinish();

0 commit comments

Comments
 (0)