|
| 1 | +// Copyright (c) 2011-2024 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 <chainparams.h> |
| 6 | + |
| 7 | +#include <boost/test/unit_test.hpp> |
| 8 | + |
| 9 | +#include <util/strencodings.h> |
| 10 | + |
| 11 | +using namespace std::literals; |
| 12 | + |
| 13 | +BOOST_AUTO_TEST_SUITE(chainparams_tests) |
| 14 | + |
| 15 | +struct ParseWrappedSignetChallenge_TestCase |
| 16 | +{ |
| 17 | + std::string wrappedChallengeHex; |
| 18 | + std::string wantParamsHex; |
| 19 | + std::string wantChallengeHex; |
| 20 | + std::string wantError; |
| 21 | +}; |
| 22 | + |
| 23 | +BOOST_AUTO_TEST_CASE(parse_wrapped_signet_challenge) |
| 24 | +{ |
| 25 | + static const ParseWrappedSignetChallenge_TestCase cases[] = { |
| 26 | + { |
| 27 | + "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae", |
| 28 | + "", |
| 29 | + "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae", |
| 30 | + "", |
| 31 | + }, |
| 32 | + { |
| 33 | + "6a4c09011e000000000000004c25512102f7561d208dd9ae99bf497273e16f389bdbd6c4742ddb8e6b216e64fa2928ad8f51ae", |
| 34 | + "011e00000000000000", |
| 35 | + "512102f7561d208dd9ae99bf497273e16f389bdbd6c4742ddb8e6b216e64fa2928ad8f51ae", |
| 36 | + "", |
| 37 | + }, |
| 38 | + { |
| 39 | + "6a4c004c25512102f7561d208dd9ae99bf497273e16f389bdbd6c4742ddb8e6b216e64fa2928ad8f51ae", |
| 40 | + "", |
| 41 | + "512102f7561d208dd9ae99bf497273e16f389bdbd6c4742ddb8e6b216e64fa2928ad8f51ae", |
| 42 | + "", |
| 43 | + }, |
| 44 | + { |
| 45 | + "6a4c004c25512102f7561d208dd9ae99bf497273e16f389bdbd6c4742ddb8e6b216e64fa2928ad8f51ae4c00", |
| 46 | + "", |
| 47 | + "", |
| 48 | + "too many operations in wrapped challenge, must be 3.", |
| 49 | + }, |
| 50 | + { |
| 51 | + "6a4c09011e00000000000000", |
| 52 | + "", |
| 53 | + "", |
| 54 | + "too few operations in wrapped challenge, must be 3, got 2.", |
| 55 | + }, |
| 56 | + { |
| 57 | + "6a4c01", |
| 58 | + "", |
| 59 | + "", |
| 60 | + "failed to parse operation 1 in wrapped challenge script.", |
| 61 | + }, |
| 62 | + { |
| 63 | + "6a4c004c25512102f7561d208dd9ae99bf497273", |
| 64 | + "", |
| 65 | + "", |
| 66 | + "failed to parse operation 2 in wrapped challenge script.", |
| 67 | + }, |
| 68 | + { |
| 69 | + "6a6a4c25512102f7561d208dd9ae99bf497273e16f389bdbd6c4742ddb8e6b216e64fa2928ad8f51ae4c00", |
| 70 | + "", |
| 71 | + "", |
| 72 | + "operation 1 of wrapped challenge script must be a PUSHDATA opcode, got 0x6a.", |
| 73 | + }, |
| 74 | + { |
| 75 | + "6a4c09011e0000000000000051", |
| 76 | + "", |
| 77 | + "", |
| 78 | + "operation 2 of wrapped challenge script must be a PUSHDATA opcode, got 0x51.", |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + for (unsigned int i=0; i<std::size(cases); i++) |
| 83 | + { |
| 84 | + const auto wrappedChallenge = ParseHex(cases[i].wrappedChallengeHex); |
| 85 | + const auto wantParamsHex = cases[i].wantParamsHex; |
| 86 | + const auto wantChallengeHex = cases[i].wantChallengeHex; |
| 87 | + const auto wantError = cases[i].wantError; |
| 88 | + |
| 89 | + std::vector<uint8_t> gotParams; |
| 90 | + std::vector<uint8_t> gotChallenge; |
| 91 | + std::string gotError; |
| 92 | + try { |
| 93 | + ParseWrappedSignetChallenge(wrappedChallenge, gotParams, gotChallenge); |
| 94 | + } catch (const std::exception& e) { |
| 95 | + gotError = e.what(); |
| 96 | + } |
| 97 | + |
| 98 | + BOOST_CHECK_EQUAL(HexStr(gotParams), wantParamsHex); |
| 99 | + BOOST_CHECK_EQUAL(HexStr(gotChallenge), wantChallengeHex); |
| 100 | + BOOST_CHECK_EQUAL(gotError, wantError); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +struct ParseSignetParams_TestCase |
| 105 | +{ |
| 106 | + std::string paramsHex; |
| 107 | + int64_t wantPowTargetSpacing; |
| 108 | + std::string wantError; |
| 109 | +}; |
| 110 | + |
| 111 | +BOOST_AUTO_TEST_CASE(parse_signet_params) |
| 112 | +{ |
| 113 | + static const ParseSignetParams_TestCase cases[] = { |
| 114 | + { |
| 115 | + "", |
| 116 | + 600, |
| 117 | + "", |
| 118 | + }, |
| 119 | + { |
| 120 | + "011e00000000000000", |
| 121 | + 30, |
| 122 | + "", |
| 123 | + }, |
| 124 | + { |
| 125 | + "01e803000000000000", |
| 126 | + 1000, |
| 127 | + "", |
| 128 | + }, |
| 129 | + { |
| 130 | + "015802000000000000", |
| 131 | + 600, |
| 132 | + "", |
| 133 | + }, |
| 134 | + { |
| 135 | + "012502", |
| 136 | + 600, |
| 137 | + "signet params must have length 9, got 3.", |
| 138 | + }, |
| 139 | + { |
| 140 | + "022502000000000000", |
| 141 | + 600, |
| 142 | + "signet params[0] must be 0x01, got 0x02.", |
| 143 | + }, |
| 144 | + { |
| 145 | + "01ffffffffffffffff", |
| 146 | + 600, |
| 147 | + "signet param pow_target_spacing <= 0.", |
| 148 | + }, |
| 149 | + }; |
| 150 | + |
| 151 | + for (unsigned int i=0; i<std::size(cases); i++) |
| 152 | + { |
| 153 | + const auto params = ParseHex(cases[i].paramsHex); |
| 154 | + const auto wantPowTargetSpacing = cases[i].wantPowTargetSpacing; |
| 155 | + const auto wantError = cases[i].wantError; |
| 156 | + |
| 157 | + CChainParams::SigNetOptions gotOptions; |
| 158 | + std::string gotError; |
| 159 | + try { |
| 160 | + ParseSignetParams(params, gotOptions); |
| 161 | + } catch (const std::exception& e) { |
| 162 | + gotError = e.what(); |
| 163 | + } |
| 164 | + |
| 165 | + BOOST_CHECK_EQUAL(gotOptions.pow_target_spacing, wantPowTargetSpacing); |
| 166 | + BOOST_CHECK_EQUAL(gotError, wantError); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments