forked from lthn/blockchain
changed difficulty adjustment
This commit is contained in:
parent
f0dfcf630d
commit
c9f93364ad
10 changed files with 109 additions and 27 deletions
|
|
@ -69,14 +69,6 @@ using namespace currency;
|
|||
#define BLOCKCHAIN_HEIGHT_FOR_POS_STRICT_SEQUENCE_LIMITATION 18000
|
||||
#endif
|
||||
|
||||
#define BLOCK_MAJOR_VERSION_INITAL 1
|
||||
|
||||
#ifndef TESTNET
|
||||
#define ZANO_HARDFORK_1_AFTER_HEIGHT ??
|
||||
#else
|
||||
#define ZANO_HARDFORK_1_AFTER_HEIGHT ??
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
DISABLE_VS_WARNINGS(4267)
|
||||
|
|
@ -979,12 +971,21 @@ wide_difficulty_type blockchain_storage::get_next_diff_conditional(bool pos) con
|
|||
wide_difficulty_type& dif = pos ? m_cached_next_pos_difficulty : m_cached_next_pow_difficulty;
|
||||
TIME_MEASURE_FINISH_PD(target_calculating_enum_blocks);
|
||||
TIME_MEASURE_START_PD(target_calculating_calc);
|
||||
dif = next_difficulty(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET);
|
||||
if (m_db_blocks.size() > ZANO_HARDFORK_1_AFTER_HEIGHT)
|
||||
{
|
||||
dif = next_difficulty_2(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET);
|
||||
}
|
||||
else
|
||||
{
|
||||
dif = next_difficulty_1(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET);
|
||||
}
|
||||
|
||||
|
||||
TIME_MEASURE_FINISH_PD(target_calculating_calc);
|
||||
return dif;
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
wide_difficulty_type blockchain_storage::get_next_diff_conditional2(bool pos, const alt_chain_type& alt_chain, uint64_t split_height) const
|
||||
wide_difficulty_type blockchain_storage::get_next_diff_conditional2(bool pos, const alt_chain_type& alt_chain, uint64_t split_height, const alt_block_extended_info& abei) const
|
||||
{
|
||||
CRITICAL_REGION_LOCAL(m_read_lock);
|
||||
std::vector<uint64_t> timestamps;
|
||||
|
|
@ -1007,7 +1008,13 @@ wide_difficulty_type blockchain_storage::get_next_diff_conditional2(bool pos, co
|
|||
return true;
|
||||
};
|
||||
enum_blockchain(cb, alt_chain, split_height);
|
||||
return next_difficulty(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET);
|
||||
|
||||
wide_difficulty_type diff = 0;
|
||||
if(abei.height > ZANO_HARDFORK_1_AFTER_HEIGHT)
|
||||
diff = next_difficulty_2(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET);
|
||||
else
|
||||
diff = next_difficulty_1(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET : DIFFICULTY_POW_TARGET);
|
||||
return diff;
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
wide_difficulty_type blockchain_storage::get_cached_next_difficulty(bool pos) const
|
||||
|
|
@ -1066,7 +1073,7 @@ wide_difficulty_type blockchain_storage::get_next_difficulty_for_alternative_cha
|
|||
commulative_difficulties.push_back(m_db_blocks[i]->cumulative_diff_precise);
|
||||
}
|
||||
|
||||
return next_difficulty(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET:DIFFICULTY_POW_TARGET);
|
||||
return next_difficulty_1(timestamps, commulative_difficulties, pos ? DIFFICULTY_POS_TARGET:DIFFICULTY_POW_TARGET);
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
bool blockchain_storage::prevalidate_miner_transaction(const block& b, uint64_t height, bool pos) const
|
||||
|
|
@ -1204,7 +1211,12 @@ bool blockchain_storage::create_block_template(block& b,
|
|||
size_t median_size;
|
||||
boost::multiprecision::uint128_t already_generated_coins;
|
||||
CRITICAL_REGION_BEGIN(m_read_lock);
|
||||
b.major_version = CURRENT_BLOCK_MAJOR_VERSION;
|
||||
height = m_db_blocks.size();
|
||||
if(height <= m_core_runtime_config.hard_fork1_starts_after_height)
|
||||
b.major_version = BLOCK_MAJOR_VERSION_INITAL;
|
||||
else
|
||||
b.major_version = CURRENT_BLOCK_MAJOR_VERSION;
|
||||
|
||||
b.minor_version = CURRENT_BLOCK_MINOR_VERSION;
|
||||
b.prev_id = get_top_block_id();
|
||||
b.timestamp = m_core_runtime_config.get_core_time();
|
||||
|
|
@ -1221,7 +1233,7 @@ bool blockchain_storage::create_block_template(block& b,
|
|||
CHECK_AND_ASSERT_MES(diffic, false, "difficulty owverhead.");
|
||||
|
||||
|
||||
height = m_db_blocks.size();
|
||||
|
||||
|
||||
median_size = m_db_current_block_cumul_sz_limit / 2;
|
||||
already_generated_coins = m_db_blocks.back()->already_generated_coins;
|
||||
|
|
@ -1507,7 +1519,7 @@ bool blockchain_storage::handle_alternative_block(const block& b, const crypto::
|
|||
CHECK_AND_ASSERT_MES_CUSTOM(!(pos_block && abei.height < m_core_runtime_config.pos_minimum_heigh), false, bvc.m_verification_failed = true, "PoS block is not allowed on this height");
|
||||
|
||||
|
||||
wide_difficulty_type current_diff = get_next_diff_conditional2(pos_block, alt_chain, connection_height);
|
||||
wide_difficulty_type current_diff = get_next_diff_conditional2(pos_block, alt_chain, connection_height, abei);
|
||||
|
||||
CHECK_AND_ASSERT_MES_CUSTOM(current_diff, false, bvc.m_verification_failed = true, "!!!!!!! DIFFICULTY OVERHEAD !!!!!!!");
|
||||
|
||||
|
|
@ -1716,7 +1728,7 @@ bool blockchain_storage::is_reorganize_required(const block_extended_info& main_
|
|||
wide_difficulty_type difficulty_pow_at_split_point = get_x_difficulty_after_height(connection_point.height - 1, false);
|
||||
|
||||
difficulties main_cumul_diff = AUTO_VAL_INIT(main_cumul_diff);
|
||||
difficulties alt_cumul_diff = = AUTO_VAL_INIT(alt_cumul_diff);
|
||||
difficulties alt_cumul_diff = AUTO_VAL_INIT(alt_cumul_diff);
|
||||
//we use get_last_alt_x_block_cumulative_precise_adj_difficulty for getting both alt chain and main chain diff of given block types
|
||||
|
||||
wide_difficulty_type alt_pos_diff_end = get_last_alt_x_block_cumulative_precise_adj_difficulty(alt_chain, alt_chain_bei.height, true);
|
||||
|
|
@ -2466,7 +2478,7 @@ bool blockchain_storage::forecast_difficulty(std::vector<std::pair<uint64_t, wid
|
|||
out_height_2_diff_vector.push_back(std::make_pair(height, last_block_diff_for_this_type)); // the first element corresponds to the last block of this type
|
||||
for (size_t i = 0; i < DIFFICULTY_CUT; ++i)
|
||||
{
|
||||
wide_difficulty_type diff = next_difficulty(timestamps, cumulative_difficulties, target_seconds);
|
||||
wide_difficulty_type diff = next_difficulty_1(timestamps, cumulative_difficulties, target_seconds);
|
||||
height += avg_interval;
|
||||
out_height_2_diff_vector.push_back(std::make_pair(height, diff));
|
||||
|
||||
|
|
@ -4912,13 +4924,25 @@ bool blockchain_storage::update_next_comulative_size_limit()
|
|||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
bool blockchain_storage::add_new_block(const block& bl_, block_verification_context& bvc)
|
||||
bool blockchain_storage::prevalidate_block(const block& bl)
|
||||
{
|
||||
if (bl.major_version == BLOCK_MAJOR_VERSION_INITAL && get_block_height(bl) <= m_core_runtime_config.hard_fork1_starts_after_height)
|
||||
return true;
|
||||
if (bl.major_version != CURRENT_BLOCK_MAJOR_VERSION)
|
||||
{
|
||||
LOG_ERROR("Failed to prevalidate_block: " << get_block_hash(bl) << ENDL << obj_to_json_str(bl));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
bool blockchain_storage::add_new_block(const block& bl, block_verification_context& bvc)
|
||||
{
|
||||
try
|
||||
{
|
||||
m_db.begin_transaction();
|
||||
|
||||
block bl = bl_;
|
||||
//block bl = bl_;
|
||||
crypto::hash id = get_block_hash(bl);
|
||||
CRITICAL_REGION_LOCAL(m_tx_pool);
|
||||
//CRITICAL_REGION_LOCAL1(m_read_lock);
|
||||
|
|
@ -4931,7 +4955,18 @@ bool blockchain_storage::add_new_block(const block& bl_, block_verification_cont
|
|||
return false;
|
||||
}
|
||||
|
||||
if (prevalidate_block(bl))
|
||||
{
|
||||
LOG_PRINT_RED_L0("block with id = " << id << " failed to prevalidate");
|
||||
bvc.m_added_to_main_chain = false;
|
||||
bvc.m_verification_failed = true;
|
||||
m_db.commit_transaction();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//check that block refers to chain tail
|
||||
|
||||
|
||||
if (!(bl.prev_id == get_top_block_id()))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -184,6 +184,7 @@ namespace currency
|
|||
|
||||
//------------- modifying members --------------
|
||||
bool add_new_block(const block& bl_, block_verification_context& bvc);
|
||||
bool prevalidate_block(const block& bl);
|
||||
bool clear();
|
||||
bool reset_and_set_genesis_block(const block& b);
|
||||
//debug function
|
||||
|
|
@ -230,7 +231,7 @@ namespace currency
|
|||
crypto::hash get_top_block_id(uint64_t& height) const;
|
||||
bool get_top_block(block& b) const;
|
||||
wide_difficulty_type get_next_diff_conditional(bool pos) const;
|
||||
wide_difficulty_type get_next_diff_conditional2(bool pos, const alt_chain_type& alt_chain, uint64_t split_height) const;
|
||||
wide_difficulty_type get_next_diff_conditional2(bool pos, const alt_chain_type& alt_chain, uint64_t split_height, const alt_block_extended_info& abei) const;
|
||||
wide_difficulty_type get_cached_next_difficulty(bool pos) const;
|
||||
|
||||
typedef bool fill_block_template_func_t(block &bl, bool pos, size_t median_size, const boost::multiprecision::uint128_t& already_generated_coins, size_t &total_size, uint64_t &fee, uint64_t height);
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ namespace currency
|
|||
uint64_t pos_minimum_heigh; //height
|
||||
uint64_t tx_pool_min_fee;
|
||||
uint64_t tx_default_fee;
|
||||
uint64_t hard_fork1_starts_after_height;
|
||||
crypto::public_key alias_validation_pubkey;
|
||||
core_time_func_t get_core_time;
|
||||
|
||||
|
|
@ -34,6 +35,7 @@ namespace currency
|
|||
pc.pos_minimum_heigh = POS_START_HEIGHT;
|
||||
pc.tx_pool_min_fee = TX_MINIMUM_FEE;
|
||||
pc.tx_default_fee = TX_DEFAULT_FEE;
|
||||
pc.hard_fork1_starts_after_height = ZANO_HARDFORK_1_AFTER_HEIGHT;
|
||||
pc.get_core_time = &core_runtime_config::_default_core_time_function;
|
||||
bool r = epee::string_tools::hex_to_pod(ALIAS_SHORT_NAMES_VALIDATION_PUB_KEY, pc.alias_validation_pubkey);
|
||||
CHECK_AND_ASSERT_THROW_MES(r, "failed to parse alias_validation_pub_key");
|
||||
|
|
|
|||
|
|
@ -207,6 +207,13 @@
|
|||
|
||||
#define CURRENT_MEMPOOL_ARCHIVE_VER (CURRENCY_FORMATION_VERSION+31)
|
||||
|
||||
//hard forks section
|
||||
#define BLOCK_MAJOR_VERSION_INITAL 1
|
||||
#ifndef TESTNET
|
||||
#define ZANO_HARDFORK_1_AFTER_HEIGHT ??
|
||||
#else
|
||||
#define ZANO_HARDFORK_1_AFTER_HEIGHT ??
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1874,7 +1874,7 @@ namespace currency
|
|||
//string_tools::parse_hexstr_to_binbuff(genesis_coinbase_tx_hex, tx_bl);
|
||||
bool r = parse_and_validate_tx_from_blob(tx_bl, bl.miner_tx);
|
||||
CHECK_AND_ASSERT_MES(r, false, "failed to parse coinbase tx from hard coded blob");
|
||||
bl.major_version = CURRENT_BLOCK_MAJOR_VERSION;
|
||||
bl.major_version = BLOCK_MAJOR_VERSION_INITAL;
|
||||
bl.minor_version = CURRENT_BLOCK_MINOR_VERSION;
|
||||
bl.timestamp = 0;
|
||||
bl.nonce = CURRENCY_GENESIS_NONCE;
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ namespace currency {
|
|||
return res.convert_to<wide_difficulty_type>();
|
||||
}
|
||||
|
||||
wide_difficulty_type next_difficulty(vector<uint64_t>& timestamps, vector<wide_difficulty_type>& cumulative_difficulties, size_t target_seconds)
|
||||
wide_difficulty_type next_difficulty_1(vector<uint64_t>& timestamps, vector<wide_difficulty_type>& cumulative_difficulties, size_t target_seconds)
|
||||
{
|
||||
|
||||
// timestamps - first is latest, back - is oldest timestamps
|
||||
|
|
@ -220,4 +220,40 @@ namespace currency {
|
|||
}
|
||||
return summ / devider;
|
||||
}
|
||||
|
||||
wide_difficulty_type next_difficulty_2(vector<uint64_t>& timestamps, vector<wide_difficulty_type>& cumulative_difficulties, size_t target_seconds)
|
||||
{
|
||||
|
||||
// timestamps - first is latest, back - is oldest timestamps
|
||||
if (timestamps.size() > DIFFICULTY_WINDOW)
|
||||
{
|
||||
timestamps.resize(DIFFICULTY_WINDOW);
|
||||
cumulative_difficulties.resize(DIFFICULTY_WINDOW);
|
||||
}
|
||||
|
||||
|
||||
size_t length = timestamps.size();
|
||||
CHECK_AND_ASSERT_MES(length == cumulative_difficulties.size(), 0, "Check \"length == cumulative_difficulties.size()\" failed");
|
||||
if (length <= 1)
|
||||
{
|
||||
return DIFFICULTY_STARTER;
|
||||
}
|
||||
static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small");
|
||||
|
||||
CHECK_AND_ASSERT_MES(length <= DIFFICULTY_WINDOW, 0, "length <= DIFFICULTY_WINDOW check failed, length=" << length);
|
||||
|
||||
sort(timestamps.begin(), timestamps.end(), std::greater<uint64_t>());
|
||||
|
||||
static_assert(2 * DIFFICULTY_CUT <= DIFFICULTY_WINDOW - 2, "Cut length is too large");
|
||||
wide_difficulty_type dif_slow = get_adjustment_for_zone(timestamps, cumulative_difficulties, target_seconds, DIFFICULTY_WINDOW, DIFFICULTY_CUT / 2, DIFFICULTY_CUT / 2);
|
||||
wide_difficulty_type dif_medium = get_adjustment_for_zone(timestamps, cumulative_difficulties, target_seconds, DIFFICULTY_WINDOW / 3, DIFFICULTY_CUT / 8, DIFFICULTY_CUT / 12);
|
||||
uint64_t devider = 1;
|
||||
wide_difficulty_type summ = dif_slow;
|
||||
if (dif_medium != 0)
|
||||
{
|
||||
summ += dif_medium;
|
||||
++devider;
|
||||
}
|
||||
return summ / devider;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ namespace currency
|
|||
typedef boost::multiprecision::uint128_t wide_difficulty_type;
|
||||
|
||||
bool check_hash(const crypto::hash &hash, wide_difficulty_type difficulty);
|
||||
wide_difficulty_type next_difficulty(std::vector<std::uint64_t>& timestamps, std::vector<wide_difficulty_type>& cumulative_difficulties, size_t target_seconds);
|
||||
wide_difficulty_type next_difficulty_1(std::vector<std::uint64_t>& timestamps, std::vector<wide_difficulty_type>& cumulative_difficulties, size_t target_seconds);
|
||||
wide_difficulty_type next_difficulty_2(std::vector<std::uint64_t>& timestamps, std::vector<wide_difficulty_type>& cumulative_difficulties, size_t target_seconds);
|
||||
uint64_t difficulty_to_boundary(wide_difficulty_type difficulty);
|
||||
void difficulty_to_boundary_long(wide_difficulty_type difficulty, crypto::hash& result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace
|
|||
for (size_t i = 0; i < new_block_count; ++i)
|
||||
{
|
||||
block blk_next;
|
||||
wide_difficulty_type diffic = next_difficulty(timestamps, cummulative_difficulties, DIFFICULTY_POW_TARGET);
|
||||
wide_difficulty_type diffic = next_difficulty_1(timestamps, cummulative_difficulties, DIFFICULTY_POW_TARGET);
|
||||
if (!generator.construct_block_manually(blk_next, blk_prev, miner_account,
|
||||
test_generator::bf_timestamp | test_generator::bf_diffic, 0, 0, blk_prev.timestamp, crypto::hash(), diffic))
|
||||
return false;
|
||||
|
|
@ -152,7 +152,7 @@ bool gen_block_invalid_nonce::generate(std::vector<test_event_entry>& events) co
|
|||
return false;
|
||||
|
||||
// Create invalid nonce
|
||||
wide_difficulty_type diffic = next_difficulty(timestamps, commulative_difficulties, DIFFICULTY_POW_TARGET);
|
||||
wide_difficulty_type diffic = next_difficulty_1(timestamps, commulative_difficulties, DIFFICULTY_POW_TARGET);
|
||||
CHECK_AND_ASSERT_MES(diffic > 1, false, "diffic > 1 validation failed");
|
||||
const block& blk_last = boost::get<block>(events.back());
|
||||
uint64_t timestamp = blk_last.timestamp;
|
||||
|
|
|
|||
|
|
@ -673,7 +673,7 @@ currency::wide_difficulty_type test_generator::get_difficulty_for_next_block(con
|
|||
timestamps.push_back(blocks[i]->b.timestamp);
|
||||
commulative_difficulties.push_back(blocks[i]->cumul_difficulty);
|
||||
}
|
||||
return next_difficulty(timestamps, commulative_difficulties, pow ? DIFFICULTY_POW_TARGET : DIFFICULTY_POS_TARGET);
|
||||
return next_difficulty_1(timestamps, commulative_difficulties, pow ? DIFFICULTY_POW_TARGET : DIFFICULTY_POS_TARGET);
|
||||
}
|
||||
|
||||
currency::wide_difficulty_type test_generator::get_cumul_difficulty_for_next_block(const crypto::hash& head_id, bool pow) const
|
||||
|
|
|
|||
|
|
@ -313,7 +313,7 @@ void run_emulation(const std::string& path)
|
|||
PERFORME_SIMULATION_FOR_FUNCTION(bbr_next_difficulty_configurable, BBR_DIFFICULTY_WINDOW, BBR_DIFFICULTY_CUT, BBR_DIFFICULTY_CUT);
|
||||
PERFORME_SIMULATION_FOR_FUNCTION(bbr_next_difficulty_configurable, 500, 60, 60);
|
||||
PERFORME_SIMULATION_FOR_FUNCTION(bbr_next_difficulty_configurable, 300, 60, 60);
|
||||
PERFORME_SIMULATION_FOR_FUNCTION_NO_WINDOW(currency::next_difficulty);
|
||||
PERFORME_SIMULATION_FOR_FUNCTION_NO_WINDOW(currency::next_difficulty_1);
|
||||
|
||||
print_blocks(result_blocks, path + "result.txt");
|
||||
LOG_PRINT_L0("Done");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue