Skip to content

Commit

Permalink
Merge pull request #1474 from evoskuil/master
Browse files Browse the repository at this point in the history
Add settings::sorted_checkpoints() and ::top_checkpoint().
  • Loading branch information
evoskuil authored Jun 2, 2024
2 parents a1b8485 + 2ad9e63 commit 2905a16
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 21 deletions.
4 changes: 4 additions & 0 deletions include/bitcoin/system/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class BC_API settings
virtual uint64_t initial_subsidy() const NOEXCEPT;
virtual uint64_t bitcoin_to_satoshi(uint64_t value) const NOEXCEPT;

/// These are used by node, top checkpoint defaults to genesis.
virtual chain::checkpoints sorted_checkpoints() const NOEXCEPT;
virtual chain::checkpoint top_checkpoint() const NOEXCEPT;

/// These are used by chain_state (only).
virtual uint32_t minimum_timespan() const NOEXCEPT;
virtual uint32_t maximum_timespan() const NOEXCEPT;
Expand Down
20 changes: 19 additions & 1 deletion src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,26 @@ uint64_t settings::bitcoin_to_satoshi(uint64_t value) const NOEXCEPT
BC_POP_WARNING()
}

// These are used internal to system.
// Computed properties.
// ----------------------------------------------------------------------------
// These are used by node.

chain::checkpoints settings::sorted_checkpoints() const NOEXCEPT
{
return sort_copy(checkpoints);
}

chain::checkpoint settings::top_checkpoint() const NOEXCEPT
{
if (checkpoints.empty())
return { genesis_block.hash(), zero };

return sorted_checkpoints().back();
}

// Computed properties.
// ----------------------------------------------------------------------------
// These are used internal to system.

// The lower bound for the retargeting timespan.
uint32_t settings::minimum_timespan() const NOEXCEPT
Expand Down
74 changes: 54 additions & 20 deletions test/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@

BOOST_AUTO_TEST_SUITE(settings_tests)

const chain::checkpoint::list mainnet_checkpoints
{
{ "00000000000000004d9b4ef50f0f9d686fd69db2e03af35a100370c64632a983", 295000 },
{ "0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40", 279000 },
{ "000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214", 250000 },
{ "00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932", 225430 },
{ "00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e", 216116 },
{ "000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e", 210000 },
{ "000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317", 193000 },
{ "000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763", 168000 },
{ "00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe", 134444 },
{ "000000000000774a7f8a7a12dc906ddb9e17e75d684f15e00f8767f9e8f36553", 118000 },
{ "00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97", 105000 },
{ "0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20", 74000 },
{ "000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6", 33333 },
{ "0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d", 11111 }
};

// constructors
// ----------------------------------------------------------------------------

Expand All @@ -45,24 +63,6 @@ BOOST_AUTO_TEST_CASE(settings__construct__default_context__expected)

BOOST_AUTO_TEST_CASE(settings__construct__mainnet_context__expected)
{
const chain::checkpoint::list checkpoints
{
{ "00000000000000004d9b4ef50f0f9d686fd69db2e03af35a100370c64632a983", 295000 },
{ "0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40", 279000 },
{ "000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214", 250000 },
{ "00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932", 225430 },
{ "00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e", 216116 },
{ "000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e", 210000 },
{ "000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317", 193000 },
{ "000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763", 168000 },
{ "00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe", 134444 },
{ "000000000000774a7f8a7a12dc906ddb9e17e75d684f15e00f8767f9e8f36553", 118000 },
{ "00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97", 105000 },
{ "0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20", 74000 },
{ "000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6", 33333 },
{ "0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d", 11111 }
};

settings configuration(chain::selection::mainnet);
BOOST_REQUIRE_EQUAL(configuration.block_spacing_seconds, 600u);
BOOST_REQUIRE_EQUAL(configuration.timestamp_limit_seconds, 7200u);
Expand Down Expand Up @@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(settings__construct__mainnet_context__expected)
BOOST_REQUIRE_EQUAL(configuration.subsidy_interval_blocks, 210000u);
BOOST_REQUIRE_EQUAL(configuration.bitcoin_to_satoshi(1), 100000000u);
BOOST_REQUIRE_EQUAL(configuration.max_money(), 2099999997690000u);
BOOST_REQUIRE_EQUAL(configuration.checkpoints, checkpoints);
BOOST_REQUIRE_EQUAL(configuration.checkpoints, mainnet_checkpoints);
BOOST_REQUIRE_EQUAL(configuration.minimum_work, to_uintx(base16_hash("000000000000000000000000000000000000000052b2559353df4117b7348b64")));
const chain::checkpoint milestone("00000000000000000001a0a448d6cf2546b06801389cc030b2b18c6491266815", 804000u);
BOOST_REQUIRE_EQUAL(configuration.milestone, milestone);
Expand Down Expand Up @@ -181,7 +181,7 @@ BOOST_AUTO_TEST_CASE(settings__construct__regtest_context__expected)
BOOST_REQUIRE_EQUAL(configuration.milestone, genesis);
}

// setter methods
// methods
// ----------------------------------------------------------------------------

BOOST_AUTO_TEST_CASE(settings__initial_block_subsidy_bitcoin__set_double_value__max_money_doubled)
Expand Down Expand Up @@ -235,4 +235,38 @@ BOOST_AUTO_TEST_CASE(settings__block_spacing_seconds__set_double_value__retarget
BOOST_REQUIRE_EQUAL(configuration.retargeting_interval(), half_retargeting_interval);
}

BOOST_AUTO_TEST_CASE(settings__sorted_checkpoints__testnet_empty__empty)
{
settings configuration(chain::selection::testnet);
configuration.checkpoints.clear();
BOOST_REQUIRE(configuration.sorted_checkpoints().empty());
}

BOOST_AUTO_TEST_CASE(settings__sorted_checkpoints__mainnet_default_expected)
{
const settings configuration(chain::selection::mainnet);
BOOST_REQUIRE_EQUAL(configuration.checkpoints, mainnet_checkpoints);
BOOST_REQUIRE_EQUAL(configuration.sorted_checkpoints().back(), mainnet_checkpoints.front());
BOOST_REQUIRE_EQUAL(configuration.sorted_checkpoints().back().height(), 295000_size);
BOOST_REQUIRE_EQUAL(configuration.sorted_checkpoints().front(), mainnet_checkpoints.back());
BOOST_REQUIRE_EQUAL(configuration.sorted_checkpoints().front().height(), 11111_size);
}

BOOST_AUTO_TEST_CASE(settings__top_checkpoint__testnet_empty__genesis)
{
settings configuration(chain::selection::testnet);
configuration.checkpoints.clear();
const chain::checkpoint genesis{ configuration.genesis_block.hash(), zero };
BOOST_REQUIRE_EQUAL(configuration.top_checkpoint(), genesis);
}

BOOST_AUTO_TEST_CASE(settings__sorted_checkpoint__mainnet_default_expected)
{
const settings configuration(chain::selection::mainnet);
BOOST_REQUIRE_EQUAL(configuration.checkpoints, mainnet_checkpoints);
BOOST_REQUIRE_EQUAL(configuration.sorted_checkpoints().back(), mainnet_checkpoints.front());
BOOST_REQUIRE_EQUAL(configuration.sorted_checkpoints().back().height(), 295000_size);
BOOST_REQUIRE_EQUAL(configuration.top_checkpoint(), mainnet_checkpoints.front());
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 2905a16

Please sign in to comment.