Skip to content

Commit

Permalink
remove coordainte mempool entry storage and use ancestor to validator…
Browse files Browse the repository at this point in the history
… assets
  • Loading branch information
balajimara committed Nov 11, 2024
1 parent 5734cae commit 678e7fb
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
9 changes: 9 additions & 0 deletions src/coordinate/coordinate_mempool_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
#include <primitives/transaction.h>
#include <validation.h>

struct CoordinateCompareTxIterByAncestorCount {
bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
{
if (a->GetCountWithAncestors() != b->GetCountWithAncestors()) {
return a->GetCountWithAncestors() < b->GetCountWithAncestors();
}
return CompareIteratorByHash()(a, b);
}
};

/**
* Get asset total amount
Expand Down
1 change: 0 additions & 1 deletion src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,6 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
}

LogPrintf("CheckProofOfWork \n");
// Check the header
if (!CheckProofOfWork(block, GetConsensus())) {
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
Expand Down
6 changes: 0 additions & 6 deletions src/policy/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,11 @@ bool AreCoordinateTransactionStandard(const CTransaction& tx, CCoinsViewCache& m
Coin coin;
CAmount coinValue = 0;

LogPrintf("input hash %s \n",tx.vin[i].prevout.hash.ToString());
LogPrintf("input hash index %i \n",tx.vin[i].prevout.n);

// check input is unspent
bool is_asset = mapInputs.getAssetCoin(tx.vin[i].prevout,fBitAsset,fBitAssetControl,nAssetID, &coin);
if(!is_asset) {
LogPrintf("Invalid inputs \n");
return false;
} else {
LogPrintf("input nAssetID %i \n",nAssetID);
LogPrintf("input value %i \n",coin.out.nValue);
coinValue = coin.out.nValue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ static RPCHelpMan getmempoolancestors()
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
}

auto ancestors{mempool.AssumeCalculateMemPoolAncestors(__func__, *it, CTxMemPool::Limits::NoLimits(), /*fSearchForParents=*/false)};
auto ancestors{mempool.AssumeCalculateMemPoolAncestors(__func__, *it, CTxMemPool::Limits::NoLimits(), /*fSearchForParents=*/true)};

if (!fVerbose) {
UniValue o(UniValue::VARR);
Expand Down
7 changes: 0 additions & 7 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,6 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
CAmount amountAssetIn = CAmount(0);
CAmount preconfRefund = CAmount(0);
int nControlN = -1;
LogPrintf("Mempool addcoins \n");
uint32_t nAssetIDOut = 0;
for (size_t x = 0; x < tx.vin.size(); x++) {
bool fBitAsset = false;
Expand All @@ -788,8 +787,6 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
uint32_t nAssetID = 0;
Coin coin;
mempoolDuplicate.SpendCoin(tx.vin[x].prevout, fBitAsset, fBitAssetControl, isPreconf, nAssetID, &coin);
LogPrintf("fBitAsset %i \n", fBitAsset);
LogPrintf("fBitAssetControl %i \n", fBitAssetControl);
if (nAssetID)
nAssetIDOut = nAssetID;

Expand All @@ -798,10 +795,6 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
if (fBitAssetControl)
nControlN = x;
}

LogPrintf("nControlN %i \n", nControlN);
LogPrintf("nAssetID %i \n", nAssetIDOut);
LogPrintf("asset in %i \n", amountAssetIn);
AddCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max(), preconfRefund, nAssetIDOut, amountAssetIn, nControlN, 0, true);
}
for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) {
Expand Down
1 change: 1 addition & 0 deletions src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ class CTxMemPool
indexed_transaction_set mapTx GUARDED_BY(cs);

using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;

std::vector<std::pair<uint256, txiter>> vTxHashes GUARDED_BY(cs); //!< All tx witness hashes/entries in mapTx, in random order

typedef std::set<txiter, CompareIteratorByHash> setEntries;
Expand Down
46 changes: 27 additions & 19 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,12 +1034,20 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)

ws.m_ancestors = *ancestors;


CCoinsViewCache dummyMempool(&m_active_chainstate.CoinsTip());
m_active_chainstate.UpdatedCoinsTip(dummyMempool,m_active_chainstate.m_chain.Height() + 1);
m_active_chainstate.UpdatedCoinsTip(dummyMempool,m_active_chainstate.m_chain.Height());
auto ancestoers{m_pool.CalculateMemPoolAncestors(*entry, CTxMemPool::Limits::NoLimits())};

std::vector<CTxMemPool::txiter> finalAncestors;
for (CTxMemPool::txiter ancestorIt : *ancestoers) {
finalAncestors.push_back(ancestorIt);
}

for (CTxMemPool::txiter ancestorIt : *ancestors) {
std::sort(finalAncestors.begin(), finalAncestors.end(), CoordinateCompareTxIterByAncestorCount());
for (CTxMemPool::txiter ancestorIt : finalAncestors) {
const CTransaction& tx = ancestorIt->GetTx();
CAmount amountAssetIn = CAmount(0);
CAmount amountAssetIn = CAmount(0);
CAmount preconfRefund = CAmount(0);
int nControlN = -1;
uint32_t nAssetIDOut = 0;
Expand All @@ -1060,24 +1068,26 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
}
AddCoins(dummyMempool, tx, std::numeric_limits<int>::max(), preconfRefund, nAssetIDOut, amountAssetIn, nControlN, 0, true);
}

if(!AreCoordinateTransactionStandard(tx, dummyMempool)) {
LogPrintf("Invalid transaction standard \n");
return false; // state filled in by CheckTxInputs
LogPrintf("Invalid transaction standard \n");
return false; // state filled in by CheckTxInputs
}

// A transaction that spends outputs that would be replaced by it is invalid. Now
// that we have the set of all ancestors we can detect this
// pathological case by making sure ws.m_conflicts and ws.m_ancestors don't
// intersect.
if (const auto err_string{EntriesAndTxidsDisjoint(ws.m_ancestors, ws.m_conflicts, hash)}) {
// We classify this as a consensus error because a transaction depending on something it
// conflicts with would be inconsistent.
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-spends-conflicting-tx", *err_string);
}

m_rbf = !ws.m_conflicts.empty();
return true;


// A transaction that spends outputs that would be replaced by it is invalid. Now
// that we have the set of all ancestors we can detect this
// pathological case by making sure ws.m_conflicts and ws.m_ancestors don't
// intersect.
if (const auto err_string{EntriesAndTxidsDisjoint(ws.m_ancestors, ws.m_conflicts, hash)}) {
// We classify this as a consensus error because a transaction depending on something it
// conflicts with would be inconsistent.
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-spends-conflicting-tx", *err_string);
}

m_rbf = !ws.m_conflicts.empty();
return true;
}

bool MemPoolAccept::ReplacementChecks(Workspace& ws)
Expand Down Expand Up @@ -4184,7 +4194,6 @@ void ChainstateManager::ReceivedBlockTransactions(const CBlock& block, CBlockInd

static bool CheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
{
LogPrintf("CheckBlockHeader \n");
// Check proof of work matches claimed amount
if (fCheckPOW && !CheckProofOfWork(block, consensusParams))
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "high-hash", "proof of work failed");
Expand All @@ -4199,7 +4208,6 @@ bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensu
if (block.fChecked)
return true;

LogPrintf("CheckBlock \n");
// Check that the header is valid (particularly PoW). This is mostly
// redundant with the call in AcceptBlockHeader.
if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
Expand Down

0 comments on commit 678e7fb

Please sign in to comment.