Skip to content

Commit 326f244

Browse files
committed
refactor: Convert RPCs and merkleblock from uint256 to Txid
1 parent 49b3d3a commit 326f244

File tree

12 files changed

+78
-80
lines changed

12 files changed

+78
-80
lines changed

src/merkleblock.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std:
3232
header = block.GetBlockHeader();
3333

3434
std::vector<bool> vMatch;
35-
std::vector<uint256> vHashes;
35+
std::vector<Txid> vHashes;
3636

3737
vMatch.reserve(block.vtx.size());
3838
vHashes.reserve(block.vtx.size());
@@ -55,13 +55,13 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std:
5555
}
5656

5757
// NOLINTNEXTLINE(misc-no-recursion)
58-
uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) {
58+
uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<Txid> &vTxid) {
5959
//we can never have zero txs in a merkle block, we always need the coinbase tx
6060
//if we do not have this assert, we can hit a memory access violation when indexing into vTxid
6161
assert(vTxid.size() != 0);
6262
if (height == 0) {
6363
// hash at height 0 is the txids themselves
64-
return vTxid[pos];
64+
return vTxid[pos].ToUint256();
6565
} else {
6666
// calculate left hash
6767
uint256 left = CalcHash(height-1, pos*2, vTxid), right;
@@ -76,7 +76,7 @@ uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::ve
7676
}
7777

7878
// NOLINTNEXTLINE(misc-no-recursion)
79-
void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) {
79+
void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<Txid> &vTxid, const std::vector<bool> &vMatch) {
8080
// determine whether this node is the parent of at least one matched txid
8181
bool fParentOfMatch = false;
8282
for (unsigned int p = pos << height; p < (pos+1) << height && p < nTransactions; p++)
@@ -95,7 +95,7 @@ void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const st
9595
}
9696

9797
// NOLINTNEXTLINE(misc-no-recursion)
98-
uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
98+
uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<Txid> &vMatch, std::vector<unsigned int> &vnIndex) {
9999
if (nBitsUsed >= vBits.size()) {
100100
// overflowed the bits array - failure
101101
fBad = true;
@@ -111,7 +111,7 @@ uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, uns
111111
}
112112
const uint256 &hash = vHash[nHashUsed++];
113113
if (height==0 && fParentOfMatch) { // in case of height 0, we have a matched txid
114-
vMatch.push_back(hash);
114+
vMatch.push_back(Txid::FromUint256(hash));
115115
vnIndex.push_back(pos);
116116
}
117117
return hash;
@@ -133,7 +133,7 @@ uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, uns
133133
}
134134
}
135135

136-
CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
136+
CPartialMerkleTree::CPartialMerkleTree(const std::vector<Txid> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
137137
// reset state
138138
vBits.clear();
139139
vHash.clear();
@@ -149,7 +149,7 @@ CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const
149149

150150
CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
151151

152-
uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
152+
uint256 CPartialMerkleTree::ExtractMatches(std::vector<Txid> &vMatch, std::vector<unsigned int> &vnIndex) {
153153
vMatch.clear();
154154
// An empty set will not work
155155
if (nTransactions == 0)

src/merkleblock.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <primitives/block.h>
1111
#include <serialize.h>
1212
#include <uint256.h>
13+
#include <util/transaction_identifier.h>
1314

1415
#include <set>
1516
#include <vector>
@@ -73,16 +74,16 @@ class CPartialMerkleTree
7374
}
7475

7576
/** calculate the hash of a node in the merkle tree (at leaf level: the txid's themselves) */
76-
uint256 CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid);
77+
uint256 CalcHash(int height, unsigned int pos, const std::vector<Txid> &vTxid);
7778

7879
/** recursive function that traverses tree nodes, storing the data as bits and hashes */
79-
void TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
80+
void TraverseAndBuild(int height, unsigned int pos, const std::vector<Txid> &vTxid, const std::vector<bool> &vMatch);
8081

8182
/**
8283
* recursive function that traverses tree nodes, consuming the bits and hashes produced by TraverseAndBuild.
8384
* it returns the hash of the respective node and its respective index.
8485
*/
85-
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex);
86+
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<Txid> &vMatch, std::vector<unsigned int> &vnIndex);
8687

8788
public:
8889

@@ -97,7 +98,7 @@ class CPartialMerkleTree
9798
}
9899

99100
/** Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them */
100-
CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
101+
CPartialMerkleTree(const std::vector<Txid> &vTxid, const std::vector<bool> &vMatch);
101102

102103
CPartialMerkleTree();
103104

@@ -106,7 +107,7 @@ class CPartialMerkleTree
106107
* and their respective indices within the partial tree.
107108
* returns the merkle root, or 0 in case of failure
108109
*/
109-
uint256 ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex);
110+
uint256 ExtractMatches(std::vector<Txid> &vMatch, std::vector<unsigned int> &vnIndex);
110111

111112
/** Get number of transactions the merkle proof is indicating for cross-reference with
112113
* local blockchain knowledge.
@@ -135,7 +136,7 @@ class CMerkleBlock
135136
* Used only when a bloom filter is specified to allow
136137
* testing the transactions which matched the bloom filter.
137138
*/
138-
std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
139+
std::vector<std::pair<unsigned int, Txid> > vMatchedTxn;
139140

140141
/**
141142
* Create from a CBlock, filtering transactions according to filter

src/net_processing.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,9 +2352,8 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
23522352
// they must either disconnect and retry or request the full block.
23532353
// Thus, the protocol spec specified allows for us to provide duplicate txn here,
23542354
// however we MUST always provide at least what the remote peer needs
2355-
typedef std::pair<unsigned int, uint256> PairType;
2356-
for (PairType& pair : merkleBlock.vMatchedTxn)
2357-
MakeAndPushMessage(pfrom, NetMsgType::TX, TX_NO_WITNESS(*pblock->vtx[pair.first]));
2355+
for (const auto& [tx_idx, _] : merkleBlock.vMatchedTxn)
2356+
MakeAndPushMessage(pfrom, NetMsgType::TX, TX_NO_WITNESS(*pblock->vtx[tx_idx]));
23582357
}
23592358
// else
23602359
// no response

src/rpc/mempool.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,12 @@ static RPCHelpMan getmempoolancestors()
461461
if (!request.params[1].isNull())
462462
fVerbose = request.params[1].get_bool();
463463

464-
uint256 hash = ParseHashV(request.params[0], "parameter 1");
464+
auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
465465

466466
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
467467
LOCK(mempool.cs);
468468

469-
const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
469+
const auto entry{mempool.GetEntry(txid)};
470470
if (entry == nullptr) {
471471
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
472472
}
@@ -483,10 +483,9 @@ static RPCHelpMan getmempoolancestors()
483483
UniValue o(UniValue::VOBJ);
484484
for (CTxMemPool::txiter ancestorIt : ancestors) {
485485
const CTxMemPoolEntry &e = *ancestorIt;
486-
const uint256& _hash = e.GetTx().GetHash();
487486
UniValue info(UniValue::VOBJ);
488487
entryToJSON(mempool, info, e);
489-
o.pushKV(_hash.ToString(), std::move(info));
488+
o.pushKV(e.GetTx().GetHash().ToString(), std::move(info));
490489
}
491490
return o;
492491
}
@@ -523,7 +522,7 @@ static RPCHelpMan getmempooldescendants()
523522
if (!request.params[1].isNull())
524523
fVerbose = request.params[1].get_bool();
525524

526-
Txid txid{Txid::FromUint256(ParseHashV(request.params[0], "parameter 1"))};
525+
auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
527526

528527
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
529528
LOCK(mempool.cs);
@@ -549,10 +548,9 @@ static RPCHelpMan getmempooldescendants()
549548
UniValue o(UniValue::VOBJ);
550549
for (CTxMemPool::txiter descendantIt : setDescendants) {
551550
const CTxMemPoolEntry &e = *descendantIt;
552-
const uint256& _hash = e.GetTx().GetHash();
553551
UniValue info(UniValue::VOBJ);
554552
entryToJSON(mempool, info, e);
555-
o.pushKV(_hash.ToString(), std::move(info));
553+
o.pushKV(e.GetTx().GetHash().ToString(), std::move(info));
556554
}
557555
return o;
558556
}
@@ -576,12 +574,12 @@ static RPCHelpMan getmempoolentry()
576574
},
577575
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
578576
{
579-
uint256 hash = ParseHashV(request.params[0], "parameter 1");
577+
auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
580578

581579
const CTxMemPool& mempool = EnsureAnyMemPool(request.context);
582580
LOCK(mempool.cs);
583581

584-
const auto entry{mempool.GetEntry(Txid::FromUint256(hash))};
582+
const auto entry{mempool.GetEntry(txid)};
585583
if (entry == nullptr) {
586584
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not in mempool");
587585
}
@@ -1080,7 +1078,7 @@ static RPCHelpMan submitpackage()
10801078
UniValue rpc_result{UniValue::VOBJ};
10811079
rpc_result.pushKV("package_msg", package_msg);
10821080
UniValue tx_result_map{UniValue::VOBJ};
1083-
std::set<uint256> replaced_txids;
1081+
std::set<Txid> replaced_txids;
10841082
for (const auto& tx : txns) {
10851083
UniValue result_inner{UniValue::VOBJ};
10861084
result_inner.pushKV("txid", tx->GetHash().GetHex());
@@ -1124,7 +1122,7 @@ static RPCHelpMan submitpackage()
11241122
}
11251123
rpc_result.pushKV("tx-results", std::move(tx_result_map));
11261124
UniValue replaced_list(UniValue::VARR);
1127-
for (const uint256& hash : replaced_txids) replaced_list.push_back(hash.ToString());
1125+
for (const auto& txid : replaced_txids) replaced_list.push_back(txid.ToString());
11281126
rpc_result.pushKV("replaced-transactions", std::move(replaced_list));
11291127
return rpc_result;
11301128
},

src/rpc/mining.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ static RPCHelpMan generateblock()
353353
const auto& str{raw_txs_or_txids[i].get_str()};
354354

355355
CMutableTransaction mtx;
356-
if (auto hash{uint256::FromHex(str)}) {
357-
const auto tx{mempool.get(*hash)};
356+
if (auto txid{Txid::FromHex(str)}) {
357+
const auto tx{mempool.get(*txid)};
358358
if (!tx) {
359359
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Transaction %s not in mempool.", str));
360360
}
@@ -517,7 +517,7 @@ static RPCHelpMan prioritisetransaction()
517517
{
518518
LOCK(cs_main);
519519

520-
uint256 hash(ParseHashV(request.params[0], "txid"));
520+
auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
521521
const auto dummy{self.MaybeArg<double>("dummy")};
522522
CAmount nAmount = request.params[2].getInt<int64_t>();
523523

@@ -528,12 +528,12 @@ static RPCHelpMan prioritisetransaction()
528528
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
529529

530530
// Non-0 fee dust transactions are not allowed for entry, and modification not allowed afterwards
531-
const auto& tx = mempool.get(hash);
531+
const auto& tx = mempool.get(txid);
532532
if (mempool.m_opts.require_standard && tx && !GetDust(*tx, mempool.m_opts.dust_relay_feerate).empty()) {
533533
throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is not supported for transactions with dust outputs.");
534534
}
535535

536-
mempool.PrioritiseTransaction(hash, nAmount);
536+
mempool.PrioritiseTransaction(txid, nAmount);
537537
return true;
538538
},
539539
};
@@ -887,14 +887,14 @@ static RPCHelpMan getblocktemplate()
887887
UniValue aCaps(UniValue::VARR); aCaps.push_back("proposal");
888888

889889
UniValue transactions(UniValue::VARR);
890-
std::map<uint256, int64_t> setTxIndex;
890+
std::map<Txid, int64_t> setTxIndex;
891891
std::vector<CAmount> tx_fees{block_template->getTxFees()};
892892
std::vector<CAmount> tx_sigops{block_template->getTxSigops()};
893893

894894
int i = 0;
895895
for (const auto& it : block.vtx) {
896896
const CTransaction& tx = *it;
897-
uint256 txHash = tx.GetHash();
897+
Txid txHash = tx.GetHash();
898898
setTxIndex[txHash] = i++;
899899

900900
if (tx.IsCoinBase())

src/rpc/rawtransaction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ static RPCHelpMan getrawtransaction()
322322
const NodeContext& node = EnsureAnyNodeContext(request.context);
323323
ChainstateManager& chainman = EnsureChainman(node);
324324

325-
uint256 hash = ParseHashV(request.params[0], "parameter 1");
325+
auto txid{Txid::FromUint256(ParseHashV(request.params[0], "parameter 1"))};
326326
const CBlockIndex* blockindex = nullptr;
327327

328-
if (hash == chainman.GetParams().GenesisBlock().hashMerkleRoot) {
328+
if (txid.ToUint256() == chainman.GetParams().GenesisBlock().hashMerkleRoot) {
329329
// Special exception for the genesis block coinbase transaction
330330
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
331331
}
@@ -348,7 +348,7 @@ static RPCHelpMan getrawtransaction()
348348
}
349349

350350
uint256 hash_block;
351-
const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), hash, hash_block, chainman.m_blockman);
351+
const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), txid, hash_block, chainman.m_blockman);
352352
if (!tx) {
353353
std::string errmsg;
354354
if (blockindex) {

src/rpc/txoutproof.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static RPCHelpMan verifytxoutproof()
150150

151151
UniValue res(UniValue::VARR);
152152

153-
std::vector<uint256> vMatch;
153+
std::vector<Txid> vMatch;
154154
std::vector<unsigned int> vIndex;
155155
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot)
156156
return res;
@@ -165,8 +165,8 @@ static RPCHelpMan verifytxoutproof()
165165

166166
// Check if proof is valid, only add results if so
167167
if (pindex->nTx == merkleBlock.txn.GetNumTransactions()) {
168-
for (const uint256& hash : vMatch) {
169-
res.push_back(hash.GetHex());
168+
for (const auto& txid : vMatch) {
169+
res.push_back(txid.GetHex());
170170
}
171171
}
172172

0 commit comments

Comments
 (0)