mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
gettxoutproof() should return consistent result
We can call gettxoutproof() with a list of transactions. Currently, if the first transaction is unspent (and all other transactions are in the same block), then the call will succeed. If the first transaction has been spent, then the call will fail. The means that the following two calls will return different results: gettxoutproof(unspent_tx1, spent_tx1) gettxoutproof(spent_tx1, unspent_tx1) This commit makes behaviour independent of transaction ordering by looping through all transactions provided and trying to find which block they're in. This commit also increases the test coverage and tests more failure cases for gettxoutproof()
This commit is contained in:
@@ -219,9 +219,13 @@ UniValue gettxoutproof(const JSONRPCRequest& request)
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
|
||||
pblockindex = mapBlockIndex[hashBlock];
|
||||
} else {
|
||||
const Coin& coin = AccessByTxid(*pcoinsTip, oneTxid);
|
||||
if (!coin.IsSpent() && coin.nHeight > 0 && coin.nHeight <= chainActive.Height()) {
|
||||
pblockindex = chainActive[coin.nHeight];
|
||||
// Loop through txids and try to find which block they're in. Exit loop once a block is found.
|
||||
for (const auto& tx : setTxids) {
|
||||
const Coin& coin = AccessByTxid(*pcoinsTip, tx);
|
||||
if (!coin.IsSpent()) {
|
||||
pblockindex = chainActive[coin.nHeight];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,7 +248,7 @@ UniValue gettxoutproof(const JSONRPCRequest& request)
|
||||
if (setTxids.count(tx->GetHash()))
|
||||
ntxFound++;
|
||||
if (ntxFound != setTxids.size())
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "(Not all) transactions not found in specified block");
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Not all transactions found in specified or retrieved block");
|
||||
|
||||
CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
|
||||
CMerkleBlock mb(block, setTxids);
|
||||
|
||||
Reference in New Issue
Block a user