add_fork: allow == timestamps and throw on error

Very annoyingly having equal timestamps just ignored the fork because
the `return false` error wasn't caught anywhere.  Allow equal timestamps
between subsequent works, and throw an exception on such errors so that
bad fork data is an error rather than just ignored.
This commit is contained in:
Jason Rhinelander 2020-08-11 21:48:47 -03:00
parent 0b62b33e09
commit 4ae80d712d
2 changed files with 23 additions and 17 deletions

View file

@ -132,30 +132,31 @@ HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, time_
throw std::logic_error{"default_threshold_percent needs to be between 0 and 100"};
}
bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
void HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
{
std::unique_lock l{lock};
// add in order
if (version == 0)
return false;
throw std::runtime_error{"Cannot add a hard fork with HF version 0"};
if (!heights.empty()) {
if (version <= heights.back().version)
return false;
if (height <= heights.back().height)
return false;
if (time <= heights.back().time)
return false;
const auto& [v, h, _thresh, t] = heights.back();
if (version <= v)
throw std::runtime_error{"Cannot add hard fork: version(" + std::to_string(version) + ") must be > previous HF version(" + std::to_string(v) + ")"};
if (height <= h)
throw std::runtime_error{"Cannot add hard fork: height(" + std::to_string(height) + ") must be > previous HF height(" + std::to_string(h) + ")"};
if (time < t)
throw std::runtime_error{"Cannot add hard fork: timestamp(" + std::to_string(time) + ") must be >= previous HF timestamp(" + std::to_string(t) + ")"};
}
if (threshold > 100)
return false;
throw std::runtime_error{"Cannot add hard fork: invalid threshold (" + std::to_string(threshold) + ")"};
heights.push_back({version, height, threshold, time});
return true;
}
bool HardFork::add_fork(uint8_t version, uint64_t height, time_t time)
void HardFork::add_fork(uint8_t version, uint64_t height, time_t time)
{
return add_fork(version, height, default_threshold_percent, time);
add_fork(version, height, default_threshold_percent, time);
}
uint8_t HardFork::get_effective_version(uint8_t voting_version) const

View file

@ -87,12 +87,15 @@ namespace cryptonote
*
* returns true if no error, false otherwise
*
* @param version the major block version for the fork
* @param height The height the hardfork takes effect
* @param version the major block version for the fork, must be > 0 and > the last-added hf
* @param height The height the hardfork takes effect; must be > the last-added hf
* @param threshold The threshold of votes needed for this fork (0-100)
* @param time Approximate time of the hardfork (seconds since epoch)
* @param time Approximate time of the hardfork (seconds since epoch); must be >= the timestamp
* of the last-added hf
*
* @throws std::invalid_argument if any parameters are invalid
*/
bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time);
void add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time);
/**
* @brief add a new hardfork height
@ -102,8 +105,10 @@ namespace cryptonote
* @param version the major block version for the fork
* @param height The height the hardfork takes effect
* @param time Approximate time of the hardfork (seconds since epoch)
*
* @throws std::invalid_argument if any parameters are invalid
*/
bool add_fork(uint8_t version, uint64_t height, time_t time);
void add_fork(uint8_t version, uint64_t height, time_t time);
/**
* @brief initialize the object