Bitcoin Core 31.99.0
P2P Digital Currency
wallet_transaction_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6
8#include <serialize.h>
9#include <streams.h>
10#include <test/util/common.h>
12
13#include <boost/test/unit_test.hpp>
14
15namespace wallet {
16BOOST_FIXTURE_TEST_SUITE(wallet_transaction_tests, WalletTestingSetup)
17
19{
20 for (uint8_t hash = 0; hash < 5; ++hash) {
21 for (int index = -2; index < 3; ++index) {
25 }
26 }
27}
28
29BOOST_AUTO_TEST_CASE(deserialize_rejects_mismatched_variant_txid)
30{
31 // Build tx_a and serialise it as a CWalletTx.
32 // Needs at least one input: a zero-input tx serialises vin_count as 0x00,
33 // which the witness-aware deserialiser misreads as the segwit marker byte.
35 mtx_a.vin.emplace_back(COutPoint{Txid::FromUint256(uint256::ONE), 0});
36 mtx_a.vout.emplace_back(COIN, CScript() << OP_TRUE);
37 CTransactionRef tx_a = MakeTransactionRef(std::move(mtx_a));
38 CWalletTx wtx_a{tx_a, TxStateInactive{}};
39 DataStream ss;
40 ss << wtx_a;
41
42 // Build tx_b with a different txid to use as a bogus variant.
44 mtx_b.vout.emplace_back(2 * COIN, CScript() << OP_TRUE);
45 CTransactionRef tx_b = MakeTransactionRef(std::move(mtx_b));
46 BOOST_REQUIRE(tx_b->GetHash() != tx_a->GetHash());
47
48 // A variant whose txid doesn't match the canonical txid must be rejected.
49 std::map<Wtxid, CTransactionRef> bad_variants{{tx_b->GetWitnessHash(), tx_b}};
50 try {
51 CWalletTx(deserialize, ss, bad_variants);
52 BOOST_FAIL("expected std::runtime_error was not thrown");
53 } catch (const std::runtime_error& e) {
54 BOOST_CHECK_EQUAL(std::string(e.what()), "variant txid does not match wallet txid");
55 }
56}
57
59} // namespace wallet
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition: uint256.h:196
static const uint256 ONE
Definition: uint256.h:205
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:192
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
std::variant< TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
Definition: transaction.h:79
static int TxStateSerializedIndex(const TxState &state)
Get TxState serialized block index. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:112
static TxState TxStateInterpretSerialized(TxStateUnrecognized data)
Try to interpret deserialized TxStateUnrecognized data as a recognized state.
Definition: transaction.h:85
BOOST_AUTO_TEST_CASE(bnb_test)
static uint256 TxStateSerializedBlockHash(const TxState &state)
Get TxState serialized block hash. Inverse of TxStateInterpretSerialized.
Definition: transaction.h:100
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
@ OP_TRUE
Definition: script.h:85
constexpr deserialize_type deserialize
Definition: serialize.h:52
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxOut > vout
Definition: transaction.h:360
std::vector< CTxIn > vin
Definition: transaction.h:359
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition: transaction.h:59
State of transaction loaded in an unrecognized state with unexpected hash or index values.
Definition: transaction.h:70