Branch data Line data Source code
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 : :
5 : : #include <wallet/transaction.h>
6 : :
7 : : #include <primitives/transaction.h>
8 : : #include <serialize.h>
9 : : #include <streams.h>
10 : : #include <test/util/common.h>
11 : : #include <wallet/test/wallet_test_fixture.h>
12 : :
13 : : #include <boost/test/unit_test.hpp>
14 : :
15 : : namespace wallet {
16 : : BOOST_FIXTURE_TEST_SUITE(wallet_transaction_tests, WalletTestingSetup)
17 : :
18 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(roundtrip)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
19 : : {
20 [ + + ]: 6 : for (uint8_t hash = 0; hash < 5; ++hash) {
21 [ + + ]: 30 : for (int index = -2; index < 3; ++index) {
22 : 25 : TxState state = TxStateInterpretSerialized(TxStateUnrecognized{uint256{hash}, index});
23 [ + - ]: 25 : BOOST_CHECK_EQUAL(TxStateSerializedBlockHash(state), uint256{hash});
24 [ + - ]: 25 : BOOST_CHECK_EQUAL(TxStateSerializedIndex(state), index);
25 : : }
26 : : }
27 : 1 : }
28 : :
29 [ + - + - : 7 : BOOST_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.
34 : 1 : CMutableTransaction mtx_a;
35 [ + - ]: 1 : mtx_a.vin.emplace_back(COutPoint{Txid::FromUint256(uint256::ONE), 0});
36 [ + - + - ]: 1 : mtx_a.vout.emplace_back(COIN, CScript() << OP_TRUE);
37 [ + - ]: 1 : CTransactionRef tx_a = MakeTransactionRef(std::move(mtx_a));
38 [ + - + - ]: 2 : CWalletTx wtx_a{tx_a, TxStateInactive{}};
39 : 1 : DataStream ss;
40 [ + - ]: 1 : ss << wtx_a;
41 : :
42 : : // Build tx_b with a different txid to use as a bogus variant.
43 [ + - ]: 1 : CMutableTransaction mtx_b;
44 [ + - + - ]: 1 : mtx_b.vout.emplace_back(2 * COIN, CScript() << OP_TRUE);
45 [ + - ]: 1 : CTransactionRef tx_b = MakeTransactionRef(std::move(mtx_b));
46 [ + - + - : 3 : BOOST_REQUIRE(tx_b->GetHash() != tx_a->GetHash());
+ - ]
47 : :
48 : : // A variant whose txid doesn't match the canonical txid must be rejected.
49 [ - + + + : 2 : std::map<Wtxid, CTransactionRef> bad_variants{{tx_b->GetWitnessHash(), tx_b}};
+ - - - -
- ]
50 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(CWalletTx(deserialize, ss, bad_variants), std::runtime_error, HasReason{"variant txid does not match wallet txid"});
- - - - -
+ + - + -
+ - ]
51 [ + - + - ]: 6 : }
52 : :
53 : : BOOST_AUTO_TEST_SUITE_END()
54 : : } // namespace wallet
|