Skip to content

Commit 6819e5a

Browse files
committed
node: use uint256::FromUserHex for -assumevalid parsing
Removes dependency on unsafe and deprecated uint256S. This makes parsing more strict, by returning an error when the input contains non-hex characters, or when it contains more than 64 hex digits. Also make feature_assumevalid.py more robust by using CBlock.hash which is guaranteed to be 64 characters long, as opposed to the variable-length hex(CBlock.sha256)
1 parent 2e58fdb commit 6819e5a

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/node/chainstatemanager_args.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
3939
}
4040
}
4141

42-
if (auto value{args.GetArg("-assumevalid")}) opts.assumed_valid_block = uint256S(*value);
42+
if (auto value{args.GetArg("-assumevalid")}) {
43+
if (auto block_hash{uint256::FromUserHex(*value)}) {
44+
opts.assumed_valid_block = *block_hash;
45+
} else {
46+
return util::Error{strprintf(Untranslated("Invalid assumevalid block hash specified (%s), must be up to %d hex digits (or 0 to disable)"), *value, uint256::size() * 2)};
47+
}
48+
}
4349

4450
if (auto value{args.GetIntArg("-maxtipage")}) opts.max_tip_age = std::chrono::seconds{*value};
4551

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,9 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_args, BasicTestingSetup)
814814
const std::string cmd{"-assumevalid=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
815815
BOOST_CHECK_EQUAL(get_valid_opts({cmd.c_str()}).assumed_valid_block.value().ToString(), cmd.substr(13, cmd.size()));
816816

817+
BOOST_CHECK(!get_opts({"-assumevalid=xyz"})); // invalid hex characters
818+
BOOST_CHECK(!get_opts({"-assumevalid=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
819+
817820
// test -minimumchainwork
818821
BOOST_CHECK(!get_valid_opts({}).minimum_chain_work.has_value());
819822
BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0"}).minimum_chain_work.value().GetCompact(), 0U);

test/functional/feature_assumevalid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def run_test(self):
139139
height += 1
140140

141141
# Start node1 and node2 with assumevalid so they accept a block with a bad signature.
142-
self.start_node(1, extra_args=["-assumevalid=" + hex(block102.sha256)])
143-
self.start_node(2, extra_args=["-assumevalid=" + hex(block102.sha256)])
142+
self.start_node(1, extra_args=["-assumevalid=" + block102.hash])
143+
self.start_node(2, extra_args=["-assumevalid=" + block102.hash])
144144

145145
p2p0 = self.nodes[0].add_p2p_connection(BaseNode())
146146
p2p0.send_header_for_blocks(self.blocks[0:2000])

0 commit comments

Comments
 (0)