Merge pull request #911

d7b681c remove hf_starting_height db (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2016-07-20 14:01:27 +02:00
commit 8e143fcf1d
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
9 changed files with 9 additions and 186 deletions

View file

@ -2186,34 +2186,6 @@ std::map<uint64_t, uint64_t>::BlockchainBDB::get_output_histogram(const std::vec
throw1(DB_ERROR("Not implemented.")); throw1(DB_ERROR("Not implemented."));
} }
void BlockchainBDB::set_hard_fork_starting_height(uint8_t version, uint64_t height)
{
LOG_PRINT_L3("BlockchainBDB::" << __func__);
check_open();
Dbt_copy<uint32_t> val_key(version + 1);
Dbt_copy<uint64_t> val(height);
if (m_hf_starting_heights->put(DB_DEFAULT_TX, &val_key, &val, 0))
throw1(DB_ERROR("Error adding hard fork starting height to db transaction."));
}
uint64_t BlockchainBDB::get_hard_fork_starting_height(uint8_t version) const
{
LOG_PRINT_L3("BlockchainBDB::" << __func__);
check_open();
Dbt_copy<uint32_t> key(version + 1);
Dbt_copy<uint64_t> result;
auto get_result = m_hf_starting_heights->get(DB_DEFAULT_TX, &key, &result, 0);
if (get_result == DB_NOTFOUND || get_result == DB_KEYEMPTY)
return std::numeric_limits<uint64_t>::max();
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve hard fork starting height from the db"));
return result;
}
void BlockchainBDB::check_hard_fork_info() void BlockchainBDB::check_hard_fork_info()
{ {
LOG_PRINT_L3("BlockchainBDB::" << __func__); LOG_PRINT_L3("BlockchainBDB::" << __func__);

View file

@ -385,8 +385,6 @@ private:
virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const; virtual bool for_all_outputs(std::function<bool(uint64_t amount, const crypto::hash &tx_hash, size_t tx_idx)> f) const;
// Hard fork related storage // Hard fork related storage
virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height);
virtual uint64_t get_hard_fork_starting_height(uint8_t version) const;
virtual void set_hard_fork_version(uint64_t height, uint8_t version); virtual void set_hard_fork_version(uint64_t height, uint8_t version);
virtual uint8_t get_hard_fork_version(uint64_t height) const; virtual uint8_t get_hard_fork_version(uint64_t height) const;
virtual void check_hard_fork_info(); virtual void check_hard_fork_info();

View file

@ -1274,27 +1274,6 @@ public:
// Hard fork related storage // Hard fork related storage
// //
// FIXME: verify that this is all correct
// - TW
/**
* @brief sets the height at which a hard fork has been voted to happen
*
*
* @param version the version voted to fork to
* @param height the height of the first block on the new fork
*/
virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height) = 0;
/**
* @brief gets the height at which a hard fork has been voted to happen
*
* @param version the version to check
*
* @return the height at which the hard fork was accepted, if it has been,
* otherwise max(uint64_t)
*/
virtual uint64_t get_hard_fork_starting_height(uint8_t version) const = 0;
/** /**
* @brief sets which hardfork version a height is on * @brief sets which hardfork version a height is on
* *

View file

@ -117,13 +117,6 @@ int compare_uint64(const MDB_val *a, const MDB_val *b)
return (va < vb) ? -1 : va > vb; return (va < vb) ? -1 : va > vb;
} }
int compare_uint8(const MDB_val *a, const MDB_val *b)
{
const uint8_t va = *(const uint8_t*)a->mv_data;
const uint8_t vb = *(const uint8_t*)b->mv_data;
return va - vb;
};
int compare_hash32(const MDB_val *a, const MDB_val *b) int compare_hash32(const MDB_val *a, const MDB_val *b)
{ {
uint32_t *va = (uint32_t*) a->mv_data; uint32_t *va = (uint32_t*) a->mv_data;
@ -1103,9 +1096,10 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags)
mdb_set_dupsort(txn, m_output_txs, compare_uint64); mdb_set_dupsort(txn, m_output_txs, compare_uint64);
mdb_set_dupsort(txn, m_block_info, compare_uint64); mdb_set_dupsort(txn, m_block_info, compare_uint64);
mdb_set_compare(txn, m_hf_starting_heights, compare_uint8);
mdb_set_compare(txn, m_properties, compare_string); mdb_set_compare(txn, m_properties, compare_string);
mdb_drop(txn, m_hf_starting_heights, 1);
// get and keep current height // get and keep current height
MDB_stat db_stats; MDB_stat db_stats;
if ((result = mdb_stat(txn, m_blocks, &db_stats))) if ((result = mdb_stat(txn, m_blocks, &db_stats)))
@ -2638,29 +2632,6 @@ std::map<uint64_t, uint64_t> BlockchainLMDB::get_output_histogram(const std::vec
void BlockchainLMDB::check_hard_fork_info() void BlockchainLMDB::check_hard_fork_info()
{ {
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
TXN_PREFIX(0);
MDB_stat db_stat1, db_stat2;
if (mdb_stat(*txn_ptr, m_blocks, &db_stat1))
throw0(DB_ERROR("Failed to query m_blocks"));
if (mdb_stat(*txn_ptr, m_hf_versions, &db_stat2))
throw0(DB_ERROR("Failed to query m_hf_starting_heights"));
if (db_stat1.ms_entries != db_stat2.ms_entries)
{
// Empty, but don't delete. This allows this function to be called after
// startup, after the subdbs have already been created, and rest of startup
// can proceed. If these don't exist, hard fork's init() will fail.
//
// If these are empty, hard fork's init() will repopulate the hard fork
// data.
mdb_drop(*txn_ptr, m_hf_starting_heights, 0);
mdb_drop(*txn_ptr, m_hf_versions, 0);
}
TXN_POSTFIX_SUCCESS();
} }
void BlockchainLMDB::drop_hard_fork_info() void BlockchainLMDB::drop_hard_fork_info()
@ -2676,49 +2647,6 @@ void BlockchainLMDB::drop_hard_fork_info()
TXN_POSTFIX_SUCCESS(); TXN_POSTFIX_SUCCESS();
} }
void BlockchainLMDB::set_hard_fork_starting_height(uint8_t version, uint64_t height)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
TXN_BLOCK_PREFIX(0);
MDB_val_copy<uint8_t> val_key(version);
MDB_val_copy<uint64_t> val_value(height);
if (auto result = mdb_put(*txn_ptr, m_hf_starting_heights, &val_key, &val_value, MDB_APPEND))
throw1(DB_ERROR(lmdb_error("Error adding hard fork starting height to db transaction: ", result).c_str()));
TXN_BLOCK_POSTFIX_SUCCESS();
}
uint64_t BlockchainLMDB::get_hard_fork_starting_height(uint8_t version) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
TXN_PREFIX_RDONLY();
MDB_val_copy<uint8_t> val_key(version);
MDB_val val_ret;
uint64_t ret = 0;
auto result = mdb_get(m_txn, m_hf_starting_heights, &val_key, &val_ret);
if (result == MDB_SUCCESS)
{
#ifdef MISALIGNED_OK
ret = *(const uint64_t*)val_ret.mv_data;
#else
memcpy(&ret, val_ret.mv_data, sizeof(uint64_t));
#endif
} else if (result == MDB_NOTFOUND)
{
ret = std::numeric_limits<uint64_t>::max();
} else if (result)
throw0(DB_ERROR(lmdb_error("Error attempting to retrieve a hard fork starting height from the db", result).c_str()));
TXN_POSTFIX_RDONLY();
return ret;
}
void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version) void BlockchainLMDB::set_hard_fork_version(uint64_t height, uint8_t version)
{ {
LOG_PRINT_L3("BlockchainLMDB::" << __func__); LOG_PRINT_L3("BlockchainLMDB::" << __func__);

View file

@ -307,8 +307,6 @@ private:
virtual void remove_spent_key(const crypto::key_image& k_image); virtual void remove_spent_key(const crypto::key_image& k_image);
// Hard fork // Hard fork
virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height);
virtual uint64_t get_hard_fork_starting_height(uint8_t version) const;
virtual void set_hard_fork_version(uint64_t height, uint8_t version); virtual void set_hard_fork_version(uint64_t height, uint8_t version);
virtual uint8_t get_hard_fork_version(uint64_t height) const; virtual uint8_t get_hard_fork_version(uint64_t height) const;
virtual void check_hard_fork_info(); virtual void check_hard_fork_info();

View file

@ -419,10 +419,6 @@ int main(int argc, char* argv[])
for (uint64_t h = 0; h < height; ++h) for (uint64_t h = 0; h < height; ++h)
write_pod(d, boost::lexical_cast<std::string>(h), (unsigned int)db->get_hard_fork_version(h)); write_pod(d, boost::lexical_cast<std::string>(h), (unsigned int)db->get_hard_fork_version(h));
end_compound(d); end_compound(d);
start_struct(d, "hf_starting_heights", true);
for (unsigned int v = 0; v <= 255; ++v)
write_pod(d, boost::lexical_cast<std::string>(v), db->get_hard_fork_starting_height(v));
end_compound(d);
} }
#endif #endif
end_compound(d); end_compound(d);

View file

@ -137,10 +137,6 @@ bool HardFork::add(uint8_t block_version, uint8_t voting_version, uint64_t heigh
uint8_t voted = get_voted_fork_index(height + 1); uint8_t voted = get_voted_fork_index(height + 1);
if (voted > current_fork_index) { if (voted > current_fork_index) {
for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) {
// we reached the vote threshold with this block, next one will be forked
db.set_hard_fork_starting_height(v, height + 1);
}
current_fork_index = voted; current_fork_index = voted;
} }
@ -172,7 +168,12 @@ void HardFork::init()
else else
height = 1; height = 1;
bool populate = db.get_hard_fork_starting_height(original_version) == std::numeric_limits<uint64_t>::max(); bool populate = false;
try
{
db.get_hard_fork_version(0);
}
catch (...) { populate = true; }
if (populate) { if (populate) {
LOG_PRINT_L0("The DB has no hard fork info, reparsing from start"); LOG_PRINT_L0("The DB has no hard fork info, reparsing from start");
height = 1; height = 1;
@ -182,7 +183,6 @@ void HardFork::init()
reorganize_from_chain_height(height); 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);
} }
else { else {
rescan_from_chain_height(height); rescan_from_chain_height(height);
@ -215,7 +215,6 @@ bool HardFork::reorganize_from_block_height(uint64_t height)
const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0; const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0;
const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height); const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height);
while (current_fork_index > 0 && heights[current_fork_index].version > start_version) { while (current_fork_index > 0 && heights[current_fork_index].version > start_version) {
db.set_hard_fork_starting_height(heights[current_fork_index].version, std::numeric_limits<uint64_t>::max());
--current_fork_index; --current_fork_index;
} }
for (uint64_t h = rescan_height; h <= height; ++h) { for (uint64_t h = rescan_height; h <= height; ++h) {
@ -227,10 +226,6 @@ bool HardFork::reorganize_from_block_height(uint64_t height)
uint8_t voted = get_voted_fork_index(height + 1); uint8_t voted = get_voted_fork_index(height + 1);
if (voted > current_fork_index) { if (voted > current_fork_index) {
for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) {
// we reached the vote threshold with this block, next one will be forked
db.set_hard_fork_starting_height(v, height + 1);
}
current_fork_index = voted; current_fork_index = voted;
} }
@ -337,12 +332,6 @@ uint8_t HardFork::get(uint64_t height) const
return db.get_hard_fork_version(height); return db.get_hard_fork_version(height);
} }
uint64_t HardFork::get_start_height(uint8_t version) const
{
CRITICAL_REGION_LOCAL(lock);
return db.get_hard_fork_starting_height(version);
}
uint8_t HardFork::get_current_version() const uint8_t HardFork::get_current_version() const
{ {
CRITICAL_REGION_LOCAL(lock); CRITICAL_REGION_LOCAL(lock);

View file

@ -154,13 +154,6 @@ namespace cryptonote
*/ */
uint8_t get(uint64_t height) const; uint8_t get(uint64_t height) const;
/**
* @brief returns the height of the first block on the fork with th given version
*
* @param version version of the fork to query the starting block for
*/
uint64_t get_start_height(uint8_t version) const;
/** /**
* @brief returns the latest "ideal" version * @brief returns the latest "ideal" version
* *

View file

@ -42,10 +42,7 @@ using namespace cryptonote;
class TestDB: public BlockchainDB { class TestDB: public BlockchainDB {
public: public:
TestDB() { TestDB() {};
for (size_t n = 0; n < 256; ++n)
starting_height[n] = std::numeric_limits<uint64_t>::max();
}
virtual void open(const std::string& filename, const int db_flags = 0) { } virtual void open(const std::string& filename, const int db_flags = 0) { }
virtual void close() {} virtual void close() {}
virtual void sync() {} virtual void sync() {}
@ -122,12 +119,6 @@ public:
virtual block get_block_from_height(const uint64_t& height) const { virtual block get_block_from_height(const uint64_t& height) const {
return blocks[height]; return blocks[height];
} }
virtual void set_hard_fork_starting_height(uint8_t version, uint64_t height) {
starting_height[version] = height;
}
virtual uint64_t get_hard_fork_starting_height(uint8_t version) const {
return starting_height[version];
}
virtual void set_hard_fork_version(uint64_t height, uint8_t version) { virtual void set_hard_fork_version(uint64_t height, uint8_t version) {
if (versions.size() <= height) if (versions.size() <= height)
versions.resize(height+1); versions.resize(height+1);
@ -140,7 +131,6 @@ public:
private: private:
std::vector<block> blocks; std::vector<block> blocks;
uint64_t starting_height[256];
std::deque<uint8_t> versions; std::deque<uint8_t> versions;
}; };
@ -321,10 +311,6 @@ TEST(reorganize, Same)
uint8_t version = hh >= history ? block_versions[hh - history] : 1; uint8_t version = hh >= history ? block_versions[hh - history] : 1;
ASSERT_EQ(hf.get(hh), version); ASSERT_EQ(hf.get(hh), version);
} }
ASSERT_EQ(hf.get_start_height(1), 0);
ASSERT_EQ(hf.get_start_height(4), 2 + history);
ASSERT_EQ(hf.get_start_height(7), 4 + history);
ASSERT_EQ(hf.get_start_height(9), 6 + history);
} }
} }
} }
@ -355,10 +341,6 @@ TEST(reorganize, Changed)
for (int hh = 0; hh < 16; ++hh) { for (int hh = 0; hh < 16; ++hh) {
ASSERT_EQ(hf.get(hh), expected_versions[hh]); ASSERT_EQ(hf.get(hh), expected_versions[hh]);
} }
ASSERT_EQ(hf.get_start_height(1), 0);
ASSERT_EQ(hf.get_start_height(4), 6);
ASSERT_EQ(hf.get_start_height(7), 8);
ASSERT_EQ(hf.get_start_height(9), 10);
} }
// delay a bit for 9, and go back to 1 to check it stays at 9 // delay a bit for 9, and go back to 1 to check it stays at 9
@ -379,10 +361,6 @@ TEST(reorganize, Changed)
for (int hh = 0; hh < 15; ++hh) { for (int hh = 0; hh < 15; ++hh) {
ASSERT_EQ(hf.get(hh), expected_versions_new[hh]); ASSERT_EQ(hf.get(hh), expected_versions_new[hh]);
} }
ASSERT_EQ(hf.get_start_height(1), 0);
ASSERT_EQ(hf.get_start_height(4), 6);
ASSERT_EQ(hf.get_start_height(7), 11);
ASSERT_EQ(hf.get_start_height(9), 14);
} }
TEST(voting, threshold) TEST(voting, threshold)
@ -463,8 +441,6 @@ TEST(new_blocks, denied)
ASSERT_TRUE(hf.add(mkblock(1, 2), 8)); // we reach 50% of the last 4 ASSERT_TRUE(hf.add(mkblock(1, 2), 8)); // we reach 50% of the last 4
ASSERT_FALSE(hf.add(mkblock(2, 1), 9)); // so this one can't get added ASSERT_FALSE(hf.add(mkblock(2, 1), 9)); // so this one can't get added
ASSERT_TRUE(hf.add(mkblock(2, 2), 9)); ASSERT_TRUE(hf.add(mkblock(2, 2), 9));
ASSERT_EQ(hf.get_start_height(2), 9);
} }
TEST(new_version, early) TEST(new_version, early)
@ -485,8 +461,6 @@ TEST(new_version, early)
ASSERT_TRUE(hf.add(mkblock(2, 2), 5)); ASSERT_TRUE(hf.add(mkblock(2, 2), 5));
ASSERT_FALSE(hf.add(mkblock(2, 1), 6)); // we don't accept 1 anymore ASSERT_FALSE(hf.add(mkblock(2, 1), 6)); // we don't accept 1 anymore
ASSERT_TRUE(hf.add(mkblock(2, 2), 7)); // but we do accept 2 ASSERT_TRUE(hf.add(mkblock(2, 2), 7)); // but we do accept 2
ASSERT_EQ(hf.get_start_height(2), 4);
} }
TEST(reorganize, changed) TEST(reorganize, changed)
@ -521,8 +495,6 @@ TEST(reorganize, changed)
ADD_TRUE(3, 7); ADD_TRUE(3, 7);
ADD_TRUE(4, 8); ADD_TRUE(4, 8);
ADD_TRUE(4, 9); ADD_TRUE(4, 9);
ASSERT_EQ(hf.get_start_height(2), 4); // reaches threshold 2 at height 3, so height 4 forks
ASSERT_EQ(hf.get_start_height(3), 9);
ASSERT_EQ(hf.get_current_version(), 3); ASSERT_EQ(hf.get_current_version(), 3);
// pop a few blocks and check current version goes back down // pop a few blocks and check current version goes back down
@ -539,9 +511,7 @@ TEST(reorganize, changed)
ADD_TRUE(2, 7); ADD_TRUE(2, 7);
ADD_TRUE(2, 8); ADD_TRUE(2, 8);
ADD_TRUE(2, 9); ADD_TRUE(2, 9);
ASSERT_EQ(hf.get_start_height(2), 4); // unchanged
ASSERT_EQ(hf.get_current_version(), 2); // we did not bump to 3 this time ASSERT_EQ(hf.get_current_version(), 2); // we did not bump to 3 this time
ASSERT_EQ(hf.get_start_height(3), std::numeric_limits<uint64_t>::max()); // not yet
} }
TEST(get, higher) TEST(get, higher)