aboutsummaryrefslogtreecommitdiff
path: root/src/rpc
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2017-02-10 11:04:13 -0500
committerJohn Newbery <john@johnnewbery.com>2017-06-07 17:38:21 -0400
commit6294f3283a5b6919795621dc067ec80c0cd2a334 (patch)
tree794537d75a4a1fde5e0626ec098045dd64be57fe /src/rpc
parent300f8e7a828f3ffaa3a3e9d979a6dc90f15d0203 (diff)
downloadbitcoin-6294f3283a5b6919795621dc067ec80c0cd2a334.tar.xz
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()
Diffstat (limited to 'src/rpc')
-rw-r--r--src/rpc/rawtransaction.cpp12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp
index e27c2a77c7..fe56519e74 100644
--- a/src/rpc/rawtransaction.cpp
+++ b/src/rpc/rawtransaction.cpp
@@ -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);