Skip to content

Commit

Permalink
Merge pull request #2914 from Taraxa-project/tn_no_balance_nonce
Browse files Browse the repository at this point in the history
fix: handle transactions with no nonce increase
  • Loading branch information
mfrankovi authored Dec 16, 2024
2 parents 88f0835 + b8fb4f3 commit 7358f7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 11 additions & 1 deletion libraries/core_libs/consensus/src/pbft/pbft_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1939,15 +1939,25 @@ std::optional<std::pair<PeriodData, std::vector<std::shared_ptr<PbftVote>>>> Pbf
}
auto non_finalized_transactions = trx_mgr_->excludeFinalizedTransactions(transactions_to_query);

if (non_finalized_transactions.size() != period_data.transactions.size()) {
if (non_finalized_transactions.size() > period_data.transactions.size()) {
LOG(log_er_) << "Synced PBFT block " << pbft_block_hash << " transactions count " << period_data.transactions.size()
<< " incorrect, expected: " << non_finalized_transactions.size();

sync_queue_.clear();
net->handleMaliciousSyncPeer(node_id);
return std::nullopt;
}
for (uint32_t i = 0; i < period_data.transactions.size(); i++) {
if (!non_finalized_transactions.contains(period_data.transactions[i]->getHash())) {
const auto account =
final_chain_->getAccount(period_data.transactions[i]->getSender()).value_or(taraxa::state_api::ZeroAccount);

// Check for transaction already included but without an increase in nonce
if (account.nonce < period_data.transactions[i]->getNonce()) {
LOG(log_nf_) << "Skipping duplicate";
continue;
}

LOG(log_er_) << "Synced PBFT block " << pbft_block_hash << " has incorrect transaction "
<< period_data.transactions[i]->getHash();
sync_queue_.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,10 @@ void TransactionManager::saveTransactionsFromDagBlock(SharedTransactions const &
std::unique_lock transactions_lock(transactions_mutex_);

for (auto t : trxs) {
const auto account = final_chain_->getAccount(t->getSender()).value_or(taraxa::state_api::ZeroAccount);
const auto tx_hash = t->getHash();

// Checking nonce in cheaper than checking db, verify with nonce if possible
bool trx_not_executed = account.nonce < t->getNonce() || !db_->transactionFinalized(tx_hash);

if (trx_not_executed) {
if (!recently_finalized_transactions_.contains(tx_hash) && !nonfinalized_transactions_in_dag_.contains(tx_hash) &&
!db_->transactionFinalized(tx_hash)) {
if (!recently_finalized_transactions_.contains(tx_hash) &&
!nonfinalized_transactions_in_dag_.contains(tx_hash)) {
db_->addTransactionToBatch(*t, write_batch);
Expand Down

0 comments on commit 7358f7e

Please sign in to comment.