diff --git a/examples/main.cpp b/examples/main.cpp index 759db38c1dc..9f891004a1c 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -45,7 +45,8 @@ int bc::main(int argc, char* argv[]) #endif // Extracting Satoshi's words from genesis block. - const auto block = bc::chain::block::genesis_mainnet(bc::settings()); + const auto block = bc::settings(bc::config::settings::mainnet) + .genesis_block; const auto& coinbase = block.transactions().front(); const auto& input = coinbase.inputs().front(); BITCOIN_ASSERT_MSG(input.script().size() > 2u, "unexpected genesis"); diff --git a/include/bitcoin/bitcoin/chain/block.hpp b/include/bitcoin/bitcoin/chain/block.hpp index 0acaec9bcdf..87ad9b5d934 100644 --- a/include/bitcoin/bitcoin/chain/block.hpp +++ b/include/bitcoin/bitcoin/chain/block.hpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -40,6 +39,12 @@ #include namespace libbitcoin { + +/** + * Forward declaration to break header cycle. + */ +class settings; + namespace chain { class BC_API block @@ -128,9 +133,6 @@ class BC_API block // Utilities. //------------------------------------------------------------------------- - static block genesis_mainnet(const settings& settings); - static block genesis_testnet(const settings& settings); - static block genesis_regtest(const settings& settings); static size_t locator_size(size_t top); static indexes locator_heights(size_t top); diff --git a/include/bitcoin/bitcoin/chain/chain_state.hpp b/include/bitcoin/bitcoin/chain/chain_state.hpp index a598ade86e1..3b044a62737 100644 --- a/include/bitcoin/bitcoin/chain/chain_state.hpp +++ b/include/bitcoin/bitcoin/chain/chain_state.hpp @@ -29,9 +29,11 @@ #include #include #include -#include namespace libbitcoin { + +class settings; + namespace chain { class block; diff --git a/include/bitcoin/bitcoin/chain/header.hpp b/include/bitcoin/bitcoin/chain/header.hpp index 67ab59ff995..0afac4c274b 100644 --- a/include/bitcoin/bitcoin/chain/header.hpp +++ b/include/bitcoin/bitcoin/chain/header.hpp @@ -29,13 +29,18 @@ #include #include #include -#include #include #include #include #include namespace libbitcoin { + +/* + * Forward declaration to break header cycle. + */ +class settings; + namespace chain { class BC_API header diff --git a/include/bitcoin/bitcoin/settings.hpp b/include/bitcoin/bitcoin/settings.hpp index e0b9c542a38..6e4b52835ca 100644 --- a/include/bitcoin/bitcoin/settings.hpp +++ b/include/bitcoin/bitcoin/settings.hpp @@ -19,10 +19,13 @@ #ifndef LIBBITCOIN_SETTINGS_HPP #define LIBBITCOIN_SETTINGS_HPP +#include +#include #include #include #include +#include namespace libbitcoin { @@ -31,6 +34,7 @@ class BC_API settings { public: settings(); + settings(config::settings context); uint32_t work_limit(bool retarget=true) const; @@ -48,6 +52,8 @@ class BC_API settings // The target number of blocks for 2 weeks of work (2016 blocks). size_t retargeting_interval; + + chain::block genesis_block; }; } // namespace libbitcoin diff --git a/src/chain/block.cpp b/src/chain/block.cpp index d8482d60aff..6984af5ef7a 100644 --- a/src/chain/block.cpp +++ b/src/chain/block.cpp @@ -59,70 +59,10 @@ using namespace bc::config; using namespace bc::machine; using namespace boost::adaptors; -static const std::string encoded_mainnet_genesis_block = - "01000000" - "0000000000000000000000000000000000000000000000000000000000000000" - "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a" - "29ab5f49" - "ffff001d" - "1dac2b7c" - "01" - "01000000" - "01" - "0000000000000000000000000000000000000000000000000000000000000000ffffffff" - "4d" - "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73" - "ffffffff" - "01" - "00f2052a01000000" - "43" - "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac" - "00000000"; - -static const std::string encoded_testnet_genesis_block = - "01000000" - "0000000000000000000000000000000000000000000000000000000000000000" - "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a" - "dae5494d" - "ffff001d" - "1aa4ae18" - "01" - "01000000" - "01" - "0000000000000000000000000000000000000000000000000000000000000000ffffffff" - "4d" - "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73" - "ffffffff" - "01" - "00f2052a01000000" - "43" - "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac" - "00000000"; - -static const std::string encoded_regtest_genesis_block = - "01000000" - "0000000000000000000000000000000000000000000000000000000000000000" - "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a" - "dae5494d" - "ffff7f20" - "02000000" - "01" - "01000000" - "01" - "0000000000000000000000000000000000000000000000000000000000000000ffffffff" - "4d" - "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73" - "ffffffff" - "01" - "00f2052a01000000" - "43" - "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac" - "00000000"; - // Constructors. //----------------------------------------------------------------------------- -block::block(const settings& settings) +block::block(const bc::settings& settings) : header_(settings), metadata{} { @@ -200,7 +140,7 @@ bool block::operator!=(const block& other) const //----------------------------------------------------------------------------- // static -block block::factory(const data_chunk& data, const settings& settings, +block block::factory(const data_chunk& data, const bc::settings& settings, bool witness) { block instance(settings); @@ -209,7 +149,7 @@ block block::factory(const data_chunk& data, const settings& settings, } // static -block block::factory(std::istream& stream, const settings& settings, +block block::factory(std::istream& stream, const bc::settings& settings, bool witness) { block instance(settings); @@ -218,7 +158,7 @@ block block::factory(std::istream& stream, const settings& settings, } // static -block block::factory(reader& source, const settings& settings, bool witness) +block block::factory(reader& source, const bc::settings& settings, bool witness) { block instance(settings); instance.from_data(source, witness); @@ -438,42 +378,6 @@ hash_digest block::hash() const // Utilities. //----------------------------------------------------------------------------- -chain::block block::genesis_mainnet(const settings& settings) -{ - data_chunk data; - decode_base16(data, encoded_mainnet_genesis_block); - const auto genesis = chain::block::factory(data, settings); - - BITCOIN_ASSERT(genesis.is_valid()); - BITCOIN_ASSERT(genesis.transactions().size() == 1); - BITCOIN_ASSERT(genesis.generate_merkle_root() == genesis.header().merkle()); - return genesis; -} - -chain::block block::genesis_testnet(const settings& settings) -{ - data_chunk data; - decode_base16(data, encoded_testnet_genesis_block); - const auto genesis = chain::block::factory(data, settings); - - BITCOIN_ASSERT(genesis.is_valid()); - BITCOIN_ASSERT(genesis.transactions().size() == 1); - BITCOIN_ASSERT(genesis.generate_merkle_root() == genesis.header().merkle()); - return genesis; -} - -chain::block block::genesis_regtest(const settings& settings) -{ - data_chunk data; - decode_base16(data, encoded_regtest_genesis_block); - const auto genesis = chain::block::factory(data, settings); - - BITCOIN_ASSERT(genesis.is_valid()); - BITCOIN_ASSERT(genesis.transactions().size() == 1); - BITCOIN_ASSERT(genesis.generate_merkle_root() == genesis.header().merkle()); - return genesis; -} - // With a 32 bit chain the size of the result should not exceed 43 and with a // 64 bit chain should not exceed 75, using a limit of: 10 + log2(height) + 1. size_t block::locator_size(size_t top) diff --git a/src/chain/chain_state.cpp b/src/chain/chain_state.cpp index 88f889035ec..7c9dac7b3f0 100644 --- a/src/chain/chain_state.cpp +++ b/src/chain/chain_state.cpp @@ -366,7 +366,7 @@ uint32_t chain_state::median_time_past(const data& values, uint32_t) //----------------------------------------------------------------------------- uint32_t chain_state::work_required(const data& values, uint32_t forks, - const settings& settings) + const bc::settings& settings) { // Invalid parameter via public interface, test is_valid for results. if (values.height == 0) @@ -556,7 +556,7 @@ uint32_t chain_state::signal_version(uint32_t forks) // This is promotion from a preceding height to the next. chain_state::data chain_state::to_pool(const chain_state& top, - const settings& settings) + const bc::settings& settings) { // Alias configured forks. const auto forks = top.forks_; @@ -608,7 +608,7 @@ chain_state::data chain_state::to_pool(const chain_state& top, // Constructor (top to pool). // This generates a state for the pool above the presumed top block state. -chain_state::chain_state(const chain_state& top, const settings& settings) +chain_state::chain_state(const chain_state& top, const bc::settings& settings) : data_(to_pool(top, settings)), forks_(top.forks_), stale_seconds_(top.stale_seconds_), @@ -656,7 +656,7 @@ chain_state::data chain_state::to_block(const chain_state& pool, // Constructor (tx pool to block). // This assumes that the pool state is the same height as the block. chain_state::chain_state(const chain_state& pool, const block& block, - const settings& settings) + const bc::settings& settings) : data_(to_block(pool, block)), forks_(pool.forks_), stale_seconds_(pool.stale_seconds_), @@ -668,7 +668,7 @@ chain_state::chain_state(const chain_state& pool, const block& block, } chain_state::data chain_state::to_header(const chain_state& parent, - const header& header, const settings& settings) + const header& header, const bc::settings& settings) { BITCOIN_ASSERT(header.previous_block_hash() == parent.hash()); @@ -705,7 +705,7 @@ chain_state::data chain_state::to_header(const chain_state& parent, // Constructor (parent to header). // This assumes that parent is the state of the header's previous block. chain_state::chain_state(const chain_state& parent, const header& header, - const settings& settings) + const bc::settings& settings) : data_(to_header(parent, header, settings)), forks_(parent.forks_), stale_seconds_(parent.stale_seconds_), @@ -718,7 +718,7 @@ chain_state::chain_state(const chain_state& parent, const header& header, // Constructor (from raw data). chain_state::chain_state(data&& values, const checkpoints& checkpoints, - uint32_t forks, uint32_t stale_seconds, const settings& settings) + uint32_t forks, uint32_t stale_seconds, const bc::settings& settings) : data_(std::move(values)), forks_(forks), stale_seconds_(stale_seconds), diff --git a/src/settings.cpp b/src/settings.cpp index e0e57c20a77..af5252d3c6b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -18,6 +18,7 @@ */ #include #include +#include namespace libbitcoin { @@ -32,10 +33,153 @@ settings::settings() no_retarget_proof_of_work_limit(0x207fffff), min_timespan(target_timespan_seconds / retargeting_factor), max_timespan(target_timespan_seconds * retargeting_factor), - retargeting_interval(target_timespan_seconds / target_spacing_seconds) + retargeting_interval(target_timespan_seconds / target_spacing_seconds), + genesis_block(*this) { } +settings::settings(config::settings context) + : settings() +{ + switch (context) + { + case config::settings::mainnet: + { + genesis_block.from_data({ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, + 0x4b, 0x1e, 0x5e, 0x4a, 0x29, 0xab, 0x5f, 0x49, + 0xff, 0xff, 0x00, 0x1d, 0x1d, 0xac, 0x2b, 0x7c, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, + 0xac, 0x00, 0x00, 0x00, 0x00}); + + break; + } + + case config::settings::testnet: + { + genesis_block.from_data({ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, + 0x4b, 0x1e, 0x5e, 0x4a, 0xda, 0xe5, 0x49, 0x4d, + 0xff, 0xff, 0x00, 0x1d, 0x1a, 0xa4, 0xae, 0x18, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, + 0xac, 0x00, 0x00, 0x00, 0x00}); + + break; + } + + case config::settings::regtest: + { + genesis_block.from_data({ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, + 0x4b, 0x1e, 0x5e, 0x4a, 0xda, 0xe5, 0x49, 0x4d, + 0xff, 0xff, 0x7f, 0x20, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, + 0xac, 0x00, 0x00, 0x00, 0x00}); + + break; + } + + default: + case config::settings::none: + { + } + } + +} + uint32_t settings::work_limit(bool retarget) const { return retarget ? retarget_proof_of_work_limit : diff --git a/test/chain/block.cpp b/test/chain/block.cpp index e899b0f6403..f7f6e7d8e6f 100644 --- a/test/chain/block.cpp +++ b/test/chain/block.cpp @@ -273,7 +273,7 @@ BOOST_AUTO_TEST_CASE(block__from_data__insufficient_transaction_bytes__failure) BOOST_AUTO_TEST_CASE(block__genesis__mainnet__valid_structure) { - const auto genesis = bc::chain::block::genesis_mainnet(settings()); + const auto genesis = settings(bc::config::settings::mainnet).genesis_block; BOOST_REQUIRE(genesis.is_valid()); BOOST_REQUIRE_EQUAL(genesis.transactions().size(), 1u); BOOST_REQUIRE(genesis.header().merkle() == genesis.generate_merkle_root()); @@ -281,7 +281,7 @@ BOOST_AUTO_TEST_CASE(block__genesis__mainnet__valid_structure) BOOST_AUTO_TEST_CASE(block__genesis__testnet__valid_structure) { - const auto genesis = bc::chain::block::genesis_testnet(settings()); + const auto genesis = settings(bc::config::settings::testnet).genesis_block; BOOST_REQUIRE(genesis.is_valid()); BOOST_REQUIRE_EQUAL(genesis.transactions().size(), 1u); BOOST_REQUIRE(genesis.header().merkle() == genesis.generate_merkle_root()); @@ -289,7 +289,7 @@ BOOST_AUTO_TEST_CASE(block__genesis__testnet__valid_structure) BOOST_AUTO_TEST_CASE(block__genesis__regtest__valid_structure) { - const auto genesis = bc::chain::block::genesis_regtest(settings()); + const auto genesis = settings(bc::config::settings::regtest).genesis_block; BOOST_REQUIRE(genesis.is_valid()); BOOST_REQUIRE_EQUAL(genesis.transactions().size(), 1u); BOOST_REQUIRE(genesis.header().merkle() == genesis.generate_merkle_root()); @@ -298,7 +298,7 @@ BOOST_AUTO_TEST_CASE(block__genesis__regtest__valid_structure) BOOST_AUTO_TEST_CASE(block__factory_1__genesis_mainnet__success) { - const auto genesis = bc::chain::block::genesis_mainnet(settings()); + const auto genesis = settings(bc::config::settings::mainnet).genesis_block; BOOST_REQUIRE_EQUAL(genesis.serialized_size(), 285u); BOOST_REQUIRE_EQUAL(genesis.header().serialized_size(), 80u); @@ -307,7 +307,8 @@ BOOST_AUTO_TEST_CASE(block__factory_1__genesis_mainnet__success) BOOST_REQUIRE_EQUAL(raw_block.size(), 285u); // Reload genesis block. - const auto block = chain::block::factory(raw_block, settings()); + const auto block = chain::block::factory(raw_block, settings( + bc::config::settings::mainnet)); BOOST_REQUIRE(block.is_valid()); BOOST_REQUIRE(genesis.header() == block.header()); @@ -318,7 +319,7 @@ BOOST_AUTO_TEST_CASE(block__factory_1__genesis_mainnet__success) BOOST_AUTO_TEST_CASE(block__factory_2__genesis_mainnet__success) { - const auto genesis = bc::chain::block::genesis_mainnet(settings()); + const auto genesis = settings(bc::config::settings::mainnet).genesis_block; BOOST_REQUIRE_EQUAL(genesis.serialized_size(), 285u); BOOST_REQUIRE_EQUAL(genesis.header().serialized_size(), 80u); @@ -328,7 +329,8 @@ BOOST_AUTO_TEST_CASE(block__factory_2__genesis_mainnet__success) // Reload genesis block. data_source stream(raw_block); - const auto block = chain::block::factory(stream, settings()); + const auto block = chain::block::factory(stream, settings( + bc::config::settings::mainnet)); BOOST_REQUIRE(block.is_valid()); BOOST_REQUIRE(genesis.header() == block.header()); @@ -339,7 +341,7 @@ BOOST_AUTO_TEST_CASE(block__factory_2__genesis_mainnet__success) BOOST_AUTO_TEST_CASE(block__factory_3__genesis_mainnet__success) { - const auto genesis = bc::chain::block::genesis_mainnet(settings()); + const auto genesis = settings(bc::config::settings::mainnet).genesis_block; BOOST_REQUIRE_EQUAL(genesis.serialized_size(), 285u); BOOST_REQUIRE_EQUAL(genesis.header().serialized_size(), 80u); @@ -350,7 +352,8 @@ BOOST_AUTO_TEST_CASE(block__factory_3__genesis_mainnet__success) // Reload genesis block. data_source stream(raw_block); istream_reader reader(stream); - const auto block = chain::block::factory(reader, settings()); + const auto block = chain::block::factory(reader, settings( + bc::config::settings::mainnet)); BOOST_REQUIRE(block.is_valid()); BOOST_REQUIRE(genesis.header() == block.header()); diff --git a/test/chain/header.cpp b/test/chain/header.cpp index f56cdcda455..0b25b882e90 100644 --- a/test/chain/header.cpp +++ b/test/chain/header.cpp @@ -453,7 +453,7 @@ BOOST_AUTO_TEST_CASE(header__proof1__genesis_mainnet__expected) BOOST_AUTO_TEST_CASE(header__proof2__genesis_mainnet__expected) { - const auto block = chain::block::genesis_mainnet(settings()); + const auto block = settings(bc::config::settings::mainnet).genesis_block; BOOST_REQUIRE_EQUAL(block.header().proof(), 0x0000000100010001); } diff --git a/test/chain/satoshi_words.cpp b/test/chain/satoshi_words.cpp index e8df2ccfa46..9ecd3db09b7 100644 --- a/test/chain/satoshi_words.cpp +++ b/test/chain/satoshi_words.cpp @@ -27,7 +27,7 @@ BOOST_AUTO_TEST_SUITE(satoshi_words) BOOST_AUTO_TEST_CASE(satoshi_words_mainnet) { // Create mainnet genesis block (contains a single coinbase transaction). - const auto block = chain::block::genesis_mainnet(settings()); + const auto block = settings(bc::config::settings::mainnet).genesis_block; const auto& transactions = block.transactions(); BOOST_REQUIRE_EQUAL(transactions.size(), 1u); diff --git a/test/message/block.cpp b/test/message/block.cpp index 99785700475..9b1e866d493 100644 --- a/test/message/block.cpp +++ b/test/message/block.cpp @@ -179,7 +179,7 @@ BOOST_AUTO_TEST_CASE(block__constructor_7__always__equals_params) BOOST_AUTO_TEST_CASE(block__factory_data_1__genesis_mainnet__success) { - const auto genesis = chain::block::genesis_mainnet(settings()); + const auto genesis = settings(bc::config::settings::mainnet).genesis_block; BOOST_REQUIRE_EQUAL(genesis.serialized_size(), 285u); BOOST_REQUIRE_EQUAL(genesis.header().serialized_size(), 80u); @@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(block__factory_data_1__genesis_mainnet__success) BOOST_AUTO_TEST_CASE(block__factory_data_2__genesis_mainnet__success) { - const auto genesis = chain::block::genesis_mainnet(settings()); + const auto genesis = settings(bc::config::settings::mainnet).genesis_block; BOOST_REQUIRE_EQUAL(genesis.serialized_size(), 285u); BOOST_REQUIRE_EQUAL(genesis.header().serialized_size(), 80u); @@ -233,7 +233,7 @@ BOOST_AUTO_TEST_CASE(block__factory_data_2__genesis_mainnet__success) BOOST_AUTO_TEST_CASE(block__factory_data_3__genesis_mainnet__success) { - const auto genesis = chain::block::genesis_mainnet(settings()); + const auto genesis = settings(config::settings::mainnet).genesis_block; BOOST_REQUIRE_EQUAL(genesis.serialized_size(), 285u); BOOST_REQUIRE_EQUAL(genesis.header().serialized_size(), 80u); diff --git a/test/settings.cpp b/test/settings.cpp index 47327859656..f6f060b1555 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -41,4 +41,160 @@ BOOST_AUTO_TEST_CASE(settings__construct__default_context__expected) BOOST_REQUIRE_EQUAL(configuration.retargeting_interval, 2016); } +BOOST_AUTO_TEST_CASE(settings__construct__mainnet_context__expected) +{ + settings configuration(config::settings::mainnet); + BOOST_REQUIRE_EQUAL(configuration.retargeting_factor, 4); + BOOST_REQUIRE_EQUAL(configuration.target_spacing_seconds, 600); + BOOST_REQUIRE_EQUAL(configuration.easy_spacing_seconds, 1200); + BOOST_REQUIRE_EQUAL(configuration.timestamp_future_seconds, 7200); + BOOST_REQUIRE_EQUAL(configuration.target_timespan_seconds, 1209600); + BOOST_REQUIRE_EQUAL(configuration.retarget_proof_of_work_limit, 0x1d00ffff); + BOOST_REQUIRE_EQUAL(configuration.no_retarget_proof_of_work_limit, 0x207fffff); + BOOST_REQUIRE_EQUAL(configuration.min_timespan, 302400); + BOOST_REQUIRE_EQUAL(configuration.max_timespan, 4838400); + BOOST_REQUIRE_EQUAL(configuration.retargeting_interval, 2016); + BOOST_REQUIRE_EQUAL(configuration.genesis_block.to_data(), data_chunk({ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, + 0x4b, 0x1e, 0x5e, 0x4a, 0x29, 0xab, 0x5f, 0x49, + 0xff, 0xff, 0x00, 0x1d, 0x1d, 0xac, 0x2b, 0x7c, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, + 0xac, 0x00, 0x00, 0x00, 0x00})); +} + +BOOST_AUTO_TEST_CASE(settings__construct__testnet_context__expected) +{ + settings configuration(config::settings::testnet); + BOOST_REQUIRE_EQUAL(configuration.retargeting_factor, 4); + BOOST_REQUIRE_EQUAL(configuration.target_spacing_seconds, 600); + BOOST_REQUIRE_EQUAL(configuration.easy_spacing_seconds, 1200); + BOOST_REQUIRE_EQUAL(configuration.timestamp_future_seconds, 7200); + BOOST_REQUIRE_EQUAL(configuration.target_timespan_seconds, 1209600); + BOOST_REQUIRE_EQUAL(configuration.retarget_proof_of_work_limit, 0x1d00ffff); + BOOST_REQUIRE_EQUAL(configuration.no_retarget_proof_of_work_limit, 0x207fffff); + BOOST_REQUIRE_EQUAL(configuration.min_timespan, 302400); + BOOST_REQUIRE_EQUAL(configuration.max_timespan, 4838400); + BOOST_REQUIRE_EQUAL(configuration.retargeting_interval, 2016); + BOOST_REQUIRE_EQUAL(configuration.genesis_block.to_data(), data_chunk({ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, + 0x4b, 0x1e, 0x5e, 0x4a, 0xda, 0xe5, 0x49, 0x4d, + 0xff, 0xff, 0x00, 0x1d, 0x1a, 0xa4, 0xae, 0x18, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, + 0xac, 0x00, 0x00, 0x00, 0x00})); +} + +BOOST_AUTO_TEST_CASE(settings__construct__regtest_context__expected) +{ + settings configuration(config::settings::regtest); + BOOST_REQUIRE_EQUAL(configuration.retargeting_factor, 4); + BOOST_REQUIRE_EQUAL(configuration.target_spacing_seconds, 600); + BOOST_REQUIRE_EQUAL(configuration.easy_spacing_seconds, 1200); + BOOST_REQUIRE_EQUAL(configuration.timestamp_future_seconds, 7200); + BOOST_REQUIRE_EQUAL(configuration.target_timespan_seconds, 1209600); + BOOST_REQUIRE_EQUAL(configuration.retarget_proof_of_work_limit, 0x1d00ffff); + BOOST_REQUIRE_EQUAL(configuration.no_retarget_proof_of_work_limit, 0x207fffff); + BOOST_REQUIRE_EQUAL(configuration.min_timespan, 302400); + BOOST_REQUIRE_EQUAL(configuration.max_timespan, 4838400); + BOOST_REQUIRE_EQUAL(configuration.retargeting_interval, 2016); + BOOST_REQUIRE_EQUAL(configuration.genesis_block.to_data(), data_chunk({ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, + 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, + 0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, + 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, + 0x4b, 0x1e, 0x5e, 0x4a, 0xda, 0xe5, 0x49, 0x4d, + 0xff, 0xff, 0x7f, 0x20, 0x02, 0x00, 0x00, 0x00, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, + 0x01, 0x04, 0x45, 0x54, 0x68, 0x65, 0x20, 0x54, + 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, + 0x4a, 0x61, 0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, + 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c, + 0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, + 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f, 0x66, 0x20, + 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, + 0x61, 0x69, 0x6c, 0x6f, 0x75, 0x74, 0x20, 0x66, + 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, + 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, + 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, + 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, + 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, + 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, + 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, + 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, + 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, + 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, + 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, + 0xac, 0x00, 0x00, 0x00, 0x00})); +} + BOOST_AUTO_TEST_SUITE_END()