Electroneum
blockchain_export.cpp File Reference
Include dependency graph for blockchain_export.cpp:

Go to the source code of this file.

Macros

#define ELECTRONEUM_DEFAULT_LOG_CATEGORY   "bcutil"
 

Functions

int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ ELECTRONEUM_DEFAULT_LOG_CATEGORY

#define ELECTRONEUM_DEFAULT_LOG_CATEGORY   "bcutil"

Definition at line 40 of file blockchain_export.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 45 of file blockchain_export.cpp.

46 {
47  TRY_ENTRY();
48 
50 
51  std::string default_db_type = "lmdb";
52 
53  std::string available_dbs = cryptonote::blockchain_db_types(", ");
54  available_dbs = "available: " + available_dbs;
55 
56  uint32_t log_level = 0;
57  uint64_t block_stop = 0;
58  bool blocks_dat = false;
59 
61 
62  boost::filesystem::path output_file_path;
63 
64  po::options_description desc_cmd_only("Command line options");
65  po::options_description desc_cmd_sett("Command line options and settings options");
66  const command_line::arg_descriptor<std::string> arg_output_file = {"output-file", "Specify output file", "", true};
67  const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
68  const command_line::arg_descriptor<uint64_t> arg_block_stop = {"block-stop", "Stop at block number", block_stop};
69  const command_line::arg_descriptor<std::string> arg_database = {
70  "database", available_dbs.c_str(), default_db_type
71  };
72  const command_line::arg_descriptor<bool> arg_blocks_dat = {"blocksdat", "Output in blocks.dat format", blocks_dat};
73 
74 
76  command_line::add_arg(desc_cmd_sett, arg_output_file);
79  command_line::add_arg(desc_cmd_sett, arg_log_level);
80  command_line::add_arg(desc_cmd_sett, arg_database);
81  command_line::add_arg(desc_cmd_sett, arg_block_stop);
82  command_line::add_arg(desc_cmd_sett, arg_blocks_dat);
83 
85 
86  po::options_description desc_options("Allowed options");
87  desc_options.add(desc_cmd_only).add(desc_cmd_sett);
88 
89  po::variables_map vm;
90  bool r = command_line::handle_error_helper(desc_options, [&]()
91  {
92  po::store(po::parse_command_line(argc, argv, desc_options), vm);
93  po::notify(vm);
94  return true;
95  });
96  if (! r)
97  return 1;
98 
100  {
101  std::cout << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")" << ENDL << ENDL;
102  std::cout << desc_options << std::endl;
103  return 1;
104  }
105 
106  mlog_configure(mlog_get_default_log_path("electroneum-blockchain-export.log"), true);
109  else
110  mlog_set_log(std::string(std::to_string(log_level) + ",bcutil:INFO").c_str());
111  block_stop = command_line::get_arg(vm, arg_block_stop);
112 
113  LOG_PRINT_L0("Starting...");
114 
115  bool opt_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on);
116  bool opt_stagenet = command_line::get_arg(vm, cryptonote::arg_stagenet_on);
117  if (opt_testnet && opt_stagenet)
118  {
119  std::cerr << "Can't specify more than one of --testnet and --stagenet" << std::endl;
120  return 1;
121  }
122  bool opt_blocks_dat = command_line::get_arg(vm, arg_blocks_dat);
123 
124  std::string m_config_folder;
125 
126  m_config_folder = command_line::get_arg(vm, cryptonote::arg_data_dir);
127 
128  std::string db_type = command_line::get_arg(vm, arg_database);
130  {
131  std::cerr << "Invalid database type: " << db_type << std::endl;
132  return 1;
133  }
134 
135  if (command_line::has_arg(vm, arg_output_file))
136  output_file_path = boost::filesystem::path(command_line::get_arg(vm, arg_output_file));
137  else
138  output_file_path = boost::filesystem::path(m_config_folder) / "export" / BLOCKCHAIN_RAW;
139  LOG_PRINT_L0("Export output file: " << output_file_path.string());
140 
141  // If we wanted to use the memory pool, we would set up a fake_core.
142 
143  // Use Blockchain instead of lower-level BlockchainDB for two reasons:
144  // 1. Blockchain has the init() method for easy setup
145  // 2. exporter needs to use get_current_blockchain_height(), get_block_id_by_height(), get_block_by_hash()
146  //
147  // cannot match blockchain_storage setup above with just one line,
148  // e.g.
149  // Blockchain* core_storage = new Blockchain(NULL);
150  // because unlike blockchain_storage constructor, which takes a pointer to
151  // tx_memory_pool, Blockchain's constructor takes tx_memory_pool object.
152  LOG_PRINT_L0("Initializing source blockchain (BlockchainDB)");
153  Blockchain* core_storage = NULL;
154  tx_memory_pool m_mempool(*core_storage);
155  core_storage = new Blockchain(m_mempool);
156 
157  BlockchainDB* db = new_db(db_type);
158  if (db == NULL)
159  {
160  LOG_ERROR("Attempted to use non-existent database type: " << db_type);
161  throw std::runtime_error("Attempting to use non-existent database type");
162  }
163  LOG_PRINT_L0("database: " << db_type);
164 
165  boost::filesystem::path folder(m_config_folder);
166  folder /= db->get_db_name();
167  const std::string filename = folder.string();
168 
169  LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
170  try
171  {
172  db->open(filename, DBF_RDONLY);
173  }
174  catch (const std::exception& e)
175  {
176  LOG_PRINT_L0("Error opening database: " << e.what());
177  return 1;
178  }
179  r = core_storage->init(db, opt_testnet ? cryptonote::TESTNET : opt_stagenet ? cryptonote::STAGENET : cryptonote::MAINNET);
180 
181  if (core_storage->get_blockchain_pruning_seed())
182  {
183  LOG_PRINT_L0("Blockchain is pruned, cannot export");
184  return 1;
185  }
186 
187  CHECK_AND_ASSERT_MES(r, 1, "Failed to initialize source blockchain storage");
188  LOG_PRINT_L0("Source blockchain storage initialized OK");
189  LOG_PRINT_L0("Exporting blockchain raw data...");
190 
191  if (opt_blocks_dat)
192  {
193  BlocksdatFile blocksdat;
194  r = blocksdat.store_blockchain_raw(core_storage, NULL, output_file_path, block_stop);
195  }
196  else
197  {
198  BootstrapFile bootstrap;
199  r = bootstrap.store_blockchain_raw(core_storage, NULL, output_file_path, block_stop);
200  }
201  CHECK_AND_ASSERT_MES(r, 1, "Failed to export blockchain raw data");
202  LOG_PRINT_L0("Blockchain raw data exported OK");
203  return 0;
204 
205  CATCH_ENTRY("Export error", 1);
206 }
const char *const ELECTRONEUM_RELEASE_NAME
virtual std::string get_db_name() const =0
gets the name of the folder the BlockchainDB&#39;s file(s) should be in
#define DBF_RDONLY
bool store_blockchain_raw(cryptonote::Blockchain *cs, cryptonote::tx_memory_pool *txp, boost::filesystem::path &output_file, uint64_t use_block_height=0)
bool set_module_name_and_folder(const std::string &path_to_process_)
Definition: string_tools.h:249
::std::string string
Definition: gtest-port.h:1097
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
void mlog_set_log(const char *log)
Definition: mlog.cpp:288
#define CATCH_ENTRY(location, return_val)
Definition: misc_log_ex.h:152
std::string mlog_get_default_log_path(const char *default_filename)
Definition: mlog.cpp:72
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
Definition: mlog.cpp:148
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
const command_line::arg_descriptor< bool, false > arg_stagenet_on
const arg_descriptor< bool > arg_help
std::string blockchain_db_types(const std::string &sep)
virtual void open(const std::string &filename, const int db_flags=0)=0
open a db, or create it if necessary.
bool on_startup()
Definition: util.cpp:778
const char *const ELECTRONEUM_VERSION_FULL
bool blockchain_valid_db_type(const std::string &db_type)
const command_line::arg_descriptor< bool, false > arg_testnet_on
BlockchainDB * new_db(const std::string &db_type)
bool handle_error_helper(const boost::program_options::options_description &desc, F parser)
Definition: command_line.h:237
std::enable_if<!std::is_same< T, bool >::value, bool >::type has_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
Definition: command_line.h:258
#define TRY_ENTRY()
Definition: misc_log_ex.h:151
unsigned int uint32_t
Definition: stdint.h:126
const command_line::arg_descriptor< std::string > arg_log_level
#define BLOCKCHAIN_RAW
unsigned __int64 uint64_t
Definition: stdint.h:136
const command_line::arg_descriptor< std::string, false, true, 2 > arg_data_dir
void add_arg(boost::program_options::options_description &description, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg, bool unique=true)
Definition: command_line.h:188
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:94
uint32_t get_blockchain_pruning_seed() const
Definition: blockchain.h:1017
bool store_blockchain_raw(cryptonote::Blockchain *cs, cryptonote::tx_memory_pool *txp, boost::filesystem::path &output_file, uint64_t use_block_height=0)
The BlockchainDB backing store interface declaration/contract.
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
#define ENDL
Definition: misc_log_ex.h:149
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
boost::program_options::basic_parsed_options< charT > parse_command_line(int argc, const charT *const argv[], const boost::program_options::options_description &desc, bool allow_unregistered=false)
Definition: command_line.h:224
bool init(BlockchainDB *db, const network_type nettype=MAINNET, bool offline=false, const cryptonote::test_options *test_options=NULL, difficulty_type fixed_difficulty=0, const GetCheckpointsCallback &get_checkpoints=nullptr, bool ignore_bsig=false, bool fallback_to_pow=false)
Initialize the Blockchain state.
Definition: blockchain.cpp:334
std::string to_string(t_connection_type type)
bool is_arg_defaulted(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
Definition: command_line.h:265
Here is the call graph for this function: