hardfork: rescan speedup

Add a block height before which version 1 is assumed
Use DB transactions
This commit is contained in:
moneromooo-monero 2015-09-26 16:22:57 +01:00
parent d9236396de
commit 4cf3028ba5
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
4 changed files with 49 additions and 23 deletions

View file

@ -80,6 +80,7 @@ static const struct {
// version 2 can start from block 1009827, setup on the 20th of september
{ 2, 1009827, 1442763710 },
};
static const uint64_t mainnet_hard_fork_version_1_till = 750000;
static const struct {
uint8_t version;
@ -89,6 +90,7 @@ static const struct {
// version 1 from the start of the blockchain
{ 1, 1, 1341378000 },
};
static const uint64_t testnet_hard_fork_version_1_till = 540000;
//------------------------------------------------------------------
Blockchain::Blockchain(tx_memory_pool& tx_pool) :
@ -296,13 +298,14 @@ bool Blockchain::init(BlockchainDB* db, const bool testnet)
m_db = db;
m_hardfork = new HardFork(*db);
if (testnet) {
m_hardfork = new HardFork(*db, 1, testnet_hard_fork_version_1_till);
for (size_t n = 0; n < sizeof(testnet_hard_forks) / sizeof(testnet_hard_forks[0]); ++n)
m_hardfork->add(testnet_hard_forks[n].version, testnet_hard_forks[n].height, testnet_hard_forks[n].time);
}
else
{
m_hardfork = new HardFork(*db, 1, mainnet_hard_fork_version_1_till);
for (size_t n = 0; n < sizeof(mainnet_hard_forks) / sizeof(mainnet_hard_forks[0]); ++n)
m_hardfork->add(mainnet_hard_forks[n].version, mainnet_hard_forks[n].height, mainnet_hard_forks[n].time);
}

View file

@ -35,9 +35,10 @@
using namespace cryptonote;
HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, time_t forked_time, time_t update_time, uint64_t window_size, int threshold_percent):
HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint64_t original_version_till_height, time_t forked_time, time_t update_time, uint64_t window_size, int threshold_percent):
db(db),
original_version(original_version),
original_version_till_height(original_version_till_height),
forked_time(forked_time),
update_time(update_time),
window_size(window_size),
@ -68,9 +69,8 @@ bool HardFork::add(uint8_t version, uint64_t height, time_t time)
return true;
}
uint8_t HardFork::get_effective_version(const cryptonote::block &block) const
uint8_t HardFork::get_effective_version(uint8_t version) const
{
uint8_t version = block.major_version;
if (!heights.empty()) {
uint8_t max_version = heights.back().version;
if (version > max_version)
@ -79,27 +79,27 @@ uint8_t HardFork::get_effective_version(const cryptonote::block &block) const
return version;
}
bool HardFork::do_check(const cryptonote::block &block) const
bool HardFork::do_check(uint8_t version) const
{
return block.major_version >= heights[current_fork_index].version;
return version >= heights[current_fork_index].version;
}
bool HardFork::check(const cryptonote::block &block) const
{
CRITICAL_REGION_LOCAL(lock);
return do_check(block);
return do_check(block.major_version);
}
bool HardFork::add(const cryptonote::block &block, uint64_t height)
bool HardFork::add(uint8_t block_version, uint64_t height)
{
CRITICAL_REGION_LOCAL(lock);
if (!do_check(block))
if (!do_check(block_version))
return false;
db.set_hard_fork_version(height, heights[current_fork_index].version);
const uint8_t version = get_effective_version(block);
const uint8_t version = get_effective_version(block_version);
while (versions.size() >= window_size) {
const uint8_t old_version = versions.front();
@ -123,6 +123,11 @@ bool HardFork::add(const cryptonote::block &block, uint64_t height)
return true;
}
bool HardFork::add(const cryptonote::block &block, uint64_t height)
{
return add(block.major_version, height);
}
void HardFork::init()
{
CRITICAL_REGION_LOCAL(lock);
@ -154,12 +159,24 @@ void HardFork::init()
LOG_PRINT_L1("reorganization done");
}
uint8_t HardFork::get_block_version(uint64_t height) const
{
if (height <= original_version_till_height)
return original_version;
const cryptonote::block &block = db.get_block_from_height(height);
return block.major_version;
}
bool HardFork::reorganize_from_block_height(uint64_t height)
{
CRITICAL_REGION_LOCAL(lock);
if (height >= db.height())
return false;
//db.set_batch_transactions(true);
//db.batch_start();
versions.clear();
for (size_t n = 0; n < 256; ++n)
@ -172,7 +189,7 @@ bool HardFork::reorganize_from_block_height(uint64_t height)
}
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);
const uint8_t v = get_effective_version(b.major_version);
last_versions[v]++;
versions.push_back(v);
}
@ -188,9 +205,11 @@ bool HardFork::reorganize_from_block_height(uint64_t height)
const uint64_t bc_height = db.height();
for (uint64_t h = height + 1; h < bc_height; ++h) {
add(db.get_block_from_height(h), h);
add(get_block_version(h), h);
}
//db.batch_stop();
return true;
}

View file

@ -44,6 +44,7 @@ namespace cryptonote
Ready,
} State;
static const uint64_t DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT = 0; // <= actual height
static const time_t DEFAULT_FORKED_TIME = 31557600; // a year in seconds
static const time_t DEFAULT_UPDATE_TIME = 31557600 / 2;
static const uint64_t DEFAULT_WINDOW_SIZE = 50; // supermajority window check length
@ -58,7 +59,7 @@ namespace cryptonote
* @param window_size the size of the window in blocks to consider for version voting
* @param threshold_percent the size of the majority in percents
*/
HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, int threshold_percent = DEFAULT_THRESHOLD_PERCENT);
HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, uint64_t original_version_till_height = DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, int threshold_percent = DEFAULT_THRESHOLD_PERCENT);
/**
* @brief add a new hardfork height
@ -182,9 +183,11 @@ namespace cryptonote
private:
bool do_check(const cryptonote::block &block) const;
uint8_t get_block_version(uint64_t height) const;
bool do_check(uint8_t version) const;
int get_voted_fork_index(uint64_t height) const;
uint8_t get_effective_version(const cryptonote::block &block) const;
uint8_t get_effective_version(uint8_t version) const;
bool add(uint8_t block_version, uint64_t height);
private:
@ -196,6 +199,7 @@ namespace cryptonote
int threshold_percent;
uint8_t original_version;
uint64_t original_version_till_height;
struct Params {
uint8_t version;

View file

@ -194,7 +194,7 @@ TEST(states, Success)
TEST(steps_asap, Success)
{
TestDB db;
HardFork hf(db, 1,1,1,1);
HardFork hf(db, 1,0,1,1,1);
// v h t
ASSERT_TRUE(hf.add(1, 0, 0));
@ -223,7 +223,7 @@ TEST(steps_asap, Success)
TEST(steps_1, Success)
{
TestDB db;
HardFork hf(db, 1,1,1,1);
HardFork hf(db, 1,0,1,1,1);
ASSERT_TRUE(hf.add(1, 0, 0));
for (int n = 1 ; n < 10; ++n)
@ -244,7 +244,7 @@ TEST(reorganize, Same)
{
for (int history = 1; history <= 12; ++history) {
TestDB db;
HardFork hf(db, 1, 1, 1, history, 100);
HardFork hf(db, 1, 0, 1, 1, history, 100);
// v h t
ASSERT_TRUE(hf.add(1, 0, 0));
@ -274,7 +274,7 @@ TEST(reorganize, Changed)
{
int history = 4;
TestDB db;
HardFork hf(db, 1, 1, 1, 4, 100);
HardFork hf(db, 1, 0, 1, 1, 4, 100);
// v h t
ASSERT_TRUE(hf.add(1, 0, 0));
@ -323,7 +323,7 @@ TEST(voting, threshold)
{
for (int threshold = 87; threshold <= 88; ++threshold) {
TestDB db;
HardFork hf(db, 1, 1, 1, 8, threshold);
HardFork hf(db, 1, 0, 1, 1, 8, threshold);
// v h t
ASSERT_TRUE(hf.add(1, 0, 0));
@ -351,7 +351,7 @@ TEST(voting, threshold)
TEST(new_blocks, denied)
{
TestDB db;
HardFork hf(db, 1, 1, 1, 4, 50);
HardFork hf(db, 1, 0, 1, 1, 4, 50);
// v h t
ASSERT_TRUE(hf.add(1, 0, 0));
@ -377,7 +377,7 @@ TEST(new_blocks, denied)
TEST(new_version, early)
{
TestDB db;
HardFork hf(db, 1, 1, 1, 4, 50);
HardFork hf(db, 1, 0, 1, 1, 4, 50);
// v h t
ASSERT_TRUE(hf.add(1, 0, 0));
@ -400,7 +400,7 @@ TEST(new_version, early)
TEST(reorganize, changed)
{
TestDB db;
HardFork hf(db, 1, 1, 1, 4, 50);
HardFork hf(db, 1, 0, 1, 1, 4, 50);
// v h t
ASSERT_TRUE(hf.add(1, 0, 0));