Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <chain.h>
7 : : #include <chainparams.h>
8 : : #include <coins.h>
9 : : #include <index/txindex.h>
10 : : #include <merkleblock.h>
11 : : #include <node/blockstorage.h>
12 : : #include <primitives/transaction.h>
13 : : #include <rpc/server.h>
14 : : #include <rpc/server_util.h>
15 : : #include <rpc/util.h>
16 : : #include <univalue.h>
17 : : #include <util/strencodings.h>
18 : : #include <validation.h>
19 : :
20 : : using node::GetTransaction;
21 : :
22 : 2440 : static RPCHelpMan gettxoutproof()
23 : : {
24 : 2440 : return RPCHelpMan{"gettxoutproof",
25 : : "\nReturns a hex-encoded proof that \"txid\" was included in a block.\n"
26 : : "\nNOTE: By default this function only works sometimes. This is when there is an\n"
27 : : "unspent output in the utxo for this transaction. To make it always work,\n"
28 : : "you need to maintain a transaction index, using the -txindex command line option or\n"
29 : : "specify the block in which the transaction is included manually (by blockhash).\n",
30 : : {
31 [ + - ]: 2440 : {"txids", RPCArg::Type::ARR, RPCArg::Optional::NO, "The txids to filter",
32 : : {
33 [ + - ]: 2440 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "A transaction hash"},
34 : : },
35 : : },
36 [ + - ]: 2440 : {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "If specified, looks for txid in the block with this hash"},
37 : : },
38 [ + - ]: 4880 : RPCResult{
39 : : RPCResult::Type::STR, "data", "A string that is a serialized, hex-encoded data for the proof."
40 [ + - + - : 2440 : },
+ - ]
41 [ + - + - ]: 7320 : RPCExamples{""},
42 : 25 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
43 : : {
44 [ + - ]: 25 : std::set<Txid> setTxids;
45 [ + - + - : 25 : UniValue txids = request.params[0].get_array();
+ - ]
46 [ + + ]: 25 : if (txids.empty()) {
47 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter 'txids' cannot be empty");
48 : : }
49 [ + + ]: 52 : for (unsigned int idx = 0; idx < txids.size(); idx++) {
50 [ + - + + : 31 : auto ret{setTxids.insert(Txid::FromUint256(ParseHashV(txids[idx], "txid")))};
+ - ]
51 [ + + ]: 29 : if (!ret.second) {
52 [ + - + - : 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ") + txids[idx].get_str());
+ - + - +
- ]
53 : : }
54 : : }
55 : :
56 : 21 : const CBlockIndex* pblockindex = nullptr;
57 : 21 : uint256 hashBlock;
58 [ + - ]: 21 : ChainstateManager& chainman = EnsureAnyChainman(request.context);
59 [ + - + + ]: 21 : if (!request.params[1].isNull()) {
60 [ + - ]: 5 : LOCK(cs_main);
61 [ + - + + ]: 5 : hashBlock = ParseHashV(request.params[1], "blockhash");
62 [ + - ]: 3 : pblockindex = chainman.m_blockman.LookupBlockIndex(hashBlock);
63 [ + + ]: 3 : if (!pblockindex) {
64 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
65 : : }
66 : 5 : } else {
67 [ + - ]: 16 : LOCK(cs_main);
68 [ + - ]: 16 : Chainstate& active_chainstate = chainman.ActiveChainstate();
69 : :
70 : : // Loop through txids and try to find which block they're in. Exit loop once a block is found.
71 [ + + ]: 19 : for (const auto& tx : setTxids) {
72 [ + - + - ]: 16 : const Coin& coin{AccessByTxid(active_chainstate.CoinsTip(), tx)};
73 [ + + ]: 16 : if (!coin.IsSpent()) {
74 [ + - + - ]: 29 : pblockindex = active_chainstate.m_chain[coin.nHeight];
75 : : break;
76 : : }
77 : : }
78 : 16 : }
79 : :
80 : :
81 : : // Allow txindex to catch up if we need to query it and before we acquire cs_main.
82 [ + + + + ]: 18 : if (g_txindex && !pblockindex) {
83 [ + - ]: 1 : g_txindex->BlockUntilSyncedToCurrentChain();
84 : : }
85 : :
86 [ + + ]: 18 : if (pblockindex == nullptr) {
87 [ + - ]: 3 : const CTransactionRef tx = GetTransaction(/*block_index=*/nullptr, /*mempool=*/nullptr, *setTxids.begin(), hashBlock, chainman.m_blockman);
88 [ + + + - ]: 3 : if (!tx || hashBlock.IsNull()) {
89 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
90 : : }
91 : :
92 [ + - ]: 1 : LOCK(cs_main);
93 [ + - ]: 1 : pblockindex = chainman.m_blockman.LookupBlockIndex(hashBlock);
94 [ - + ]: 1 : if (!pblockindex) {
95 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Transaction index corrupt");
96 : : }
97 [ + - ]: 4 : }
98 : :
99 : 16 : CBlock block;
100 [ + - - + ]: 16 : if (!chainman.m_blockman.ReadBlockFromDisk(block, *pblockindex)) {
101 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
102 : : }
103 : :
104 : 16 : unsigned int ntxFound = 0;
105 [ + + ]: 57 : for (const auto& tx : block.vtx) {
106 [ + + ]: 41 : if (setTxids.count(tx->GetHash())) {
107 : 21 : ntxFound++;
108 : : }
109 : : }
110 [ + + ]: 16 : if (ntxFound != setTxids.size()) {
111 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Not all transactions found in specified or retrieved block");
112 : : }
113 : :
114 [ + - ]: 15 : DataStream ssMB{};
115 [ + - ]: 15 : CMerkleBlock mb(block, setTxids);
116 [ + - ]: 15 : ssMB << mb;
117 [ + - ]: 15 : std::string strHex = HexStr(ssMB);
118 [ + - ]: 15 : return strHex;
119 : 25 : },
120 [ + - + - : 31720 : };
+ - + - +
- + - + -
+ + - - ]
121 [ + - + - : 17080 : }
+ - + - +
- + - + -
+ - + + -
- - - ]
122 : :
123 : 2428 : static RPCHelpMan verifytxoutproof()
124 : : {
125 : 2428 : return RPCHelpMan{"verifytxoutproof",
126 : : "\nVerifies that a proof points to a transaction in a block, returning the transaction it commits to\n"
127 : : "and throwing an RPC error if the block is not in our best chain\n",
128 : : {
129 [ + - ]: 2428 : {"proof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex-encoded proof generated by gettxoutproof"},
130 : : },
131 [ + - + - : 9712 : RPCResult{
+ + - - ]
132 : : RPCResult::Type::ARR, "", "",
133 : : {
134 : : {RPCResult::Type::STR_HEX, "txid", "The txid(s) which the proof commits to, or empty array if the proof cannot be validated."},
135 : : }
136 [ + - + - : 4856 : },
+ - + - +
- ]
137 [ + - + - ]: 7284 : RPCExamples{""},
138 : 13 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
139 : : {
140 [ + - ]: 26 : DataStream ssMB{ParseHexV(request.params[0], "proof")};
141 [ + - ]: 13 : CMerkleBlock merkleBlock;
142 [ + - ]: 13 : ssMB >> merkleBlock;
143 : :
144 : 13 : UniValue res(UniValue::VARR);
145 : :
146 : 13 : std::vector<uint256> vMatch;
147 : 13 : std::vector<unsigned int> vIndex;
148 [ + - + - ]: 13 : if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot)
149 : : return res;
150 : :
151 [ + - ]: 13 : ChainstateManager& chainman = EnsureAnyChainman(request.context);
152 [ + - ]: 13 : LOCK(cs_main);
153 : :
154 [ + - + - ]: 13 : const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(merkleBlock.header.GetHash());
155 [ + - + - : 26 : if (!pindex || !chainman.ActiveChain().Contains(pindex) || pindex->nTx == 0) {
- + ]
156 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
157 : : }
158 : :
159 : : // Check if proof is valid, only add results if so
160 [ + + ]: 13 : if (pindex->nTx == merkleBlock.txn.GetNumTransactions()) {
161 [ + + ]: 29 : for (const uint256& hash : vMatch) {
162 [ + - + - : 18 : res.push_back(hash.GetHex());
+ - ]
163 : : }
164 : : }
165 : :
166 [ + - ]: 13 : return res;
167 : 13 : },
168 [ + - + - : 21852 : };
+ - + - +
- + + -
- ]
169 [ + - + - : 12140 : }
+ - + - +
- ]
170 : :
171 : 1325 : void RegisterTxoutProofRPCCommands(CRPCTable& t)
172 : : {
173 : 1325 : static const CRPCCommand commands[]{
174 : : {"blockchain", &gettxoutproof},
175 : : {"blockchain", &verifytxoutproof},
176 [ + + + - : 1325 : };
+ - + - +
- + - -
- ]
177 [ + + ]: 3975 : for (const auto& c : commands) {
178 : 2650 : t.appendCommand(c.name, &c);
179 : : }
180 : 1325 : }
|