Hive network recognition and proper asset encoding

This commit is contained in:
serkixenos 2021-05-25 13:28:28 +02:00
parent c6a7ab2057
commit 189229b20e
6 changed files with 61 additions and 95 deletions

View file

@ -9,12 +9,33 @@
#define ASSET_PRECISION_KEY "precision"
#define ASSET_NAI_KEY "nai"
namespace graphene { namespace peerplays_sidechain { namespace hive {
uint64_t asset::hbd_symbol_ser = HBD_SYMBOL_SER;
uint64_t asset::hive_symbol_ser = HIVE_SYMBOL_SER;
}}} // namespace graphene::peerplays_sidechain::hive
namespace fc {
void to_variant(const graphene::peerplays_sidechain::hive::asset &var, fc::variant &vo, uint32_t max_depth) {
try {
variant v = mutable_variant_object(ASSET_AMOUNT_KEY, boost::lexical_cast<std::string>(var.amount.value))(ASSET_PRECISION_KEY, uint64_t(TESTS_PRECISION))(ASSET_NAI_KEY, TESTS_NAI);
vo = v;
if (var.symbol == HBD_SYMBOL_SER) {
variant v = mutable_variant_object(ASSET_AMOUNT_KEY, boost::lexical_cast<std::string>(var.amount.value))(ASSET_PRECISION_KEY, uint64_t(HBD_PRECISION))(ASSET_NAI_KEY, HBD_NAI);
vo = v;
}
if (var.symbol == HIVE_SYMBOL_SER) {
variant v = mutable_variant_object(ASSET_AMOUNT_KEY, boost::lexical_cast<std::string>(var.amount.value))(ASSET_PRECISION_KEY, uint64_t(HIVE_PRECISION))(ASSET_NAI_KEY, HIVE_NAI);
vo = v;
}
if (var.symbol == TBD_SYMBOL_SER) {
variant v = mutable_variant_object(ASSET_AMOUNT_KEY, boost::lexical_cast<std::string>(var.amount.value))(ASSET_PRECISION_KEY, uint64_t(TBD_PRECISION))(ASSET_NAI_KEY, TBD_NAI);
vo = v;
}
if (var.symbol == TESTS_SYMBOL_SER) {
variant v = mutable_variant_object(ASSET_AMOUNT_KEY, boost::lexical_cast<std::string>(var.amount.value))(ASSET_PRECISION_KEY, uint64_t(TESTS_PRECISION))(ASSET_NAI_KEY, TESTS_NAI);
vo = v;
}
}
FC_CAPTURE_AND_RETHROW()
}
@ -36,7 +57,12 @@ void from_variant(const fc::variant &var, graphene::peerplays_sidechain::hive::a
FC_ASSERT(v_object.contains(ASSET_NAI_KEY), "NAI field doesn't exist.");
FC_ASSERT(v_object[ASSET_NAI_KEY].is_string(), "Expected a string type for value '${key}'.", ("key", ASSET_NAI_KEY));
vo.symbol = TESTS_SYMBOL_SER;
if (v_object[ASSET_NAI_KEY].as<std::string>(max_depth) == HBD_NAI) {
vo.symbol = graphene::peerplays_sidechain::hive::asset::hbd_symbol_ser;
}
if (v_object[ASSET_NAI_KEY].as<std::string>(max_depth) == HIVE_NAI) {
vo.symbol = graphene::peerplays_sidechain::hive::asset::hive_symbol_ser;
}
}
FC_CAPTURE_AND_RETHROW()
}

View file

@ -25,6 +25,8 @@ namespace graphene { namespace peerplays_sidechain { namespace hive {
#define TESTS_SYMBOL_SER (uint64_t(3) | (TESTS_SYMBOL_U64 << 8))
struct asset {
static uint64_t hbd_symbol_ser;
static uint64_t hive_symbol_ser;
share_type amount;
uint64_t symbol;
};

View file

@ -5,6 +5,11 @@
namespace graphene { namespace peerplays_sidechain { namespace hive {
enum network {
mainnet,
testnet
};
struct void_t {};
typedef fc::static_variant<void_t> future_extensions;

View file

@ -1,90 +0,0 @@
//#pragma once
//#include <boost/container/flat_set.hpp>
//#include <cstdint>
//#include <fc/uint128.hpp>
//
//#include "asset_symbol.hpp"
//
//namespace fc {
//class variant;
//} // namespace fc
//
//namespace graphene { namespace peerplays_sidechain { namespace hive {
////template< typename Storage = fc::uint128 >
////class fixed_string_impl;
//
//class asset_symbol_type;
////class legacy_hive_asset_symbol_type;
////struct legacy_hive_asset;
//}}} // namespace graphene::peerplays_sidechain::hive
//
////using boost::container::flat_set;
////
////template< class Key >
////class flat_set_ex: public flat_set< Key >
////{
//// public:
////
//// flat_set_ex& operator=( const flat_set< Key >& obj )
//// {
//// flat_set< Key >::operator=( obj );
//// return *this;
//// }
////
//// flat_set_ex& operator=( const flat_set_ex& obj )
//// {
//// flat_set< Key >::operator=( obj );
//// return *this;
//// }
////};
//
//namespace fc {
//namespace raw {
//
////template<typename Stream, typename T>
////void pack( Stream& s, const flat_set_ex<T>& value );
////template<typename Stream, typename T>
////void unpack( Stream& s, flat_set_ex<T>& value, uint32_t depth = 0 );
//
////template< typename Stream, typename Storage >
////inline void pack( Stream& s, const hive::protocol::fixed_string_impl< Storage >& u );
////template< typename Stream, typename Storage >
////inline void unpack( Stream& s, hive::protocol::fixed_string_impl< Storage >& u, uint32_t depth = 0 );
//
//template <typename Stream>
//inline void pack(Stream &s, const graphene::peerplays_sidechain::hive::asset_symbol_type &sym, uint32_t depth = 0);
//template <typename Stream>
//inline void unpack(Stream &s, graphene::peerplays_sidechain::hive::asset_symbol_type &sym, uint32_t depth = 0);
//
////template< typename Stream >
////inline void pack( Stream& s, const hive::protocol::legacy_hive_asset_symbol_type& sym );
////template< typename Stream >
////inline void unpack( Stream& s, hive::protocol::legacy_hive_asset_symbol_type& sym, uint32_t depth = 0 );
//
//} // namespace raw
//
////template<typename T>
////void to_variant( const flat_set_ex<T>& var, variant& vo );
//
////template<typename T>
////void from_variant( const variant& var, flat_set_ex<T>& vo );
//
////template< typename Storage >
////inline void to_variant( const hive::protocol::fixed_string_impl< Storage >& s, fc::variant& v );
////template< typename Storage >
////inline void from_variant( const variant& v, hive::protocol::fixed_string_impl< Storage >& s );
//
//inline void to_variant(const graphene::peerplays_sidechain::hive::asset_symbol_type &sym, fc::variant &v, uint32_t max_depth);
//
////inline void from_variant( const fc::variant& v, hive::protocol::legacy_hive_asset& leg );
////inline void to_variant( const hive::protocol::legacy_hive_asset& leg, fc::variant& v );
//
////template<typename T> struct get_typename<flat_set_ex<T>>
////{
//// static const char* name() {
//// static std::string n = std::string("flat_set<") + get_typename< fc::flat_set<T> >::name() + ">";
//// return n.c_str();
//// }
////};
//
//} // namespace fc

View file

@ -18,6 +18,7 @@ public:
std::string account_history_api_get_transaction(std::string transaction_id);
std::string block_api_get_block(uint32_t block_number);
std::string condenser_api_get_config();
std::string condenser_api_get_transaction(std::string transaction_id);
std::string database_api_get_dynamic_global_properties();
std::string database_api_get_version();
@ -26,6 +27,7 @@ public:
std::string get_chain_id();
std::string get_head_block_id();
std::string get_head_block_time();
std::string get_is_test_net();
};
class hive_wallet_rpc_client : public rpc_client {
@ -71,6 +73,7 @@ private:
hive_wallet_rpc_client *wallet_rpc_client;
hive::chain_id_type chain_id;
hive::network network_type;
uint64_t last_block_received;
fc::future<void> _listener_task;

View file

@ -42,6 +42,11 @@ std::string hive_node_rpc_client::block_api_get_block(uint32_t block_number) {
return send_post_request("block_api.get_block", params, false);
}
std::string hive_node_rpc_client::condenser_api_get_config() {
std::string params = "[]";
return send_post_request("condenser_api.get_config", params, false);
}
std::string hive_node_rpc_client::condenser_api_get_transaction(std::string transaction_id) {
std::string params = "[\"" + transaction_id + "\"]";
return send_post_request("condenser_api.get_transaction", params, false);
@ -75,6 +80,11 @@ std::string hive_node_rpc_client::get_head_block_time() {
return retrieve_value_from_reply(reply_str, "time");
}
std::string hive_node_rpc_client::get_is_test_net() {
std::string reply_str = condenser_api_get_config();
return retrieve_value_from_reply(reply_str, "IS_TEST_NET");
}
hive_wallet_rpc_client::hive_wallet_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password) :
rpc_client(_ip, _port, _user, _password) {
}
@ -182,6 +192,16 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
std::string chain_id_str = node_rpc_client->get_chain_id();
chain_id = chain_id_type(chain_id_str);
std::string is_test_net = node_rpc_client->get_is_test_net();
network_type = is_test_net.compare("true") == 0 ? hive::network::testnet : hive::network::mainnet;
if (network_type == hive::network::mainnet) {
hive::asset::hbd_symbol_ser = HBD_SYMBOL_SER;
hive::asset::hive_symbol_ser = HIVE_SYMBOL_SER;
} else {
hive::asset::hbd_symbol_ser = TBD_SYMBOL_SER;
hive::asset::hive_symbol_ser = TESTS_SYMBOL_SER;
}
last_block_received = 0;
schedule_hive_listener();
event_received.connect([this](const std::string &event_data) {
@ -480,10 +500,10 @@ bool sidechain_net_handler_hive::process_withdrawal(const son_wallet_withdraw_ob
uint64_t symbol = 0;
if (swwo.withdraw_currency == "HBD") {
symbol = TBD_SYMBOL_SER;
symbol = hive::asset::hbd_symbol_ser;
}
if (swwo.withdraw_currency == "HIVE") {
symbol = TESTS_SYMBOL_SER;
symbol = hive::asset::hive_symbol_ser;
}
hive::transfer_operation t_op;