1
0
Fork 0
forked from lthn/blockchain

fixed bug in get_est_height_from_date

This commit is contained in:
cryptozoidberg 2020-05-23 12:13:14 +02:00
parent 4db837bbc7
commit 2be08eae0d
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
2 changed files with 42 additions and 8 deletions

View file

@ -2995,9 +2995,10 @@ bool blockchain_storage::get_est_height_from_date(uint64_t date, uint64_t& res_h
CRITICAL_REGION_LOCAL(m_read_lock); CRITICAL_REGION_LOCAL(m_read_lock);
#define GET_EST_HEIGHT_FROM_DATE_THRESHOLD 1440 #define GET_EST_HEIGHT_FROM_DATE_THRESHOLD 1440
res_h = 0;
if (date < get_blockchain_launch_timestamp()) if (date < get_blockchain_launch_timestamp())
{ {
res_h = 0;
return true; return true;
} }
@ -3012,14 +3013,13 @@ bool blockchain_storage::get_est_height_from_date(uint64_t date, uint64_t& res_h
if (m_db_blocks.size() > 1440) if (m_db_blocks.size() > 1440)
{ {
res_h = m_db_blocks.size() - 1440; res_h = m_db_blocks.size() - 1440;
return true;
} }
else else
{ {
//likely impossible, but just in case //likely impossible, but just in case
res_h = 0; res_h = 0;
} }
return true;
} }
if (calculated_estimated_height > m_db_blocks.size() - 1) if (calculated_estimated_height > m_db_blocks.size() - 1)
calculated_estimated_height = m_db_blocks.size() - 1; calculated_estimated_height = m_db_blocks.size() - 1;
@ -3030,27 +3030,59 @@ bool blockchain_storage::get_est_height_from_date(uint64_t date, uint64_t& res_h
uint64_t aim = date - 46800; uint64_t aim = date - 46800;
uint64_t high_boundary = date - 3600; //1 hour uint64_t high_boundary = date - 3600; //1 hour
std::cout << "ENTRY: low_boundary(minutes):" << low_boundary/60 << " high_boundary(minutes): " << high_boundary / 60 << std::endl;
uint64_t iteration_coun = 0; uint64_t iteration_coun = 0;
uint64_t current_low_boundary = 0;
uint64_t current_hight_boundary = m_db_blocks.size() - 1;
while (true) while (true)
{ {
iteration_coun++; iteration_coun++;
if (iteration_coun > 29) // Log2(CURRENCY_MAX_BLOCK_NUMBER)
{
LOG_ERROR("Internal error: too much iterations on get_est_height_from_date, date = " << date);
return true;
}
uint64_t correction = 0; uint64_t correction = 0;
uint64_t ts = m_db_blocks[calculated_estimated_height]->bl.timestamp; uint64_t ts = m_db_blocks[calculated_estimated_height]->bl.timestamp;
if (ts > high_boundary) if (ts > high_boundary)
{ {
//we moved too much forward //we moved too much forward
uint64_t offset = (ts - aim) / DIFFICULTY_TOTAL_TARGET;
if (offset > calculated_estimated_height) current_hight_boundary = calculated_estimated_height;
CHECK_AND_ASSERT_MES(current_hight_boundary > current_low_boundary, true,
"Internal error: current_hight_boundary(" << current_hight_boundary << ") > current_low_boundary("<< current_low_boundary << ")");
uint64_t offset = (current_hight_boundary - current_low_boundary)/2;
if (offset <= 2)
{ {
res_h = 0; //something really wrong with distribution of blocks, just use current_low_boundary to be sure that we didn't mess any transactions
break; res_h = current_low_boundary;
return true;
} }
std::cout << "est_h:" << calculated_estimated_height << ", ts(min): " << ts / 60 << " distance to RIGHT minutes: " << int64_t((int64_t(ts) - int64_t(high_boundary))) / 60 << std::endl;
std::cout << "OOFFSET: -" << offset << std::endl;
calculated_estimated_height -= offset; calculated_estimated_height -= offset;
} }
else if (ts < low_boundary) else if (ts < low_boundary)
{ {
//we too much in past //we too much in past
calculated_estimated_height += (aim - ts) / DIFFICULTY_TOTAL_TARGET; current_low_boundary = calculated_estimated_height;
CHECK_AND_ASSERT_MES(current_hight_boundary > current_low_boundary, true,
"Internal error: current_hight_boundary(" << current_hight_boundary << ") > current_low_boundary(" << current_low_boundary << ")");
uint64_t offset = (current_hight_boundary - current_low_boundary) / 2;
if (offset <= 2)
{
//something really wrong with distribution of blocks, just use current_low_boundary to be sure that we didn't mess any transactions
res_h = current_low_boundary;
return true;
}
//CHECK_AND_ASSERT_MES(offset > 2, true,
// "offset is too low = " << offset);
std::cout << "est_h:" << calculated_estimated_height << ", ts(min): " << ts / 60 << " distance to LEFT minutes: " << int64_t((int64_t(low_boundary) - int64_t(ts))) / 60 << std::endl;
std::cout << "OOFFSET: +" << offset << std::endl;
calculated_estimated_height += offset;
} }
else else
{ {
@ -3058,6 +3090,7 @@ bool blockchain_storage::get_est_height_from_date(uint64_t date, uint64_t& res_h
break; break;
} }
} }
LOG_PRINT_L0("[get_est_height_from_date] returned " << calculated_estimated_height << " with " << iteration_coun << " iterations"); LOG_PRINT_L0("[get_est_height_from_date] returned " << calculated_estimated_height << " with " << iteration_coun << " iterations");
return true; return true;
} }

View file

@ -0,0 +1 @@
{"method": "get_est_height_from_date","params": {"timestamp": 1575501289}}