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