Skip to content

Commit

Permalink
parametrize genesis block
Browse files Browse the repository at this point in the history
  • Loading branch information
toxeus committed Jul 12, 2018
1 parent 2e1e679 commit 9b200ba
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 129 deletions.
3 changes: 2 additions & 1 deletion examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 6 additions & 4 deletions include/bitcoin/bitcoin/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@
#include <bitcoin/bitcoin/define.hpp>
#include <bitcoin/bitcoin/error.hpp>
#include <bitcoin/bitcoin/math/hash.hpp>
#include <bitcoin/bitcoin/settings.hpp>
#include <bitcoin/bitcoin/utility/asio.hpp>
#include <bitcoin/bitcoin/utility/data.hpp>
#include <bitcoin/bitcoin/utility/reader.hpp>
#include <bitcoin/bitcoin/utility/thread.hpp>
#include <bitcoin/bitcoin/utility/writer.hpp>

namespace libbitcoin {

/**
* Forward declaration to break header cycle.
*/
class settings;

namespace chain {

class BC_API block
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion include/bitcoin/bitcoin/chain/chain_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
#include <bitcoin/bitcoin/math/hash.hpp>
#include <bitcoin/bitcoin/machine/opcode.hpp>
#include <bitcoin/bitcoin/machine/rule_fork.hpp>
#include <bitcoin/bitcoin/settings.hpp>

namespace libbitcoin {

class settings;

namespace chain {

class block;
Expand Down
7 changes: 6 additions & 1 deletion include/bitcoin/bitcoin/chain/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@
#include <bitcoin/bitcoin/define.hpp>
#include <bitcoin/bitcoin/error.hpp>
#include <bitcoin/bitcoin/math/hash.hpp>
#include <bitcoin/bitcoin/settings.hpp>
#include <bitcoin/bitcoin/utility/data.hpp>
#include <bitcoin/bitcoin/utility/reader.hpp>
#include <bitcoin/bitcoin/utility/thread.hpp>
#include <bitcoin/bitcoin/utility/writer.hpp>

namespace libbitcoin {

/*
* Forward declaration to break header cycle.
*/
class settings;

namespace chain {

class BC_API header
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/bitcoin/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
#ifndef LIBBITCOIN_SETTINGS_HPP
#define LIBBITCOIN_SETTINGS_HPP

#include <bitcoin/bitcoin/chain/block.hpp>
#include <bitcoin/bitcoin/config/settings.hpp>
#include <bitcoin/bitcoin/define.hpp>

#include <cstdint>
#include <cstddef>
#include <string>

namespace libbitcoin {

Expand All @@ -31,6 +34,7 @@ class BC_API settings
{
public:
settings();
settings(config::settings context);

uint32_t work_limit(bool retarget=true) const;

Expand All @@ -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
Expand Down
104 changes: 4 additions & 100 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/chain/chain_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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_),
Expand Down Expand Up @@ -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_),
Expand All @@ -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());

Expand Down Expand Up @@ -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_),
Expand All @@ -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),
Expand Down
Loading

0 comments on commit 9b200ba

Please sign in to comment.