Add 'generate_uia_sharedrop_genesis_plugin' used for generating the initial PPY

balances based on the BitShares PEERPLAYS UIA.
Modify the other generate_genesis plugin to be able to run disabled.
This commit is contained in:
Viktor 2017-06-02 11:56:01 +03:00
parent 687088a13b
commit 9e8bc0b27c
8 changed files with 520 additions and 53 deletions

View file

@ -3,4 +3,5 @@ add_subdirectory( account_history )
add_subdirectory( market_history )
add_subdirectory( delayed_node )
add_subdirectory( generate_genesis )
add_subdirectory( generate_uia_sharedrop_genesis )
add_subdirectory( debug_witness )

View file

@ -51,7 +51,7 @@ void generate_genesis_plugin::plugin_set_program_options(
command_line_options.add_options()
("output-genesis-file,o", bpo::value<std::string>()->default_value("genesis.json"), "Genesis file to create")
("output-csvlog-file,o", bpo::value<std::string>()->default_value("log.csv"), "CSV log file to create")
("snapshot-block-number", bpo::value<uint32_t>()->default_value(0), "Block number at which to snapshot balances")
("snapshot-block-number", bpo::value<uint32_t>(), "Block number at which to snapshot balances")
;
config_file_options.add(command_line_options);
}
@ -68,6 +68,7 @@ void generate_genesis_plugin::plugin_initialize(const boost::program_options::va
_genesis_filename = options["output-genesis-file"].as<std::string>();
_csvlog_filename = options["output-csvlog-file"].as<std::string>();
if (options.count("snapshot-block-number"))
_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");
@ -76,24 +77,29 @@ void generate_genesis_plugin::plugin_initialize(const boost::program_options::va
void generate_genesis_plugin::plugin_startup()
{ try {
ilog("generate genesis plugin: plugin_startup() begin");
if (_block_to_snapshot)
{
chain::database& d = database();
if (d.head_block_num() == _block_to_snapshot)
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)
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()));
}
else
ilog("generate genesis plugin: no snapshot block number provided, plugin is disabled");
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)
if (_block_to_snapshot && b.block_num() == *_block_to_snapshot)
{
ilog("generate genesis plugin: snapshot block has arrived");
generate_snapshot();

View file

@ -52,7 +52,7 @@ namespace graphene { namespace generate_genesis_plugin {
boost::program_options::variables_map _options;
uint32_t _block_to_snapshot;
fc::optional<uint32_t> _block_to_snapshot;
std::string _genesis_filename;
std::string _csvlog_filename;
};

View file

@ -0,0 +1,17 @@
file(GLOB HEADERS "include/graphene/generate_uia_sharedrop_genesis/*.hpp")
add_library( graphene_generate_uia_sharedrop_genesis
generate_uia_sharedrop_genesis.cpp
)
target_link_libraries( graphene_generate_uia_sharedrop_genesis graphene_chain graphene_app graphene_time )
target_include_directories( graphene_generate_uia_sharedrop_genesis
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )
install( TARGETS
graphene_generate_uia_sharedrop_genesis
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)

View file

@ -0,0 +1,361 @@
/*
* 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_uia_sharedrop_genesis/generate_uia_sharedrop_genesis.hpp>
#include <graphene/chain/database.hpp>
#include <graphene/chain/genesis_state.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/utilities/key_conversion.hpp>
#include <fc/smart_ref_impl.hpp>
#include <fc/thread/thread.hpp>
#include <graphene/chain/market_object.hpp>
#include <iostream>
#include <fstream>
using namespace graphene::generate_uia_sharedrop_genesis;
using std::string;
using std::vector;
namespace bpo = boost::program_options;
void generate_uia_sharedrop_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()
("input-uia-sharedrop-genesis-file", bpo::value<std::string>()->default_value("genesis.json"), "Genesis file to read")
("output-uia-sharedrop-genesis-file", bpo::value<std::string>()->default_value("genesis.json"), "Genesis file to create")
("output-uia-sharedrop-csvlog-file", bpo::value<std::string>()->default_value("log.csv"), "CSV log file to create")
("uia-sharedrop-snapshot-block-number", bpo::value<uint32_t>(), "Block number at which to snapshot balances")
;
config_file_options.add(command_line_options);
}
std::string generate_uia_sharedrop_genesis_plugin::plugin_name()const
{
return "generate_uia_sharedrop_genesis";
}
void generate_uia_sharedrop_genesis_plugin::plugin_initialize(const boost::program_options::variables_map& options)
{ try {
ilog("generate uia sharedrop genesis plugin: plugin_initialize() begin");
_options = &options;
_output_genesis_filename = options["output-uia-sharedrop-genesis-file"].as<std::string>();
_input_genesis_filename = options["input-uia-sharedrop-genesis-file"].as<std::string>();
_csvlog_filename = options["output-uia-sharedrop-csvlog-file"].as<std::string>();
if (options.count("uia-sharedrop-snapshot-block-number"))
_block_to_snapshot = options["uia-sharedrop-snapshot-block-number"].as<uint32_t>();
database().applied_block.connect([this](const graphene::chain::signed_block& b){ block_applied(b); });
ilog("generate uia sharedrop genesis plugin: plugin_initialize() end");
} FC_LOG_AND_RETHROW() }
void generate_uia_sharedrop_genesis_plugin::plugin_startup()
{ try {
ilog("generate uia sharedrop genesis plugin: plugin_startup() begin");
if (_block_to_snapshot)
{
chain::database& d = database();
if (d.head_block_num() == *_block_to_snapshot)
{
ilog("generate uia sharedrop genesis plugin: already at snapshot block");
generate_snapshot();
}
else if (d.head_block_num() > *_block_to_snapshot)
elog("generate uia sharedrop genesis plugin: already passed snapshot block, you must reindex to return to the snapshot state");
else
elog("generate uia sharedrop genesis plugin: waiting for block ${snapshot_block} to generate snapshot, current head is ${head}",
("snapshot_block", _block_to_snapshot)("head", d.head_block_num()));
}
else
ilog("generate uia sharedrop genesis plugin: no snapshot block number provided, plugin is disabled");
ilog("generate uia sharedrop genesis plugin: plugin_startup() end");
} FC_CAPTURE_AND_RETHROW() }
void generate_uia_sharedrop_genesis_plugin::block_applied(const graphene::chain::signed_block& b)
{
if (_block_to_snapshot && b.block_num() == *_block_to_snapshot)
{
ilog("generate uia sharedrop genesis plugin: snapshot block has arrived");
generate_snapshot();
}
}
namespace
{
// anonymous namespace for file-scoped helper functions
std::string modify_account_name(const std::string& name)
{
return std::string("bts-") + name;
}
std::string unmodify_account_name(const std::string& name)
{
FC_ASSERT(name.substr(0, 4) == "bts-");
return name.substr(4);
}
bool is_special_account(const graphene::chain::account_id_type& account_id)
{
return account_id.instance < 100;
}
bool is_scam(const std::string& account_name)
{
static std::set<std::string> scam_accounts{
"polonie-wallet",
"polonie-xwallet",
"poloniewallet",
"poloniex-deposit",
"poloniex-wallet",
"poloniexwall-et",
"poloniexwallett",
"poloniexwall-t",
"poloniexwalle",
"poloniex",
"poloneix",
"poloniex1",
"bittrex-deopsit",
"bittrex-deposi",
"bittrex-depositt",
"bittrex-dposit",
"bittrex",
"bittrex-deposits",
"coinbase",
"blocktrade",
"locktrades",
"yun.bts",
"transwiser-walle",
"transwiser-wallets",
"ranswiser-wallet",
"yun.btc",
"pay.coinbase.com",
"pay.bts.com",
"btc38.com",
"yunbi.com",
"coinbase.com",
"ripple.com"
};
return scam_accounts.find(account_name) != scam_accounts.end();
}
bool is_exchange(const std::string& account_name)
{
static std::set<std::string> exchange_accounts{
"poloniexcoldstorage",
"btc38-public-for-bts-cold",
"poloniexwallet",
"btercom",
"yunbi-cold-wallet",
"btc38-btsx-octo-72722",
"bittrex-deposit",
"btc38btsxwithdrawal"
};
return exchange_accounts.find(account_name) != exchange_accounts.end();
}
}
void generate_uia_sharedrop_genesis_plugin::generate_snapshot()
{
ilog("generate genesis plugin: generating snapshot now");
chain::database& d = database();
// Lookup the ID of the UIA we will be sharedropping on
std::string uia_symbol("PEERPLAYS");
const auto& assets_by_symbol = d.get_index_type<graphene::chain::asset_index>().indices().get<graphene::chain::by_symbol>();
auto itr = assets_by_symbol.find(uia_symbol);
FC_ASSERT(itr != assets_by_symbol.end(), "Unable to find asset named ${uia_symbol}", ("uia_symbol", uia_symbol));
graphene::chain::asset_id_type uia_id = itr->get_id();
ilog("Scanning for all balances of asset ${uia_symbol} (${uia_id})", ("uia_symbol", uia_symbol)("uia_id", uia_id));
uia_sharedrop_balance_object_index_type sharedrop_balances;
// load the balances from the input genesis file, if any
graphene::chain::genesis_state_type new_genesis_state;
if (!_input_genesis_filename.empty())
{
new_genesis_state = fc::json::from_file<graphene::chain::genesis_state_type>(_input_genesis_filename);
for (const graphene::chain::genesis_state_type::initial_bts_account_type& initial_bts_account : new_genesis_state.initial_bts_accounts)
{
std::string account_name = unmodify_account_name(initial_bts_account.name);
auto& account_by_name_index = d.get_index_type<graphene::chain::account_index>().indices().get<graphene::chain::by_name>();
auto account_iter = account_by_name_index.find(account_name);
FC_ASSERT(account_iter != account_by_name_index.end(), "No account ${name}", ("name", account_name));
uia_sharedrop_balance_object balance_object;
balance_object.account_id = account_iter->id;
balance_object.genesis = initial_bts_account.core_balance;
sharedrop_balances.insert(balance_object);
ilog("Loaded genesis balance for ${name}: ${balance}", ("name", account_name)("balance", initial_bts_account.core_balance));
}
}
new_genesis_state.initial_bts_accounts.clear();
auto& balance_index = d.get_index_type<graphene::chain::account_balance_index>().indices().get<graphene::chain::by_asset_balance>();
for (auto balance_iter = balance_index.begin(); balance_iter != balance_index.end(); ++balance_iter)
if (balance_iter->asset_type == uia_id && balance_iter->balance != graphene::chain::share_type())
{
if (is_special_account(balance_iter->owner) || is_exchange(balance_iter->owner(d).name) || is_scam(balance_iter->owner(d).name))
{
ilog("skipping balance in ${account_id} because special or exchange", ("account_id", balance_iter->owner));
}
else
{
auto sharedrop_balance_iter = sharedrop_balances.find(balance_iter->owner);
if (sharedrop_balance_iter == sharedrop_balances.end())
{
uia_sharedrop_balance_object balance_object;
balance_object.account_id = balance_iter->owner;
balance_object.balance = balance_iter->balance;
sharedrop_balances.insert(balance_object);
}
else
{
sharedrop_balances.modify(sharedrop_balance_iter, [&](uia_sharedrop_balance_object& balance_object) {
balance_object.balance = balance_iter->balance;
});
}
}
}
// scan for PEERPLAYS tied up in market orders
auto& limit_order_index = d.get_index_type<graphene::chain::limit_order_index>().indices().get<graphene::chain::by_account>();
for (auto limit_order_iter = limit_order_index.begin(); limit_order_iter != limit_order_index.end(); ++limit_order_iter)
{
if (limit_order_iter->sell_price.base.asset_id == uia_id)
{
if (is_special_account(limit_order_iter->seller) || is_exchange(limit_order_iter->seller(d).name) || is_scam(limit_order_iter->seller(d).name))
ilog("Skipping account ${name} because special/scam/exchange", ("name", limit_order_iter->seller(d).name));
else
{
auto sharedrop_balance_iter = sharedrop_balances.find(limit_order_iter->seller);
if (sharedrop_balance_iter == sharedrop_balances.end())
{
//ilog("found order for new account ${account_id}", ("account_id", limit_order_iter->seller));
uia_sharedrop_balance_object balance_object;
balance_object.account_id = limit_order_iter->seller;
balance_object.orders = limit_order_iter->for_sale;
sharedrop_balances.insert(balance_object);
}
else
{
//ilog("found order for existing account ${account_id}", ("account_id", limit_order_iter->seller));
sharedrop_balances.modify(sharedrop_balance_iter, [&](uia_sharedrop_balance_object& balance_object) {
balance_object.orders += limit_order_iter->for_sale;
});
}
}
}
}
// compute the sharedrop
for (auto sharedrop_balance_iter = sharedrop_balances.begin(); sharedrop_balance_iter != sharedrop_balances.end();)
{
auto this_iter = sharedrop_balance_iter;
++sharedrop_balance_iter;
sharedrop_balances.modify(this_iter, [&](uia_sharedrop_balance_object& balance_object) {
balance_object.sharedrop = balance_object.genesis + (balance_object.balance + balance_object.orders) * 10;
});
}
// Generate CSV file of all sharedrops and the balances we used to calculate them
std::ofstream csv_log_file;
csv_log_file.open(_csvlog_filename);
assert(csv_log_file.is_open());
csv_log_file << "name,genesis,balance,orders,sharedrop\n";
for (const uia_sharedrop_balance_object& balance_object : sharedrop_balances)
csv_log_file << balance_object.account_id(d).name << "," << balance_object.genesis.value << "," << balance_object.balance.value << "," << balance_object.orders.value << "," << balance_object.sharedrop.value << "\n";
ilog("CSV log written to file ${filename}", ("filename", _csvlog_filename));
csv_log_file.close();
//auto& account_index = d.get_index_type<graphene::chain::account_index>();
//auto& account_by_id_index = account_index.indices().get<graphene::chain::by_id>();
// inefficient way of crawling the graph, but we only do it once
std::set<graphene::chain::account_id_type> already_generated;
for (;;)
{
unsigned accounts_generated_this_round = 0;
for (const uia_sharedrop_balance_object& balance_object : sharedrop_balances)
{
const graphene::chain::account_id_type& account_id = balance_object.account_id;
const graphene::chain::share_type& sharedrop_amount = balance_object.sharedrop;
const graphene::chain::account_object& account_obj = account_id(d);
if (already_generated.find(account_id) == already_generated.end())
{
graphene::chain::genesis_state_type::initial_bts_account_type::initial_authority owner;
owner.weight_threshold = account_obj.owner.weight_threshold;
owner.key_auths = account_obj.owner.key_auths;
for (const auto& value : account_obj.owner.account_auths)
{
owner.account_auths.insert(std::make_pair(modify_account_name(value.first(d).name), value.second));
auto owner_balance_iter = sharedrop_balances.find(value.first);
if (owner_balance_iter == sharedrop_balances.end())
{
uia_sharedrop_balance_object balance_object;
balance_object.account_id = value.first;
sharedrop_balances.insert(balance_object);
}
}
owner.key_auths = account_obj.owner.key_auths;
owner.address_auths = account_obj.owner.address_auths;
graphene::chain::genesis_state_type::initial_bts_account_type::initial_authority active;
active.weight_threshold = account_obj.active.weight_threshold;
active.key_auths = account_obj.active.key_auths;
for (const auto& value : account_obj.active.account_auths)
{
active.account_auths.insert(std::make_pair(modify_account_name(value.first(d).name), value.second));
auto active_balance_iter = sharedrop_balances.find(value.first);
if (active_balance_iter == sharedrop_balances.end())
{
uia_sharedrop_balance_object balance_object;
balance_object.account_id = value.first;
sharedrop_balances.insert(balance_object);
}
}
active.key_auths = account_obj.active.key_auths;
active.address_auths = account_obj.active.address_auths;
new_genesis_state.initial_bts_accounts.emplace_back(
graphene::chain::genesis_state_type::initial_bts_account_type(modify_account_name(account_obj.name),
owner, active,
sharedrop_amount));
already_generated.insert(account_id);
++accounts_generated_this_round;
}
}
if (accounts_generated_this_round == 0)
break;
}
fc::json::save_to_file(new_genesis_state, _output_genesis_filename);
ilog("New genesis state written to file ${filename}", ("filename", _output_genesis_filename));
}
void generate_uia_sharedrop_genesis_plugin::plugin_shutdown()
{
}

View file

@ -0,0 +1,79 @@
/*
* 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_uia_sharedrop_genesis {
class generate_uia_sharedrop_genesis_plugin : public graphene::app::plugin {
public:
~generate_uia_sharedrop_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;
fc::optional<uint32_t> _block_to_snapshot;
std::string _input_genesis_filename;
std::string _output_genesis_filename;
std::string _csvlog_filename;
};
class uia_sharedrop_balance_object
{
public:
graphene::chain::account_id_type account_id;
graphene::chain::share_type genesis;
graphene::chain::share_type balance;
graphene::chain::share_type orders;
graphene::chain::share_type sharedrop;
};
using namespace boost::multi_index;
struct by_account{};
typedef multi_index_container<uia_sharedrop_balance_object,
indexed_by<ordered_unique<tag<by_account>,
member<uia_sharedrop_balance_object, graphene::chain::account_id_type, &uia_sharedrop_balance_object::account_id> > > > uia_sharedrop_balance_object_index_type;
} } //graphene::generate_uia_sharedrop_genesis_plugin

View file

@ -11,7 +11,8 @@ endif()
# We have to link against graphene_debug_witness because deficiency in our API infrastructure doesn't allow plugins to be fully abstracted #246
target_link_libraries( witness_node
PRIVATE graphene_app graphene_account_history graphene_market_history graphene_generate_genesis graphene_witness graphene_chain graphene_debug_witness graphene_egenesis_full fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )
PRIVATE graphene_app graphene_account_history graphene_market_history graphene_witness graphene_chain graphene_debug_witness graphene_egenesis_full fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )
# also add dependencies to graphene_generate_genesis graphene_generate_uia_sharedrop_genesis if you want those plugins
install( TARGETS
witness_node

View file

@ -26,7 +26,8 @@
#include <graphene/witness/witness.hpp>
#include <graphene/account_history/account_history_plugin.hpp>
#include <graphene/market_history/market_history_plugin.hpp>
#include <graphene/generate_genesis/generate_genesis_plugin.hpp>
//#include <graphene/generate_genesis/generate_genesis_plugin.hpp>
//#include <graphene/generate_uia_sharedrop_genesis/generate_uia_sharedrop_genesis.hpp>
#include <fc/exception/exception.hpp>
#include <fc/thread/thread.hpp>
@ -76,6 +77,7 @@ int main(int argc, char** argv) {
auto history_plug = node->register_plugin<account_history::account_history_plugin>();
auto market_history_plug = node->register_plugin<market_history::market_history_plugin>();
//auto generate_genesis_plug = node->register_plugin<generate_genesis_plugin::generate_genesis_plugin>();
//auto generate_uia_sharedrop_genesis_plug = node->register_plugin<generate_uia_sharedrop_genesis::generate_uia_sharedrop_genesis_plugin>();
try
{