From fc239a880818fa2fb9c98ec1611b4c8a31fb970e Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Tue, 21 Sep 2021 21:16:51 +0100 Subject: [PATCH] Add --initialize command The --initialize command initialises the data folder, if it is not already initialised. It creates a ledger file and adds the genesis block. --- nano/node/cli.cpp | 10 +++++++- systest/node_initialize.sh | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 systest/node_initialize.sh diff --git a/nano/node/cli.cpp b/nano/node/cli.cpp index 7034014b..c8ea3456 100644 --- a/nano/node/cli.cpp +++ b/nano/node/cli.cpp @@ -44,6 +44,7 @@ void nano::add_node_options (boost::program_options::options_description & descr { // clang-format off description_a.add_options () + ("initialize", "Initialize the data folder, if it is not already initialised. This command is meant to be run when the data folder is empty, to populate it with the genesis block.") ("account_create", "Insert next deterministic key in to ") ("account_get", "Get account number for the ") ("account_key", "Get the public key for ") @@ -262,7 +263,14 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map std::error_code ec; boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as ()) : nano::working_path (); - if (vm.count ("account_create")) + if (vm.count ("initialize")) + { + auto node_flags = nano::inactive_node_flag_defaults (); + node_flags.read_only = false; + nano::update_flags (node_flags, vm); + nano::inactive_node node (data_path, node_flags); + } + else if (vm.count ("account_create")) { if (vm.count ("wallet") == 1) { diff --git a/systest/node_initialize.sh b/systest/node_initialize.sh new file mode 100755 index 00000000..ae80e33d --- /dev/null +++ b/systest/node_initialize.sh @@ -0,0 +1,49 @@ +#!/bin/sh + +set -e + +DATADIR=data.systest + +# the caller should set the env var NANO_NODE_EXE to point to the nano_node executable +# if NANO_NODE_EXE is unser ot empty then "../../build/nano_node" is used +NANO_NODE_EXE=${NANO_NODE_EXE:-../../build/nano_node} + +clean_data_dir() { + rm -f "$DATADIR"/log/log_*.log + rm -f "$DATADIR"/wallets.ldb* + rm -f "$DATADIR"/data.ldb* + rm -f "$DATADIR"/config-*.toml +} + +test_initialize_cmd() { + netmatch="$1" + netcmd="$2" + netarg="$3" + genesishash="$4" + + clean_data_dir + + # initialise data directory + $NANO_NODE_EXE --initialize --data_path "$DATADIR" "$netcmd" "$netarg" + + # check that it is the live network + grep -q "Active network: $netmatch" "$DATADIR"/log/log_*.log + + # check that the ledger file is created + test -e "$DATADIR/data.ldb" + $NANO_NODE_EXE --debug_block_count --data_path "$DATADIR" "$netcmd" "$netarg" | grep -q 'Block count: 1' + + # check the genesis block is correct + $NANO_NODE_EXE --debug_block_dump --data_path "$DATADIR" "$netcmd" "$netarg" | head -n 1 | grep -qi "$genesishash" +} + +mkdir -p "$DATADIR/log" + +test_initialize_cmd "live" "" "" "991CF190094C00F0B68E2E5F75F6BEE95A2E0BD93CEAA4A6734DB9F19B728948" +test_initialize_cmd "live" "--network" "live" "991CF190094C00F0B68E2E5F75F6BEE95A2E0BD93CEAA4A6734DB9F19B728948" +test_initialize_cmd "beta" "--network" "beta" "01A92459E69440D5C1088D3B31F4CA678BE944BAB3776C2E6B7665E9BD99BD5A" +test_initialize_cmd "test" "--network" "test" "B1D60C0B886B57401EF5A1DAA04340E53726AA6F4D706C085706F31BBD100CEE" + +# if it got this far then it is a pass +echo $0: PASSED +exit 0