diff --git a/src/bench/disconnected_transactions.cpp b/src/bench/disconnected_transactions.cpp index d0724f1d5c..7c2c1ae958 100644 --- a/src/bench/disconnected_transactions.cpp +++ b/src/bench/disconnected_transactions.cpp @@ -28,7 +28,7 @@ struct ReorgTxns { static BlockTxns CreateRandomTransactions(size_t num_txns) { // Ensure every transaction has a different txid by having each one spend the previous one. - static uint256 prevout_hash{uint256::ZERO}; + static Txid prevout_hash{}; BlockTxns txns; txns.reserve(num_txns); diff --git a/src/bench/duplicate_inputs.cpp b/src/bench/duplicate_inputs.cpp index a1c8d4d942..b56054ae89 100644 --- a/src/bench/duplicate_inputs.cpp +++ b/src/bench/duplicate_inputs.cpp @@ -47,7 +47,7 @@ static void DuplicateInputs(benchmark::Bench& bench) uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100; for (uint64_t x = 0; x < (n_inputs - 1); ++x) { - naughtyTx.vin.emplace_back(GetRandHash(), 0, CScript(), 0); + naughtyTx.vin.emplace_back(Txid::FromUint256(GetRandHash()), 0, CScript(), 0); } naughtyTx.vin.emplace_back(naughtyTx.vin.back()); diff --git a/src/bench/mempool_stress.cpp b/src/bench/mempool_stress.cpp index e68cad38a8..cfecd4ae2e 100644 --- a/src/bench/mempool_stress.cpp +++ b/src/bench/mempool_stress.cpp @@ -58,7 +58,7 @@ static std::vector CreateOrderedCoins(FastRandomContext& det_ra for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){ size_t idx = det_rand.randrange(available_coins.size()); Available coin = available_coins[idx]; - uint256 hash = coin.ref->GetHash(); + Txid hash = coin.ref->GetHash(); // biased towards taking min_ancestors parents, but maybe more size_t n_to_take = det_rand.randrange(2) == 0 ? min_ancestors : diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index e2688cddc7..7f556c0d43 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -283,7 +283,7 @@ static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInpu } // append to transaction input list - CTxIn txin(txid, vout, CScript(), nSequenceIn); + CTxIn txin(Txid::FromUint256(txid), vout, CScript(), nSequenceIn); tx.vin.push_back(txin); } @@ -662,7 +662,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr) if (nOut < 0) throw std::runtime_error("vout cannot be negative"); - COutPoint out(txid, nOut); + COutPoint out(Txid::FromUint256(txid), nOut); std::vector pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey")); CScript scriptPubKey(pkData.begin(), pkData.end()); diff --git a/src/coins.cpp b/src/coins.cpp index a52b9a6286..fc5886b0f7 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -129,7 +129,7 @@ void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coi void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) { bool fCoinbase = tx.IsCoinBase(); - const uint256& txid = tx.GetHash(); + const Txid& txid = tx.GetHash(); for (size_t i = 0; i < tx.vout.size(); ++i) { bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase; // Coinbase transactions can always be overwritten, in order to correctly @@ -431,7 +431,7 @@ void CCoinsViewCache::SanityCheck() const static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut()); static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT; -const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid) +const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid) { COutPoint iter(txid, 0); while (iter.n < MAX_OUTPUTS_PER_BLOCK) { diff --git a/src/coins.h b/src/coins.h index 6cae3ae603..c77188baf7 100644 --- a/src/coins.h +++ b/src/coins.h @@ -380,7 +380,7 @@ void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, bool //! This function can be quite expensive because in the event of a transaction //! which is not found in the cache, it can cause up to MAX_OUTPUTS_PER_BLOCK //! lookups to database, so it should be used with care. -const Coin& AccessByTxid(const CCoinsViewCache& cache, const uint256& txid); +const Coin& AccessByTxid(const CCoinsViewCache& cache, const Txid& txid); /** * This is a minimally invasive approach to shutdown on LevelDB read errors from the diff --git a/src/common/bloom.cpp b/src/common/bloom.cpp index 5c3ad882a1..ca6af90b76 100644 --- a/src/common/bloom.cpp +++ b/src/common/bloom.cpp @@ -98,8 +98,8 @@ bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx) // for finding tx when they appear in a block if (vData.empty()) // zero-size = "match-all" filter return true; - const uint256& hash = tx.GetHash(); - if (contains(hash)) + const Txid& hash = tx.GetHash(); + if (contains(hash.ToUint256())) fFound = true; for (unsigned int i = 0; i < tx.vout.size(); i++) diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index befd932a83..dc8171297e 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -976,7 +976,7 @@ protected: // Calculate pegged Bitcoin asset std::vector commit = CommitToArguments(consensus, m_chain_type.chain_name); uint256 entropy; - GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash); + GenerateAssetEntropy(entropy, COutPoint(Txid::FromUint256(uint256(commit)), 0), parentGenesisBlockHash); consensus.total_valid_epochs = args.GetIntArg("-total_valid_epochs", 2); @@ -1009,7 +1009,7 @@ protected: std::vector commit = CommitToArguments(consensus, m_chain_type.chain_name); genesis = CreateGenesisBlock(consensus, CScript() << commit, CScript(OP_RETURN), 1296688602, 2, 0x207fffff, 1, 0); if (initialFreeCoins != 0 || initial_reissuance_tokens != 0) { - AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), parentGenesisBlockHash, (initialFreeCoins > 0) ? 1 : 0, initialFreeCoins, (initial_reissuance_tokens > 0) ? 1 : 0, initial_reissuance_tokens, CScript() << OP_TRUE); + AppendInitialIssuance(genesis, COutPoint(Txid::FromUint256(uint256(commit)), 0), parentGenesisBlockHash, (initialFreeCoins > 0) ? 1 : 0, initialFreeCoins, (initial_reissuance_tokens > 0) ? 1 : 0, initial_reissuance_tokens, CScript() << OP_TRUE); } } else if (consensus.genesis_style == "dynamic") { // Liquid v2 HF, from genesis. Upgrading networks still use "elements". @@ -1230,7 +1230,7 @@ public: // Calculate pegged Bitcoin asset std::vector commit = CommitToArguments(consensus, m_chain_type.chain_name); uint256 entropy; - GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash); + GenerateAssetEntropy(entropy, COutPoint(Txid::FromUint256(uint256(commit)), 0), parentGenesisBlockHash); // Elements serialization uses derivation, bitcoin serialization uses 0x00 if (g_con_elementsmode) { @@ -1596,7 +1596,7 @@ public: // Calculate pegged Bitcoin asset std::vector commit = CommitToArguments(consensus, m_chain_type.chain_name); uint256 entropy; - GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash); + GenerateAssetEntropy(entropy, COutPoint(Txid::FromUint256(uint256(commit)), 0), parentGenesisBlockHash); CalculateAsset(consensus.pegged_asset, entropy); if (args.IsArgSet("-con_parent_pegged_asset")) { @@ -1628,7 +1628,7 @@ public: std::vector commit = CommitToArguments(consensus, m_chain_type.chain_name); genesis = CreateGenesisBlock(consensus, CScript() << commit, CScript(OP_RETURN), 1296688602, 2, 0x207fffff, 1, 0); if (initialFreeCoins != 0 || initial_reissuance_tokens != 0) { - AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), parentGenesisBlockHash, (initialFreeCoins > 0) ? 1 : 0, initialFreeCoins, (initial_reissuance_tokens > 0) ? 1 : 0, initial_reissuance_tokens, CScript() << OP_TRUE); + AppendInitialIssuance(genesis, COutPoint(Txid::FromUint256(uint256(commit)), 0), parentGenesisBlockHash, (initialFreeCoins > 0) ? 1 : 0, initialFreeCoins, (initial_reissuance_tokens > 0) ? 1 : 0, initial_reissuance_tokens, CScript() << OP_TRUE); } } else if (consensus.genesis_style == "dynamic") { // Liquid v2 HF, from genesis. Upgrading networks still use "elements". diff --git a/src/kernel/coinstats.cpp b/src/kernel/coinstats.cpp index 51fbf9742c..5d173061fc 100644 --- a/src/kernel/coinstats.cpp +++ b/src/kernel/coinstats.cpp @@ -89,7 +89,7 @@ static void ApplyCoinHash(std::nullptr_t, const COutPoint& outpoint, const Coin& //! construction could cause a previously invalid (and potentially malicious) //! UTXO snapshot to be considered valid. template -static void ApplyHash(T& hash_obj, const uint256& hash, const std::map& outputs) +static void ApplyHash(T& hash_obj, const Txid& hash, const std::map& outputs) { for (auto it = outputs.begin(); it != outputs.end(); ++it) { COutPoint outpoint = COutPoint(hash, it->first); @@ -118,7 +118,7 @@ static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, c std::unique_ptr pcursor(view->Cursor()); assert(pcursor); - uint256 prevkey; + Txid prevkey; std::map outputs; while (pcursor->Valid()) { if (interruption_point) interruption_point(); diff --git a/src/node/transaction.cpp b/src/node/transaction.cpp index 026c8084dd..e8ab2326c1 100644 --- a/src/node/transaction.cpp +++ b/src/node/transaction.cpp @@ -40,7 +40,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t assert(node.peerman); std::promise promise; - uint256 txid = tx->GetHash(); + Txid txid = tx->GetHash(); uint256 wtxid = tx->GetWitnessHash(); bool callback_set = false; diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index b98ae42d66..ae4e7043e1 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -31,7 +31,7 @@ CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) nSequence = nSequenceIn; } -CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn) +CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn) { prevout = COutPoint(hashPrevTx, nOut); scriptSig = scriptSigIn; diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index 995645c758..ba47e15af8 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -30,7 +30,7 @@ class COutPoint { public: - uint256 hash; + Txid hash; uint32_t n; // @@ -55,7 +55,7 @@ public: static constexpr uint32_t NULL_INDEX = std::numeric_limits::max(); COutPoint(): n(NULL_INDEX) { } - COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { } + COutPoint(const Txid& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { } SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); } @@ -64,8 +64,7 @@ public: friend bool operator<(const COutPoint& a, const COutPoint& b) { - int cmp = a.hash.Compare(b.hash); - return cmp < 0 || (cmp == 0 && a.n < b.n); + return std::tie(a.hash, a.n) < std::tie(b.hash, b.n); } friend bool operator==(const COutPoint& a, const COutPoint& b) @@ -157,7 +156,7 @@ public: } explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL); - CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL); + CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL); // ELEMENTS: explicit serialization methods for selective asset/pegin encoding template diff --git a/src/psbt.cpp b/src/psbt.cpp index e08140ce1a..0894326c27 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -121,7 +121,7 @@ CMutableTransaction PartiallySignedTransaction::GetUnsignedTx(bool force_unblind uint32_t max_sequence = CTxIn::SEQUENCE_FINAL; for (const PSBTInput& input : inputs) { CTxIn txin; - txin.prevout.hash = input.prev_txid; + txin.prevout.hash = Txid::FromUint256(input.prev_txid); txin.prevout.n = *input.prev_out; txin.nSequence = input.sequence.value_or(max_sequence); txin.assetIssuance.assetBlindingNonce = input.m_issuance_blinding_nonce; @@ -222,7 +222,7 @@ bool PartiallySignedTransaction::AddInput(PSBTInput& psbtin) if (tx != std::nullopt) { // This is a v0 psbt, so do the v0 AddInput - CTxIn txin(COutPoint(psbtin.prev_txid, *psbtin.prev_out)); + CTxIn txin(COutPoint(Txid::FromUint256(psbtin.prev_txid), *psbtin.prev_out)); if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) { return false; } @@ -337,7 +337,7 @@ bool PSBTInput::GetUTXO(CTxOut& utxo) const COutPoint PSBTInput::GetOutPoint() const { - return COutPoint(prev_txid, *prev_out); + return COutPoint(Txid::FromUint256(prev_txid), *prev_out); } bool PartiallySignedTransaction::IsBlinded() const diff --git a/src/qt/coincontroldialog.cpp b/src/qt/coincontroldialog.cpp index f366b67cab..8bebd343e7 100644 --- a/src/qt/coincontroldialog.cpp +++ b/src/qt/coincontroldialog.cpp @@ -181,7 +181,7 @@ void CoinControlDialog::showMenu(const QPoint &point) if (item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode) { m_copy_transaction_outpoint_action->setEnabled(true); - if (model->wallet().isLockedCoin(COutPoint(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt()))) + if (model->wallet().isLockedCoin(COutPoint(TxidFromString(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt()))) { lockAction->setEnabled(false); unlockAction->setEnabled(true); @@ -244,7 +244,7 @@ void CoinControlDialog::lockCoin() if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked) contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked); - COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt()); + COutPoint outpt(TxidFromString(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt()); model->wallet().lockCoin(outpt, /* write_to_db = */ true); contextMenuItem->setDisabled(true); contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed")); @@ -254,7 +254,7 @@ void CoinControlDialog::lockCoin() // context menu action: unlock coin void CoinControlDialog::unlockCoin() { - COutPoint outpt(uint256S(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt()); + COutPoint outpt(TxidFromString(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt()); model->wallet().unlockCoin(outpt); contextMenuItem->setDisabled(false); contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon()); @@ -346,7 +346,7 @@ void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column) { if (column == COLUMN_CHECKBOX && item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode) { - COutPoint outpt(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt()); + COutPoint outpt(TxidFromString(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt()); if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked) m_coin_control.UnSelect(outpt); diff --git a/src/rest.cpp b/src/rest.cpp index c22fe4809f..57e685dbf4 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -795,7 +795,6 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std:: for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++) { - uint256 txid; int32_t nOutput; std::string strTxid = uriParts[i].substr(0, uriParts[i].find('-')); std::string strOutput = uriParts[i].substr(uriParts[i].find('-')+1); @@ -803,8 +802,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std:: if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid)) return RESTERR(req, HTTP_BAD_REQUEST, "Parse error"); - txid.SetHex(strTxid); - vOutPoints.emplace_back(txid, (uint32_t)nOutput); + vOutPoints.emplace_back(TxidFromString(strTxid), (uint32_t)nOutput); } if (vOutPoints.size() > 0) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 7ea2cf129e..ff54bf389d 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1193,7 +1193,7 @@ static RPCHelpMan gettxout() UniValue ret(UniValue::VOBJ); - uint256 hash(ParseHashV(request.params[0], "txid")); + auto hash{Txid::FromUint256(ParseHashV(request.params[0], "txid"))}; COutPoint out{hash, request.params[1].getInt()}; bool fMempool = true; if (!request.params[2].isNull()) @@ -2249,7 +2249,7 @@ bool FindScriptPubKey(std::atomic& scan_progress, const std::atomic& } if (count % 256 == 0) { // update progress reference every 256 item - uint32_t high = 0x100 * *key.hash.begin() + *(key.hash.begin() + 1); + uint32_t high = 0x100 * *UCharCast(key.hash.begin()) + *(UCharCast(key.hash.begin()) + 1); scan_progress = (int)(high * 100.0 / 65536.0 + 0.5); } if (needles.count(coin.out.scriptPubKey)) { diff --git a/src/rpc/mempool.cpp b/src/rpc/mempool.cpp index 215d2ee071..029d312805 100644 --- a/src/rpc/mempool.cpp +++ b/src/rpc/mempool.cpp @@ -644,7 +644,7 @@ static RPCHelpMan gettxspendingprevout() {"vout", UniValueType(UniValue::VNUM)}, }, /*fAllowNull=*/false, /*fStrict=*/true); - const uint256 txid(ParseHashO(o, "txid")); + const Txid txid = Txid::FromUint256(ParseHashO(o, "txid")); const int nOutput{o.find_value("vout").getInt()}; if (nOutput < 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative"); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index d5326e1266..854cc3ed24 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -3125,7 +3125,7 @@ static RPCHelpMan calculateasset() uint256 entropy; CAsset asset; CAsset token; - COutPoint outpoint(txid, vout); + COutPoint outpoint(Txid::FromUint256(txid), vout); GenerateAssetEntropy(entropy, outpoint, asset_entropy); CalculateAsset(asset, entropy); CalculateReissuanceToken(token, entropy, blind_reissuance); diff --git a/src/rpc/rawtransaction_util.cpp b/src/rpc/rawtransaction_util.cpp index 963a3d04eb..942414ff0f 100644 --- a/src/rpc/rawtransaction_util.cpp +++ b/src/rpc/rawtransaction_util.cpp @@ -113,7 +113,7 @@ static void CreatePegInInputInner(CMutableTransaction& mtx, uint32_t input_idx, if (mtx.vin.size() <= input_idx) { mtx.vin.resize(input_idx + 1); } - mtx.vin[input_idx] = CTxIn(COutPoint(txHashes[0], nOut), CScript(), ~(uint32_t)0); + mtx.vin[input_idx] = CTxIn(COutPoint(Txid::FromUint256(txHashes[0]), nOut), CScript(), ~(uint32_t)0); // Construct pegin proof CScriptWitness pegin_witness = CreatePeginWitness(value, Params().GetConsensus().pegged_asset, Params().ParentGenesisBlockHash(), witness_script, txBTCRef, merkleBlock); @@ -158,7 +158,7 @@ void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optio const UniValue& input = inputs[idx]; const UniValue& o = input.get_obj(); - uint256 txid = ParseHashO(o, "txid"); + Txid txid = Txid::FromUint256(ParseHashO(o, "txid")); const UniValue& vout_v = o.find_value("vout"); if (!vout_v.isNum()) @@ -442,7 +442,7 @@ void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keyst {"scriptPubKey", UniValueType(UniValue::VSTR)}, }); - uint256 txid = ParseHashO(prevOut, "txid"); + Txid txid = Txid::FromUint256(ParseHashO(prevOut, "txid")); int nOut = prevOut.find_value("vout").getInt(); if (nOut < 0) { diff --git a/src/rpc/txoutproof.cpp b/src/rpc/txoutproof.cpp index 37c9d7d442..e1b0724351 100644 --- a/src/rpc/txoutproof.cpp +++ b/src/rpc/txoutproof.cpp @@ -69,7 +69,7 @@ static RPCHelpMan gettxoutproof() // 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(active_chainstate.CoinsTip(), tx); + const Coin& coin = AccessByTxid(active_chainstate.CoinsTip(), Txid::FromUint256(tx)); if (!coin.IsSpent()) { pblockindex = active_chainstate.m_chain[coin.nHeight]; break; diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index 03ef7e3cbb..2796390556 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1835,7 +1835,7 @@ bool EvalScript(std::vector >& stack, const CScript& case OP_INSPECTINPUTOUTPOINT: { // Push prev txid - stack.emplace_back(inp.prevout.hash.begin(), inp.prevout.hash.end()); + stack.emplace_back(inp.prevout.hash.ToUint256().begin(), inp.prevout.hash.ToUint256().end()); push4_le(stack, inp.prevout.n); // Push the outpoint flag @@ -2579,7 +2579,7 @@ void PrecomputedTransactionData::Init(const T& txTo, std::vector&& spent std::vector simplicityRawAnnex(txTo.witness.vtxinwit.size()); std::vector simplicityRawInput(txTo.vin.size()); for (size_t i = 0; i < txTo.vin.size(); ++i) { - simplicityRawInput[i].prevTxid = txTo.vin[i].prevout.hash.begin(); + simplicityRawInput[i].prevTxid = txTo.vin[i].prevout.hash.ToUint256().begin(); simplicityRawInput[i].prevIx = txTo.vin[i].prevout.n; simplicityRawInput[i].sequence = txTo.vin[i].nSequence; simplicityRawInput[i].txo.asset = m_spent_outputs[i].nAsset.vchCommitment.empty() ? nullptr : m_spent_outputs[i].nAsset.vchCommitment.data(); diff --git a/src/test/blind_tests.cpp b/src/test/blind_tests.cpp index 76065340e4..2ce8312f0a 100644 --- a/src/test/blind_tests.cpp +++ b/src/test/blind_tests.cpp @@ -64,10 +64,10 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) // Build a transaction that spends 2 unblinded coins (11, 111), and produces a single blinded one (100) and fee (22). CMutableTransaction tx3; tx3.vin.resize(2); - tx3.vin[0].prevout.hash = ArithToUint256(1); + tx3.vin[0].prevout.hash = Txid::FromUint256(ArithToUint256(1)); tx3.vin[0].prevout.n = 0; - tx3.vin[1].prevout.hash = ArithToUint256(2); + tx3.vin[1].prevout.hash = Txid::FromUint256(ArithToUint256(2)); tx3.vin[1].prevout.n = 0; tx3.vout.resize(0); tx3.vout.emplace_back(bitcoinID, 100, CScript() << OP_TRUE); @@ -156,9 +156,9 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) // Build a transactions that spends an unblinded (111) and blinded (100) coin, and produces only unblinded coins (impossible) CMutableTransaction tx4; tx4.vin.resize(2); - tx4.vin[0].prevout.hash = ArithToUint256(2); + tx4.vin[0].prevout.hash = Txid::FromUint256(ArithToUint256(2)); tx4.vin[0].prevout.n = 0; - tx4.vin[1].prevout.hash = ArithToUint256(3); + tx4.vin[1].prevout.hash = Txid::FromUint256(ArithToUint256(3)); tx4.vin[1].prevout.n = 0; tx4.vout.emplace_back(bitcoinID, 30, CScript() << OP_TRUE); tx4.vout.emplace_back(bitcoinID, 40, CScript() << OP_TRUE); @@ -194,9 +194,9 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) // Build a transactions that spends an unblinded (111) and blinded (100) coin, and produces a blinded (30), unblinded (40), and blinded (50) coin and fee (91) CMutableTransaction tx4; tx4.vin.resize(2); - tx4.vin[0].prevout.hash = ArithToUint256(2); + tx4.vin[0].prevout.hash = Txid::FromUint256(ArithToUint256(2)); tx4.vin[0].prevout.n = 0; - tx4.vin[1].prevout.hash = ArithToUint256(3); + tx4.vin[1].prevout.hash = Txid::FromUint256(ArithToUint256(3)); tx4.vin[1].prevout.n = 0; tx4.vout.emplace_back(bitcoinID, 30, CScript() << OP_TRUE); tx4.vout.emplace_back(bitcoinID, 40, CScript() << OP_TRUE); @@ -288,8 +288,8 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) CMutableTransaction tx5; tx5.vin.resize(0); tx5.vout.resize(0); - tx5.vin.emplace_back(COutPoint(ArithToUint256(3), 0)); - tx5.vin.emplace_back(COutPoint(ArithToUint256(5), 0)); + tx5.vin.emplace_back(COutPoint(Txid::FromUint256(ArithToUint256(3)), 0)); + tx5.vin.emplace_back(COutPoint(Txid::FromUint256(ArithToUint256(5)), 0)); tx5.vout.emplace_back(bitcoinID, 29, CScript() << OP_TRUE); tx5.vout.emplace_back(bitcoinID, 70, CScript() << OP_TRUE); tx5.vout.emplace_back(otherID, 250, CScript() << OP_TRUE); diff --git a/src/test/blockencodings_tests.cpp b/src/test/blockencodings_tests.cpp index e4ef019daf..7968966303 100644 --- a/src/test/blockencodings_tests.cpp +++ b/src/test/blockencodings_tests.cpp @@ -32,13 +32,13 @@ static CBlock BuildBlockTestCase() { block.hashPrevBlock = InsecureRand256(); block.nBits = 0x207fffff; - tx.vin[0].prevout.hash = InsecureRand256(); + tx.vin[0].prevout.hash = Txid::FromUint256(InsecureRand256()); tx.vin[0].prevout.n = 0; block.vtx[1] = MakeTransactionRef(tx); tx.vin.resize(10); for (size_t i = 0; i < tx.vin.size(); i++) { - tx.vin[i].prevout.hash = InsecureRand256(); + tx.vin[i].prevout.hash = Txid::FromUint256(InsecureRand256()); tx.vin[i].prevout.n = 0; } block.vtx[2] = MakeTransactionRef(tx); diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp index 3bcc50166a..8b86cc3d22 100644 --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -136,11 +136,11 @@ BOOST_AUTO_TEST_CASE(bloom_match) BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); + filter.insert(COutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - COutPoint prevOutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0); + COutPoint prevOutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0); { std::vector data(32 + sizeof(unsigned int)); memcpy(data.data(), prevOutPoint.hash.begin(), 32); @@ -158,11 +158,11 @@ BOOST_AUTO_TEST_CASE(bloom_match) BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256S("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 1)); + filter.insert(COutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 1)); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); - filter.insert(COutPoint(uint256S("0x000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); + filter.insert(COutPoint(TxidFromString("0x000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0)); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about"); } @@ -414,9 +414,9 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_p2pubkey_only) BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We should match the generation outpoint - BOOST_CHECK(filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); + BOOST_CHECK(filter.contains(COutPoint(TxidFromString("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); // ... but not the 4th transaction's output (its not pay-2-pubkey) - BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); + BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); } BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none) @@ -437,8 +437,8 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none) BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We shouldn't match any outpoints (UPDATE_NONE) - BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); - BOOST_CHECK(!filter.contains(COutPoint(uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); + BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0))); + BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0))); } static std::vector RandomData() diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index 2eba430199..123b88a0e8 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -137,16 +137,16 @@ void SimulationTest(CCoinsView* base, bool fake_best_block) stack.push_back(std::make_unique(base)); // Start with one cache. // Use a limited set of random transaction ids, so we do test overwriting entries. - std::vector txids; + std::vector txids; txids.resize(NUM_SIMULATION_ITERATIONS / 8); for (unsigned int i = 0; i < txids.size(); i++) { - txids[i] = InsecureRand256(); + txids[i] = Txid::FromUint256(InsecureRand256()); } for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { // Do a random modification. { - uint256 txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration. + auto txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration. Coin& coin = result[COutPoint(txid, 0)]; // Determine whether to test HaveCoin before or after Access* (or both). As these functions @@ -290,7 +290,7 @@ UtxoData utxoData; UtxoData::iterator FindRandomFrom(const std::set &utxoSet) { assert(utxoSet.size()); - auto utxoSetIt = utxoSet.lower_bound(COutPoint(InsecureRand256(), 0)); + auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(InsecureRand256()), 0)); if (utxoSetIt == utxoSet.end()) { utxoSetIt = utxoSet.begin(); } @@ -927,7 +927,7 @@ void TestFlushBehavior( } }; - uint256 txid = InsecureRand256(); + Txid txid = Txid::FromUint256(InsecureRand256()); COutPoint outp = COutPoint(txid, 0); Coin coin = MakeCoin(); // Ensure the coins views haven't seen this coin before. @@ -1018,7 +1018,7 @@ void TestFlushBehavior( // --- Bonus check: ensure that a coin added to the base view via one cache // can be spent by another cache which has never seen it. // - txid = InsecureRand256(); + txid = Txid::FromUint256(InsecureRand256()); outp = COutPoint(txid, 0); coin = MakeCoin(); BOOST_CHECK(!base.HaveCoin(outp)); @@ -1041,7 +1041,7 @@ void TestFlushBehavior( // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync() // - txid = InsecureRand256(); + txid = Txid::FromUint256(InsecureRand256()); outp = COutPoint(txid, 0); coin = MakeCoin(); CAmount coin_val = coin.out.nValue.GetAmount(); diff --git a/src/test/fuzz/coinscache_sim.cpp b/src/test/fuzz/coinscache_sim.cpp index b1eb6ea96b..2b1bc1694b 100644 --- a/src/test/fuzz/coinscache_sim.cpp +++ b/src/test/fuzz/coinscache_sim.cpp @@ -43,7 +43,9 @@ struct PrecomputedData for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) { uint32_t idx = (i * 1200U) >> 12; /* Map 3 or 4 entries to same txid. */ const uint8_t ser[4] = {uint8_t(idx), uint8_t(idx >> 8), uint8_t(idx >> 16), uint8_t(idx >> 24)}; - CSHA256().Write(PREFIX_O, 1).Write(ser, sizeof(ser)).Finalize(outpoints[i].hash.begin()); + uint256 txid; + CSHA256().Write(PREFIX_O, 1).Write(ser, sizeof(ser)).Finalize(txid.begin()); + outpoints[i].hash = Txid::FromUint256(txid); outpoints[i].n = i; } diff --git a/src/test/fuzz/mini_miner.cpp b/src/test/fuzz/mini_miner.cpp index c7893c70f2..539a41e073 100644 --- a/src/test/fuzz/mini_miner.cpp +++ b/src/test/fuzz/mini_miner.cpp @@ -25,7 +25,7 @@ void initialize_miner() static const auto testing_setup = MakeNoLogFileContext(); g_setup = testing_setup.get(); for (uint32_t i = 0; i < uint32_t{100}; ++i) { - g_available_coins.emplace_back(uint256::ZERO, i); + g_available_coins.emplace_back(Txid::FromUint256(uint256::ZERO), i); } } diff --git a/src/test/fuzz/package_eval.cpp b/src/test/fuzz/package_eval.cpp index f117ce3822..64a90c75f5 100644 --- a/src/test/fuzz/package_eval.cpp +++ b/src/test/fuzz/package_eval.cpp @@ -256,10 +256,10 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool) } if (fuzzed_data_provider.ConsumeBool()) { const auto& txid = fuzzed_data_provider.ConsumeBool() ? - txs.back()->GetHash().ToUint256() : + txs.back()->GetHash() : PickValue(fuzzed_data_provider, mempool_outpoints).hash; const auto delta = fuzzed_data_provider.ConsumeIntegralInRange(-50 * COIN, +50 * COIN); - tx_pool.PrioritiseTransaction(txid, delta); + tx_pool.PrioritiseTransaction(txid.ToUint256(), delta); } // Remember all added transactions diff --git a/src/test/fuzz/simplicity.cpp b/src/test/fuzz/simplicity.cpp index 6800fad76f..fc35b7e8d1 100644 --- a/src/test/fuzz/simplicity.cpp +++ b/src/test/fuzz/simplicity.cpp @@ -164,7 +164,7 @@ FUZZ_TARGET(simplicity, .init = initialize_simplicity) // // We do skip the first byte since that has pegin/issuance flag in it and // therefore already has semantic information. - size_t nIn = mtx.vin[0].prevout.hash.data()[1] % mtx.vin.size(); + size_t nIn = mtx.vin[0].prevout.hash.ToUint256().data()[1] % mtx.vin.size(); std::vector spent_outs{}; for (unsigned int i = 0; i < mtx.vin.size(); i++) { // Null asset or value would assert in the interpreter, and are impossible @@ -192,7 +192,7 @@ FUZZ_TARGET(simplicity, .init = initialize_simplicity) mtx.witness.vtxinwit[nIn].scriptWitness.stack.clear(); mtx.witness.vtxinwit[nIn].scriptWitness.stack.push_back(prog_bytes); mtx.witness.vtxinwit[nIn].scriptWitness.stack.push_back(TAPROOT_CONTROL); - if (mtx.vin[0].prevout.hash.data()[2] & 1) { + if (mtx.vin[0].prevout.hash.ToUint256().data()[2] & 1) { mtx.witness.vtxinwit[nIn].scriptWitness.stack.push_back(TAPROOT_ANNEX); } @@ -209,7 +209,7 @@ FUZZ_TARGET(simplicity, .init = initialize_simplicity) // 4. Main test unsigned char imr_out[32]; - unsigned char *imr = mtx.vin[0].prevout.hash.data()[2] & 2 ? imr_out : nullptr; + unsigned char *imr = mtx.vin[0].prevout.hash.ToUint256().data()[2] & 2 ? imr_out : nullptr; const elementsTransaction* tx = txdata.m_simplicity_tx_data.get(); elementsTapEnv* taproot = simplicity_elements_mallocTapEnv(&simplicityRawTap); diff --git a/src/test/fuzz/simplicity_tx.cpp b/src/test/fuzz/simplicity_tx.cpp index b4a6bb1cea..d6ee92795d 100644 --- a/src/test/fuzz/simplicity_tx.cpp +++ b/src/test/fuzz/simplicity_tx.cpp @@ -110,7 +110,7 @@ FUZZ_TARGET(simplicity_tx, .init = initialize_simplicity_tx) return; } } - const auto& random_bytes = mtx.vin[0].prevout.hash; + const auto& random_bytes = mtx.vin[0].prevout.hash.ToUint256(); // 3. Construct `nIn` and `spent_outs` arrays. bool expect_simplicity = false; diff --git a/src/test/fuzz/tx_pool.cpp b/src/test/fuzz/tx_pool.cpp index b78b4ec2da..202ef40cc2 100644 --- a/src/test/fuzz/tx_pool.cpp +++ b/src/test/fuzz/tx_pool.cpp @@ -280,10 +280,10 @@ FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool) } if (fuzzed_data_provider.ConsumeBool()) { const auto& txid = fuzzed_data_provider.ConsumeBool() ? - tx->GetHash().ToUint256() : + tx->GetHash() : PickValue(fuzzed_data_provider, outpoints_rbf).hash; const auto delta = fuzzed_data_provider.ConsumeIntegralInRange(-50 * COIN, +50 * COIN); - tx_pool.PrioritiseTransaction(txid, delta); + tx_pool.PrioritiseTransaction(txid.ToUint256(), delta); } // Remember all removed and added transactions @@ -370,7 +370,7 @@ FUZZ_TARGET(tx_pool, .init = initialize_tx_pool) MockTime(fuzzed_data_provider, chainstate); - std::vector txids; + std::vector txids; txids.reserve(g_outpoints_coinbase_init_mature.size()); for (const auto& outpoint : g_outpoints_coinbase_init_mature) { txids.push_back(outpoint.hash); @@ -378,7 +378,7 @@ FUZZ_TARGET(tx_pool, .init = initialize_tx_pool) for (int i{0}; i <= 3; ++i) { // Add some immature and non-existent outpoints txids.push_back(g_outpoints_coinbase_init_immature.at(i).hash); - txids.push_back(ConsumeUInt256(fuzzed_data_provider)); + txids.push_back(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))); } SetMempoolConstraints(*node.args, fuzzed_data_provider); @@ -399,10 +399,10 @@ FUZZ_TARGET(tx_pool, .init = initialize_tx_pool) } if (fuzzed_data_provider.ConsumeBool()) { const auto txid = fuzzed_data_provider.ConsumeBool() ? - mut_tx.GetHash().ToUint256() : + mut_tx.GetHash() : PickValue(fuzzed_data_provider, txids); const auto delta = fuzzed_data_provider.ConsumeIntegralInRange(-50 * COIN, +50 * COIN); - tx_pool.PrioritiseTransaction(txid, delta); + tx_pool.PrioritiseTransaction(txid.ToUint256(), delta); } const auto tx = MakeTransactionRef(mut_tx); diff --git a/src/test/fuzz/txorphan.cpp b/src/test/fuzz/txorphan.cpp index 866e39a1ef..48e614a961 100644 --- a/src/test/fuzz/txorphan.cpp +++ b/src/test/fuzz/txorphan.cpp @@ -39,7 +39,7 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage) std::vector outpoints; // initial outpoints used to construct transactions later for (uint8_t i = 0; i < 4; i++) { - outpoints.emplace_back(uint256{i}, 0); + outpoints.emplace_back(Txid::FromUint256(uint256{i}), 0); } // if true, allow duplicate input when constructing tx const bool duplicate_input = fuzzed_data_provider.ConsumeBool(); diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index 13494662bb..944e205ca7 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -40,7 +40,7 @@ int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optiona return fuzzed_data_provider.ConsumeIntegralInRange(min.value_or(time_min), max.value_or(time_max)); } -CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, const std::optional>& prevout_txids, const int max_num_in, const int max_num_out) noexcept +CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, const std::optional>& prevout_txids, const int max_num_in, const int max_num_out) noexcept { CMutableTransaction tx_mut; const auto p2wsh_op_true = fuzzed_data_provider.ConsumeBool(); @@ -53,7 +53,7 @@ CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, for (int i = 0; i < num_in; ++i) { const auto& txid_prev = prevout_txids ? PickValue(fuzzed_data_provider, *prevout_txids) : - ConsumeUInt256(fuzzed_data_provider); + Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)); const auto index_out = fuzzed_data_provider.ConsumeIntegralInRange(0, max_num_out); const auto sequence = ConsumeSequence(fuzzed_data_provider); const auto script_sig = p2wsh_op_true ? CScript{} : ConsumeScript(fuzzed_data_provider); diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index 9e360a4217..045476ab86 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -145,7 +145,7 @@ template [[nodiscard]] int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional& min = std::nullopt, const std::optional& max = std::nullopt) noexcept; -[[nodiscard]] CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, const std::optional>& prevout_txids, const int max_num_in = 10, const int max_num_out = 10) noexcept; +[[nodiscard]] CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, const std::optional>& prevout_txids, const int max_num_in = 10, const int max_num_out = 10) noexcept; [[nodiscard]] CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, const size_t max_stack_elem_size = 32) noexcept; diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index 7fc38d107a..f3d7996e99 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -118,19 +118,19 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const tx.vout.resize(1); tx.vout[0].nValue = 5000000000LL - 1000; // This tx has a low fee: 1000 satoshis - uint256 hashParentTx = tx.GetHash(); // save this txid for later use + Txid hashParentTx = tx.GetHash(); // save this txid for later use tx_mempool.addUnchecked(entry.Fee(1000).Time(Now()).SpendsCoinbase(true).FromTx(tx)); // This tx has a medium fee: 10000 satoshis tx.vin[0].prevout.hash = txFirst[1]->GetHash(); tx.vout[0].nValue = 5000000000LL - 10000; - uint256 hashMediumFeeTx = tx.GetHash(); + Txid hashMediumFeeTx = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(10000).Time(Now()).SpendsCoinbase(true).FromTx(tx)); // This tx has a high fee, but depends on the first transaction tx.vin[0].prevout.hash = hashParentTx; tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 50k satoshi fee - uint256 hashHighFeeTx = tx.GetHash(); + Txid hashHighFeeTx = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(50000).Time(Now()).SpendsCoinbase(false).FromTx(tx)); std::unique_ptr pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey); @@ -142,7 +142,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const // Test that a package below the block min tx fee doesn't get included tx.vin[0].prevout.hash = hashHighFeeTx; tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee - uint256 hashFreeTx = tx.GetHash(); + Txid hashFreeTx = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(0).FromTx(tx)); size_t freeTxSize = ::GetSerializeSize(TX_WITH_WITNESS(tx)); @@ -152,7 +152,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const tx.vin[0].prevout.hash = hashFreeTx; tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse; - uint256 hashLowFeeTx = tx.GetHash(); + Txid hashLowFeeTx = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(feeToUse).FromTx(tx)); pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey); // Verify that the free tx and the low fee tx didn't get selected @@ -180,7 +180,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const tx.vout.resize(2); tx.vout[0].nValue = 5000000000LL - 100000000; tx.vout[1].nValue = 100000000; // 1BTC output - uint256 hashFreeTx2 = tx.GetHash(); + Txid hashFreeTx2 = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx)); // This tx can't be mined by itself @@ -188,7 +188,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const tx.vout.resize(1); feeToUse = blockMinFeeRate.GetFee(freeTxSize); tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse; - uint256 hashLowFeeTx2 = tx.GetHash(); + Txid hashLowFeeTx2 = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx)); pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey); @@ -210,7 +210,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const void MinerTestingSetup::TestBasicMining(const CScript& scriptPubKey, const std::vector& txFirst, int baseheight) { - uint256 hash; + Txid hash; CMutableTransaction tx; TestMemPoolEntryHelper entry; entry.nFee = 11; @@ -545,20 +545,20 @@ void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const tx.vin[0].prevout.n = 0; tx.vout[0].nValue = 5000000000LL - 1000; // This tx has a low fee: 1000 satoshis - uint256 hashParentTx = tx.GetHash(); // save this txid for later use + Txid hashParentTx = tx.GetHash(); // save this txid for later use tx_mempool.addUnchecked(entry.Fee(1000).Time(Now()).SpendsCoinbase(true).FromTx(tx)); // This tx has a medium fee: 10000 satoshis tx.vin[0].prevout.hash = txFirst[2]->GetHash(); tx.vout[0].nValue = 5000000000LL - 10000; - uint256 hashMediumFeeTx = tx.GetHash(); + Txid hashMediumFeeTx = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(10000).Time(Now()).SpendsCoinbase(true).FromTx(tx)); tx_mempool.PrioritiseTransaction(hashMediumFeeTx, -5 * COIN); // This tx also has a low fee, but is prioritised tx.vin[0].prevout.hash = hashParentTx; tx.vout[0].nValue = 5000000000LL - 1000 - 1000; // 1000 satoshi fee - uint256 hashPrioritsedChild = tx.GetHash(); + Txid hashPrioritsedChild = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(1000).Time(Now()).SpendsCoinbase(false).FromTx(tx)); tx_mempool.PrioritiseTransaction(hashPrioritsedChild, 2 * COIN); @@ -570,19 +570,19 @@ void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const // When FreeChild is included, FreeChild's prioritisation should also not be included. tx.vin[0].prevout.hash = txFirst[3]->GetHash(); tx.vout[0].nValue = 5000000000LL; // 0 fee - uint256 hashFreeParent = tx.GetHash(); + Txid hashFreeParent = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(true).FromTx(tx)); tx_mempool.PrioritiseTransaction(hashFreeParent, 10 * COIN); tx.vin[0].prevout.hash = hashFreeParent; tx.vout[0].nValue = 5000000000LL; // 0 fee - uint256 hashFreeChild = tx.GetHash(); + Txid hashFreeChild = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(false).FromTx(tx)); tx_mempool.PrioritiseTransaction(hashFreeChild, 1 * COIN); tx.vin[0].prevout.hash = hashFreeChild; tx.vout[0].nValue = 5000000000LL; // 0 fee - uint256 hashFreeGrandchild = tx.GetHash(); + Txid hashFreeGrandchild = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(0).SpendsCoinbase(false).FromTx(tx)); auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey); diff --git a/src/test/miniminer_tests.cpp b/src/test/miniminer_tests.cpp index 311e402e3e..5b007997b9 100644 --- a/src/test/miniminer_tests.cpp +++ b/src/test/miniminer_tests.cpp @@ -190,7 +190,7 @@ BOOST_FIXTURE_TEST_CASE(miniminer_1p1c, TestChain100Setup) CFeeRate(23330), CFeeRate(50000), CFeeRate(5*CENT)}); // All nonexistent entries have a bumpfee of zero, regardless of feerate - std::vector nonexistent_outpoints({ COutPoint{GetRandHash(), 0}, COutPoint{GetRandHash(), 3} }); + std::vector nonexistent_outpoints({ COutPoint{Txid::FromUint256(GetRandHash()), 0}, COutPoint{Txid::FromUint256(GetRandHash()), 3} }); for (const auto& outpoint : nonexistent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint)); for (const auto& feerate : various_normal_feerates) { node::MiniMiner mini_miner(pool, nonexistent_outpoints); @@ -590,9 +590,17 @@ BOOST_FIXTURE_TEST_CASE(calculate_cluster, TestChain100Setup) CTxMemPool& pool = *Assert(m_node.mempool); LOCK2(cs_main, pool.cs); + // TODO this can be removed once the mempool interface uses Txid, Wtxid + auto convert_to_uint256_vec = [](const std::vector& vec) -> std::vector { + std::vector out; + std::transform(vec.begin(), vec.end(), std::back_inserter(out), + [](const Txid& txid) { return txid.ToUint256(); }); + return out; + }; + // Add chain of size 500 TestMemPoolEntryHelper entry; - std::vector chain_txids; + std::vector chain_txids; auto& lasttx = m_coinbase_txns[0]; for (auto i{0}; i < 500; ++i) { const auto tx = make_tx({COutPoint{lasttx->GetHash(), 0}}, /*num_outputs=*/1); @@ -603,7 +611,7 @@ BOOST_FIXTURE_TEST_CASE(calculate_cluster, TestChain100Setup) const auto cluster_500tx = pool.GatherClusters({lasttx->GetHash()}); CTxMemPool::setEntries cluster_500tx_set{cluster_500tx.begin(), cluster_500tx.end()}; BOOST_CHECK_EQUAL(cluster_500tx.size(), cluster_500tx_set.size()); - const auto vec_iters_500 = pool.GetIterVec(chain_txids); + const auto vec_iters_500 = pool.GetIterVec(convert_to_uint256_vec(chain_txids)); for (const auto& iter : vec_iters_500) BOOST_CHECK(cluster_500tx_set.count(iter)); // GatherClusters stops at 500 transactions. @@ -618,9 +626,9 @@ BOOST_FIXTURE_TEST_CASE(calculate_cluster, TestChain100Setup) * txc0 txc1 txc2 ... txc48 * Note that each transaction's ancestor size is 1 or 3, and each descendant size is 1, 2 or 3. * However, all of these transactions are in the same cluster. */ - std::vector zigzag_txids; + std::vector zigzag_txids; for (auto p{0}; p < 50; ++p) { - const auto txp = make_tx({COutPoint{GetRandHash(), 0}}, /*num_outputs=*/2); + const auto txp = make_tx({COutPoint{Txid::FromUint256(GetRandHash()), 0}}, /*num_outputs=*/2); pool.addUnchecked(entry.Fee(CENT).FromTx(txp)); zigzag_txids.push_back(txp->GetHash()); } @@ -629,7 +637,7 @@ BOOST_FIXTURE_TEST_CASE(calculate_cluster, TestChain100Setup) pool.addUnchecked(entry.Fee(CENT).FromTx(txc)); zigzag_txids.push_back(txc->GetHash()); } - const auto vec_iters_zigzag = pool.GetIterVec(zigzag_txids); + const auto vec_iters_zigzag = pool.GetIterVec(convert_to_uint256_vec(zigzag_txids)); // It doesn't matter which tx we calculate cluster for, everybody is in it. const std::vector indices{0, 22, 72, zigzag_txids.size() - 1}; for (const auto index : indices) { diff --git a/src/test/orphanage_tests.cpp b/src/test/orphanage_tests.cpp index b51de30cb2..bf465c0c64 100644 --- a/src/test/orphanage_tests.cpp +++ b/src/test/orphanage_tests.cpp @@ -68,7 +68,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans) CMutableTransaction tx; tx.vin.resize(1); tx.vin[0].prevout.n = 0; - tx.vin[0].prevout.hash = InsecureRand256(); + tx.vin[0].prevout.hash = Txid::FromUint256(InsecureRand256()); tx.vin[0].scriptSig << OP_1; tx.vout.resize(1); tx.vout[0].nValue = 1*CENT; diff --git a/src/test/pegin_spent_tests.cpp b/src/test/pegin_spent_tests.cpp index e3e961ddc0..90c8737180 100644 --- a/src/test/pegin_spent_tests.cpp +++ b/src/test/pegin_spent_tests.cpp @@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(PeginSpent_validity) //Basic insert of blank outpoint pair, blank COutPoint allows for checking coinsCache - std::pair outpoint = std::make_pair(GetRandHash(), COutPoint(GetRandHash(), 42)); + std::pair outpoint = std::make_pair(GetRandHash(), COutPoint(Txid::FromUint256(GetRandHash()), 42)); BOOST_CHECK(!coinsCache.GetCoin(outpoint.second, ret)); //Checking for pegin spentness should not create an entry @@ -78,7 +78,7 @@ BOOST_AUTO_TEST_CASE(PeginSpent_validity) CCoinsMapMemoryResource resource; CCoinsMap mapCoins{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource}; CCoinsCacheEntry entry; - std::pair outpoint3(std::make_pair(GetRandHash(), COutPoint(GetRandHash(), 42))); + std::pair outpoint3(std::make_pair(GetRandHash(), COutPoint(Txid::FromUint256(GetRandHash()), 42))); //Attempt batch write of non-dirty pegin, no effect entry.flags = CCoinsCacheEntry::PEGIN; @@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(PeginSpent_validity) BOOST_CHECK(mapCoins.size() == 0); //Add an entry we never IsPeginSpent'd first (ie added to cache via SetPeginSpent) - std::pair outpoint4(std::make_pair(GetRandHash(), COutPoint(GetRandHash(), 42))); + std::pair outpoint4(std::make_pair(GetRandHash(), COutPoint(Txid::FromUint256(GetRandHash()), 42))); coinsCache.SetPeginSpent(outpoint4, true); // Check the final state of coinsCache.mapCoins is sane. diff --git a/src/test/pegin_witness_tests.cpp b/src/test/pegin_witness_tests.cpp index f551530171..6492cbb11d 100644 --- a/src/test/pegin_witness_tests.cpp +++ b/src/test/pegin_witness_tests.cpp @@ -34,7 +34,7 @@ std::vector > witness_stack = { std::vector pegin_transaction = ParseHex("020000000101f321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce0100004000ffffffff0201ef4699c160d014d5ff79636d8a4cb990b9df4ebab649f144d19f5c495c585e4701000000003b9ab2e0001976a914809326f7628dc976fbe63806479a1b8dfcc8c4b988ac01ef4699c160d014d5ff79636d8a4cb990b9df4ebab649f144d19f5c495c585e47010000000000001720000000000000000002483045022100ae17064745d80650a6a5cbcbe15c8c45ba498d1c6f45a7c0f5f32d871b463fc60220799f2836471702c21f7cfe124651727b530ad41f7af4dc213c65f5030a2f6fc4012103a9d3c6c7c161a565a76113632fe13330cf2c0207ba79a76d1154cdc3cb94d940060800ca9a3b0000000020ef4699c160d014d5ff79636d8a4cb990b9df4ebab649f144d19f5c495c585e472006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f1600141eef6361cd1507a303834285d1521d6baf1b19aebe0200000001b399292c8100b8a1b66eb23896f799c1712390d560af0f70e81acd2d17a3b06e0000000049483045022100c3c749623486ea57ea93dfaf78d85590d78c7590a25768fe80f0ea4d6047419002202a0a00a90392b86c53c0fdda908c4591ba28040c16c25734c23b7df3c8b70acd01feffffff0228196bee000000001976a914470dd41542ee1a1bd75f1a838878648c8d65622488ac00ca9a3b0000000017a914cb60b1d7f76ba12b45a116c482c165a74c5d7e38876500000097000000205e3913a320cd2e3a2efa141e47419f54cb9e82320cf8dbc812fc19b9a1b2413a57f5e9fb4fa22de191454a241387f5d10cc794ee0fbf72ae2841baf3129a4eab8133025affff7f20000000000200000002f9d0be670007d38fceece999cb6144658a99c307ccc37f6d8f69129ed0f4545ff321df9790633bc33c67239c4174df8142ee616ee6a2e2788fe4820fe70e9bce010500000000"); -COutPoint prevout(uint256S("ce9b0ee70f82e48f78e2a2e66e61ee4281df74419c23673cc33b639097df21f3"), 1); +COutPoint prevout(Txid::FromUint256(uint256S("ce9b0ee70f82e48f78e2a2e66e61ee4281df74419c23673cc33b639097df21f3")), 1); const std::string fedpegscript_str = "512103dff4923d778550cc13ce0d887d737553b4b58f4e8e886507fc39f5e447b2186451ae"; @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(witness_valid) // Test mismatched but valid txid fake_prevout = prevout; - fake_prevout.hash = uint256S("2f103ee04a5649eecb932b4da4ca9977f53a12bbe04d9d1eb5ccc0f4a06334"); + fake_prevout.hash = Txid::FromUint256(uint256S("2f103ee04a5649eecb932b4da4ca9977f53a12bbe04d9d1eb5ccc0f4a06334")); BOOST_CHECK(!IsValidPeginWitness(witness, fedpegscripts, fake_prevout, err, false)); // Ensure that all witness stack sizes are handled diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp index 7ba1b24585..755a32b459 100644 --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -102,7 +102,7 @@ void static RandomTransaction(CMutableTransaction& tx, bool fSingle) for (int in = 0; in < ins; in++) { tx.vin.emplace_back(); CTxIn &txin = tx.vin.back(); - txin.prevout.hash = InsecureRand256(); + txin.prevout.hash = Txid::FromUint256(InsecureRand256()); txin.prevout.n = InsecureRandBits(2); RandomScript(txin.scriptSig); txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : std::numeric_limits::max(); diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 08ff9d446b..f31d633d51 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -222,7 +223,7 @@ BOOST_AUTO_TEST_CASE(tx_valid) fValid = false; break; } - COutPoint outpoint{uint256S(vinput[0].get_str()), uint32_t(vinput[1].getInt())}; + COutPoint outpoint{TxidFromString(vinput[0].get_str()), uint32_t(vinput[1].getInt())}; mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str()); if (vinput.size() >= 4) { @@ -310,7 +311,7 @@ BOOST_AUTO_TEST_CASE(tx_invalid) fValid = false; break; } - COutPoint outpoint{uint256S(vinput[0].get_str()), uint32_t(vinput[1].getInt())}; + COutPoint outpoint{TxidFromString(vinput[0].get_str()), uint32_t(vinput[1].getInt())}; mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str()); if (vinput.size() >= 4) { @@ -520,9 +521,7 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction) // create a big transaction of 4500 inputs signed by the same key for(uint32_t ij = 0; ij < 4500; ij++) { uint32_t i = mtx.vin.size(); - uint256 prevId; - prevId.SetHex("0000000000000000000000000000000000000000000000000000000000000100"); - COutPoint outpoint(prevId, i); + COutPoint outpoint(TxidFromString("0000000000000000000000000000000000000000000000000000000000000100"), i); mtx.vin.resize(mtx.vin.size() + 1); mtx.vin[i].prevout = outpoint; diff --git a/src/test/txpackage_tests.cpp b/src/test/txpackage_tests.cpp index 89d4cb661a..44e4a14957 100644 --- a/src/test/txpackage_tests.cpp +++ b/src/test/txpackage_tests.cpp @@ -29,7 +29,7 @@ inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outpu mtx.vout.resize(num_outputs); auto random_script = CScript() << ToByteVector(InsecureRand256()) << ToByteVector(InsecureRand256()); for (size_t i{0}; i < num_inputs; ++i) { - mtx.vin[i].prevout.hash = InsecureRand256(); + mtx.vin[i].prevout.hash = Txid::FromUint256(InsecureRand256()); mtx.vin[i].prevout.n = 0; mtx.vin[i].scriptSig = random_script; } @@ -83,7 +83,7 @@ BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup) // Packages can't have transactions spending the same prevout CMutableTransaction tx_zero_1; CMutableTransaction tx_zero_2; - COutPoint same_prevout{InsecureRand256(), 0}; + COutPoint same_prevout{Txid::FromUint256(InsecureRand256()), 0}; tx_zero_1.vin.emplace_back(same_prevout); tx_zero_2.vin.emplace_back(same_prevout); // Different vouts (not the same tx) @@ -101,7 +101,7 @@ BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup) // IsConsistentPackage only cares about conflicts between transactions, not about a transaction // conflicting with itself (i.e. duplicate prevouts in vin). CMutableTransaction dup_tx; - const COutPoint rand_prevout{InsecureRand256(), 0}; + const COutPoint rand_prevout{Txid::FromUint256(InsecureRand256()), 0}; dup_tx.vin.emplace_back(rand_prevout); dup_tx.vin.emplace_back(rand_prevout); Package package_with_dup_tx{MakeTransactionRef(dup_tx)}; diff --git a/src/test/util/coins.cpp b/src/test/util/coins.cpp index 9b6c5535c5..742dbc04d1 100644 --- a/src/test/util/coins.cpp +++ b/src/test/util/coins.cpp @@ -16,8 +16,7 @@ COutPoint AddTestCoin(CCoinsViewCache& coins_view) { Coin new_coin; - const uint256 txid{InsecureRand256()}; - COutPoint outpoint{txid, /*nIn=*/0}; + COutPoint outpoint{Txid::FromUint256(InsecureRand256()), /*nIn=*/0}; new_coin.nHeight = 1; new_coin.out.nValue = InsecureRandMoneyAmount(); new_coin.out.scriptPubKey.assign(uint32_t{56}, 1); diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index f7bc948aa8..9f1e7723d0 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -524,7 +524,7 @@ void TestChain100Setup::MockMempoolMinFee(const CFeeRate& target_feerate) // Manually create an invalid transaction. Manually set the fee in the CTxMemPoolEntry to // achieve the exact target feerate. CMutableTransaction mtx = CMutableTransaction(); - mtx.vin.emplace_back(COutPoint{g_insecure_rand_ctx.rand256(), 0}); + mtx.vin.emplace_back(COutPoint{Txid::FromUint256(g_insecure_rand_ctx.rand256()), 0}); mtx.vout.emplace_back(CAsset(), 1 * COIN, GetScriptForDestination(WitnessV0ScriptHash(CScript() << OP_TRUE))); const auto tx{MakeTransactionRef(mtx)}; LockPoints lp; diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp index cfb8b5f91a..5e0ac758d1 100644 --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -727,7 +727,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, Sna badcoin.out.nValue = InsecureRand32(); badcoin.nHeight = 1; badcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0); - uint256 txid = InsecureRand256(); + Txid txid = Txid::FromUint256(InsecureRand256()); ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false); fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot"; diff --git a/src/txmempool.cpp b/src/txmempool.cpp index b9bc89d440..a1a6d1d1a7 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -128,7 +128,7 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector& vHashes if (it == mapTx.end()) { continue; } - auto iter = mapNextTx.lower_bound(COutPoint(hash, 0)); + auto iter = mapNextTx.lower_bound(COutPoint(Txid::FromUint256(hash), 0)); // First calculate the children, and update CTxMemPoolEntry::m_children to // include them, and update their CTxMemPoolEntry::m_parents to include this tx. // we cache the in-mempool children to avoid duplicate updates diff --git a/src/util/transaction_identifier.h b/src/util/transaction_identifier.h index 4fb9b49966..89e10dee01 100644 --- a/src/util/transaction_identifier.h +++ b/src/util/transaction_identifier.h @@ -65,4 +65,9 @@ using Txid = transaction_identifier; /** Wtxid commits to all transaction fields including the witness. */ using Wtxid = transaction_identifier; +inline Txid TxidFromString(std::string_view str) +{ + return Txid::FromUint256(uint256S(str.data())); +} + #endif // BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H diff --git a/src/validation.cpp b/src/validation.cpp index da2352f756..58c04982bc 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -715,7 +715,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws) AssertLockHeld(m_pool.cs); const CTransactionRef& ptx = ws.m_ptx; const CTransaction& tx = *ws.m_ptx; - const uint256& hash = ws.m_hash; + const Txid& hash = ws.m_hash; // Copy/alias what we need out of args const int64_t nAcceptTime = args.m_accept_time; @@ -2183,7 +2183,7 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn // undo transactions in reverse order for (int i = block.vtx.size() - 1; i >= 0; i--) { const CTransaction &tx = *(block.vtx[i]); - uint256 hash = tx.GetHash(); + Txid hash = tx.GetHash(); bool is_coinbase = tx.IsCoinBase(); bool is_bip30_exception = (is_coinbase && !fEnforceBIP30); diff --git a/src/wallet/receive.cpp b/src/wallet/receive.cpp index d9c486a7ad..ed993e3956 100644 --- a/src/wallet/receive.cpp +++ b/src/wallet/receive.cpp @@ -206,7 +206,7 @@ CAmountMap CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wt bool allow_used_addresses = (filter & ISMINE_USED) || !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE); CAmountMap nCredit; - uint256 hashTx = wtx.GetHash(); + Txid hashTx = wtx.GetHash(); CAsset pegged_asset{Params().GetConsensus().pegged_asset}; for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) { const CTxOut& txout = wtx.tx->vout[i]; @@ -390,7 +390,7 @@ std::map GetAddressBalances(const CWallet& wallet) if(!ExtractDestination(output.scriptPubKey, addr)) continue; - CAmount n = wallet.IsSpent(COutPoint(walletEntry.first, i)) ? 0 : wtx.GetOutputValueOut(wallet, i); + CAmount n = wallet.IsSpent(COutPoint(Txid::FromUint256(walletEntry.first), i)) ? 0 : wtx.GetOutputValueOut(wallet, i); if (n < 0) { continue; } diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp index a4dfb029bd..02e420666e 100644 --- a/src/wallet/rpc/backup.cpp +++ b/src/wallet/rpc/backup.cpp @@ -2248,7 +2248,7 @@ RPCHelpMan importissuanceblindingkey() // Import the key in that slot uint256 keyval; memcpy(keyval.begin(), &keydata[0], 32); - CScript blindingScript(CScript() << OP_RETURN << std::vector(pcoin->tx->vin[vindex].prevout.hash.begin(), pcoin->tx->vin[vindex].prevout.hash.end()) << pcoin->tx->vin[vindex].prevout.n); + CScript blindingScript(CScript() << OP_RETURN << std::vector(pcoin->tx->vin[vindex].prevout.hash.ToUint256().begin(), pcoin->tx->vin[vindex].prevout.hash.ToUint256().end()) << pcoin->tx->vin[vindex].prevout.n); if (!pwallet->AddSpecificBlindingKey(CScriptID(blindingScript), keyval)) { throw JSONRPCError(RPC_WALLET_ERROR, "Failed to import blinding key"); } @@ -2380,7 +2380,7 @@ RPCHelpMan dumpissuanceblindingkey() } // We can actually deblind the input if (pcoin->GetIssuanceAmount(*pwallet, vindex, false) != -1 ) { - CScript blindingScript(CScript() << OP_RETURN << std::vector(pcoin->tx->vin[vindex].prevout.hash.begin(), pcoin->tx->vin[vindex].prevout.hash.end()) << pcoin->tx->vin[vindex].prevout.n); + CScript blindingScript(CScript() << OP_RETURN << std::vector(pcoin->tx->vin[vindex].prevout.hash.ToUint256().begin(), pcoin->tx->vin[vindex].prevout.hash.ToUint256().end()) << pcoin->tx->vin[vindex].prevout.n); CKey key; key = wallet->GetBlindingKey(&blindingScript); return HexStr(Span(key.begin(), key.size())); diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp index 5ba008b30f..b494460b12 100644 --- a/src/wallet/rpc/coins.cpp +++ b/src/wallet/rpc/coins.cpp @@ -374,7 +374,7 @@ RPCHelpMan lockunspent() {"vout", UniValueType(UniValue::VNUM)}, }); - const uint256 txid(ParseHashO(o, "txid")); + const Txid txid = Txid::FromUint256(ParseHashO(o, "txid")); const int nOutput = o.find_value("vout").getInt(); if (nOutput < 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative"); diff --git a/src/wallet/rpc/elements.cpp b/src/wallet/rpc/elements.cpp index 253d5da383..2579c37962 100644 --- a/src/wallet/rpc/elements.cpp +++ b/src/wallet/rpc/elements.cpp @@ -1094,7 +1094,7 @@ void FillBlinds(CWallet* pwallet, CMutableTransaction& tx, std::vector& } // Special format for issuance blinding keys, unique for each transaction - CScript blindingScript = CScript() << OP_RETURN << std::vector(tx.vin[nIn].prevout.hash.begin(), tx.vin[nIn].prevout.hash.end()) << tx.vin[nIn].prevout.n; + CScript blindingScript = CScript() << OP_RETURN << std::vector(tx.vin[nIn].prevout.hash.ToUint256().begin(), tx.vin[nIn].prevout.hash.ToUint256().end()) << tx.vin[nIn].prevout.n; for (size_t nPseudo = 0; nPseudo < 2; nPseudo++) { bool issuance_asset = (nPseudo == 0); diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index 0c1d7bab4f..c19ae1fefb 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -751,7 +751,7 @@ void FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& fee_out, if (options.exists("input_weights")) { for (const UniValue& input : options["input_weights"].get_array().getValues()) { - uint256 txid = ParseHashO(input, "txid"); + Txid txid = Txid::FromUint256(ParseHashO(input, "txid")); const UniValue& vout_v = input.find_value("vout"); if (!vout_v.isNum()) { diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 34c1f8ab10..979d024385 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -363,7 +363,7 @@ CoinsResult AvailableCoins(const CWallet& wallet, std::set trusted_parents; for (const auto& entry : wallet.mapWallet) { - const uint256& wtxid = entry.first; + const uint256& txid = entry.first; const CWalletTx& wtx = entry.second; if (wallet.IsTxImmatureCoinBase(wtx) && !params.include_immature_coinbase) @@ -423,7 +423,7 @@ CoinsResult AvailableCoins(const CWallet& wallet, for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) { const CTxOut& output = wtx.tx->vout[i]; - const COutPoint outpoint(wtxid, i); + const COutPoint outpoint(Txid::FromUint256(txid), i); CAmount outValue = wtx.GetOutputValueOut(wallet, i); CAsset asset = wtx.GetOutputAsset(wallet, i); @@ -1661,7 +1661,7 @@ static util::Result CreateTransactionInternal( GenerateAssetEntropy(entropy, txNew.vin[0].prevout, issuance_details->contract_hash); CalculateAsset(asset, entropy); CalculateReissuanceToken(token, entropy, issuance_details->blind_issuance); - CScript blindingScript(CScript() << OP_RETURN << std::vector(txNew.vin[0].prevout.hash.begin(), txNew.vin[0].prevout.hash.end()) << txNew.vin[0].prevout.n); + CScript blindingScript(CScript() << OP_RETURN << std::vector(txNew.vin[0].prevout.hash.ToUint256().begin(), txNew.vin[0].prevout.hash.ToUint256().end()) << txNew.vin[0].prevout.n); txNew.vin[0].assetIssuance.assetEntropy = issuance_details->contract_hash; // We're making asset outputs, fill out asset type and issuance input if (asset_index != -1) { @@ -1706,7 +1706,7 @@ static util::Result CreateTransactionInternal( CAsset temp_token; CalculateReissuanceToken(temp_token, issuance_details->entropy, true); if (temp_token == issuance_details->reissuance_token && blind_details) { - CScript blindingScript(CScript() << OP_RETURN << std::vector(txNew.vin[reissuance_index].prevout.hash.begin(), txNew.vin[reissuance_index].prevout.hash.end()) << txNew.vin[reissuance_index].prevout.n); + CScript blindingScript(CScript() << OP_RETURN << std::vector(txNew.vin[reissuance_index].prevout.hash.ToUint256().begin(), txNew.vin[reissuance_index].prevout.hash.ToUint256().end()) << txNew.vin[reissuance_index].prevout.n); issuance_asset_keys.resize(reissuance_index); issuance_asset_keys.push_back(wallet.GetBlindingKey(&blindingScript)); blind_details->num_to_blind++; diff --git a/src/wallet/test/feebumper_tests.cpp b/src/wallet/test/feebumper_tests.cpp index fbb74be665..c56109e179 100644 --- a/src/wallet/test/feebumper_tests.cpp +++ b/src/wallet/test/feebumper_tests.cpp @@ -21,7 +21,7 @@ static void CheckMaxWeightComputation(const std::string& script_str, const std:: { std::vector script_data(ParseHex(script_str)); CScript script(script_data.begin(), script_data.end()); - CTxIn input(uint256(), 0, script); + CTxIn input(Txid{}, 0, script); CMutableTransaction mtx; mtx.vin.push_back(input); mtx.witness.vtxinwit.resize(1); diff --git a/src/wallet/test/fuzz/notifications.cpp b/src/wallet/test/fuzz/notifications.cpp index ae25ab63fb..676175ad45 100644 --- a/src/wallet/test/fuzz/notifications.cpp +++ b/src/wallet/test/fuzz/notifications.cpp @@ -100,7 +100,7 @@ FUZZ_TARGET(wallet_notifications, .init = initialize_setup) // Add the initial entry chain.emplace_back(); auto& [coins, block]{chain.back()}; - coins.emplace(total_amount, COutPoint{uint256::ONE, 1}); + coins.emplace(total_amount, COutPoint{Txid::FromUint256(uint256::ONE), 1}); } LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 200) { diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index cd8586a63e..4979f23d11 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -950,7 +950,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup) CMutableTransaction mtx; mtx.vout.emplace_back(CAsset(), COIN, GetScriptForDestination(op_dest)); - mtx.vin.emplace_back(g_insecure_rand_ctx.rand256(), 0); + mtx.vin.emplace_back(Txid::FromUint256(g_insecure_rand_ctx.rand256()), 0); const auto& tx_id_to_spend = wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInMempool{})->GetHash(); { @@ -967,12 +967,12 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup) mtx.vin.clear(); mtx.vin.emplace_back(tx_id_to_spend, 0); wallet.transactionAddedToMempool(MakeTransactionRef(mtx)); - const auto good_tx_id{mtx.GetHash().ToUint256()}; + const auto good_tx_id{mtx.GetHash()}; { // Verify balance update for the new tx and the old one LOCK(wallet.cs_wallet); - const CWalletTx* new_wtx = wallet.GetWalletTx(good_tx_id); + const CWalletTx* new_wtx = wallet.GetWalletTx(good_tx_id.ToUint256()); BOOST_CHECK_EQUAL(CachedTxGetAvailableCredit(wallet, *new_wtx)[CAsset()], 1 * COIN); // Now the old wtx diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 3df9247e7d..94b6a53d16 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -690,7 +690,7 @@ std::set CWallet::GetConflicts(const uint256& txid) const bool CWallet::HasWalletSpend(const CTransactionRef& tx) const { AssertLockHeld(cs_wallet); - const uint256& txid = tx->GetHash(); + const Txid& txid = tx->GetHash(); for (unsigned int i = 0; i < tx->vout.size(); ++i) { if (IsSpent(COutPoint(txid, i))) { return true; @@ -1382,7 +1382,7 @@ void CWallet::RecursiveUpdateTxState(const uint256& tx_hash, const TryUpdatingSt batch.WriteTx(wtx); // Iterate over all its outputs, and update those tx states as well (if applicable) for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) { - std::pair range = mapTxSpends.equal_range(COutPoint(now, i)); + std::pair range = mapTxSpends.equal_range(COutPoint(Txid::FromUint256(now), i)); for (TxSpends::const_iterator iter = range.first; iter != range.second; ++iter) { if (!done.count(iter->second)) { todo.insert(iter->second); @@ -2335,7 +2335,7 @@ BlindingStatus CWallet::WalletBlindPSBT(PartiallySignedTransaction& psbtx) const if (!this->IsMine(CTxOut(Params().GetConsensus().pegged_asset, *input.m_peg_in_value, input.m_peg_in_claim_script))) continue; our_input_data[i] = std::make_tuple(*input.m_peg_in_value, Params().GetConsensus().pegged_asset, uint256(), uint256()); } else { - if (!InputIsMine(*this, COutPoint(input.prev_txid, *input.prev_out))) continue; + if (!InputIsMine(*this, COutPoint(Txid::FromUint256(input.prev_txid), *input.prev_out))) continue; const CWalletTx* wtx = GetWalletTx(input.prev_txid); if (!wtx) continue; CPubKey blinding_pubkey; @@ -4403,7 +4403,7 @@ uint256 CWalletTx::GetIssuanceBlindingFactor(const CWallet& wallet, unsigned int unsigned int mapValueInd = GetPseudoInputOffset(input_index, reissuance_token) + tx->vout.size(); uint256 ret; - CScript blindingScript(CScript() << OP_RETURN << std::vector(tx->vin[input_index].prevout.hash.begin(), tx->vin[input_index].prevout.hash.end()) << tx->vin[input_index].prevout.n); + CScript blindingScript(CScript() << OP_RETURN << std::vector(tx->vin[input_index].prevout.hash.ToUint256().begin(), tx->vin[input_index].prevout.hash.ToUint256().end()) << tx->vin[input_index].prevout.n); GetBlindingData(wallet, mapValueInd, rangeproof, reissuance_token ? issuance.nInflationKeys : issuance.nAmount, CConfidentialAsset(asset), CConfidentialNonce(), blindingScript, nullptr, nullptr, &ret, nullptr, nullptr); return ret; } @@ -4421,7 +4421,7 @@ CAmount CWalletTx::GetIssuanceAmount(const CWallet& wallet, unsigned int input_i const std::vector& rangeproof = wit.vtxinwit.size() <= input_index ? std::vector() : (reissuance_token ? wit.vtxinwit[input_index].vchInflationKeysRangeproof : wit.vtxinwit[input_index].vchIssuanceAmountRangeproof); CAmount ret; - CScript blindingScript(CScript() << OP_RETURN << std::vector(tx->vin[input_index].prevout.hash.begin(), tx->vin[input_index].prevout.hash.end()) << tx->vin[input_index].prevout.n); + CScript blindingScript(CScript() << OP_RETURN << std::vector(tx->vin[input_index].prevout.hash.ToUint256().begin(), tx->vin[input_index].prevout.hash.ToUint256().end()) << tx->vin[input_index].prevout.n); GetBlindingData(wallet, mapValueInd, rangeproof, reissuance_token ? issuance.nInflationKeys : issuance.nAmount, CConfidentialAsset(asset), CConfidentialNonce(), blindingScript, nullptr, &ret, nullptr, nullptr, nullptr); return ret; } diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 006b805cd4..260c84c0ef 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -1205,7 +1205,7 @@ static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vecto // Load locked utxo record LoadResult locked_utxo_res = LoadRecords(pwallet, batch, DBKeys::LOCKED_UTXO, [] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) { - uint256 hash; + Txid hash; uint32_t n; key >> hash; key >> n;