diff --git a/src/app/configurator.cpp b/src/app/configurator.cpp index 288b349..8ef89a3 100644 --- a/src/app/configurator.cpp +++ b/src/app/configurator.cpp @@ -163,6 +163,8 @@ namespace lean::app { if (vm.contains("help")) { std::cout << "Lean-node version " << buildVersion() << '\n'; std::cout << cli_options_ << '\n'; + std::println(std::cout, "Other commands:"); + std::println(std::cout, " qlean key generate-node-key"); return true; } diff --git a/src/executable/CMakeLists.txt b/src/executable/CMakeLists.txt index 9897ab9..367f91a 100644 --- a/src/executable/CMakeLists.txt +++ b/src/executable/CMakeLists.txt @@ -29,12 +29,14 @@ if (MACOS_BIG_EXE_USE_DYLIB) target_link_libraries(lean_node ${LIBRARIES}) add_executable(lean_node_dlopen dlopen.cpp) - set_target_properties(lean_node_dlopen PROPERTIES OUTPUT_NAME lean_node) + set_target_properties(lean_node_dlopen PROPERTIES OUTPUT_NAME qlean) set_target_properties(lean_node_dlopen PROPERTIES LINKER_LANGUAGE CXX) + add_dependencies(lean_node_dlopen lean_node) else () add_executable(lean_node lean_node.cpp) target_link_libraries(lean_node ${LIBRARIES}) endif () +set_target_properties(lean_node PROPERTIES OUTPUT_NAME qlean) add_dependencies(lean_node all_modules) #if (BACKWARD) diff --git a/src/executable/cmd_key_generate_node_key.hpp b/src/executable/cmd_key_generate_node_key.hpp new file mode 100644 index 0000000..3177b0e --- /dev/null +++ b/src/executable/cmd_key_generate_node_key.hpp @@ -0,0 +1,24 @@ +/** + * Copyright Quadrivium LLC + * All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include + +#include +#include +#include +#include + +inline void cmdKeyGenerateNodeKey() { + libp2p::crypto::secp256k1::Secp256k1ProviderImpl secp256k1{ + std::make_shared()}; + auto keypair = secp256k1.generate().value(); + auto peer_id = libp2p::peerIdFromSecp256k1(keypair.public_key); + std::println("{}", fmt::format("{:0xx}", qtils::Hex{keypair.private_key})); + std::println("{}", peer_id.toBase58()); +} diff --git a/src/executable/lean_node.cpp b/src/executable/lean_node.cpp index 060ed2e..5a8371a 100644 --- a/src/executable/lean_node.cpp +++ b/src/executable/lean_node.cpp @@ -15,6 +15,7 @@ #include "app/application.hpp" #include "app/configuration.hpp" #include "app/configurator.hpp" +#include "executable/cmd_key_generate_node_key.hpp" #include "injector/node_injector.hpp" #include "loaders/loader.hpp" #include "log/logger.hpp" @@ -100,6 +101,12 @@ namespace { int main(int argc, const char **argv, const char **env) { soralog::util::setThreadName("lean-node"); + auto getArg = [&](size_t i) { + return static_cast(i) < argc + ? std::make_optional(std::string_view{argv[i]}) + : std::nullopt; + }; + qtils::FinalAction flush_std_streams_at_exit([] { std::cout.flush(); std::cerr.flush(); @@ -117,6 +124,16 @@ int main(int argc, const char **argv, const char **env) { return EXIT_FAILURE; } + if (getArg(1) == "key") { + if (getArg(2) == "generate-node-key") { + cmdKeyGenerateNodeKey(); + return EXIT_SUCCESS; + } + std::println(std::cerr, "Expected one of following commands:"); + std::println(std::cerr, " qlean key generate-node-key"); + return EXIT_FAILURE; + } + auto app_configurator = std::make_unique(argc, argv, env); diff --git a/src/modules/production/production.cpp b/src/modules/production/production.cpp index 5cc67b3..732f42d 100644 --- a/src/modules/production/production.cpp +++ b/src/modules/production/production.cpp @@ -80,7 +80,7 @@ namespace lean::modules { SL_INFO(logger_, "New leaf {} appeared{}", msg->header.index(), - msg->best ? "; it's the new the best leaf" : ""); + msg->best ? "; it's the new best leaf" : ""); } void ProductionModuleImpl::on_block_finalized( diff --git a/src/serde/serialization.hpp b/src/serde/serialization.hpp index f30513a..23f0580 100644 --- a/src/serde/serialization.hpp +++ b/src/serde/serialization.hpp @@ -10,9 +10,21 @@ #include #include +#include #include namespace lean { + enum class SszError { + DecodeError, + }; + Q_ENUM_ERROR_CODE(SszError) { + using E = decltype(e); + switch (e) { + case E::DecodeError: + return "ssz decode error"; + } + abort(); + } // template // qtils::ByteVec encode(const T &v) { @@ -52,8 +64,10 @@ namespace lean { try { return ssz::deserialize( reinterpret_cast &>(data)); - } catch (...) { - return std::make_error_code(std::errc::invalid_argument); + } catch (const std::out_of_range &) { + return outcome::failure(SszError::DecodeError); + } catch (const std::invalid_argument &) { + return outcome::failure(SszError::DecodeError); } } @@ -66,23 +80,23 @@ namespace lean { } template - std::array& as_u8(std::array& v) { - return reinterpret_cast&>(v); + std::array &as_u8(std::array &v) { + return reinterpret_cast &>(v); } template - std::array& as_byte(std::array& v) { - return reinterpret_cast&>(v); + std::array &as_byte(std::array &v) { + return reinterpret_cast &>(v); } template - std::array as_u8(std::array&& v) { - return reinterpret_cast&>(v); + std::array as_u8(std::array &&v) { + return reinterpret_cast &>(v); } template - std::array as_byte(std::array&& v) { - return reinterpret_cast&>(v); + std::array as_byte(std::array &&v) { + return reinterpret_cast &>(v); }