diff options
author | stickies-v <stickies-v@protonmail.com> | 2024-07-26 15:01:48 +0100 |
---|---|---|
committer | stickies-v <stickies-v@protonmail.com> | 2024-08-23 13:53:40 +0100 |
commit | 8a44d7d3c1e5d5af6779c3e4befe514c9dafb8ff (patch) | |
tree | 5fb73a929e81cf3a9b8ac7f5c9300143ae9afdf1 | |
parent | 70e2c87737e77ee85812cc328c4ddfaea7147533 (diff) |
node: use uint256::FromUserHex for -minimumchainwork parsing
Removes dependency on unsafe and deprecated uint256S.
This makes parsing more strict, by returning an error
when the input contains more than 64 hex digits.
-rw-r--r-- | src/node/chainstatemanager_args.cpp | 7 | ||||
-rw-r--r-- | src/test/validation_chainstatemanager_tests.cpp | 3 | ||||
-rwxr-xr-x | test/functional/feature_minchainwork.py | 2 |
3 files changed, 7 insertions, 5 deletions
diff --git a/src/node/chainstatemanager_args.cpp b/src/node/chainstatemanager_args.cpp index 39b5f3ad3e..129d51c404 100644 --- a/src/node/chainstatemanager_args.cpp +++ b/src/node/chainstatemanager_args.cpp @@ -32,10 +32,11 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage if (auto value{args.GetBoolArg("-checkpoints")}) opts.checkpoints_enabled = *value; if (auto value{args.GetArg("-minimumchainwork")}) { - if (!IsHexNumber(*value)) { - return util::Error{strprintf(Untranslated("Invalid non-hex (%s) minimum chain work value specified"), *value)}; + if (auto min_work{uint256::FromUserHex(*value)}) { + opts.minimum_chain_work = UintToArith256(*min_work); + } else { + return util::Error{strprintf(Untranslated("Invalid minimum work specified (%s), must be up to %d hex digits"), *value, uint256::size() * 2)}; } - opts.minimum_chain_work = UintToArith256(uint256S(*value)); } if (auto value{args.GetArg("-assumevalid")}) opts.assumed_valid_block = uint256S(*value); diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp index d6e4c6a340..052f5cd1a7 100644 --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -820,7 +820,8 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_args, BasicTestingSetup) BOOST_CHECK_EQUAL(get_valid_opts({"-nominimumchainwork"}).minimum_chain_work.value().GetCompact(), 0U); BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0x1234"}).minimum_chain_work.value().GetCompact(), 0x02123400U); - BOOST_CHECK(!get_opts({"-minimumchainwork=xyz"})); // invalid hex characters + BOOST_CHECK(!get_opts({"-minimumchainwork=xyz"})); // invalid hex characters + BOOST_CHECK(!get_opts({"-minimumchainwork=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/functional/feature_minchainwork.py b/test/functional/feature_minchainwork.py index 8327a0477b..34228f6f38 100755 --- a/test/functional/feature_minchainwork.py +++ b/test/functional/feature_minchainwork.py @@ -110,7 +110,7 @@ class MinimumChainWorkTest(BitcoinTestFramework): self.stop_node(0) self.nodes[0].assert_start_raises_init_error( ["-minimumchainwork=test"], - expected_msg='Error: Invalid non-hex (test) minimum chain work value specified', + expected_msg='Error: Invalid minimum work specified (test), must be up to 64 hex digits', ) |