Merge branch 'feature/sidechain-api' into 'develop'

Sidechain API, SONs listener log

See merge request PBSA/peerplays!94
This commit is contained in:
serkixenos 2022-04-05 12:47:32 +00:00
commit 44b2d21d78
18 changed files with 195 additions and 10 deletions

View file

@ -15,7 +15,7 @@ add_library( graphene_app
#target_link_libraries( graphene_app graphene_market_history graphene_account_history graphene_chain fc graphene_db graphene_net graphene_utilities graphene_debug_witness )
target_link_libraries( graphene_app
PUBLIC graphene_net graphene_utilities
graphene_account_history graphene_accounts_list graphene_affiliate_stats graphene_bookie graphene_debug_witness graphene_elasticsearch graphene_es_objects graphene_generate_genesis graphene_market_history )
graphene_account_history graphene_accounts_list graphene_affiliate_stats graphene_bookie graphene_debug_witness graphene_elasticsearch graphene_es_objects graphene_generate_genesis graphene_market_history peerplays_sidechain )
target_include_directories( graphene_app
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"

View file

@ -106,6 +106,10 @@ void login_api::enable_api(const std::string &api_name) {
// can only enable this API if the plugin was loaded
if (_app.get_plugin("affiliate_stats"))
_affiliate_stats_api = std::make_shared<graphene::affiliate_stats::affiliate_stats_api>(std::ref(_app));
} else if (api_name == "sidechain_api") {
// can only enable this API if the plugin was loaded
if (_app.get_plugin("peerplays_sidechain"))
_sidechain_api = std::make_shared<graphene::peerplays_sidechain::sidechain_api>(std::ref(_app));
}
return;
}
@ -310,6 +314,11 @@ fc::api<graphene::affiliate_stats::affiliate_stats_api> login_api::affiliate_sta
return *_affiliate_stats_api;
}
fc::api<graphene::peerplays_sidechain::sidechain_api> login_api::sidechain() const {
FC_ASSERT(_sidechain_api);
return *_sidechain_api;
}
vector<order_history_object> history_api::get_fill_order_history(std::string asset_a, std::string asset_b, uint32_t limit) const {
FC_ASSERT(_app.chain_database());
const auto &db = *_app.chain_database();

View file

@ -401,6 +401,7 @@ public:
wild_access.allowed_apis.push_back("crypto_api");
wild_access.allowed_apis.push_back("bookie_api");
wild_access.allowed_apis.push_back("affiliate_stats_api");
wild_access.allowed_apis.push_back("sidechain_api");
_apiaccess.permission_map["*"] = wild_access;
}

View file

@ -28,16 +28,15 @@
#include <graphene/chain/protocol/confidential.hpp>
#include <graphene/chain/protocol/types.hpp>
#include <graphene/net/node.hpp>
#include <graphene/accounts_list/accounts_list_plugin.hpp>
#include <graphene/market_history/market_history_plugin.hpp>
#include <graphene/elasticsearch/elasticsearch_plugin.hpp>
#include <graphene/affiliate_stats/affiliate_stats_api.hpp>
#include <graphene/bookie/bookie_api.hpp>
#include <graphene/debug_witness/debug_api.hpp>
#include <graphene/net/node.hpp>
#include <graphene/elasticsearch/elasticsearch_plugin.hpp>
#include <graphene/market_history/market_history_plugin.hpp>
#include <graphene/peerplays_sidechain/sidechain_api.hpp>
#include <fc/api.hpp>
#include <fc/crypto/elliptic.hpp>
@ -405,6 +404,8 @@ public:
fc::api<graphene::bookie::bookie_api> bookie() const;
/// @brief Retrieve the affiliate_stats API (if available)
fc::api<graphene::affiliate_stats::affiliate_stats_api> affiliate_stats() const;
/// @brief Retrieve the sidechain_api API (if available)
fc::api<graphene::peerplays_sidechain::sidechain_api> sidechain() const;
/// @brief Called to enable an API, not reflected.
void enable_api(const string &api_name);
@ -421,6 +422,7 @@ private:
optional<fc::api<graphene::debug_witness::debug_api>> _debug_api;
optional<fc::api<graphene::bookie::bookie_api>> _bookie_api;
optional<fc::api<graphene::affiliate_stats::affiliate_stats_api>> _affiliate_stats_api;
optional<fc::api<graphene::peerplays_sidechain::sidechain_api>> _sidechain_api;
};
}} // namespace graphene::app
@ -498,6 +500,7 @@ FC_API(graphene::app::login_api,
(asset)
(debug)
(bookie)
(affiliate_stats))
(affiliate_stats)
(sidechain))
// clang-format on

View file

@ -4,7 +4,7 @@ add_library( graphene_delayed_node
delayed_node_plugin.cpp
)
target_link_libraries( graphene_delayed_node PRIVATE graphene_plugin graphene_accounts_list graphene_affiliate_stats graphene_bookie graphene_debug_witness graphene_elasticsearch graphene_market_history )
target_link_libraries( graphene_delayed_node PRIVATE graphene_plugin graphene_accounts_list graphene_affiliate_stats graphene_bookie graphene_debug_witness graphene_elasticsearch graphene_market_history peerplays_sidechain )
target_include_directories( graphene_delayed_node
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )

View file

@ -2,6 +2,7 @@ file(GLOB_RECURSE HEADERS "include/graphene/peerplays_sidechain/*.hpp")
add_library( peerplays_sidechain
peerplays_sidechain_plugin.cpp
sidechain_api.cpp
sidechain_net_manager.cpp
sidechain_net_handler.cpp
sidechain_net_handler_bitcoin.cpp

View file

@ -51,6 +51,7 @@ public:
fc::ecc::private_key get_private_key(chain::public_key_type public_key);
void log_son_proposal_retry(int op_type, object_id_type object_id);
bool can_son_participate(int op_type, object_id_type object_id);
std::map<sidechain_type, std::vector<std::string>> get_son_listener_log();
};
}} // namespace graphene::peerplays_sidechain

View file

@ -0,0 +1,34 @@
#pragma once
#include <map>
#include <string>
#include <vector>
#include <fc/api.hpp>
#include <graphene/peerplays_sidechain/peerplays_sidechain_plugin.hpp>
namespace graphene { namespace app {
class application;
}} // namespace graphene::app
namespace graphene { namespace peerplays_sidechain {
namespace detail {
class sidechain_api_impl;
}
class sidechain_api {
public:
sidechain_api(app::application &_app);
virtual ~sidechain_api();
std::shared_ptr<detail::sidechain_api_impl> my;
std::map<sidechain_type, std::vector<std::string>> get_son_listener_log();
};
}} // namespace graphene::peerplays_sidechain
FC_API(graphene::peerplays_sidechain::sidechain_api,
(get_son_listener_log))

View file

@ -47,6 +47,9 @@ public:
virtual std::string send_sidechain_transaction(const sidechain_transaction_object &sto) = 0;
virtual bool settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount) = 0;
void add_to_son_listener_log(std::string trx_id);
std::vector<std::string> get_son_listener_log();
protected:
peerplays_sidechain_plugin &plugin;
graphene::chain::database &database;
@ -56,6 +59,8 @@ protected:
std::map<std::string, std::string> private_keys;
std::vector<std::string> son_listener_log;
void on_applied_block(const signed_block &b);
private:

View file

@ -25,6 +25,8 @@ public:
void send_sidechain_transactions();
void settle_sidechain_transactions();
std::map<sidechain_type, std::vector<std::string>> get_son_listener_log();
private:
peerplays_sidechain_plugin &plugin;
graphene::chain::database &database;

View file

@ -10,6 +10,7 @@
#include <graphene/chain/sidechain_address_object.hpp>
#include <graphene/chain/son_wallet_object.hpp>
#include <graphene/chain/son_wallet_withdraw_object.hpp>
#include <graphene/peerplays_sidechain/sidechain_api.hpp>
#include <graphene/peerplays_sidechain/sidechain_net_manager.hpp>
#include <graphene/utilities/key_conversion.hpp>
@ -44,6 +45,7 @@ public:
fc::ecc::private_key get_private_key(chain::public_key_type public_key);
void log_son_proposal_retry(int op_type, object_id_type object_id);
bool can_son_participate(int op_type, object_id_type object_id);
std::map<sidechain_type, std::vector<std::string>> get_son_listener_log();
void schedule_heartbeat_loop();
void heartbeat_loop();
@ -528,6 +530,10 @@ bool peerplays_sidechain_plugin_impl::can_son_participate(int op_type, object_id
return (itr == son_retry_count.end() || itr->second < retries_threshold);
}
std::map<sidechain_type, std::vector<std::string>> peerplays_sidechain_plugin_impl::get_son_listener_log() {
return net_manager->get_son_listener_log();
}
void peerplays_sidechain_plugin_impl::approve_proposals() {
auto check_approve_proposal = [&](const chain::son_id_type &son_id, const chain::proposal_object &proposal) {
@ -788,4 +794,8 @@ bool peerplays_sidechain_plugin::can_son_participate(int op_type, object_id_type
return my->can_son_participate(op_type, object_id);
}
std::map<sidechain_type, std::vector<std::string>> peerplays_sidechain_plugin::get_son_listener_log() {
return my->get_son_listener_log();
}
}} // namespace graphene::peerplays_sidechain

View file

@ -0,0 +1,48 @@
#include <graphene/peerplays_sidechain/sidechain_api.hpp>
namespace graphene { namespace peerplays_sidechain {
namespace detail {
class sidechain_api_impl {
public:
sidechain_api_impl(app::application &app);
virtual ~sidechain_api_impl();
std::shared_ptr<graphene::peerplays_sidechain::peerplays_sidechain_plugin> get_plugin();
std::map<sidechain_type, std::vector<std::string>> get_son_listener_log();
private:
app::application &app;
};
sidechain_api_impl::sidechain_api_impl(app::application &_app) :
app(_app) {
}
sidechain_api_impl::~sidechain_api_impl() {
}
std::shared_ptr<graphene::peerplays_sidechain::peerplays_sidechain_plugin> sidechain_api_impl::get_plugin() {
return app.get_plugin<graphene::peerplays_sidechain::peerplays_sidechain_plugin>("peerplays_sidechain");
}
std::map<sidechain_type, std::vector<std::string>> sidechain_api_impl::get_son_listener_log() {
return get_plugin()->get_son_listener_log();
}
} // namespace detail
sidechain_api::sidechain_api(graphene::app::application &_app) :
my(std::make_shared<detail::sidechain_api_impl>(_app)) {
}
sidechain_api::~sidechain_api() {
}
std::map<sidechain_type, std::vector<std::string>> sidechain_api::get_son_listener_log() {
return my->get_son_listener_log();
}
}} // namespace graphene::peerplays_sidechain

View file

@ -618,6 +618,17 @@ void sidechain_net_handler::settle_sidechain_transactions() {
});
}
void sidechain_net_handler::add_to_son_listener_log(std::string trx_id) {
son_listener_log.insert(son_listener_log.begin(), trx_id);
if (son_listener_log.size() > 33) {
son_listener_log.erase(son_listener_log.end());
}
}
std::vector<std::string> sidechain_net_handler::get_son_listener_log() {
return son_listener_log;
}
void sidechain_net_handler::on_applied_block(const signed_block &b) {
const chain::global_property_object &gpo = plugin.database().get_global_properties();

View file

@ -1911,6 +1911,8 @@ void sidechain_net_handler_bitcoin::handle_event(const std::string &event_data)
if (block.empty())
return;
add_to_son_listener_log("BLOCK : " + event_data);
auto vins = extract_info_from_block(block);
scoped_lock interlock(event_handler_mutex);
const auto &sidechain_addresses_idx = database.get_index_type<sidechain_address_index>().indices().get<by_sidechain_and_deposit_address_and_expires>();
@ -1940,6 +1942,9 @@ void sidechain_net_handler_bitcoin::handle_event(const std::string &event_data)
sed.peerplays_to = database.get_global_properties().parameters.son_account();
price btc_price = database.get<asset_object>(database.get_global_properties().parameters.btc_asset()).options.core_exchange_rate;
sed.peerplays_asset = asset(sed.sidechain_amount * btc_price.base.amount / btc_price.quote.amount);
add_to_son_listener_log("TRX : " + sed.sidechain_transaction_id);
sidechain_event_data_received(sed);
}
}
@ -2056,7 +2061,7 @@ void sidechain_net_handler_bitcoin::on_changed_objects_cb(const vector<object_id
if (!address_or_script_array.empty()) {
//! Unlock wallet
if (!wallet_password.empty()) {
if( !bitcoin_client->walletpassphrase(wallet_password) )
if (!bitcoin_client->walletpassphrase(wallet_password))
return;
}

View file

@ -846,6 +846,7 @@ void sidechain_net_handler_hive::hive_listener_loop() {
void sidechain_net_handler_hive::handle_event(const std::string &event_data) {
std::string block = node_rpc_client->block_api_get_block(std::atoll(event_data.c_str()));
if (block != "") {
add_to_son_listener_log("BLOCK : " + event_data);
std::stringstream ss(block);
boost::property_tree::ptree block_json;
boost::property_tree::read_json(ss, block_json);
@ -932,6 +933,9 @@ void sidechain_net_handler_hive::handle_event(const std::string &event_data) {
sed.peerplays_from = accn;
sed.peerplays_to = database.get_global_properties().parameters.son_account();
sed.peerplays_asset = asset(sed.sidechain_amount * sidechain_currency_price.base.amount / sidechain_currency_price.quote.amount);
add_to_son_listener_log("TRX : " + sed.sidechain_transaction_id);
sidechain_event_data_received(sed);
}
}

View file

@ -98,6 +98,14 @@ void sidechain_net_manager::settle_sidechain_transactions() {
}
}
std::map<sidechain_type, std::vector<std::string>> sidechain_net_manager::get_son_listener_log() {
std::map<sidechain_type, std::vector<std::string>> result;
for (size_t i = 0; i < net_handlers.size(); i++) {
result[net_handlers.at(i)->get_sidechain()] = net_handlers.at(i)->get_son_listener_log();
}
return result;
}
void sidechain_net_manager::on_applied_block(const signed_block &b) {
}

View file

@ -2591,6 +2591,11 @@ class wallet_api
*/
voters_info get_voters(const string &account_name_or_id) const;
/**
* @brief Demo plugin api
* @return The hello world string
*/
std::map<sidechain_type, std::vector<std::string>> get_son_listener_log() const;
fc::signal<void(bool)> lock_changed;
std::shared_ptr<detail::wallet_api_impl> my;
@ -2905,4 +2910,5 @@ FC_API( graphene::wallet::wallet_api,
(get_votes)
(get_voters_by_id)
(get_voters)
(get_son_listener_log)
)

View file

@ -84,6 +84,7 @@
#include <graphene/wallet/api_documentation.hpp>
#include <graphene/wallet/reflect_util.hpp>
#include <graphene/debug_witness/debug_api.hpp>
#include <graphene/peerplays_sidechain/sidechain_api.hpp>
#ifndef WIN32
# include <sys/types.h>
@ -4178,6 +4179,26 @@ public:
}
}
void use_sidechain_api()
{
if( _remote_sidechain )
return;
try
{
_remote_sidechain = _remote_api->sidechain();
}
catch( const fc::exception& e )
{
std::cerr << "\nCouldn't get sidechain API. You probably are not configured\n"
"to access the sidechain API on the node you are connecting to.\n"
"\n"
"To fix this problem:\n"
"- Please ensure peerplays_sidechain plugin is enabled.\n"
"- Please follow the instructions in README.md to set up an apiaccess file.\n"
"\n";
}
}
void network_add_nodes( const vector<string>& nodes )
{
use_network_node_api();
@ -4294,6 +4315,16 @@ public:
FC_CAPTURE_AND_RETHROW( (account_name_or_id) )
}
std::map<sidechain_type, std::vector<std::string>> get_son_listener_log()
{
use_sidechain_api();
try
{
return (*_remote_sidechain)->get_son_listener_log();
}
FC_CAPTURE_AND_RETHROW()
}
string _wallet_filename;
wallet_data _wallet;
@ -4308,6 +4339,7 @@ public:
fc::api<bookie_api> _remote_bookie;
optional< fc::api<network_node_api> > _remote_net_node;
optional< fc::api<graphene::debug_witness::debug_api> > _remote_debug;
optional< fc::api<graphene::peerplays_sidechain::sidechain_api> > _remote_sidechain;
flat_map<string, operation> _prototype_ops;
@ -7874,6 +7906,11 @@ voters_info wallet_api::get_voters(const string &account_name_or_id) const
return my->get_voters(account_name_or_id);
}
std::map<sidechain_type, std::vector<std::string>> wallet_api::get_son_listener_log() const
{
return my->get_son_listener_log();
}
// default ctor necessary for FC_REFLECT
signed_block_with_info::signed_block_with_info( const signed_block& block )
: signed_block( block )