forked from dtr-org/unit-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_verify_tests.cpp
More file actions
158 lines (122 loc) · 5.23 KB
/
Copy pathtx_verify_tests.cpp
File metadata and controls
158 lines (122 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2018-2019 The Unit-e developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <proposer/block_builder.h>
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
#include <test/test_unite.h>
#include <test/test_unite_mocks.h>
#include <boost/test/unit_test.hpp>
#include <algorithm>
BOOST_AUTO_TEST_SUITE(tx_verify_tests)
BOOST_AUTO_TEST_CASE(check_tx_inputs_no_haz_coins) {
mocks::CoinsViewMock utxos;
utxos.default_have_inputs = false;
CTransaction tx;
CValidationState validation_state;
CAmount fees;
const bool result = Consensus::CheckTxInputs(tx, validation_state, utxos, 2, fees);
BOOST_CHECK(!result);
BOOST_CHECK(!validation_state.IsValid());
BOOST_CHECK_EQUAL(validation_state.GetRejectCode(), REJECT_INVALID);
BOOST_CHECK_EQUAL(validation_state.GetRejectReason(), "bad-txns-inputs-missingorspent");
}
BOOST_AUTO_TEST_CASE(check_tx_inputs_no_reward) {
const CTxIn meta_input;
BOOST_REQUIRE(meta_input.prevout.IsNull());
const uint256 some_txid = uint256S("4623a9438473459c466ea4fe87b5a614362e08c47454cf59646e49c5759cb60d");
const CTxIn staking_input(some_txid, 0);
CTransaction tx = [&] {
CMutableTransaction tx;
tx.SetType(TxType::COINBASE);
tx.vin = {meta_input, staking_input};
return tx;
}();
mocks::CoinsViewMock utxos;
utxos.default_coin = Coin(CTxOut(21, CScript()), 1, TxType::REGULAR);
CValidationState validation_state;
CAmount fees;
const bool result = Consensus::CheckTxInputs(tx, validation_state, utxos, 2, fees);
BOOST_CHECK(!result);
BOOST_CHECK(!validation_state.IsValid());
BOOST_CHECK_EQUAL(validation_state.GetRejectCode(), REJECT_INVALID);
BOOST_CHECK_EQUAL(validation_state.GetRejectReason(), "bad-cb-no-reward");
}
BOOST_AUTO_TEST_CASE(check_tx_inputs_does_not_access_coinbase_meta_input) {
const CTxIn meta_input;
BOOST_REQUIRE(meta_input.prevout.IsNull());
const uint256 some_txid = uint256S("4623a9438473459c466ea4fe87b5a614362e08c47454cf59646e49c5759cb60d");
const CTxIn staking_input(some_txid, 0);
CTransaction tx = [&] {
CMutableTransaction tx;
tx.SetType(TxType::COINBASE);
tx.vin = {meta_input, staking_input};
tx.vout = {CTxOut(21, CScript())};
return tx;
}();
std::vector<COutPoint> coins_accessed;
mocks::CoinsViewMock utxos;
utxos.default_coin = Coin(CTxOut(21, CScript()), 1, TxType::REGULAR);
utxos.access_coin = [&](const COutPoint &coin) -> const Coin & {
coins_accessed.emplace_back(coin);
return utxos.default_coin;
};
CValidationState validation_state;
CAmount fees;
Consensus::CheckTxInputs(tx, validation_state, utxos, 2, fees);
// check vin[0] was not tried to be retrieved from coins view
BOOST_CHECK(std::find(coins_accessed.begin(), coins_accessed.end(), meta_input.prevout) == coins_accessed.end());
// check vin[1] was tried to be retrieved from coins view
BOOST_CHECK(std::find(coins_accessed.begin(), coins_accessed.end(), staking_input.prevout) != coins_accessed.end());
}
BOOST_AUTO_TEST_CASE(check_tx_inputs_rejects_coinbase_that_spends_too_little) {
const CTxIn meta_input;
BOOST_REQUIRE(meta_input.prevout.IsNull());
const uint256 some_txid = uint256S("4623a9438473459c466ea4fe87b5a614362e08c47454cf59646e49c5759cb60d");
const CTxIn staking_input(some_txid, 0);
const CAmount reward = 21;
const CAmount stake_in = 19;
const CAmount stake_out = stake_in - 1;
CTransaction tx = [&] {
CMutableTransaction tx;
tx.SetType(TxType::COINBASE);
tx.vin = {meta_input, staking_input};
tx.vout = {CTxOut(reward, CScript()), CTxOut(stake_out, CScript())};
return tx;
}();
mocks::CoinsViewMock utxos;
utxos.default_coin = Coin(CTxOut(stake_in, CScript()), 1, TxType::REGULAR);
CValidationState validation_state;
CAmount fees;
const bool result = Consensus::CheckTxInputs(tx, validation_state, utxos, 2, fees);
BOOST_CHECK(!result);
BOOST_CHECK(!validation_state.IsValid());
BOOST_CHECK_EQUAL(validation_state.GetRejectCode(), REJECT_INVALID);
BOOST_CHECK_EQUAL(validation_state.GetRejectReason(), "bad-cb-spends-too-little");
}
BOOST_AUTO_TEST_CASE(check_tx_inputs_rejects_coinbase_that_spends_too_much) {
const CTxIn meta_input;
BOOST_REQUIRE(meta_input.prevout.IsNull());
const uint256 some_txid = uint256S("4623a9438473459c466ea4fe87b5a614362e08c47454cf59646e49c5759cb60d");
const CTxIn staking_input(some_txid, 0);
const CAmount reward = 21;
const CAmount stake_in = 19;
const CAmount stake_out = stake_in + 1;
CTransaction tx = [&] {
CMutableTransaction tx;
tx.SetType(TxType::COINBASE);
tx.vin = {meta_input, staking_input};
tx.vout = {CTxOut(reward, CScript()), CTxOut(stake_out, CScript())};
return tx;
}();
mocks::CoinsViewMock utxos;
utxos.default_coin = Coin(CTxOut(stake_in, CScript()), 1, TxType::REGULAR);
CValidationState validation_state;
CAmount fees;
const bool result = Consensus::CheckTxInputs(tx, validation_state, utxos, 2, fees);
BOOST_CHECK(!result);
BOOST_CHECK(!validation_state.IsValid());
BOOST_CHECK_EQUAL(validation_state.GetRejectCode(), REJECT_INVALID);
BOOST_CHECK_EQUAL(validation_state.GetRejectReason(), "bad-cb-spends-too-much");
}
BOOST_AUTO_TEST_SUITE_END()