|
| 1 | +// Copyright (c) 2025-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 | +#include <arith_uint256.h> |
| 5 | +#include <coins.h> |
| 6 | +#include <consensus/amount.h> |
| 7 | +#include <key.h> |
| 8 | +#include <primitives/transaction.h> |
| 9 | +#include <pubkey.h> |
| 10 | +#include <random.h> |
| 11 | +#include <script/script.h> |
| 12 | +#include <serialize.h> |
| 13 | +#include <streams.h> |
| 14 | +#include <test/util/setup_common.h> |
| 15 | +#include <undo.h> |
| 16 | + |
| 17 | +#include <boost/test/unit_test.hpp> |
| 18 | + |
| 19 | +namespace { |
| 20 | +CAmount amt_1{111}; |
| 21 | +CAmount amt_2{4321}; |
| 22 | +CAmount amt_3{12345}; |
| 23 | +CAmount amt_4{94949}; |
| 24 | +CAmount amt_5{5222322}; |
| 25 | +CAmount amt_6{34112}; |
| 26 | +int height_1{3}; |
| 27 | +int height_2{424002}; |
| 28 | +int height_3{2244002}; |
| 29 | +int height_4{983999}; |
| 30 | +int height_5{2455}; |
| 31 | +int height_6{20}; |
| 32 | +} // namespace |
| 33 | + |
| 34 | +BOOST_FIXTURE_TEST_SUITE(undo_tests, BasicTestingSetup) |
| 35 | + |
| 36 | +BOOST_AUTO_TEST_CASE(network_undo_serialization) |
| 37 | +{ |
| 38 | + // case one |
| 39 | + CPubKey key_1 = GenerateRandomKey().GetPubKey(); |
| 40 | + CScript script_1 = CScript() << OP_DUP << OP_HASH160 << ToByteVector(key_1.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 41 | + Coin coin_1{ |
| 42 | + CTxOut{amt_1, script_1}, |
| 43 | + height_1, |
| 44 | + false, |
| 45 | + }; |
| 46 | + // case two |
| 47 | + CScript redeem{}; |
| 48 | + CScript script_2 = CScript() << OP_HASH160 << ToByteVector(CScriptID(redeem)) << OP_EQUAL; |
| 49 | + Coin coin_2{ |
| 50 | + CTxOut{amt_2, script_2}, |
| 51 | + height_2, |
| 52 | + false, |
| 53 | + }; |
| 54 | + // case three |
| 55 | + CKey key_2 = GenerateRandomKey(); |
| 56 | + CScript script_3 = CScript() << ToByteVector(key_2.GetPubKey()) << OP_CHECKSIG; |
| 57 | + Coin coin_3{ |
| 58 | + CTxOut{amt_3, script_3}, |
| 59 | + height_3, |
| 60 | + false, |
| 61 | + }; |
| 62 | + // case four |
| 63 | + CKey key_3 = GenerateRandomKey(/*compressed=*/false); |
| 64 | + CScript script_4 = CScript() << ToByteVector(key_3.GetPubKey()) << OP_CHECKSIG; |
| 65 | + Coin coin_4{ |
| 66 | + CTxOut{amt_4, script_4}, |
| 67 | + height_4, |
| 68 | + false, |
| 69 | + }; |
| 70 | + CKey key_4 = GenerateRandomKey(); |
| 71 | + XOnlyPubKey x_only{key_4.GetPubKey()}; |
| 72 | + CScript script_5 = CScript() << OP_1 << 0x20 << ToByteVector(x_only); |
| 73 | + Coin coin_5{ |
| 74 | + CTxOut{amt_5, script_5}, |
| 75 | + height_5, |
| 76 | + false, |
| 77 | + }; |
| 78 | + CPubKey key_5 = GenerateRandomKey().GetPubKey(); |
| 79 | + CScript script_6 = CScript() << OP_DUP << OP_HASH160 << ToByteVector(key_5.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 80 | + Coin coin_6{ |
| 81 | + CTxOut{amt_6, script_6}, |
| 82 | + height_6, |
| 83 | + true, |
| 84 | + }; |
| 85 | + |
| 86 | + CTxUndo undo_1{std::vector{coin_1, coin_2}}; |
| 87 | + CTxUndo undo_2{std::vector{coin_3, coin_4}}; |
| 88 | + CTxUndo undo_3{std::vector{coin_5, coin_6}}; |
| 89 | + CBlockUndo undo{std::vector{undo_1, undo_2, undo_3}}; |
| 90 | + uint256 block_hash{GetRandHash()}; |
| 91 | + NetworkBlockUndo want{block_hash, undo}; |
| 92 | + BOOST_CHECK(want.m_coins.size() > 0); |
| 93 | + DataStream undo_byte_stream{}; |
| 94 | + Serialize(undo_byte_stream, want); |
| 95 | + BOOST_CHECK(undo_byte_stream.size() > 0); |
| 96 | + NetworkBlockUndo got; |
| 97 | + Unserialize(undo_byte_stream, got); |
| 98 | + BOOST_CHECK_EQUAL(want.m_coins.size(), got.m_coins.size()); |
| 99 | + for (size_t i = 0; i < want.m_coins.size(); ++i) { |
| 100 | + const auto& coin_1 = want.m_coins[i]; |
| 101 | + const auto& coin_2 = got.m_coins[i]; |
| 102 | + BOOST_CHECK_EQUAL(coin_1.fCoinBase, coin_2.fCoinBase); |
| 103 | + BOOST_CHECK_EQUAL(coin_1.nHeight, coin_2.nHeight); |
| 104 | + BOOST_CHECK_EQUAL(coin_1.out.nValue, coin_2.out.nValue); |
| 105 | + const auto sct_1{coin_1.out.scriptPubKey}; |
| 106 | + const auto sct_2{coin_2.out.scriptPubKey}; |
| 107 | + BOOST_CHECK(sct_1 == sct_2); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +BOOST_AUTO_TEST_SUITE_END(); |
0 commit comments