Skip to content
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
36 changes: 36 additions & 0 deletions src/digidollar/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using DigiDollar::GetScriptMetadata;
#include <consensus/dca.h>
#include <consensus/err.h>
#include <index/txindex.h> // For decentralized DD amount lookup via g_txindex
#include <txmempool.h> // For mempool DD amount lookup (Bug #35)
#include <script/standard.h>
#include <script/solver.h>
#include <script/interpreter.h>
Expand Down Expand Up @@ -329,6 +330,30 @@ static bool ExtractDDAmountFromTxRef(const CTransactionRef& prev_tx, const COutP
return false;
}

/**
* Extract DD amount from a transaction in the mempool (Bug #35).
* Mempool txs have already passed full validation, so their OP_RETURN is trustworthy.
* This enables chaining DD transfers without waiting for block confirmation.
*/
bool ExtractDDAmountFromMempool(const COutPoint& prevout, const CTxMemPool* mempool, CAmount& amount) {
amount = 0;
if (!mempool) return false;

CTransactionRef prev_tx = mempool->get(prevout.hash);
if (!prev_tx) {
LogPrint(BCLog::DIGIDOLLAR, "DigiDollar: ExtractDDAmountFromMempool - tx %s not in mempool\n",
prevout.hash.ToString());
return false;
}

bool ok = ExtractDDAmountFromTxRef(prev_tx, prevout, amount);
if (ok) {
LogPrint(BCLog::DIGIDOLLAR, "DigiDollar: ExtractDDAmountFromMempool - tx %s vout %d = %lld cents\n",
prevout.hash.ToString(), prevout.n, (long long)amount);
}
return ok;
}

bool ExtractDDAmountFromPrevTx(const COutPoint& prevout, CAmount& amount) {
amount = 0;

Expand Down Expand Up @@ -1255,6 +1280,13 @@ bool ValidateTransferTransaction(const CTransaction& tx,
CAmount ddAmt = 0;
bool found = false;

// 0. Try mempool lookup (for spending unconfirmed DD outputs — Bug #35)
if (!found && ctx.mempool) {
if (ExtractDDAmountFromMempool(txin.prevout, ctx.mempool, ddAmt) && ddAmt > 0) {
found = true;
}
}

// 1. Try txindex (authoritative — reads creating tx's OP_RETURN)
if (!found && ExtractDDAmountFromPrevTx(txin.prevout, ddAmt) && ddAmt > 0) {
found = true;
Expand Down Expand Up @@ -1389,6 +1421,10 @@ bool ValidateRedemptionTransaction(const CTransaction& tx,
totalDDInputs += ddAmount;
LogPrint(BCLog::DIGIDOLLAR, "DigiDollar: DD input %d - amount: %lld cents (from registry)\n",
i, (long long)ddAmount);
} else if (ctx.mempool && ExtractDDAmountFromMempool(input.prevout, ctx.mempool, ddAmount) && ddAmount > 0) {
totalDDInputs += ddAmount;
LogPrint(BCLog::DIGIDOLLAR, "DigiDollar: DD input %d - amount: %lld cents (from mempool)\n",
i, (long long)ddAmount);
} else if (ExtractDDAmountFromPrevTx(input.prevout, ddAmount) && ddAmount > 0) {
totalDDInputs += ddAmount;
LogPrint(BCLog::DIGIDOLLAR, "DigiDollar: DD input %d - amount: %lld cents (from txindex)\n",
Expand Down
7 changes: 5 additions & 2 deletions src/digidollar/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <chainparams.h>
#include <coins.h>

class CTxMemPool;

#include <cstdint>
#include <functional>
#include <vector>
Expand Down Expand Up @@ -68,13 +70,14 @@ struct ValidationContext {
const CCoinsViewCache* coins; // Coins view for UTXO lookups (nullptr if not available)
bool skipOracleValidation; // Skip oracle-dependent validation (for historical blocks)
TxLookupFn txLookup; // Look up tx from block database (for DD amount extraction)
const CTxMemPool* mempool; // Mempool for resolving DD amounts from unconfirmed parent txs

ValidationContext(int height, CAmount price_micro_usd, int collateral, const CChainParams& chainParams,
const CCoinsViewCache* coins_view = nullptr, bool skip_oracle = false,
TxLookupFn tx_lookup = nullptr)
TxLookupFn tx_lookup = nullptr, const CTxMemPool* pool = nullptr)
: nHeight(height), oraclePriceMicroUSD(price_micro_usd), systemCollateral(collateral),
params(chainParams), coins(coins_view), skipOracleValidation(skip_oracle),
txLookup(std::move(tx_lookup)) {}
txLookup(std::move(tx_lookup)), mempool(pool) {}
};

// ============================================================================
Expand Down
3 changes: 2 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,8 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
args.m_chainparams, // Chain parameters
&m_active_chainstate.CoinsTip(), // Coins view for UTXO lookup
false, // Don't skip oracle validation in mempool
txLookup // Block-db tx lookup for DD amounts
txLookup, // Block-db tx lookup for DD amounts
&m_pool // Mempool for unconfirmed DD input lookup
);

if (!DigiDollar::ValidateDigiDollarTransaction(tx, ddContext, state)) {
Expand Down
Loading