Add --block-number option to blockchain_import

This enables the importer to stop after reaching a specified block
number (zero-based index), before reaching the end of the source
blockchain.
This commit is contained in:
warptangent 2015-05-08 13:54:46 -07:00
parent 1eb4c66ad8
commit ed9c639612
No known key found for this signature in database
GPG Key ID: 0E490BEBFBE4E92D
1 changed files with 17 additions and 7 deletions

View File

@ -169,7 +169,7 @@ int pop_blocks(FakeCore& simple_core, int num_blocks)
}
template <typename FakeCore>
int import_from_file(FakeCore& simple_core, std::string& import_file_path)
int import_from_file(FakeCore& simple_core, std::string& import_file_path, uint64_t stop_height=0)
{
#if !defined(BLOCKCHAIN_DB)
static_assert(std::is_same<fake_core_memory, FakeCore>::value || std::is_same<fake_core_lmdb, FakeCore>::value,
@ -230,9 +230,10 @@ int import_from_file(FakeCore& simple_core, std::string& import_file_path)
// Note that a new blockchain will start with block number 0 (total blocks: 1)
// due to genesis block being added at initialization.
// CONFIG
// TODO: can expand on this, e.g. with --block-number option
uint64_t stop_height = total_source_blocks - 1;
if (! stop_height)
{
stop_height = total_source_blocks - 1;
}
// These are what we'll try to use, and they don't have to be a determination
// from source and destination blockchains, but those are the defaults.
@ -530,6 +531,7 @@ int main(int argc, char* argv[])
uint32_t log_level = LOG_LEVEL_0;
uint64_t num_blocks = 0;
uint64_t block_height = 0;
std::string dirname;
std::string db_arg_str;
@ -539,6 +541,7 @@ int main(int argc, char* argv[])
po::options_description desc_cmd_only("Command line options");
po::options_description desc_cmd_sett("Command line options and settings options");
const command_line::arg_descriptor<uint32_t> arg_log_level = {"log-level", "", log_level};
const command_line::arg_descriptor<uint64_t> arg_block_height = {"block-number", "stop at block number", block_height};
const command_line::arg_descriptor<uint64_t> arg_batch_size = {"batch-size", "", db_batch_size};
const command_line::arg_descriptor<uint64_t> arg_pop_blocks = {"pop-blocks", "", num_blocks};
const command_line::arg_descriptor<bool> arg_testnet_on = {
@ -565,6 +568,7 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, command_line::arg_data_dir, default_data_path.string());
command_line::add_arg(desc_cmd_sett, command_line::arg_testnet_data_dir, default_testnet_data_path.string());
command_line::add_arg(desc_cmd_sett, arg_log_level);
command_line::add_arg(desc_cmd_sett, arg_block_height);
command_line::add_arg(desc_cmd_sett, arg_batch_size);
command_line::add_arg(desc_cmd_sett, arg_pop_blocks);
command_line::add_arg(desc_cmd_sett, arg_testnet_on);
@ -598,6 +602,7 @@ int main(int argc, char* argv[])
opt_verify = command_line::get_arg(vm, arg_verify);
opt_batch = command_line::get_arg(vm, arg_batch);
opt_resume = command_line::get_arg(vm, arg_resume);
block_height = command_line::get_arg(vm, arg_block_height);
db_batch_size = command_line::get_arg(vm, arg_batch_size);
if (command_line::get_arg(vm, command_line::arg_help))
@ -629,6 +634,11 @@ int main(int argc, char* argv[])
db_batch_size = db_batch_size_verify;
}
}
uint64_t stop_height = 0;
if (! vm["block-number"].defaulted())
{
stop_height = block_height;
}
std::vector<std::string> db_engines {"memory", "lmdb"};
@ -702,12 +712,12 @@ int main(int argc, char* argv[])
if (db_engine == "lmdb")
{
fake_core_lmdb simple_core(dirname, opt_testnet, opt_batch, mdb_flags);
import_from_file(simple_core, import_file_path);
import_from_file(simple_core, import_file_path, stop_height);
}
else if (db_engine == "memory")
{
fake_core_memory simple_core(dirname, opt_testnet);
import_from_file(simple_core, import_file_path);
import_from_file(simple_core, import_file_path, stop_height);
}
else
{
@ -737,7 +747,7 @@ int main(int argc, char* argv[])
exit(0);
}
import_from_file(simple_core, import_file_path);
import_from_file(simple_core, import_file_path, stop_height);
#endif
}