Support debugging command --pop-blocks on in-memory blockchain

Add public method blockchain_storage::debug_pop_block_from_blockchain()

Ensure blockchain_import calls destructors before exit.

To test:

DATABASE=memory make release

// create blockchain.bin from blockchain.raw if needed
build/release/bin/blockchain_import --block-stop 1000

// try popping a single block
build/release/bin/blockchain_import --pop-blocks 1
This commit is contained in:
warptangent 2015-05-16 19:27:26 -07:00
parent 2dc1cd9ae6
commit d1eac1b71c
No known key found for this signature in database
GPG Key ID: 0E490BEBFBE4E92D
2 changed files with 17 additions and 9 deletions

View File

@ -142,8 +142,12 @@ int pop_blocks(FakeCore& simple_core, int num_blocks)
std::vector<transaction> popped_txs; std::vector<transaction> popped_txs;
for (int i=0; i < num_blocks; ++i) for (int i=0; i < num_blocks; ++i)
{ {
#if defined(BLOCKCHAIN_DB) && (BLOCKCHAIN_DB == DB_MEMORY)
simple_core.m_storage.debug_pop_block_from_blockchain();
#else
// simple_core.m_storage.pop_block_from_blockchain() is private, so call directly through db // simple_core.m_storage.pop_block_from_blockchain() is private, so call directly through db
simple_core.m_storage.get_db().pop_block(popped_block, popped_txs); simple_core.m_storage.get_db().pop_block(popped_block, popped_txs);
#endif
quit = 1; quit = 1;
} }
@ -615,12 +619,12 @@ int main(int argc, char* argv[])
if (! opt_batch && ! vm["batch-size"].defaulted()) if (! opt_batch && ! vm["batch-size"].defaulted())
{ {
std::cerr << "Error: batch-size set, but batch option not enabled" << ENDL; std::cerr << "Error: batch-size set, but batch option not enabled" << ENDL;
exit(1); return 1;
} }
if (! db_batch_size) if (! db_batch_size)
{ {
std::cerr << "Error: batch-size must be > 0" << ENDL; std::cerr << "Error: batch-size must be > 0" << ENDL;
exit(1); return 1;
} }
if (opt_verify && vm["batch-size"].defaulted()) if (opt_verify && vm["batch-size"].defaulted())
{ {
@ -655,7 +659,7 @@ int main(int argc, char* argv[])
{ {
BootstrapFile bootstrap; BootstrapFile bootstrap;
bootstrap.count_blocks(import_file_path); bootstrap.count_blocks(import_file_path);
exit(0); return 0;
} }
@ -666,13 +670,13 @@ int main(int argc, char* argv[])
if (res) if (res)
{ {
std::cerr << "Error parsing database argument(s)" << ENDL; std::cerr << "Error parsing database argument(s)" << ENDL;
exit(1); return 1;
} }
if (std::find(db_engines.begin(), db_engines.end(), db_engine) == db_engines.end()) if (std::find(db_engines.begin(), db_engines.end(), db_engine) == db_engines.end())
{ {
std::cerr << "Invalid database engine: " << db_engine << std::endl; std::cerr << "Invalid database engine: " << db_engine << std::endl;
exit(1); return 1;
} }
LOG_PRINT_L0("database: " << db_engine); LOG_PRINT_L0("database: " << db_engine);
@ -717,7 +721,7 @@ int main(int argc, char* argv[])
else else
{ {
std::cerr << "database engine unrecognized" << ENDL; std::cerr << "database engine unrecognized" << ENDL;
exit(1); return 1;
} }
// for multi_db_compile: // for multi_db_compile:
@ -725,7 +729,7 @@ int main(int argc, char* argv[])
if (db_engine != default_db_engine) if (db_engine != default_db_engine)
{ {
std::cerr << "Invalid database engine for compiled version: " << db_engine << std::endl; std::cerr << "Invalid database engine for compiled version: " << db_engine << std::endl;
exit(1); return 1;
} }
#if BLOCKCHAIN_DB == DB_LMDB #if BLOCKCHAIN_DB == DB_LMDB
fake_core_lmdb simple_core(dirname, opt_testnet, opt_batch, mdb_flags); fake_core_lmdb simple_core(dirname, opt_testnet, opt_batch, mdb_flags);
@ -739,7 +743,7 @@ int main(int argc, char* argv[])
LOG_PRINT_L0("height: " << simple_core.m_storage.get_current_blockchain_height()); LOG_PRINT_L0("height: " << simple_core.m_storage.get_current_blockchain_height());
pop_blocks(simple_core, num_blocks); pop_blocks(simple_core, num_blocks);
LOG_PRINT_L0("height: " << simple_core.m_storage.get_current_blockchain_height()); LOG_PRINT_L0("height: " << simple_core.m_storage.get_current_blockchain_height());
exit(0); return 0;
} }
import_from_file(simple_core, import_file_path, block_stop); import_from_file(simple_core, import_file_path, block_stop);
@ -749,7 +753,7 @@ int main(int argc, char* argv[])
catch (const DB_ERROR& e) catch (const DB_ERROR& e)
{ {
std::cout << std::string("Error loading blockchain db: ") + e.what() + " -- shutting down now" << ENDL; std::cout << std::string("Error loading blockchain db: ") + e.what() + " -- shutting down now" << ENDL;
exit(1); return 1;
} }
// destructors called at exit: // destructors called at exit:
@ -762,4 +766,5 @@ int main(int argc, char* argv[])
// LMDB fake_core, it calls Blockchain::deinit() on its object, which in turn // LMDB fake_core, it calls Blockchain::deinit() on its object, which in turn
// calls delete on its BlockchainDB derived class' object, which closes its // calls delete on its BlockchainDB derived class' object, which closes its
// files. // files.
return 0;
} }

View File

@ -190,6 +190,9 @@ namespace cryptonote
difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return m_blocks[height].cumulative_difficulty; } difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return m_blocks[height].cumulative_difficulty; }
uint64_t get_block_coins_generated(uint64_t height) const { return m_blocks[height].already_generated_coins; } uint64_t get_block_coins_generated(uint64_t height) const { return m_blocks[height].already_generated_coins; }
// use for testing only
bool debug_pop_block_from_blockchain() { return pop_block_from_blockchain(); }
private: private:
typedef std::unordered_map<crypto::hash, size_t> blocks_by_id_index; typedef std::unordered_map<crypto::hash, size_t> blocks_by_id_index;
typedef std::unordered_map<crypto::hash, transaction_chain_entry> transactions_container; typedef std::unordered_map<crypto::hash, transaction_chain_entry> transactions_container;