Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
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) {
22 TxState state = TxStateInterpretSerialized(TxStateUnrecognized{uint256{hash}, index});
23 BOOST_CHECK_EQUAL(TxStateSerializedBlockHash(state), uint256{hash});
24 BOOST_CHECK_EQUAL(TxStateSerializedIndex(state), 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{}};
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 BOOST_CHECK_EXCEPTION(CWalletTx(deserialize, ss, bad_variants), std::runtime_error, HasReason{"variant txid does not match wallet txid"});
51}
52
54} // namespace wallet
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:30
CScript()=default
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:165
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition common.h:19
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.
CWalletTx(CTransactionRef tx, const TxState &state)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EXCEPTION(req.LoadBody(reader), std::runtime_error, HasReason{"Cannot parse Content-Length value"})
BOOST_AUTO_TEST_CASE(bnb_test)
std::variant< TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
Definition transaction.h:81
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:17
std::shared_ptr< const CTransaction > CTransactionRef
@ OP_TRUE
Definition script.h:85
constexpr deserialize_type deserialize
Definition serialize.h:52
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< CTxIn > vin
Testing setup and teardown for wallet.
DataStream ss