Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing ABLA for 2024-May #21

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _is_legacy_db(self):

def build_requirements(self):
if self.options.tests:
self.test_requires("catch2/3.3.2")
self.test_requires("catch2/3.5.2")

def requirements(self):
self.requires("database/0.38.0", transitive_headers=True, transitive_libs=True)
Expand Down
3 changes: 3 additions & 0 deletions include/kth/blockchain/interface/block_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class BCB_API block_chain : public safe_chain, public fast_chain, noncopyable {
/// Get the header of the block at the given height.
bool get_header(domain::chain::header& out_header, size_t height) const override;

/// Get the header of the block with the given height, also the ABLA state.
std::optional<database::header_with_abla_state_t> get_header_and_abla_state(size_t height) const override;

/// Get a sequence of block headers [from, to].
domain::chain::header::list get_headers(size_t from, size_t to) const override;

Expand Down
3 changes: 3 additions & 0 deletions include/kth/blockchain/interface/fast_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class BCB_API fast_chain {
/// Get the header of the block at the given height.
virtual bool get_header(domain::chain::header& out_header, size_t height) const = 0;

/// Get the header of the block with the given height, also the ABLA state.
virtual std::optional<database::header_with_abla_state_t> get_header_and_abla_state(size_t height) const = 0;

/// Get a sequence of block headers [from, to].
virtual domain::chain::header::list get_headers(size_t from, size_t to) const = 0;

Expand Down
4 changes: 4 additions & 0 deletions include/kth/blockchain/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class BCB_API settings {
// The half life for the ASERTi3-2d DAA. For every (asert_half_life) seconds behind schedule the blockchain gets, difficulty is cut in half.
// Doubled if blocks are ahead of schedule.
uint64_t asert_half_life = 2ull * 24 * 60 * 60; //two days

uint64_t default_consensus_block_size;
kth::domain::chain::abla::config abla_config {};

#else
// Just for Segwit coins
bool bip141 = true;
Expand Down
6 changes: 5 additions & 1 deletion src/interface/block_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ bool block_chain::get_header(domain::chain::header& out_header, size_t height) c
return out_header.is_valid();
}

std::optional<database::header_with_abla_state_t> block_chain::get_header_and_abla_state(size_t height) const {
return database_.internal_db().get_header_and_abla_state(height);
}

domain::chain::header::list block_chain::get_headers(size_t from, size_t to) const {
return database_.internal_db().get_headers(from, to);
}
Expand Down Expand Up @@ -224,7 +228,7 @@ bool block_chain::get_version(uint32_t& out_version, size_t height) const {

bool block_chain::get_last_height(size_t& out_height) const {
uint32_t temp;
auto res = database_.internal_db().get_last_height(temp);
auto const res = database_.internal_db().get_last_height(temp);
out_height = temp;
return succeed(res);
}
Expand Down
36 changes: 35 additions & 1 deletion src/populate/populate_chain_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,33 @@ chain_state::assert_anchor_block_info_t populate_chain_state::get_assert_anchor_

chain_state::ptr populate_chain_state::populate() const {
size_t top;

if ( ! fast_chain_.get_last_height(top)) {
LOG_ERROR(LOG_BLOCKCHAIN, "Failed to populate chain state, last height.");
return {};
}
auto opt = fast_chain_.get_header_and_abla_state(top);
if ( ! opt) {
LOG_ERROR(LOG_BLOCKCHAIN, "Failed to populate chain state, last header.");
return {};
}
auto [last_header, block_size, control_block_size, elastic_buffer_size] = *opt;
if ( ! last_header.is_valid()) {
LOG_ERROR(LOG_BLOCKCHAIN, "Failed to populate chain state, last header.");
return {};
}

chain_state::data data;
data.hash = null_hash;
data.height = *safe_add(top, size_t(1));

if (block_size == 0) {
data.abla_state = abla::state(settings_.abla_config, static_max_block_size(network_));
} else {
data.abla_state = abla::state(settings_.abla_config, block_size);
data.abla_state.control_block_size = control_block_size;
data.abla_state.elastic_buffer_size = elastic_buffer_size;
}

auto branch_ptr = std::make_shared<branch>(top);

// Use an empty branch to represent the transaction pool.
Expand All @@ -263,6 +280,7 @@ chain_state::ptr populate_chain_state::populate() const {
#if defined(KTH_CURRENCY_BCH)
, anchor
, settings_.asert_half_life
, settings_.abla_config
// , settings_.pythagoras_activation_time
// , settings_.euclid_activation_time
// , settings_.pisano_activation_time
Expand Down Expand Up @@ -297,6 +315,21 @@ chain_state::ptr populate_chain_state::populate(chain_state::ptr pool, branch::c
return {};
}

auto const is_lobachevski_enabled = chain_state::is_mtp_activated(chain_state::median_time_past(data), settings_.lobachevski_activation_time);
if (is_lobachevski_enabled) {
if ( ! pool->is_lobachevski_enabled()) {
data.abla_state = abla::state(settings_.abla_config, block->serialized_size(1));
} else {
auto const abla_config_opt = abla::next(pool->abla_state(), settings_.abla_config, block->serialized_size(1));
if ( ! abla_config_opt) {
return {};
}
data.abla_state = *abla_config_opt;
}
} else {
data.abla_state = abla::state(settings_.abla_config, block->serialized_size(1));
}

return std::make_shared<chain_state>(
std::move(data)
, configured_forks_
Expand All @@ -305,6 +338,7 @@ chain_state::ptr populate_chain_state::populate(chain_state::ptr pool, branch::c
#if defined(KTH_CURRENCY_BCH)
, pool->assert_anchor_block_info()
, settings_.asert_half_life
, settings_.abla_config
// , settings_.pythagoras_activation_time
// , settings_.euclid_activation_time
// , settings_.pisano_activation_time
Expand Down
13 changes: 13 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
namespace kth::blockchain {

settings::settings(domain::config::network net) {
namespace abla = kth::domain::chain::abla;
switch (net) {
case domain::config::network::mainnet: {
#if defined(KTH_CURRENCY_BCH)
asert_half_life = 2ull * 24 * 60 * 60; // two days
default_consensus_block_size = max_block_size::mainnet_new;
abla_config = abla::default_config(default_consensus_block_size, false);
#endif
break;
}
case domain::config::network::testnet: {
easy_blocks = true;
#if defined(KTH_CURRENCY_BCH)
asert_half_life = 60ull * 60; // one hour
default_consensus_block_size = max_block_size::testnet3;
abla_config = abla::default_config(default_consensus_block_size, true);
#endif
break;
}
Expand All @@ -34,23 +39,31 @@ settings::settings(domain::config::network net) {

#if defined(KTH_CURRENCY_BCH)
asert_half_life = 2ull * 24 * 60 * 60; // two days
default_consensus_block_size = max_block_size::regtest;
abla_config = abla::default_config(default_consensus_block_size, false);
#endif
break;
}
#if defined(KTH_CURRENCY_BCH)
case domain::config::network::testnet4: {
easy_blocks = true;
asert_half_life = 60ull * 60; // one hour
default_consensus_block_size = max_block_size::testnet4;
abla_config = abla::default_config(default_consensus_block_size, true);
break;
}
case domain::config::network::scalenet: {
easy_blocks = true;
asert_half_life = 2ull * 24 * 60 * 60; // two days
default_consensus_block_size = max_block_size::scalenet;
abla_config = abla::default_config(default_consensus_block_size, false);
break;
}
case domain::config::network::chipnet: {
easy_blocks = true;
asert_half_life = 60ull * 60; // one hour
default_consensus_block_size = max_block_size::chipnet;
abla_config = abla::default_config(default_consensus_block_size, false);
break;
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions src/validate/validate_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void validate_block::handle_checked(code const& ec, block_const_ptr block, resul
}

// Run context free checks, sets time internally.
handler(block->check(get_max_block_size(network_)));
handler(block->check());
}

// Accept sequence.
Expand Down Expand Up @@ -336,7 +336,8 @@ void validate_block::connect_inputs(block_const_ptr block, size_t bucket, size_t

#if defined(KTH_CURRENCY_BCH)
block_sigchecks += sigchecks;
if (block_sigchecks > get_max_block_sigchecks(network_)) {
// if (block_sigchecks > get_max_block_sigchecks(network_)) {
if (block_sigchecks > block->validation.state->dynamic_max_block_sigchecks()) {
ec = error::block_sigchecks_limit;
break;
}
Expand Down