Skip to content

Commit 21f10f7

Browse files
committed
Lookup also bare txid in rpcrawtransaction.
rpcrawtransaction.cpp contains two places that use heuristic transaction lookups by txid: One in the spent index to see if there are transations spending its outputs, and one in sendrawtransaction to check the UTXO set as a cheap heuristic on whether or not the transaction is already contained in the blockchain. This extends both places to try looking up both the txid and also the bare txid. At most one of them can ever match (unless there is a collision in SHA-256), so it doesn't hurt, but it will ensure that the code works independent of the state of segwit light.
1 parent cecaced commit 21f10f7

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

divi/src/rpcrawtransaction.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,15 @@ void TxToJSONExpanded(const CTransaction& tx, const uint256 hashBlock, Object& e
120120
ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
121121
out.push_back(Pair("scriptPubKey", o));
122122

123-
// Add spent information if spentindex is enabled
123+
// Add spent information if spentindex is enabled. We don't know
124+
// whether or not segwit light is in effect for the transaction,
125+
// so we simply try looking up by both txid and bare txid as at
126+
// most one of them can match anyway.
124127
CSpentIndexValue spentInfo;
125-
CSpentIndexKey spentKey(txid, i);
126-
if (GetSpentIndex(spentKey, spentInfo)) {
128+
bool found = GetSpentIndex(CSpentIndexKey(txid, i), spentInfo);
129+
if (!found)
130+
found = GetSpentIndex(CSpentIndexKey(tx.GetBareTxid(), i), spentInfo);
131+
if (found) {
127132
out.push_back(Pair("spentTxId", spentInfo.txid.GetHex()));
128133
out.push_back(Pair("spentIndex", (int)spentInfo.inputIndex));
129134
out.push_back(Pair("spentHeight", spentInfo.blockHeight));
@@ -878,9 +883,19 @@ Value sendrawtransaction(const Array& params, bool fHelp)
878883
fOverrideFees = params[1].get_bool();
879884

880885
CCoinsViewCache& view = *pcoinsTip;
886+
const bool fHaveMempool = mempool.exists(hashTx);
887+
888+
/* We use the UTXO set as heuristic about whether or not a transaction
889+
has already been confirmed. This is not 100% accurate (as all outputs
890+
could have been spent), but useful in practice. Since we don't know
891+
whether or not segwit light has been in effect for the transaction,
892+
we just try locating both txid and bare txid as at most one of them
893+
can match anyway. */
881894
const CCoins* existingCoins = view.AccessCoins(hashTx);
882-
bool fHaveMempool = mempool.exists(hashTx);
883-
bool fHaveChain = existingCoins && existingCoins->nHeight < 1000000000;
895+
if (existingCoins == nullptr)
896+
existingCoins = view.AccessCoins(tx.GetBareTxid());
897+
const bool fHaveChain = existingCoins && existingCoins->nHeight < 1000000000;
898+
884899
if (!fHaveMempool && !fHaveChain) {
885900
// push to local node and sync with wallets
886901
std::pair<CAmount,bool> feeTotalsAndStatus = ComputeFeeTotalsAndIfInputsAreKnown(tx);

0 commit comments

Comments
 (0)