Skip to content

Commit

Permalink
Merge pull request #118 from jagerman/fix-block-timestamp
Browse files Browse the repository at this point in the history
Don't generate a block template with invalid timestamp
  • Loading branch information
mbg033 authored Aug 7, 2018
2 parents 5f94911 + 5b349da commit 772db03
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1146,8 +1146,26 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m
b.major_version = m_hardfork->get_current_version();
b.minor_version = m_hardfork->get_ideal_version();
b.prev_id = get_tail_id();

b.timestamp = time(NULL);

uint8_t version = get_current_hard_fork_version();
uint64_t blockchain_timestamp_check_window = version < 9 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V9;

if(m_db->height() >= blockchain_timestamp_check_window) {
std::vector<uint64_t> timestamps;
auto h = m_db->height();

for(size_t offset = h - blockchain_timestamp_check_window; offset < h; ++offset)
{
timestamps.push_back(m_db->get_block_timestamp(offset));
}
uint64_t median_ts = epee::misc_utils::median(timestamps);
if (b.timestamp < median_ts) {
b.timestamp = median_ts;
}
}

diffic = get_difficulty_for_next_block();
CHECK_AND_ASSERT_MES(diffic, false, "difficulty overhead.");

Expand Down

0 comments on commit 772db03

Please sign in to comment.