2015-02-11 04:54:27 +01:00
|
|
|
// Copyright (c) 2014-2015, The Monero Project
|
|
|
|
//
|
2014-11-02 01:55:22 +01:00
|
|
|
// All rights reserved.
|
2015-02-11 04:54:27 +01:00
|
|
|
//
|
2014-11-02 01:55:22 +01:00
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
2015-02-11 04:54:27 +01:00
|
|
|
//
|
2014-11-02 01:55:22 +01:00
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
2015-02-11 04:54:27 +01:00
|
|
|
//
|
2014-11-02 01:55:22 +01:00
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
2015-02-11 04:54:27 +01:00
|
|
|
//
|
2014-11-02 01:55:22 +01:00
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
2015-02-11 04:54:27 +01:00
|
|
|
//
|
2014-11-02 01:55:22 +01:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "include_base_utils.h"
|
|
|
|
#include "common/util.h"
|
|
|
|
#include "warnings.h"
|
|
|
|
#include "crypto/crypto.h"
|
|
|
|
#include "cryptonote_config.h"
|
|
|
|
#include "cryptonote_core/cryptonote_format_utils.h"
|
|
|
|
#include "misc_language.h"
|
|
|
|
#include "cryptonote_core/blockchain_storage.h"
|
2015-03-06 21:20:45 +01:00
|
|
|
#include "blockchain_db/blockchain_db.h"
|
2014-11-02 01:55:22 +01:00
|
|
|
#include "cryptonote_core/blockchain.h"
|
2015-03-06 21:20:45 +01:00
|
|
|
#include "blockchain_db/lmdb/db_lmdb.h"
|
2014-11-02 01:55:22 +01:00
|
|
|
#include "cryptonote_core/tx_pool.h"
|
2015-03-04 02:20:27 +01:00
|
|
|
#include "common/command_line.h"
|
|
|
|
#include "serialization/json_utils.h"
|
|
|
|
#include "include_base_utils.h"
|
|
|
|
#include "version.h"
|
2014-11-02 02:13:15 +01:00
|
|
|
#include <iostream>
|
2014-11-02 01:55:22 +01:00
|
|
|
|
2015-04-07 23:56:18 +02:00
|
|
|
|
|
|
|
unsigned int epee::g_test_dbg_lock_sleep = 0;
|
|
|
|
|
2015-03-22 20:33:50 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
// CONFIG
|
2015-03-22 20:33:50 +01:00
|
|
|
bool opt_batch = true;
|
|
|
|
bool opt_resume = true;
|
|
|
|
bool opt_testnet = false;
|
2015-03-04 02:20:27 +01:00
|
|
|
|
|
|
|
// number of blocks per batch transaction
|
|
|
|
// adjustable through command-line argument according to available RAM
|
2015-05-08 22:46:26 +02:00
|
|
|
#if !defined(WIN32)
|
|
|
|
uint64_t db_batch_size_verify = 5000;
|
|
|
|
#else
|
|
|
|
// set a lower default batch size for Windows, pending possible LMDB issue with
|
|
|
|
// large batch size.
|
|
|
|
uint64_t db_batch_size_verify = 1000;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// converter only uses verify mode
|
|
|
|
uint64_t db_batch_size = db_batch_size_verify;
|
2015-03-04 02:20:27 +01:00
|
|
|
|
2015-03-22 20:33:50 +01:00
|
|
|
}
|
2015-03-04 02:20:27 +01:00
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2014-11-02 01:55:22 +01:00
|
|
|
using namespace cryptonote;
|
2015-03-04 02:20:27 +01:00
|
|
|
using namespace epee;
|
2014-11-02 01:55:22 +01:00
|
|
|
|
|
|
|
struct fake_core
|
|
|
|
{
|
|
|
|
Blockchain dummy;
|
2015-03-04 02:20:27 +01:00
|
|
|
tx_memory_pool m_pool;
|
2014-11-02 01:55:22 +01:00
|
|
|
|
|
|
|
blockchain_storage m_storage;
|
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
#if !defined(BLOCKCHAIN_DB)
|
|
|
|
// for multi_db_runtime:
|
|
|
|
fake_core(const boost::filesystem::path &path, const bool use_testnet) : dummy(m_pool), m_pool(&dummy), m_storage(m_pool)
|
|
|
|
#else
|
|
|
|
// for multi_db_compile:
|
|
|
|
fake_core(const boost::filesystem::path &path, const bool use_testnet) : dummy(m_pool), m_pool(dummy), m_storage(&m_pool)
|
|
|
|
#endif
|
2014-11-02 01:55:22 +01:00
|
|
|
{
|
2014-12-12 11:28:46 +01:00
|
|
|
m_pool.init(path.string());
|
2015-03-04 02:20:27 +01:00
|
|
|
m_storage.init(path.string(), use_testnet);
|
2014-11-02 01:55:22 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2015-03-04 02:20:27 +01:00
|
|
|
uint64_t height = 0;
|
|
|
|
uint64_t start_block = 0;
|
|
|
|
uint64_t end_block = 0;
|
|
|
|
uint64_t num_blocks = 0;
|
|
|
|
|
|
|
|
boost::filesystem::path default_data_path {tools::get_default_data_dir()};
|
|
|
|
boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"};
|
|
|
|
|
|
|
|
|
|
|
|
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_0};
|
|
|
|
const command_line::arg_descriptor<uint64_t> arg_batch_size = {"batch-size", "", db_batch_size};
|
|
|
|
const command_line::arg_descriptor<bool> arg_testnet_on = {
|
|
|
|
"testnet"
|
|
|
|
, "Run on testnet."
|
|
|
|
, opt_testnet
|
|
|
|
};
|
|
|
|
const command_line::arg_descriptor<uint64_t> arg_block_number =
|
|
|
|
{"block-number", "Number of blocks (default: use entire source blockchain)",
|
|
|
|
0};
|
|
|
|
|
|
|
|
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_batch_size);
|
|
|
|
command_line::add_arg(desc_cmd_sett, arg_testnet_on);
|
|
|
|
command_line::add_arg(desc_cmd_sett, arg_block_number);
|
|
|
|
|
|
|
|
command_line::add_arg(desc_cmd_only, command_line::arg_help);
|
|
|
|
|
|
|
|
const command_line::arg_descriptor<bool> arg_batch = {"batch",
|
|
|
|
"Batch transactions for faster import", true};
|
2015-03-22 20:33:50 +01:00
|
|
|
const command_line::arg_descriptor<bool> arg_resume = {"resume",
|
|
|
|
"Resume from current height if output database already exists", true};
|
2015-03-04 02:20:27 +01:00
|
|
|
|
|
|
|
// call add_options() directly for these arguments since command_line helpers
|
|
|
|
// support only boolean switch, not boolean argument
|
|
|
|
desc_cmd_sett.add_options()
|
2015-03-22 20:33:50 +01:00
|
|
|
(arg_batch.name, make_semantic(arg_batch), arg_batch.description)
|
|
|
|
(arg_resume.name, make_semantic(arg_resume), arg_resume.description)
|
2015-03-04 02:20:27 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
po::options_description desc_options("Allowed options");
|
|
|
|
desc_options.add(desc_cmd_only).add(desc_cmd_sett);
|
|
|
|
|
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
bool r = command_line::handle_error_helper(desc_options, [&]()
|
|
|
|
{
|
|
|
|
po::store(po::parse_command_line(argc, argv, desc_options), vm);
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (!r)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int log_level = command_line::get_arg(vm, arg_log_level);
|
|
|
|
opt_batch = command_line::get_arg(vm, arg_batch);
|
2015-03-22 20:33:50 +01:00
|
|
|
opt_resume = command_line::get_arg(vm, arg_resume);
|
2015-03-04 02:20:27 +01:00
|
|
|
db_batch_size = command_line::get_arg(vm, arg_batch_size);
|
|
|
|
|
|
|
|
if (command_line::get_arg(vm, command_line::arg_help))
|
|
|
|
{
|
|
|
|
std::cout << CRYPTONOTE_NAME << " v" << MONERO_VERSION_FULL << ENDL << ENDL;
|
|
|
|
std::cout << desc_options << std::endl;
|
|
|
|
return 1;
|
2014-12-19 19:18:21 +01:00
|
|
|
}
|
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
if (! opt_batch && ! vm["batch-size"].defaulted())
|
|
|
|
{
|
|
|
|
std::cerr << "Error: batch-size set, but batch option not enabled" << ENDL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (! db_batch_size)
|
|
|
|
{
|
|
|
|
std::cerr << "Error: batch-size must be > 0" << ENDL;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-11-02 01:55:22 +01:00
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
log_space::get_set_log_detalisation_level(true, log_level);
|
|
|
|
log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
|
|
|
|
LOG_PRINT_L0("Starting...");
|
2014-11-02 01:55:22 +01:00
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
std::string src_folder;
|
|
|
|
opt_testnet = command_line::get_arg(vm, arg_testnet_on);
|
|
|
|
auto data_dir_arg = opt_testnet ? command_line::arg_testnet_data_dir : command_line::arg_data_dir;
|
|
|
|
src_folder = command_line::get_arg(vm, data_dir_arg);
|
|
|
|
boost::filesystem::path dest_folder(src_folder);
|
2014-11-02 01:55:22 +01:00
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
num_blocks = command_line::get_arg(vm, arg_block_number);
|
2015-03-14 02:39:27 +01:00
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
if (opt_batch)
|
|
|
|
{
|
|
|
|
LOG_PRINT_L0("batch: " << std::boolalpha << opt_batch << std::noboolalpha
|
|
|
|
<< " batch size: " << db_batch_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_PRINT_L0("batch: " << std::boolalpha << opt_batch << std::noboolalpha);
|
|
|
|
}
|
2015-03-22 20:33:50 +01:00
|
|
|
LOG_PRINT_L0("resume: " << std::boolalpha << opt_resume << std::noboolalpha);
|
2015-03-04 02:20:27 +01:00
|
|
|
LOG_PRINT_L0("testnet: " << std::boolalpha << opt_testnet << std::noboolalpha);
|
|
|
|
|
|
|
|
fake_core c(src_folder, opt_testnet);
|
2015-03-14 02:39:27 +01:00
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
height = c.m_storage.get_current_blockchain_height();
|
|
|
|
|
|
|
|
BlockchainDB *blockchain;
|
|
|
|
blockchain = new BlockchainLMDB(opt_batch);
|
|
|
|
dest_folder /= blockchain->get_db_name();
|
|
|
|
LOG_PRINT_L0("Source blockchain: " << src_folder);
|
|
|
|
LOG_PRINT_L0("Dest blockchain: " << dest_folder.string());
|
2015-03-22 20:33:50 +01:00
|
|
|
LOG_PRINT_L0("Opening dest blockchain (BlockchainDB " << blockchain->get_db_name() << ")");
|
2015-03-04 02:20:27 +01:00
|
|
|
blockchain->open(dest_folder.string());
|
2015-03-22 20:33:50 +01:00
|
|
|
LOG_PRINT_L0("Source blockchain height: " << height);
|
|
|
|
LOG_PRINT_L0("Dest blockchain height: " << blockchain->height());
|
|
|
|
|
|
|
|
if (opt_resume)
|
|
|
|
// next block number to add is same as current height
|
|
|
|
start_block = blockchain->height();
|
|
|
|
|
|
|
|
if (! num_blocks || (start_block + num_blocks > height))
|
|
|
|
end_block = height - 1;
|
|
|
|
else
|
|
|
|
end_block = start_block + num_blocks - 1;
|
|
|
|
|
|
|
|
LOG_PRINT_L0("start height: " << start_block+1 << " stop height: " <<
|
|
|
|
end_block+1);
|
|
|
|
|
|
|
|
if (start_block > end_block)
|
|
|
|
{
|
|
|
|
LOG_PRINT_L0("Finished: no blocks to add");
|
|
|
|
delete blockchain;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 01:55:22 +01:00
|
|
|
|
2015-03-04 02:20:27 +01:00
|
|
|
if (opt_batch)
|
2015-07-11 21:32:49 +02:00
|
|
|
blockchain->batch_start(db_batch_size);
|
2015-03-04 02:20:27 +01:00
|
|
|
uint64_t i = 0;
|
|
|
|
for (i = start_block; i < end_block + 1; ++i)
|
2014-11-02 01:55:22 +01:00
|
|
|
{
|
2015-03-04 02:20:27 +01:00
|
|
|
// block: i height: i+1 end height: end_block + 1
|
|
|
|
if ((i+1) % 10 == 0)
|
2014-12-12 12:01:17 +01:00
|
|
|
{
|
2015-03-04 02:20:27 +01:00
|
|
|
std::cout << "\r \r" << "height " << i+1 << "/" <<
|
|
|
|
end_block+1 << " (" << (i+1)*100/(end_block+1)<< "%)" << std::flush;
|
2014-12-12 12:01:17 +01:00
|
|
|
}
|
2015-03-04 02:20:27 +01:00
|
|
|
// for debugging:
|
|
|
|
// std::cout << "height " << i+1 << "/" << end_block+1
|
|
|
|
// << " ((" << i+1 << ")*100/(end_block+1))" << "%)" << ENDL;
|
|
|
|
|
2014-11-02 01:55:22 +01:00
|
|
|
block b = c.m_storage.get_block(i);
|
|
|
|
size_t bsize = c.m_storage.get_block_size(i);
|
|
|
|
difficulty_type bdiff = c.m_storage.get_block_cumulative_difficulty(i);
|
|
|
|
uint64_t bcoins = c.m_storage.get_block_coins_generated(i);
|
|
|
|
std::vector<transaction> txs;
|
|
|
|
std::vector<crypto::hash> missed;
|
|
|
|
|
|
|
|
c.m_storage.get_transactions(b.tx_hashes, txs, missed);
|
|
|
|
if (missed.size())
|
|
|
|
{
|
2015-03-04 02:20:27 +01:00
|
|
|
std::cout << ENDL;
|
|
|
|
std::cerr << "Missed transaction(s) for block at height " << i + 1 << ", exiting" << ENDL;
|
2014-11-02 01:55:22 +01:00
|
|
|
delete blockchain;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
blockchain->add_block(b, bsize, bdiff, bcoins, txs);
|
2015-03-04 02:20:27 +01:00
|
|
|
|
|
|
|
if (opt_batch)
|
|
|
|
{
|
|
|
|
if ((i < end_block) && ((i + 1) % db_batch_size == 0))
|
|
|
|
{
|
|
|
|
std::cout << "\r \r";
|
|
|
|
std::cout << "[- batch commit at height " << i + 1 << " -]" << ENDL;
|
|
|
|
blockchain->batch_stop();
|
2015-07-11 21:32:49 +02:00
|
|
|
blockchain->batch_start(db_batch_size);
|
2015-03-04 02:20:27 +01:00
|
|
|
std::cout << ENDL;
|
|
|
|
blockchain->show_stats();
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:55:22 +01:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2015-03-04 02:20:27 +01:00
|
|
|
std::cout << ENDL;
|
2015-03-22 20:33:50 +01:00
|
|
|
std::cerr << "Error adding block " << i << " to new blockchain: " << e.what() << ENDL;
|
2014-11-02 01:55:22 +01:00
|
|
|
delete blockchain;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 02:20:27 +01:00
|
|
|
if (opt_batch)
|
|
|
|
{
|
|
|
|
std::cout << "\r \r" << "height " << i << "/" <<
|
|
|
|
end_block+1 << " (" << (i)*100/(end_block+1)<< "%)" << std::flush;
|
|
|
|
std::cout << ENDL;
|
|
|
|
std::cout << "[- batch commit at height " << i << " -]" << ENDL;
|
|
|
|
blockchain->batch_stop();
|
|
|
|
}
|
|
|
|
std::cout << ENDL;
|
|
|
|
blockchain->show_stats();
|
|
|
|
std::cout << "Finished at height: " << i << " block: " << i-1 << ENDL;
|
2014-11-02 01:55:22 +01:00
|
|
|
|
2014-12-06 23:53:27 +01:00
|
|
|
delete blockchain;
|
2014-11-02 01:55:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|