Skip to content

Commit

Permalink
Parametrize timespan constants.
Browse files Browse the repository at this point in the history
The constants are parametrized such that libbitcoin can
be used by various altcoins by solely editing the
config file.
  • Loading branch information
toxeus committed Jun 6, 2018
1 parent 52258bf commit 583c974
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 80 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ src_libbitcoin_la_LDFLAGS = ${boost_LDFLAGS}
src_libbitcoin_la_LIBADD = ${boost_chrono_LIBS} ${boost_date_time_LIBS} ${boost_filesystem_LIBS} ${boost_iostreams_LIBS} ${boost_locale_LIBS} ${boost_log_LIBS} ${boost_program_options_LIBS} ${boost_regex_LIBS} ${boost_system_LIBS} ${boost_thread_LIBS} ${pthread_LIBS} ${rt_LIBS} ${icu_i18n_LIBS} ${dl_LIBS} ${png_LIBS} ${qrencode_LIBS} ${secp256k1_LIBS}
src_libbitcoin_la_SOURCES = \
src/error.cpp \
src/settings.cpp \
src/chain/block.cpp \
src/chain/chain_state.cpp \
src/chain/compact.cpp \
Expand Down Expand Up @@ -363,6 +364,7 @@ include_bitcoin_bitcoin_HEADERS = \
include/bitcoin/bitcoin/define.hpp \
include/bitcoin/bitcoin/error.hpp \
include/bitcoin/bitcoin/handlers.hpp \
include/bitcoin/bitcoin/settings.hpp \
include/bitcoin/bitcoin/version.hpp

include_bitcoin_bitcoin_chaindir = ${includedir}/bitcoin/bitcoin/chain
Expand Down
38 changes: 21 additions & 17 deletions include/bitcoin/bitcoin/chain/chain_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cstdint>
#include <memory>
#include <deque>
#include <bitcoin/bitcoin/settings.hpp>
#include <bitcoin/bitcoin/config/checkpoint.hpp>
#include <bitcoin/bitcoin/constants.hpp>
#include <bitcoin/bitcoin/define.hpp>
Expand Down Expand Up @@ -124,24 +125,26 @@ class BC_API chain_state
};

/// Checkpoints must be ordered by height with greatest at back.
static map get_map(size_t height, const checkpoints& checkpoints,
map get_map(size_t height, const checkpoints& checkpoints,
uint32_t forks);

static uint32_t signal_version(uint32_t forks);

/// Create pool state from top chain top block state.
chain_state(const chain_state& top);
chain_state(const chain_state& top, const settings& settings);

/// Create block state from tx pool chain state of same height.
chain_state(const chain_state& pool, const chain::block& block);
chain_state(const chain_state& pool, const chain::block& block,
const settings& settings);

/// Create header state from header pool chain state of parent block.
chain_state(const chain_state& parent, const chain::header& header);
chain_state(const chain_state& parent, const chain::header& header,
const settings& settings);

/// Checkpoints must be ordered by height with greatest at back.
/// Forks and checkpoints must match those provided for map creation.
chain_state(data&& values, const checkpoints& checkpoints, uint32_t forks,
uint32_t stale_seconds);
uint32_t stale_seconds, const settings& settings);

/// Properties.
size_t height() const;
Expand Down Expand Up @@ -181,29 +184,29 @@ class BC_API chain_state

static activations activation(const data& values, uint32_t forks);
static uint32_t median_time_past(const data& values, uint32_t forks);
static uint32_t work_required(const data& values, uint32_t forks);
uint32_t work_required(const data& values, uint32_t forks);

private:
static size_t bits_count(size_t height, uint32_t forks);
size_t bits_count(size_t height, uint32_t forks);
static size_t version_count(size_t height, uint32_t forks);
static size_t timestamp_count(size_t height, uint32_t forks);
static size_t retarget_height(size_t height, uint32_t forks);
size_t retarget_height(size_t height, uint32_t forks);
static size_t bip9_bit0_height(size_t height, uint32_t forks);
static size_t bip9_bit1_height(size_t height, uint32_t forks);

static data to_pool(const chain_state& top);
data to_pool(const chain_state& top);
static data to_block(const chain_state& pool, const block& block);
static data to_header(const chain_state& parent, const header& header);
data to_header(const chain_state& parent, const header& header);

static uint32_t work_required_retarget(const data& values);
static uint32_t retarget_timespan(const chain_state::data& values);
uint32_t work_required_retarget(const data& values);
uint32_t retarget_timespan(const chain_state::data& values);

// easy blocks
static uint32_t easy_work_required(const data& values);
static uint32_t easy_time_limit(const chain_state::data& values);
static bool is_retarget_or_non_limit(size_t height, uint32_t bits);
static bool is_retarget_height(size_t height);
static size_t retarget_distance(size_t height);
uint32_t easy_work_required(const data& values);
uint32_t easy_time_limit(const chain_state::data& values);
bool is_retarget_or_non_limit(size_t height, uint32_t bits);
bool is_retarget_height(size_t height);
size_t retarget_distance(size_t height);

// This is retained as an optimization for other constructions.
// A similar height clone can be partially computed, reducing query cost.
Expand All @@ -222,6 +225,7 @@ class BC_API chain_state
const activations active_;
const uint32_t median_time_past_;
const uint32_t work_required_;
const settings& settings_;
};

} // namespace chain
Expand Down
6 changes: 4 additions & 2 deletions include/bitcoin/bitcoin/chain/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <bitcoin/bitcoin/chain/chain_state.hpp>
#include <bitcoin/bitcoin/define.hpp>
#include <bitcoin/bitcoin/error.hpp>
#include <bitcoin/bitcoin/settings.hpp>
#include <bitcoin/bitcoin/math/hash.hpp>
#include <bitcoin/bitcoin/utility/data.hpp>
#include <bitcoin/bitcoin/utility/reader.hpp>
Expand Down Expand Up @@ -74,10 +75,10 @@ class BC_API header

header(uint32_t version, const hash_digest& previous_block_hash,
const hash_digest& merkle, uint32_t timestamp, uint32_t bits,
uint32_t nonce);
uint32_t nonce, const settings& settings);
header(uint32_t version, hash_digest&& previous_block_hash,
hash_digest&& merkle, uint32_t timestamp, uint32_t bits,
uint32_t nonce);
uint32_t nonce, const settings& settings);

// Operators.
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -179,6 +180,7 @@ class BC_API header
uint32_t timestamp_;
uint32_t bits_;
uint32_t nonce_;
const settings& settings_;
};

} // namespace chain
Expand Down
25 changes: 0 additions & 25 deletions include/bitcoin/bitcoin/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,6 @@ BC_CONSTEXPR uint32_t relative_locktime_mask = 0x0000ffff;
BC_CONSTEXPR uint32_t relative_locktime_disabled = 0x80000000;
BC_CONSTEXPR uint32_t relative_locktime_time_locked = 0x00400000;

// Timespan constants.
//-----------------------------------------------------------------------------

BC_CONSTEXPR uint32_t retargeting_factor = 4;
BC_CONSTEXPR uint32_t easy_spacing_seconds = 20 * 60;
BC_CONSTEXPR uint32_t target_spacing_seconds = 10 * 60;
BC_CONSTEXPR uint32_t target_timespan_seconds = 2 * 7 * 24 * 60 * 60;
BC_CONSTEXPR uint32_t timestamp_future_seconds = 2 * 60 * 60;
BC_CONSTEXPR uint32_t retarget_proof_of_work_limit = 0x1d00ffff;
BC_CONSTEXPR uint32_t no_retarget_proof_of_work_limit = 0x207fffff;
BC_CONSTFUNC uint32_t work_limit(bool retarget=true)
{
return retarget ? retarget_proof_of_work_limit : no_retarget_proof_of_work_limit;
}

// The upper and lower bounds for the retargeting timespan.
BC_CONSTEXPR uint32_t min_timespan =
target_timespan_seconds / retargeting_factor;
BC_CONSTEXPR uint32_t max_timespan =
target_timespan_seconds * retargeting_factor;

// The target number of blocks for 2 weeks of work (2016 blocks).
BC_CONSTEXPR size_t retargeting_interval =
target_timespan_seconds / target_spacing_seconds;

// Fork constants.
//-----------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/bitcoin/message/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class BC_API header
header();
header(uint32_t version, const hash_digest& previous_block_hash,
const hash_digest& merkle, uint32_t timestamp, uint32_t bits,
uint32_t nonce);
uint32_t nonce, const settings& settings);
header(uint32_t version, hash_digest&& previous_block_hash,
hash_digest&& merkle, uint32_t timestamp, uint32_t bits,
uint32_t nonce);
uint32_t nonce, const settings& settings);
header(const chain::header& other);
header(chain::header&& other);
header(const header& other);
Expand Down
62 changes: 62 additions & 0 deletions include/bitcoin/bitcoin/settings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2011-2018 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_SETTINGS_HPP
#define LIBBITCOIN_SETTINGS_HPP

#include <bitcoin/bitcoin/define.hpp>

#include <cstdint>
#include <cstddef>

namespace libbitcoin {

/// Common database configuration settings, properties not thread safe.
class BC_API settings
{

private:
uint32_t retargeting_factor_;
uint32_t target_spacing_seconds_;

public:
settings();

uint32_t easy_spacing_seconds;
uint32_t timestamp_future_seconds;
uint32_t target_timespan_seconds;
uint32_t retarget_proof_of_work_limit;
uint32_t no_retarget_proof_of_work_limit;

// The upper and lower bounds for the retargeting timespan.
uint32_t min_timespan;
uint32_t max_timespan;

// The target number of blocks for 2 weeks of work (2016 blocks).
size_t retargeting_interval;

uint32_t work_limit(bool retarget=true) const
{
return retarget ? retarget_proof_of_work_limit : no_retarget_proof_of_work_limit;
}

};

} // namespace libbitcoin

#endif
Loading

0 comments on commit 583c974

Please sign in to comment.