Skip to content

Commit

Permalink
Skip block size limit check from mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Sep 13, 2023
1 parent 95b593b commit 80716c2
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl EVMCoreService {
tx: &str,
queue_id: u64,
pre_validate: bool,
test_tx: bool,
skip_block_limit: bool,
) -> Result<ValidateTxInfo> {
debug!("[validate_raw_tx] queue_id {}", queue_id);
debug!("[validate_raw_tx] raw transaction : {:#?}", tx);
Expand Down Expand Up @@ -329,7 +329,7 @@ impl EVMCoreService {
.get_total_gas_used_in(queue_id)
.unwrap_or_default();

if !test_tx {
if !skip_block_limit {
let block_gas_limit = self.storage.get_attributes_or_default()?.block_gas_limit;
if total_current_gas_used + U256::from(used_gas) > U256::from(block_gas_limit) {
return Err(format_err!("Tx can't make it in block. Block size limit {}, pending block gas used : {:x?}, tx used gas : {:x?}, total : {:x?}", block_gas_limit, total_current_gas_used, U256::from(used_gas), total_current_gas_used + U256::from(used_gas)).into());
Expand Down
4 changes: 2 additions & 2 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ pub fn evm_unsafe_try_validate_raw_tx_in_q(
queue_id: u64,
raw_tx: &str,
pre_validate: bool,
test_tx: bool,
skip_block_limit: bool,
) -> ffi::ValidateTxMiner {
debug!("[evm_unsafe_try_validate_raw_tx_in_q]");
match SERVICES.evm.verify_tx_fees(raw_tx) {
Expand All @@ -474,7 +474,7 @@ pub fn evm_unsafe_try_validate_raw_tx_in_q(
match SERVICES
.evm
.core
.validate_raw_tx(raw_tx, queue_id, pre_validate, test_tx)
.validate_raw_tx(raw_tx, queue_id, pre_validate, skip_block_limit)
{
Ok(ValidateTxInfo {
signed_tx,
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub mod ffi {
queue_id: u64,
raw_tx: &str,
pre_validate: bool,
test_tx: bool,
skip_block_limit: bool,
) -> ValidateTxMiner;
fn evm_unsafe_try_push_tx_in_q(
result: &mut CrossBoundaryResult,
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c
// Note: TXs are already filtered. So we pass isEVMEnabled to false, but for future proof, refactor this enough,
// that it's propagated.
uint64_t gasUsed{};
auto res = ApplyCustomTx(discardCache, inputs, tx, chainparams.GetConsensus(), nSpendHeight, gasUsed, 0, &canSpend, 0, 0, false);
auto res = ApplyCustomTx(discardCache, inputs, tx, chainparams.GetConsensus(), nSpendHeight, gasUsed, 0, &canSpend, 0, 0, false, false);
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-customtx", res.msg);
}
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/consensus/txvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CCustomTxVisitor::CCustomTxVisitor(const CTransaction &tx,
const bool isEvmEnabledForBlock,
uint64_t &gasUsed,
const bool evmPreValidate,
const bool testTx)
const bool skipBlockSizeLimit)
: height(height),
mnview(mnview),
tx(tx),
Expand All @@ -76,7 +76,7 @@ CCustomTxVisitor::CCustomTxVisitor(const CTransaction &tx,
isEvmEnabledForBlock(isEvmEnabledForBlock),
gasUsed(gasUsed),
evmPreValidate(evmPreValidate),
testTx(testTx) {}
skipBlockSizeLimit(skipBlockSizeLimit) {}

Res CCustomTxVisitor::HasAuth(const CScript &auth) const {
return ::HasAuth(tx, coins, auth);
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/consensus/txvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CCustomTxVisitor {
bool isEvmEnabledForBlock;
uint64_t &gasUsed;
bool evmPreValidate;
bool testTx;
bool skipBlockSizeLimit;

public:
CCustomTxVisitor(const CTransaction &tx,
Expand All @@ -68,7 +68,7 @@ class CCustomTxVisitor {
const bool isEvmEnabledForBlock,
uint64_t &gasUsed,
const bool evmPreValidate,
const bool testTx);
const bool skipBlockSizeLimit);

protected:
Res HasAuth(const CScript &auth) const;
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/consensus/xvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ Res CXVMConsensus::operator()(const CEvmTxMessage &obj) const {
return Res::Err("evm tx size too large");

CrossBoundaryResult result;
auto validateResults = evm_unsafe_try_validate_raw_tx_in_q(result, evmQueueId, HexStr(obj.evmTx), evmPreValidate, testTx);
auto validateResults = evm_unsafe_try_validate_raw_tx_in_q(result, evmQueueId, HexStr(obj.evmTx), evmPreValidate, skipBlockSizeLimit);
if (!result.ok) {
LogPrintf("[evm_try_validate_raw_tx] failed, reason : %s\n", result.reason);
return Res::Err("evm tx failed to validate %s", result.reason);
Expand Down
17 changes: 9 additions & 8 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ class CCustomTxApplyVisitor {
bool isEvmEnabledForBlock;
uint64_t &gasUsed;
bool evmPreValidate;
bool testTx;
bool skipBlockSizeLimit;

template<typename T, typename T1, typename ...Args>
Res ConsensusHandler(const T& obj) const {

static_assert(std::is_base_of_v<CCustomTxVisitor, T1>, "CCustomTxVisitor base required");

if constexpr (std::is_invocable_v<T1, T>)
return T1{tx, height, coins, mnview, consensus, time, txn, evmQueueId, isEvmEnabledForBlock, gasUsed, evmPreValidate, testTx}(obj);
return T1{tx, height, coins, mnview, consensus, time, txn, evmQueueId, isEvmEnabledForBlock, gasUsed, evmPreValidate, skipBlockSizeLimit}(obj);
else if constexpr (sizeof...(Args) != 0)
return ConsensusHandler<T, Args...>(obj);
else
Expand All @@ -362,7 +362,7 @@ class CCustomTxApplyVisitor {
const bool isEvmEnabledForBlock,
uint64_t &gasUsed,
const bool evmPreValidate,
const bool testTx)
const bool skipBlockSizeLimit)

: tx(tx),
height(height),
Expand All @@ -375,7 +375,7 @@ class CCustomTxApplyVisitor {
isEvmEnabledForBlock(isEvmEnabledForBlock),
gasUsed(gasUsed),
evmPreValidate(evmPreValidate),
testTx(testTx) {}
skipBlockSizeLimit(skipBlockSizeLimit) {}

template<typename T>
Res operator()(const T& obj) const {
Expand Down Expand Up @@ -455,7 +455,7 @@ Res CustomTxVisit(CCustomCSView &mnview,
const uint64_t evmQueueId,
const bool isEvmEnabledForBlock,
const bool evmPreValidate,
const bool testTx) {
const bool skipBlockSizeLimit) {
if (IsDisabledTx(height, tx, consensus)) {
return Res::ErrCode(CustomTxErrCodes::Fatal, "Disabled custom transaction");
}
Expand All @@ -470,7 +470,7 @@ Res CustomTxVisit(CCustomCSView &mnview,

try {
auto res = std::visit(
CCustomTxApplyVisitor(tx, height, coins, mnview, consensus, time, txn, q, isEvmEnabledForBlock, gasUsed, evmPreValidate, testTx),
CCustomTxApplyVisitor(tx, height, coins, mnview, consensus, time, txn, q, isEvmEnabledForBlock, gasUsed, evmPreValidate, skipBlockSizeLimit),
txMessage);
if (wipeQueue) {
XResultStatusLogged(evm_unsafe_try_remove_queue(result, q));
Expand Down Expand Up @@ -569,7 +569,8 @@ Res ApplyCustomTx(CCustomCSView &mnview,
uint256 *canSpend,
uint32_t txn,
const uint64_t evmQueueId,
const bool isEvmEnabledForBlock) {
const bool isEvmEnabledForBlock,
const bool skipBlockSizeLimit) {
auto res = Res::Ok();
if (tx.IsCoinBase() && height > 0) { // genesis contains custom coinbase txs
return res;
Expand Down Expand Up @@ -608,7 +609,7 @@ Res ApplyCustomTx(CCustomCSView &mnview,
PopulateVaultHistoryData(mnview.GetHistoryWriters(), view, txMessage, txType, height, txn, tx.GetHash());
}

res = CustomTxVisit(view, coins, tx, height, consensus, txMessage, time, gasUsed, txn, evmQueueId, isEvmEnabledForBlock, false, false);
res = CustomTxVisit(view, coins, tx, height, consensus, txMessage, time, gasUsed, txn, evmQueueId, isEvmEnabledForBlock, false, skipBlockSizeLimit);

if (res) {
if (canSpend && txType == CustomTxType::UpdateMasternode) {
Expand Down
5 changes: 3 additions & 2 deletions src/masternodes/mn_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ Res ApplyCustomTx(CCustomCSView &mnview,
uint256 *canSpend,
uint32_t txn,
const uint64_t evmQueueId,
const bool isEvmEnabledForBlock);
const bool isEvmEnabledForBlock,
const bool skipBlockSizeLimit);

Res CustomTxVisit(CCustomCSView &mnview,
const CCoinsViewCache &coins,
Expand All @@ -189,7 +190,7 @@ Res CustomTxVisit(CCustomCSView &mnview,
const uint64_t evmQueueId,
const bool isEvmEnabledForBlock,
const bool evmPreValidate,
const bool testTx);
const bool skipBlockSizeLimit);


ResVal<uint256> ApplyAnchorRewardTx(CCustomCSView &mnview,
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/rpc_tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ UniValue getcustomtx(const JSONRPCRequest& request)
auto isEvmEnabledForBlock = IsEVMEnabled(nHeight, mnview, consensus);

uint64_t gasUsed{};
auto res = ApplyCustomTx(mnview, view, *tx, consensus, nHeight, gasUsed, 0, nullptr, 0, 0, isEvmEnabledForBlock);
auto res = ApplyCustomTx(mnview, view, *tx, consensus, nHeight, gasUsed, 0, nullptr, 0, 0, isEvmEnabledForBlock, true);

result.pushKV("valid", res.ok);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
}

uint64_t gasUsed{};
const auto res = ApplyCustomTx(view, coins, tx, chainparams.GetConsensus(), nHeight, gasUsed, pblock->nTime, nullptr, 0, evmQueueId, isEvmEnabledForBlock);
const auto res = ApplyCustomTx(view, coins, tx, chainparams.GetConsensus(), nHeight, gasUsed, pblock->nTime, nullptr, 0, evmQueueId, isEvmEnabledForBlock, false);
// Not okay invalidate, undo and skip
if (!res.ok) {
customTxPassed = false;
Expand Down
8 changes: 4 additions & 4 deletions src/test/applytx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false);
res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false, false);
BOOST_CHECK(!res.ok);
BOOST_CHECK_NE(res.msg.find("negative amount"), std::string::npos);
// check that nothing changes:
Expand All @@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)
rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

uint64_t gasUsed{};
res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false);
res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false, false);
BOOST_CHECK(!res.ok);
BOOST_CHECK_EQUAL(res.code, (uint32_t) CustomTxErrCodes::NotEnoughBalance);
// check that nothing changes:
Expand All @@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false);
res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false, false);
BOOST_CHECK(!res.ok);
BOOST_CHECK_NE(res.msg.find("negative amount"), std::string::npos);
// check that nothing changes:
Expand All @@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false);
res = ApplyCustomTx(mnview, coinview, CTransaction(rawTx), amkCheated, 1, gasUsed, 0, nullptr, 0, 0, false, false);
BOOST_CHECK(res.ok);
// check result balances:
auto const dfi90 = CTokenAmount{DFI, 90};
Expand Down
2 changes: 1 addition & 1 deletion src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ Res CTxMemPool::rebuildAccountsView(int height, const CCoinsViewCache& coinsCach
feeMapLookup.emplace(tx->GetHash(), feeMapRes.first);
}

auto res = ApplyCustomTx(viewDuplicate, coinsCache, *tx, consensus, height, gasUsed, 0, nullptr, 0, evmQueueId, isEvmEnabledForBlock);
auto res = ApplyCustomTx(viewDuplicate, coinsCache, *tx, consensus, height, gasUsed, 0, nullptr, 0, evmQueueId, isEvmEnabledForBlock, true);
if (!res) {
failedTxSet.insert(tx->GetHash());
if ((res.code & CustomTxErrCodes::Fatal)) {
Expand Down
6 changes: 3 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
// be tested against the current TX in rebuildAccountsView.
if (!pool.getAccountViewDirty() && pool.getEvmQueueId()) {
uint64_t gasUsed{};
auto res = ApplyCustomTx(mnview, view, tx, consensus, height, gasUsed, nAcceptTime, nullptr, 0, 0, isEvmEnabledForBlock);
auto res = ApplyCustomTx(mnview, view, tx, consensus, height, gasUsed, nAcceptTime, nullptr, 0, 0, isEvmEnabledForBlock, false);
if (!res.ok || (res.code & CustomTxErrCodes::Fatal)) {
return state.Invalid(ValidationInvalidReason::TX_MEMPOOL_POLICY, false, REJECT_INVALID, res.msg);
}
Expand Down Expand Up @@ -2414,7 +2414,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
mnview.GetHistoryWriters().GetBurnView() = nullptr;
for (size_t i = 0; i < block.vtx.size(); ++i) {
uint64_t gasUsed{};
const auto res = ApplyCustomTx(mnview, view, *block.vtx[i], chainparams.GetConsensus(), pindex->nHeight, gasUsed, pindex->GetBlockTime(), nullptr, i, 0, false);
const auto res = ApplyCustomTx(mnview, view, *block.vtx[i], chainparams.GetConsensus(), pindex->nHeight, gasUsed, pindex->GetBlockTime(), nullptr, i, 0, false, false);
if (!res.ok) {
return error("%s: Genesis block ApplyCustomTx failed. TX: %s Error: %s",
__func__, block.vtx[i]->GetHash().ToString(), res.msg);
Expand Down Expand Up @@ -2698,7 +2698,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl

const auto applyCustomTxTime = GetTimeMicros();
uint64_t gasUsed{};
const auto res = ApplyCustomTx(accountsView, view, tx, consensus, pindex->nHeight, gasUsed, pindex->GetBlockTime(), nullptr, i, evmQueueId, isEvmEnabledForBlock);
const auto res = ApplyCustomTx(accountsView, view, tx, consensus, pindex->nHeight, gasUsed, pindex->GetBlockTime(), nullptr, i, evmQueueId, isEvmEnabledForBlock, false);

LogApplyCustomTx(tx, applyCustomTxTime);
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
Expand Down

0 comments on commit 80716c2

Please sign in to comment.