|
5 | 5 | #include <chainparams.h>
|
6 | 6 | #include <consensus/validation.h>
|
7 | 7 | #include <kernel/disconnected_transactions.h>
|
| 8 | +#include <node/chainstatemanager_args.h> |
8 | 9 | #include <node/kernel_notifications.h>
|
9 | 10 | #include <node/utxo_snapshot.h>
|
10 | 11 | #include <random.h>
|
|
16 | 17 | #include <test/util/setup_common.h>
|
17 | 18 | #include <test/util/validation.h>
|
18 | 19 | #include <uint256.h>
|
| 20 | +#include <util/result.h> |
| 21 | +#include <util/vector.h> |
19 | 22 | #include <validation.h>
|
20 | 23 | #include <validationinterface.h>
|
21 | 24 |
|
@@ -769,4 +772,55 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, Sna
|
769 | 772 | }
|
770 | 773 | }
|
771 | 774 |
|
| 775 | +/** Helper function to parse args into args_man and return the result of applying them to opts */ |
| 776 | +template <typename Options> |
| 777 | +util::Result<Options> SetOptsFromArgs(ArgsManager& args_man, Options opts, |
| 778 | + const std::vector<const char*>& args) |
| 779 | +{ |
| 780 | + const auto argv{Cat({"ignore"}, args)}; |
| 781 | + std::string error{}; |
| 782 | + if (!args_man.ParseParameters(argv.size(), argv.data(), error)) { |
| 783 | + return util::Error{Untranslated("ParseParameters failed with error: " + error)}; |
| 784 | + } |
| 785 | + const auto result{node::ApplyArgsManOptions(args_man, opts)}; |
| 786 | + if (!result) return util::Error{util::ErrorString(result)}; |
| 787 | + return opts; |
| 788 | +} |
| 789 | + |
| 790 | +BOOST_FIXTURE_TEST_CASE(chainstatemanager_args, BasicTestingSetup) |
| 791 | +{ |
| 792 | + //! Try to apply the provided args to a ChainstateManager::Options |
| 793 | + auto get_opts = [&](const std::vector<const char*>& args) { |
| 794 | + static kernel::Notifications notifications{}; |
| 795 | + static const ChainstateManager::Options options{ |
| 796 | + .chainparams = ::Params(), |
| 797 | + .datadir = {}, |
| 798 | + .notifications = notifications}; |
| 799 | + return SetOptsFromArgs(*this->m_node.args, options, args); |
| 800 | + }; |
| 801 | + //! Like get_opts, but requires the provided args to be valid and unwraps the result |
| 802 | + auto get_valid_opts = [&](const std::vector<const char*>& args) { |
| 803 | + const auto result{get_opts(args)}; |
| 804 | + BOOST_REQUIRE_MESSAGE(result, util::ErrorString(result).original); |
| 805 | + return *result; |
| 806 | + }; |
| 807 | + |
| 808 | + // test -assumevalid |
| 809 | + BOOST_CHECK(!get_valid_opts({}).assumed_valid_block.has_value()); |
| 810 | + BOOST_CHECK(get_valid_opts({"-assumevalid="}).assumed_valid_block.value().IsNull()); |
| 811 | + BOOST_CHECK(get_valid_opts({"-assumevalid=0"}).assumed_valid_block.value().IsNull()); |
| 812 | + BOOST_CHECK(get_valid_opts({"-noassumevalid"}).assumed_valid_block.value().IsNull()); |
| 813 | + BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0x1234"}).assumed_valid_block.value().ToString(), std::string(60, '0') + "1234"); |
| 814 | + const std::string cmd{"-assumevalid=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}; |
| 815 | + BOOST_CHECK_EQUAL(get_valid_opts({cmd.c_str()}).assumed_valid_block.value().ToString(), cmd.substr(13, cmd.size())); |
| 816 | + |
| 817 | + // test -minimumchainwork |
| 818 | + BOOST_CHECK(!get_valid_opts({}).minimum_chain_work.has_value()); |
| 819 | + BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0"}).minimum_chain_work.value().GetCompact(), 0U); |
| 820 | + BOOST_CHECK_EQUAL(get_valid_opts({"-nominimumchainwork"}).minimum_chain_work.value().GetCompact(), 0U); |
| 821 | + BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0x1234"}).minimum_chain_work.value().GetCompact(), 0x02123400U); |
| 822 | + |
| 823 | + BOOST_CHECK(!get_opts({"-minimumchainwork=xyz"})); // invalid hex characters |
| 824 | +} |
| 825 | + |
772 | 826 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments