hardfork: simplify work done on reload

There is no need to fully recalculate and rewrite state, just
refill state from the DB.
This commit is contained in:
moneromooo-monero 2015-10-16 19:38:44 +01:00
parent e6d2460263
commit 439c4555e9
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 40 additions and 1 deletions

View file

@ -150,12 +150,15 @@ void HardFork::init()
height = 1; height = 1;
} }
LOG_PRINT_L1("reorganizing from " << height); LOG_PRINT_L1("reorganizing from " << height);
reorganize_from_chain_height(height);
if (populate) { if (populate) {
reorganize_from_chain_height(height);
// reorg will not touch the genesis block, use this as a flag for populating done // reorg will not touch the genesis block, use this as a flag for populating done
db.set_hard_fork_version(0, original_version); db.set_hard_fork_version(0, original_version);
db.set_hard_fork_starting_height(original_version, 0); db.set_hard_fork_starting_height(original_version, 0);
} }
else {
rescan_from_chain_height(height);
}
LOG_PRINT_L1("reorganization done"); LOG_PRINT_L1("reorganization done");
} }
@ -220,6 +223,39 @@ bool HardFork::reorganize_from_chain_height(uint64_t height)
return reorganize_from_block_height(height - 1); return reorganize_from_block_height(height - 1);
} }
bool HardFork::rescan_from_block_height(uint64_t height)
{
CRITICAL_REGION_LOCAL(lock);
if (height >= db.height())
return false;
versions.clear();
for (size_t n = 0; n < 256; ++n)
last_versions[n] = 0;
const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0;
for (uint64_t h = rescan_height; h <= height; ++h) {
cryptonote::block b = db.get_block_from_height(h);
const uint8_t v = get_effective_version(b.major_version);
last_versions[v]++;
versions.push_back(v);
}
uint8_t lastv = db.get_hard_fork_version(height);
current_fork_index = 0;
while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv)
++current_fork_index;
return true;
}
bool HardFork::rescan_from_chain_height(uint64_t height)
{
if (height == 0)
return false;
return rescan_from_block_height(height - 1);
}
int HardFork::get_voted_fork_index(uint64_t height) const int HardFork::get_voted_fork_index(uint64_t height) const
{ {
CRITICAL_REGION_LOCAL(lock); CRITICAL_REGION_LOCAL(lock);

View file

@ -189,6 +189,9 @@ namespace cryptonote
uint8_t get_effective_version(uint8_t version) const; uint8_t get_effective_version(uint8_t version) const;
bool add(uint8_t block_version, uint64_t height); bool add(uint8_t block_version, uint64_t height);
bool rescan_from_block_height(uint64_t height);
bool rescan_from_chain_height(uint64_t height);
private: private:
BlockchainDB &db; BlockchainDB &db;