Skip to content

Commit

Permalink
Merge pull request #1411 from evoskuil/master
Browse files Browse the repository at this point in the history
Add settings, checkpoint.equals, uint256_t serialize, chain_state.cumulative_work.
  • Loading branch information
evoskuil authored Feb 27, 2024
2 parents e829651 + 2399024 commit e48f8d5
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 17 deletions.
2 changes: 2 additions & 0 deletions include/bitcoin/system/chain/chain_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class BC_API chain_state
/// Properties.
chain::context context() const NOEXCEPT;
const hash_digest& hash() const NOEXCEPT;
const uint256_t& cumulative_work() const NOEXCEPT;
uint32_t minimum_block_version() const NOEXCEPT;
uint32_t work_required() const NOEXCEPT;
uint32_t timestamp() const NOEXCEPT;
Expand Down Expand Up @@ -237,6 +238,7 @@ class BC_API chain_state
const activations active_;
const uint32_t work_required_;
const uint32_t median_time_past_;
const uint256_t cumulative_work_;
};

} // namespace chain
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/system/chain/checkpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class BC_API checkpoint
////bool to_string(std::ostream& stream) const NOEXCEPT;
////bool to_string(writer& sink) const NOEXCEPT;

/// Methods.
/// -----------------------------------------------------------------------

bool equals(const hash_digest& hash, size_t height) const NOEXCEPT;

/// Properties.
/// -----------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/system/config/hash256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BC_API hash256 final
std::string to_string() const NOEXCEPT;

operator const hash_digest&() const NOEXCEPT;
operator uint256_t() const NOEXCEPT;

friend std::istream& operator>>(std::istream& stream,
hash256& argument) THROWS;
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/system/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ class BC_API settings

/// This cannot be reactivated in a future branch due to window expiration.
chain::checkpoint bip9_bit1_active_checkpoint{};

/// A block that is presumed to be valid but not required to be present.
chain::checkpoint milestone{};

/// The minimum work for any branch to be considered valid.
config::hash256 minimum_work{};
};

} // namespace system
Expand Down
41 changes: 25 additions & 16 deletions src/chain/chain_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,15 @@ chain_state::data chain_state::to_pool(const chain_state& top,

// Top to pool.
// This generates a state for the pool above the presumed top block state.
// Work is not acculuated for a pool state.
chain_state::chain_state(const chain_state& top,
const system::settings& settings) NOEXCEPT
: data_(to_pool(top, settings)),
forks_(top.forks_),
active_(activation(data_, forks_, settings)),
work_required_(work_required(data_, forks_, settings)),
median_time_past_(median_time_past(data_, forks_))
median_time_past_(median_time_past(data_, forks_)),
cumulative_work_(top.cumulative_work())
{
}

Expand Down Expand Up @@ -627,7 +629,8 @@ chain_state::chain_state(const chain_state& pool, const block& block,
forks_(pool.forks_),
active_(activation(data_, forks_, settings)),
work_required_(work_required(data_, forks_, settings)),
median_time_past_(median_time_past(data_, forks_))
median_time_past_(median_time_past(data_, forks_)),
cumulative_work_(pool.cumulative_work() + block.header().proof())
{
}

Expand Down Expand Up @@ -665,7 +668,8 @@ chain_state::chain_state(const chain_state& parent, const header& header,
forks_(parent.forks_),
active_(activation(data_, forks_, settings)),
work_required_(work_required(data_, forks_, settings)),
median_time_past_(median_time_past(data_, forks_))
median_time_past_(median_time_past(data_, forks_)),
cumulative_work_(parent.cumulative_work() + header.proof())
{
}

Expand All @@ -683,11 +687,29 @@ chain_state::chain_state(data&& values,
// Properties.
// ----------------------------------------------------------------------------

chain::context chain_state::context() const NOEXCEPT
{
return
{
forks(),
timestamp(),
median_time_past(),
possible_narrow_cast<uint32_t>(height()),
minimum_block_version(),
work_required()
};
}

const hash_digest& chain_state::hash() const NOEXCEPT
{
return data_.hash;
}

const uint256_t& chain_state::cumulative_work() const NOEXCEPT
{
return cumulative_work_;
}

uint32_t chain_state::minimum_block_version() const NOEXCEPT
{
return active_.minimum_block_version;
Expand Down Expand Up @@ -720,19 +742,6 @@ size_t chain_state::height() const NOEXCEPT
return data_.height;
}

chain::context chain_state::context() const NOEXCEPT
{
return
{
forks(),
timestamp(),
median_time_past(),
possible_narrow_cast<uint32_t>(height()),
minimum_block_version(),
work_required()
};
}

} // namespace chain
} // namespace system
} // namespace libbitcoin
8 changes: 7 additions & 1 deletion src/chain/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ checkpoint checkpoint::from_string(const std::string& hash,
return { std::move(digest), height, true };
}

// functional equality
bool checkpoint::equals(const hash_digest& hash, size_t height) const NOEXCEPT
{
return this->hash() == hash && this->height() == height;
}

// Operators.
// ----------------------------------------------------------------------------

Expand All @@ -120,7 +126,7 @@ bool operator<(const checkpoint& left, const checkpoint& right) NOEXCEPT

bool operator==(const checkpoint& left, const checkpoint& right) NOEXCEPT
{
return left.hash() == right.hash() && left.height() == right.height();
return left.equals(right.hash(), right.height());
}

bool operator!=(const checkpoint& left, const checkpoint& right) NOEXCEPT
Expand Down
5 changes: 5 additions & 0 deletions src/config/hash256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ hash256::operator const hash_digest&() const NOEXCEPT
return value_;
}

hash256::operator uint256_t() const NOEXCEPT
{
return to_uintx(value_);
}

std::istream& operator>>(std::istream& stream, hash256& argument) THROWS
{
std::string base16;
Expand Down
6 changes: 6 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ settings::settings(chain::selection context) NOEXCEPT
{ "000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6", 33333 },
{ "0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d", 11111 }
};
milestone = { "00000000000000000001a0a448d6cf2546b06801389cc030b2b18c6491266815", 804000 };
minimum_work = base16_hash("000000000000000000000000000000000000000052b2559353df4117b7348b64");
break;
}

Expand Down Expand Up @@ -195,6 +197,8 @@ settings::settings(chain::selection context) NOEXCEPT
{
{ "000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70", 546 }
};
milestone = { "0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f", 2500000 };
minimum_work = base16_hash("000000000000000000000000000000000000000000000b6a51f415a67c0da307");
break;
}

Expand Down Expand Up @@ -252,6 +256,8 @@ settings::settings(chain::selection context) NOEXCEPT
bip9_bit0_active_checkpoint = { "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", 0 };
bip9_bit1_active_checkpoint = { "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", 0 };
checkpoints = {};
milestone = { "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", 0 };
minimum_work = base16_hash("0000000000000000000000000000000000000000000000000000000000000000");
break;
}

Expand Down
8 changes: 8 additions & 0 deletions test/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ BOOST_AUTO_TEST_CASE(settings__construct__mainnet_context__expected)
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.minimum_work, to_uintx(base16_hash("000000000000000000000000000000000000000052b2559353df4117b7348b64")));
const chain::checkpoint milestone("00000000000000000001a0a448d6cf2546b06801389cc030b2b18c6491266815", 804000u);
BOOST_REQUIRE_EQUAL(configuration.milestone, milestone);
}

BOOST_AUTO_TEST_CASE(settings__construct__testnet_context__expected)
Expand Down Expand Up @@ -138,6 +141,9 @@ BOOST_AUTO_TEST_CASE(settings__construct__testnet_context__expected)
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.minimum_work, to_uintx(base16_hash("000000000000000000000000000000000000000000000b6a51f415a67c0da307")));
const chain::checkpoint milestone("0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f", 2500000u);
BOOST_REQUIRE_EQUAL(configuration.milestone, milestone);
}

BOOST_AUTO_TEST_CASE(settings__construct__regtest_context__expected)
Expand Down Expand Up @@ -171,6 +177,8 @@ BOOST_AUTO_TEST_CASE(settings__construct__regtest_context__expected)
BOOST_REQUIRE_EQUAL(configuration.bitcoin_to_satoshi(1), 100000000u);
BOOST_REQUIRE_EQUAL(configuration.max_money(), 1499999998350u);
BOOST_REQUIRE(configuration.checkpoints.empty());
BOOST_REQUIRE_EQUAL(configuration.minimum_work, to_uintx(base16_hash("0000000000000000000000000000000000000000000000000000000000000000")));
BOOST_REQUIRE_EQUAL(configuration.milestone, genesis);
}

// setter methods
Expand Down

0 comments on commit e48f8d5

Please sign in to comment.