From 6294f3283a5b6919795621dc067ec80c0cd2a334 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Fri, 10 Feb 2017 11:04:13 -0500 Subject: 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() --- src/rpc/rawtransaction.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/rpc') 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); -- cgit v1.2.3