Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
wallet
test
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
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
BOOST_AUTO_TEST_CASE
(roundtrip)
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
29
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
CMutableTransaction
mtx_a;
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.
43
CMutableTransaction
mtx_b;
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
53
BOOST_AUTO_TEST_SUITE_END
()
54
}
// namespace wallet
COIN
constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition
amount.h:15
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition
transaction.h:30
CScript::CScript
CScript()=default
DataStream
Double ended buffer combining vector and stream-like interfaces.
Definition
streams.h:165
HasReason
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition
common.h:19
transaction_identifier< false >::FromUint256
static transaction_identifier FromUint256(const uint256 &id)
Definition
transaction_identifier.h:38
uint256
256-bit opaque blob.
Definition
uint256.h:196
uint256::ONE
static const uint256 ONE
Definition
uint256.h:205
wallet::CWalletTx
A transaction with a bunch of additional info that only the owner cares about.
Definition
transaction.h:194
wallet::CWalletTx::CWalletTx
CWalletTx(CTransactionRef tx, const TxState &state)
Definition
transaction.h:239
BOOST_FIXTURE_TEST_SUITE
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EXCEPTION
BOOST_CHECK_EXCEPTION(req.LoadBody(reader), std::runtime_error, HasReason{"Cannot parse Content-Length value"})
wallet
Definition
coin_selection.cpp:31
wallet::BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(bnb_test)
Definition
coinselection_tests.cpp:141
wallet::TxState
std::variant< TxStateConfirmed, TxStateInMempool, TxStateBlockConflicted, TxStateInactive, TxStateUnrecognized > TxState
All possible CWalletTx states.
Definition
transaction.h:81
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition
object.cpp:17
transaction.h
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition
transaction.h:417
OP_TRUE
@ OP_TRUE
Definition
script.h:85
serialize.h
deserialize
constexpr deserialize_type deserialize
Definition
serialize.h:52
streams.h
CMutableTransaction
A mutable version of CTransaction.
Definition
transaction.h:372
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition
transaction.h:374
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition
transaction.h:373
wallet::TxStateInactive
Definition
transaction.h:61
wallet::TxStateUnrecognized
Definition
transaction.h:72
wallet::WalletTestingSetup
Testing setup and teardown for wallet.
Definition
wallet_test_fixture.h:22
common.h
ss
DataStream ss
Definition
uint256_tests.cpp:181
transaction.h
wallet_test_fixture.h
Generated on
for Bitcoin Core by
1.18.0