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

Replace EVM TXs with higher fee TXs #2094

Merged
merged 1 commit into from
Jun 29, 2023
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
67 changes: 59 additions & 8 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,25 @@ int BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& already
return nDescendantsUpdated;
}

void BlockAssembler::RemoveEVMTransaction(const CTxMemPool::txiter iter) {

// Make sure we only remove EVM TXs which have no descendants or TX fees.
// Removing others TXs is more complicated and should be handled by RemoveFailedTransactions.
const auto &tx = iter->GetTx();
std::vector<unsigned char> metadata;
const auto txType = GuessCustomTxType(tx, metadata, false);
if (txType != CustomTxType::EvmTx) {
return;
}

for (size_t i = 0; i < pblock->vtx.size(); ++i) {
if (pblock->vtx[i] && pblock->vtx[i]->GetHash() == iter->GetTx().GetHash()) {
pblock->vtx.erase(pblock->vtx.begin() + i);
return;
}
}
}

void BlockAssembler::RemoveFailedTransactions(const std::vector<std::string> &failedTransactions, const std::map<uint256, CAmount> &txFees) {
if (failedTransactions.empty()) {
return;
Expand Down Expand Up @@ -666,6 +685,15 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
// Variable to tally total gas used in the block
uint64_t totalGas{};

// Used to track EVM TXs by sender and nonce to allow replacing of TXs with higher
// TXs with the same nonce.
// Key: sender address, nonce
// Value: fee, TX iter
std::map<std::pair<std::array<std::uint8_t, 20>, uint64_t>, std::pair<uint64_t, CTxMemPool::txiter>> evmTXs;

// Used to track gas fees of EVM TXs
std::map<uint256, uint64_t> evmGasFees;

while (mi != mempool.mapTx.get<T>().end() || !mapModifiedTx.empty() || !failedNonces.empty())
{
// First try to find a new transaction in mapTx to evaluate.
Expand Down Expand Up @@ -778,6 +806,9 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
continue;
}

// Holder for an EVM TX that needs to have its gas removed
uint256 removeEVMGas{};

// temporary view to ensure failed tx
// to not be kept in parent view
CCoinsViewCache coins(&coinsView);
Expand Down Expand Up @@ -807,20 +838,34 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
break;
}

const auto nonce = evm_get_next_valid_nonce_in_context(evmContext, txResult.sender);
if (nonce != txResult.nonce) {
// Only add if not already in failed TXs to prevent adding on second attempt.
if (!failedTx.count(iter)) {
failedNonces.emplace(nonce, iter);
const auto evmKey = std::make_pair(txResult.sender, txResult.nonce);
if (evmTXs.count(evmKey)) {
const auto& [usedGas, txIter] = evmTXs.at(evmKey);
if (txResult.used_gas > usedGas) {
RemoveEVMTransaction(txIter);
removeEVMGas = txIter->GetTx().GetHash();
evmTXs[evmKey] = std::make_pair(txResult.used_gas, sortedEntries[i]);
}
customTxPassed = false;
break;
}

if (removeEVMGas.IsNull()) {
const auto nonce = evm_get_next_valid_nonce_in_context(evmContext, txResult.sender);
if (nonce != txResult.nonce) {
// Only add if not already in failed TXs to prevent adding on second attempt.
if (!failedTx.count(iter)) {
failedNonces.emplace(nonce, iter);
}
customTxPassed = false;
break;
}

evmTXs.emplace(std::make_pair(txResult.sender, txResult.nonce), std::make_pair(txResult.used_gas, sortedEntries[i]));
}
}

uint64_t gasUsed{};
const auto res = ApplyCustomTx(view, coins, tx, chainparams.GetConsensus(), nHeight, gasUsed, pblock->nTime, nullptr, 0, evmContext);
// Not okay invalidate, undo and skip
// Not okay invalidate, undo and skip
if (!res.ok) {
customTxPassed = false;

Expand All @@ -829,6 +874,12 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
break;
}

evmGasFees.emplace(tx.GetHash(), gasUsed);

if (!removeEVMGas.IsNull() && evmGasFees.count(removeEVMGas)) {
totalGas -= evmGasFees.at(removeEVMGas);
}

if (totalGas + gasUsed > MAX_BLOCK_GAS_LIMIT) {
customTxPassed = false;
break;
Expand Down
4 changes: 3 additions & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ class BlockAssembler
* state updated assuming given transactions are inBlock. Returns number
* of updated descendants. */
int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
/** Remove failed EVM transactions from the block */
/** Remove failed TransferDoamin transactions from the block */
void RemoveFailedTransactions(const std::vector<std::string> &failedTransactions, const std::map<uint256, CAmount> &txFees);
/** Remove specific TX from the block */
void RemoveEVMTransaction(const CTxMemPool::txiter iter);
};

/** Modify the extranonce in a block */
Expand Down