diff --git a/libraries/plugins/generate_genesis/CMakeLists.txt b/libraries/plugins/generate_genesis/CMakeLists.txt new file mode 100644 index 00000000..6699eb9e --- /dev/null +++ b/libraries/plugins/generate_genesis/CMakeLists.txt @@ -0,0 +1,17 @@ +file(GLOB HEADERS "include/graphene/generate_genesis/*.hpp") + +add_library( grapehen_generate_genesis + generate_genesis.cpp + ) + +target_link_libraries( grapehen_generate_genesis graphene_chain graphene_app graphene_time ) +target_include_directories( grapehen_generate_genesis + PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) + +install( TARGETS + grapehen_generate_genesis + + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib +) diff --git a/libraries/plugins/generate_genesis/generate_genesis.cpp b/libraries/plugins/generate_genesis/generate_genesis.cpp new file mode 100644 index 00000000..f580f0ca --- /dev/null +++ b/libraries/plugins/generate_genesis/generate_genesis.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2015 Cryptonomex, Inc., and contributors. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#include + +using namespace graphene::generate_genesis_plugin; +using std::string; +using std::vector; + +namespace bpo = boost::program_options; + +void generate_genesis_plugin::plugin_set_program_options( + boost::program_options::options_description& command_line_options, + boost::program_options::options_description& config_file_options) +{ + command_line_options.add_options() + ("output-genesis-file,o", bpo::value()->default_value("genesis.json"), "Genesis file to create") + ("snapshot-block-number", bpo::value()->default_value(1), "Block number at which to snapshot balances") + ; + config_file_options.add(command_line_options); +} + +std::string generate_genesis_plugin::plugin_name()const +{ + return "generate_genesis"; +} + +void generate_genesis_plugin::plugin_initialize(const boost::program_options::variables_map& options) +{ try { + ilog("generate genesis plugin: plugin_initialize() begin"); + _options = &options; + + _genesis_filename = options["output-genesis-file"].as(); + _block_to_snapshot = options["snapshot-block-number"].as(); + database().applied_block.connect([this](const graphene::chain::signed_block& b){ block_applied(b); }); + ilog("generate genesis plugin: plugin_initialize() end"); +} FC_LOG_AND_RETHROW() } + +void generate_genesis_plugin::plugin_startup() +{ try { + ilog("generate genesis plugin: plugin_startup() begin"); + chain::database& d = database(); + if (d.head_block_num() == _block_to_snapshot) + { + ilog("generate genesis plugin: already at snapshot block"); + generate_snapshot(); + } + else if (d.head_block_num() > _block_to_snapshot) + elog("generate genesis plugin: already passed snapshot block, you must reindex to return to the snapshot state"); + else + elog("generate genesis plugin: waiting for block ${snapshot_block} to generate snapshot, current head is ${head}", + ("snapshot_block", _block_to_snapshot)("head", d.head_block_num())); + + ilog("generate genesis plugin: plugin_startup() end"); +} FC_CAPTURE_AND_RETHROW() } + +void generate_genesis_plugin::block_applied(const graphene::chain::signed_block& b) +{ + if (b.block_num() == _block_to_snapshot) + { + ilog("generate genesis plugin: snapshot block has arrived"); + generate_snapshot(); + } +} + +void generate_genesis_plugin::generate_snapshot() +{ + ilog("generate genesis plugin: generating snapshot now"); + graphene::chain::genesis_state_type new_genesis_state; + chain::database& d = database(); + auto& account_index = d.get_index_type(); + auto& account_by_id_index = account_index.indices().get(); + for (const graphene::chain::account_object& account_obj : account_by_id_index) + { + //new_genesis_state.initial_accounts.emplace_back(genesis_state_type::initial_account_type(account_obj.name, account + } +} + +void generate_genesis_plugin::plugin_shutdown() +{ +} + diff --git a/libraries/plugins/generate_genesis/include/graphene/generate_genesis/generate_genesis.hpp b/libraries/plugins/generate_genesis/include/graphene/generate_genesis/generate_genesis.hpp new file mode 100644 index 00000000..d91dd900 --- /dev/null +++ b/libraries/plugins/generate_genesis/include/graphene/generate_genesis/generate_genesis.hpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015 Cryptonomex, Inc., and contributors. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#pragma once + +#include +#include + +#include + +namespace graphene { namespace generate_genesis_plugin { + +class generate_genesis_plugin : public graphene::app::plugin { +public: + ~generate_genesis_plugin() { + } + + std::string plugin_name()const override; + + virtual void plugin_set_program_options( + boost::program_options::options_description &command_line_options, + boost::program_options::options_description &config_file_options + ) override; + + virtual void plugin_initialize( const boost::program_options::variables_map& options ) override; + virtual void plugin_startup() override; + virtual void plugin_shutdown() override; + +private: + void block_applied(const graphene::chain::signed_block& b); + void generate_snapshot(); + + boost::program_options::variables_map _options; + + uint32_t _block_to_snapshot; + std::string _genesis_filename; +}; + +} } //graphene::generate_genesis_plugin