Add plugin for dumping a new genesis block based on balances at a given block. It calls
a function to dump state at the block, but we haven't decided exactly what it should dump yet.
This commit is contained in:
parent
66bf0545c5
commit
84a43f9a3c
3 changed files with 190 additions and 0 deletions
17
libraries/plugins/generate_genesis/CMakeLists.txt
Normal file
17
libraries/plugins/generate_genesis/CMakeLists.txt
Normal file
|
|
@ -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
|
||||
)
|
||||
114
libraries/plugins/generate_genesis/generate_genesis.cpp
Normal file
114
libraries/plugins/generate_genesis/generate_genesis.cpp
Normal file
|
|
@ -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 <graphene/generate_genesis/generate_genesis.hpp>
|
||||
|
||||
#include <graphene/chain/database.hpp>
|
||||
#include <graphene/chain/genesis_state.hpp>
|
||||
#include <graphene/chain/witness_object.hpp>
|
||||
#include <graphene/time/time.hpp>
|
||||
|
||||
#include <graphene/utilities/key_conversion.hpp>
|
||||
|
||||
#include <fc/smart_ref_impl.hpp>
|
||||
#include <fc/thread/thread.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<std::string>()->default_value("genesis.json"), "Genesis file to create")
|
||||
("snapshot-block-number", bpo::value<uint32_t>()->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<std::string>();
|
||||
_block_to_snapshot = options["snapshot-block-number"].as<uint32_t>();
|
||||
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<graphene::chain::account_index>();
|
||||
auto& account_by_id_index = account_index.indices().get<graphene::chain::by_id>();
|
||||
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()
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -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 <graphene/app/plugin.hpp>
|
||||
#include <graphene/chain/database.hpp>
|
||||
|
||||
#include <fc/thread/future.hpp>
|
||||
|
||||
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
|
||||
Loading…
Reference in a new issue