Skip to content

Commit

Permalink
Use median timestamp if current time renders a block invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
thaerkh committed Apr 30, 2018
1 parent 67322d0 commit 9997556
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Dates are provided in the format YYYY-MM-DD.
| 2017-10-05 | v2 | 0.1.2.0 | 0.1.2.0 | Difficulty adjustment algorithm adjusted |
| 2017-11-29 | v3 | 0.1.3.0 | 0.1.3.1 | Difficulty adjustment algorithm updated to WWHM |
| 2017-12-06 | v4 | 0.1.4.0 | 0.1.4.0 | Difficulty adjustment tweaks |
| 2018-05-01 | v5 | 0.2.0.0 | 0.2.0.1 | Upstream track of v0.12.0 with Multisig, Subaddresses, CN variant 1 |
| 2018-05-01 | v5 | 0.2.0.0 | 0.2.0.2 | Upstream track of v0.12.0 with Multisig, Subaddresses, CN variant 1 |

X's indicate that these details have not been determined as of commit date.

Expand Down
27 changes: 23 additions & 4 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,11 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m
b.prev_id = get_tail_id();
b.timestamp = time(NULL);

uint64_t median_timestamp;
if (!check_median_block_timestamp(b, median_timestamp)) {
b.timestamp = median_timestamp;
}

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

Expand Down Expand Up @@ -2916,10 +2921,10 @@ uint64_t Blockchain::get_adjusted_time() const
}
//------------------------------------------------------------------
//TODO: revisit, has changed a bit on upstream
bool Blockchain::check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b) const
bool Blockchain::check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b, uint64_t& median_ts) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
uint64_t median_ts = epee::misc_utils::median(timestamps);
median_ts = epee::misc_utils::median(timestamps);
size_t blockchain_timestamp_check_window = get_current_hard_fork_version() < 2 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2;

if(b.timestamp < median_ts)
Expand All @@ -2930,6 +2935,13 @@ bool Blockchain::check_block_timestamp(std::vector<uint64_t>& timestamps, const

return true;
}

bool Blockchain::check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b) const
{
uint64_t median_ts;
return check_block_timestamp(timestamps, b, median_ts);
}

//------------------------------------------------------------------
// This function grabs the timestamps from the most recent <n> blocks,
// where n = BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW. If there are not those many
Expand All @@ -2942,13 +2954,20 @@ bool Blockchain::check_block_timestamp(const block& b) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
uint64_t cryptonote_block_future_time_limit = get_current_hard_fork_version() < 2 ? CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT : CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT_V2;
size_t blockchain_timestamp_check_window = get_current_hard_fork_version() < 2 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2;
if(b.timestamp > get_adjusted_time() + cryptonote_block_future_time_limit)
{
MERROR_VER("Timestamp of block with id: " << get_block_hash(b) << ", " << b.timestamp << ", bigger than adjusted time + " << (get_current_hard_fork_version() < 2 ? "2 hours" : "30 minutes"));
return false;
}

uint64_t median_ts;
return check_median_block_timestamp(b, median_ts);
}

bool Blockchain::check_median_block_timestamp(const block& b, uint64_t& median_ts) const
{
size_t blockchain_timestamp_check_window = get_current_hard_fork_version() < 2 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2;

// if not enough blocks, no proper median yet, return true
if(m_db->height() < blockchain_timestamp_check_window)
{
Expand All @@ -2965,7 +2984,7 @@ bool Blockchain::check_block_timestamp(const block& b) const
timestamps.push_back(m_db->get_block_timestamp(offset));
}

return check_block_timestamp(timestamps, b);
return check_block_timestamp(timestamps, b, median_ts);
}
//------------------------------------------------------------------
void Blockchain::return_tx_to_pool(std::vector<transaction> &txs)
Expand Down
10 changes: 10 additions & 0 deletions src/cryptonote_core/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,16 @@ namespace cryptonote
*/
bool check_block_timestamp(const block& b) const;

/**
* @brief wrapper for check_block_timestamp(const block& b)
*/
bool check_block_timestamp(std::vector<uint64_t>& timestamps, const block& b, uint64_t& median_ts) const;

/**
* @brief checks and returns median block timestamp as defined by protocol
*/
bool check_median_block_timestamp(const block& b, uint64_t& median_ts) const;

/**
* @brief checks a block's timestamp
*
Expand Down
2 changes: 1 addition & 1 deletion src/version.cpp.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define DEF_MONERO_VERSION_TAG "@VERSIONTAG@"
#define DEF_MONERO_VERSION "0.2.0.1"
#define DEF_MONERO_VERSION "0.2.0.2"
#define DEF_MONERO_RELEASE_NAME "Brazen Badger"
#define DEF_MONERO_VERSION_FULL DEF_MONERO_VERSION "-" DEF_MONERO_VERSION_TAG

Expand Down

0 comments on commit 9997556

Please sign in to comment.