Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(block): improve performance for transaction packing #4756

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public void addTransaction(TransactionCapsule pendingTrx) {
getTransactions().add(pendingTrx);
}

public void addAllTransactions(List<TransactionCapsule> pendingTrxs) {
List<Transaction> list = pendingTrxs.stream().map(TransactionCapsule::getInstance).collect(
Collectors.toList());
this.block = this.block.toBuilder().addAllTransactions(list).build();
getTransactions().addAll(pendingTrxs);
}

public List<TransactionCapsule> getTransactions() {
return transactions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tron.core.capsule;

import com.google.protobuf.InvalidProtocolBufferException;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.tron.core.exception.BadItemException;
Expand Down Expand Up @@ -29,7 +30,7 @@ public TransactionRetCapsule() {

public TransactionRetCapsule(byte[] data) throws BadItemException {
try {
this.transactionRet = transactionRet.parseFrom(data);
this.transactionRet = TransactionRet.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
throw new BadItemException("TransactionInfoCapsule proto data parse exception");
}
Expand All @@ -39,6 +40,10 @@ public void addTransactionInfo(TransactionInfo result) {
this.transactionRet = this.transactionRet.toBuilder().addTransactioninfo(result).build();
}

public void addAllTransactionInfos(List<TransactionInfo> results) {
this.transactionRet = this.transactionRet.toBuilder().addAllTransactioninfo(results).build();
}

@Override
public byte[] getData() {
if (Objects.isNull(transactionRet)) {
Expand Down
39 changes: 21 additions & 18 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1470,16 +1470,17 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
}
}

TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule(blockCapsule);

Set<String> accountSet = new HashSet<>();
AtomicInteger shieldedTransCounts = new AtomicInteger(0);
List<TransactionCapsule> toBePacked = new ArrayList<>();
long currentSize = blockCapsule.getInstance().getSerializedSize();
boolean isSort = Args.getInstance().isOpenTransactionSort();
while (pendingTransactions.size() > 0 || rePushTransactions.size() > 0) {
boolean fromPending = false;
TransactionCapsule trx;
if (pendingTransactions.size() > 0) {
trx = pendingTransactions.peek();
if (Args.getInstance().isOpenTransactionSort()) {
if (isSort) {
TransactionCapsule trxRepush = rePushTransactions.peek();
if (trxRepush == null || trx.getOrder() >= trxRepush.getOrder()) {
fromPending = true;
Expand Down Expand Up @@ -1516,13 +1517,14 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
}

// check the block size
if ((blockCapsule.getInstance().getSerializedSize() + trx.getSerializedSize() + 3)
if ((currentSize + trx.getSerializedSize() + 3)
> ChainConstant.BLOCK_SIZE) {
postponedTrxCount++;
continue;
continue; // try pack more small trx
}
//shielded transaction
if (isShieldedTransaction(trx.getInstance())
Transaction transaction = trx.getInstance();
if (isShieldedTransaction(transaction)
&& shieldedTransCounts.incrementAndGet() > SHIELDED_TRANS_IN_BLOCK_COUNTS) {
continue;
}
Expand All @@ -1532,7 +1534,7 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
if (accountSet.contains(ownerAddress)) {
continue;
} else {
if (isMultiSignTransaction(trx.getInstance())) {
if (isMultiSignTransaction(transaction)) {
accountSet.add(ownerAddress);
}
}
Expand All @@ -1542,27 +1544,25 @@ public BlockCapsule generateBlock(Miner miner, long blockTime, long timeout) {
// apply transaction
try (ISession tmpSession = revokingStore.buildSession()) {
accountStateCallBack.preExeTrans();
TransactionInfo result = processTransaction(trx, blockCapsule);
processTransaction(trx, blockCapsule);
accountStateCallBack.exeTransFinish();
tmpSession.merge();
blockCapsule.addTransaction(trx);
if (Objects.nonNull(result)) {
transactionRetCapsule.addTransactionInfo(result);
}
toBePacked.add(trx);
currentSize += trx.getSerializedSize() + 2; // proto tag num is 1 , so tag size is 2
} catch (Exception e) {
logger.error("Process trx {} failed when generating block {}, {}.", trx.getTransactionId(),
blockCapsule.getNum(), e.getMessage());
}
}

blockCapsule.addAllTransactions(toBePacked);
accountStateCallBack.executeGenerateFinish();

session.reset();

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

blockCapsule.setMerkleRoot();
blockCapsule.sign(miner.getPrivateKey());
Expand Down Expand Up @@ -1654,18 +1654,21 @@ private void processBlock(BlockCapsule block, List<TransactionCapsule> txs)
try {
merkleContainer.resetCurrentMerkleTree();
accountStateCallBack.preExecute(block);
List<TransactionInfo> results = new ArrayList<>();
long num = block.getNum();
for (TransactionCapsule transactionCapsule : block.getTransactions()) {
transactionCapsule.setBlockNum(block.getNum());
transactionCapsule.setBlockNum(num);
if (block.generatedByMyself) {
transactionCapsule.setVerified(true);
}
accountStateCallBack.preExeTrans();
TransactionInfo result = processTransaction(transactionCapsule, block);
accountStateCallBack.exeTransFinish();
if (Objects.nonNull(result)) {
transactionRetCapsule.addTransactionInfo(result);
results.add(result);
}
}
transactionRetCapsule.addAllTransactionInfos(results);
accountStateCallBack.executePushFinish();
} finally {
accountStateCallBack.exceptionFinish();
Expand Down