Skip to content

Commit

Permalink
General fixes + refactor getSublistCandidate
Browse files Browse the repository at this point in the history
  • Loading branch information
ilanolkies committed Nov 23, 2022
1 parent e532933 commit fb96716
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 109 deletions.
72 changes: 45 additions & 27 deletions rskj-core/src/main/java/co/rsk/core/TransactionListExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public Boolean call() {
int numberOfTransactions = block.getTransactionsList().size();
boolean isRemascTransaction = tx.isRemascTransaction(this.i, numberOfTransactions);

if (this.remascEnabled && isRemascTransaction) {
addFeesToRemasc();
}
addFeesToRemascIfEnabled(isRemascTransaction);

TransactionExecutor txExecutor = transactionExecutorFactory.newInstance(
tx,
Expand All @@ -101,16 +99,7 @@ public Boolean call() {
boolean transactionExecuted = txExecutor.executeTransaction();

if (!acceptInvalidTransactions && !transactionExecuted) {
// It's used just for testing, the last tx should be always the REMASC.
payToRemascWhenThereIsNoRemascTx(numberOfTransactions, isRemascTransaction);
if (!discardInvalidTxs) {
logger.warn("block: [{}] execution interrupted because of invalid tx: [{}]",
block.getNumber(), tx.getHash()
);
return false;
}

logger.warn("block: [{}] discarded tx: [{}]", block.getNumber(), tx.getHash());
if (discardIfInvalid(tx, numberOfTransactions, isRemascTransaction)) return false;
continue;
}

Expand All @@ -130,31 +119,21 @@ public Boolean call() {
long txGasUsed = txExecutor.getGasUsed();
totalGasUsed += txGasUsed;

Coin txPaidFees = txExecutor.getPaidFees();
if (txPaidFees != null) {
totalPaidFees = totalPaidFees.add(txPaidFees);
}
addPaidFeesToToal(txExecutor);

// It's used just for testing, the last tx should be always the REMASC.
payToRemascWhenThereIsNoRemascTx(numberOfTransactions, isRemascTransaction);

deletedAccounts.addAll(txExecutor.getResult().getDeleteAccounts());

TransactionReceipt receipt = new TransactionReceipt();
receipt.setGasUsed(txGasUsed);
receipt.setCumulativeGas(totalGasUsed);

receipt.setTxStatus(txExecutor.getReceipt().isSuccessful());
receipt.setTransaction(tx);
receipt.setLogInfoList(txExecutor.getVMLogs());
receipt.setStatus(txExecutor.getReceipt().getStatus());
TransactionReceipt receipt = createTransactionReceipt(totalGasUsed, tx, txExecutor, txGasUsed);

logger.trace("block: [{}] executed tx: [{}]", block.getNumber(), tx.getHash());

logger.trace("tx[{}].receipt", i + 1);

i++;

logger.trace("tx[{}].receipt", i);

receipts.put(i, receipt);

logger.trace("tx[{}] done", i);
Expand All @@ -163,6 +142,45 @@ public Boolean call() {
return true;
}

private boolean discardIfInvalid(Transaction tx, int numberOfTransactions, boolean isRemascTransaction) {
// It's used just for testing, the last tx should be always the REMASC.
payToRemascWhenThereIsNoRemascTx(numberOfTransactions, isRemascTransaction);
if (!discardInvalidTxs) {
logger.warn("block: [{}] execution interrupted because of invalid tx: [{}]",
block.getNumber(), tx.getHash()
);
return true;
}

logger.warn("block: [{}] discarded tx: [{}]", block.getNumber(), tx.getHash());
return false;
}

private TransactionReceipt createTransactionReceipt(long totalGasUsed, Transaction tx, TransactionExecutor txExecutor, long txGasUsed) {
TransactionReceipt receipt = new TransactionReceipt();
receipt.setGasUsed(txGasUsed);
receipt.setCumulativeGas(totalGasUsed);

receipt.setTxStatus(txExecutor.getReceipt().isSuccessful());
receipt.setTransaction(tx);
receipt.setLogInfoList(txExecutor.getVMLogs());
receipt.setStatus(txExecutor.getReceipt().getStatus());
return receipt;
}

private void addPaidFeesToToal(TransactionExecutor txExecutor) {
Coin txPaidFees = txExecutor.getPaidFees();
if (txPaidFees != null) {
totalPaidFees = totalPaidFees.add(txPaidFees);
}
}

private void addFeesToRemascIfEnabled(boolean isRemascTransaction) {
if (this.remascEnabled && isRemascTransaction) {
addFeesToRemasc();
}
}

private void payToRemascWhenThereIsNoRemascTx(int numberOfTransactions, boolean isRemascTransaction) {
boolean isLastTx = this.i == numberOfTransactions - 1;
if (this.remascEnabled && isLastTx && !isRemascTransaction) {
Expand Down
139 changes: 86 additions & 53 deletions rskj-core/src/main/java/co/rsk/core/bc/BlockExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,7 @@ private BlockResult executePreviousRSKIP144(

for (Transaction tx : block.getTransactionsList()) {

if (this.isRemascEnabled() && tx.isRemascTransaction(block.getTransactionsList().size(), txindex)) {
addFeesToRemasc(totalPaidFees, track);
}
addFeesToRemascIfRemascTx(block, track, totalPaidFees, txindex, tx);

loggingApplyBlockToTx(block, i);

Expand All @@ -367,23 +365,11 @@ private BlockResult executePreviousRSKIP144(
continue;
}

executedTransactions.add(tx);

if (this.registerProgramResults) {
this.transactionResults.put(tx.getHash(), txExecutor.getResult());
}

if (vmTrace) {
txExecutor.extractTrace(programTraceProcessor);
}

loggingTxExecuted();
registerExecutedTx(programTraceProcessor, vmTrace, executedTransactions, tx, txExecutor);
long gasUsed = txExecutor.getGasUsed();
totalGasUsed += gasUsed;
Coin paidFees = txExecutor.getPaidFees();
if (paidFees != null) {
totalPaidFees = totalPaidFees.add(paidFees);
}

totalPaidFees = addTotalPaidFees(totalPaidFees, txExecutor);

deletedAccounts.addAll(txExecutor.getResult().getDeleteAccounts());

Expand Down Expand Up @@ -416,6 +402,34 @@ private BlockResult executePreviousRSKIP144(
return result;
}

private void registerExecutedTx(ProgramTraceProcessor programTraceProcessor, boolean vmTrace, List<Transaction> executedTransactions, Transaction tx, TransactionExecutor txExecutor) {
executedTransactions.add(tx);

if (this.registerProgramResults) {
this.transactionResults.put(tx.getHash(), txExecutor.getResult());
}

if (vmTrace) {
txExecutor.extractTrace(programTraceProcessor);
}

loggingTxExecuted();
}

private void addFeesToRemascIfRemascTx(Block block, Repository track, Coin totalPaidFees, int txindex, Transaction tx) {
if (this.isRemascEnabled() && tx.isRemascTransaction(block.getTransactionsList().size(), txindex)) {
addFeesToRemasc(totalPaidFees, track);
}
}

private Coin addTotalPaidFees(Coin totalPaidFees, TransactionExecutor txExecutor) {
Coin paidFees = txExecutor.getPaidFees();
if (paidFees != null) {
totalPaidFees = totalPaidFees.add(paidFees);
}
return totalPaidFees;
}

private BlockResult executeParallel(
@Nullable ProgramTraceProcessor programTraceProcessor,
int vmTraceOptions,
Expand Down Expand Up @@ -625,9 +639,7 @@ private BlockResult executeForMiningAfterRSKIP144(
int numberOfTransactions = transactionsList.size();
boolean isRemascTransaction = tx.isRemascTransaction(txindex, numberOfTransactions);

if (this.isRemascEnabled() && isRemascTransaction) {
addFeesToRemasc(totalPaidFees, track);
}
addFeesToRemascIfRemascTxAndTrack(track, totalPaidFees, isRemascTransaction);

TransactionExecutor txExecutor = transactionExecutorFactory.newInstance(
tx,
Expand All @@ -643,53 +655,32 @@ private BlockResult executeForMiningAfterRSKIP144(
boolean transactionExecuted = txExecutor.executeTransaction();

if (!acceptInvalidTransactions && !transactionExecuted) {
payToRemascWhenThereIsNoRemascTx(track, totalPaidFees, txindex, numberOfTransactions, isRemascTransaction);

if (!discardInvalidTxs) {
if (discardIfInvalid(block, discardInvalidTxs, track, totalPaidFees, txindex, tx, numberOfTransactions, isRemascTransaction)) {
return getBlockResultAndLogExecutionInterrupted(block, metric, tx);
}

loggingDiscardedBlock(block, tx);
txindex++;
continue;
}

Optional<Long> sublistGasAccumulated;
if (isRemascTransaction) {
sublistGasAccumulated = parallelizeTransactionHandler.addRemascTransaction(tx, txExecutor.getGasUsed());
} else {
sublistGasAccumulated = parallelizeTransactionHandler.addTransaction(tx, readWrittenKeysTracker.getThisThreadReadKeys(), readWrittenKeysTracker.getThisThreadWrittenKeys(), txExecutor.getGasUsed());
}
Optional<Long> sublistGasAccumulated = calculateSublistGasAccumulated(readWrittenKeysTracker, parallelizeTransactionHandler, tx, isRemascTransaction, txExecutor);

if (!acceptInvalidTransactions && !sublistGasAccumulated.isPresent()) {
payToRemascWhenThereIsNoRemascTx(track, totalPaidFees, txindex, numberOfTransactions, isRemascTransaction);

if (!discardInvalidTxs) {
if (discardIfInvalid(block, discardInvalidTxs, track, totalPaidFees, txindex, tx, numberOfTransactions, isRemascTransaction)) {
return getBlockResultAndLogExecutionInterrupted(block, metric, tx);
}

loggingDiscardedBlock(block, tx);
txindex++;
continue;
}

readWrittenKeysTracker.clear();

if (this.registerProgramResults) {
this.transactionResults.put(tx.getHash(), txExecutor.getResult());
}
registerTxExecutedForMiningAfterRSKIP144(readWrittenKeysTracker, tx, txExecutor);

loggingTxExecuted();
long gasUsed = txExecutor.getGasUsed();
gasUsedInBlock += gasUsed;
Coin paidFees = txExecutor.getPaidFees();
if (paidFees != null) {
totalPaidFees = totalPaidFees.add(paidFees);
}
totalPaidFees = addTotalPaidFees(totalPaidFees, txExecutor);

payToRemascWhenThereIsNoRemascTx(track, totalPaidFees, txindex, numberOfTransactions, isRemascTransaction);
deletedAccounts.addAll(txExecutor.getResult().getDeleteAccounts());

deletedAccounts.addAll(txExecutor.getResult().getDeleteAccounts());

//orElseGet is used for testing only when acceptInvalidTransactions is set.
long cumulativeGas = sublistGasAccumulated
Expand All @@ -712,11 +703,7 @@ private BlockResult executeForMiningAfterRSKIP144(

List<Transaction> executedTransactions = parallelizeTransactionHandler.getTransactionsInOrder();
short[] sublistOrder = parallelizeTransactionHandler.getTransactionsPerSublistInOrder();
List<TransactionReceipt> receipts = new ArrayList<>();

for (Transaction tx : executedTransactions) {
receipts.add(receiptsByTx.get(tx));
}
List<TransactionReceipt> receipts = getTransactionReceipts(receiptsByTx, executedTransactions);

BlockResult result = new BlockResult(
block,
Expand All @@ -732,6 +719,52 @@ private BlockResult executeForMiningAfterRSKIP144(
return result;
}

private void registerTxExecutedForMiningAfterRSKIP144(IReadWrittenKeysTracker readWrittenKeysTracker, Transaction tx, TransactionExecutor txExecutor) {
readWrittenKeysTracker.clear();

if (this.registerProgramResults) {
this.transactionResults.put(tx.getHash(), txExecutor.getResult());
}

loggingTxExecuted();
}

private List<TransactionReceipt> getTransactionReceipts(Map<Transaction, TransactionReceipt> receiptsByTx, List<Transaction> executedTransactions) {
List<TransactionReceipt> receipts = new ArrayList<>();

for (Transaction tx : executedTransactions) {
receipts.add(receiptsByTx.get(tx));
}
return receipts;
}

private boolean discardIfInvalid(Block block, boolean discardInvalidTxs, Repository track, Coin totalPaidFees, int txindex, Transaction tx, int numberOfTransactions, boolean isRemascTransaction) {
payToRemascWhenThereIsNoRemascTx(track, totalPaidFees, txindex, numberOfTransactions, isRemascTransaction);

if (!discardInvalidTxs) {
return true;
}

loggingDiscardedBlock(block, tx);
return false;
}

private Optional<Long> calculateSublistGasAccumulated(IReadWrittenKeysTracker readWrittenKeysTracker, ParallelizeTransactionHandler parallelizeTransactionHandler, Transaction tx, boolean isRemascTransaction, TransactionExecutor txExecutor) {
Optional<Long> sublistGasAccumulated;
if (isRemascTransaction) {
sublistGasAccumulated = parallelizeTransactionHandler.addRemascTransaction(tx, txExecutor.getGasUsed());
} else {
sublistGasAccumulated = parallelizeTransactionHandler.addTransaction(tx, readWrittenKeysTracker.getThisThreadReadKeys(), readWrittenKeysTracker.getThisThreadWrittenKeys(), txExecutor.getGasUsed());
}
return sublistGasAccumulated;
}

private void addFeesToRemascIfRemascTxAndTrack(Repository track, Coin totalPaidFees, boolean isRemascTransaction) {
if (this.isRemascEnabled() && isRemascTransaction) {
addFeesToRemasc(totalPaidFees, track);
}
}

private void saveOrCommitTrackState(boolean saveState, Repository track) {
logger.trace("End txs executions.");
if (saveState) {
Expand Down
Loading

0 comments on commit fb96716

Please sign in to comment.