From ee9692226ab21118ab806ec8bbb4dc03ef71bc75 Mon Sep 17 00:00:00 2001 From: maslenitsa93 Date: Tue, 28 Aug 2018 04:58:40 +0300 Subject: [PATCH] Added annotated_signed_block #936 --- libraries/api/CMakeLists.txt | 2 + libraries/api/block_objects.cpp | 24 ++++++++ .../api/include/golos/api/block_objects.hpp | 58 +++++++++++++++++++ .../include/golos/wallet/remote_node_api.hpp | 3 +- .../wallet/include/golos/wallet/wallet.hpp | 14 +---- libraries/wallet/wallet.cpp | 11 +--- plugins/database_api/api.cpp | 34 ++--------- plugins/operation_history/CMakeLists.txt | 1 + .../plugins/operation_history/plugin.hpp | 8 +++ plugins/operation_history/plugin.cpp | 36 ++++++++++++ 10 files changed, 137 insertions(+), 54 deletions(-) create mode 100644 libraries/api/block_objects.cpp create mode 100644 libraries/api/include/golos/api/block_objects.hpp diff --git a/libraries/api/CMakeLists.txt b/libraries/api/CMakeLists.txt index b7efed09ef..5f43ba4695 100644 --- a/libraries/api/CMakeLists.txt +++ b/libraries/api/CMakeLists.txt @@ -9,6 +9,7 @@ list(APPEND CURRENT_TARGET_HEADERS include/golos/api/vote_state.hpp include/golos/api/account_vote.hpp include/golos/api/discussion_helper.hpp + include/golos/api/block_objects.hpp ) list(APPEND CURRENT_TARGET_SOURCES @@ -16,6 +17,7 @@ list(APPEND CURRENT_TARGET_SOURCES discussion_helper.cpp chain_api_properties.cpp witness_api_object.cpp + block_objects.cpp ) if(BUILD_SHARED_LIBRARIES) diff --git a/libraries/api/block_objects.cpp b/libraries/api/block_objects.cpp new file mode 100644 index 0000000000..00515f5b26 --- /dev/null +++ b/libraries/api/block_objects.cpp @@ -0,0 +1,24 @@ +#include + +namespace golos { namespace api { + +block_operation::block_operation() = default; + +annotated_signed_block::annotated_signed_block() = default; + +annotated_signed_block::annotated_signed_block(const signed_block& block) + : signed_block(block) { + block_id = id(); + signing_key = signee(); + transaction_ids.reserve(transactions.size()); + for (const signed_transaction& tx : transactions) { + transaction_ids.push_back(tx.id()); + } +} + +annotated_signed_block::annotated_signed_block(const signed_block& block, const block_operations& ops) + : annotated_signed_block(block) { + _virtual_operations = ops; +} + +} } // golos::api \ No newline at end of file diff --git a/libraries/api/include/golos/api/block_objects.hpp b/libraries/api/include/golos/api/block_objects.hpp new file mode 100644 index 0000000000..fb5cb66885 --- /dev/null +++ b/libraries/api/include/golos/api/block_objects.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace golos { namespace api { + +using namespace golos::protocol; +using golos::chain::operation_notification; + +// block_operation used in block_applied_callback to represent virtual operations. +// default operation type have no position info (trx, op_in_trx) +struct block_operation { + + block_operation(); + + block_operation(const operation_notification& o) : + trx_in_block(o.trx_in_block), + op_in_trx(o.op_in_trx), + virtual_op(o.virtual_op), + op(o.op) {}; + + uint32_t trx_in_block = 0; + uint16_t op_in_trx = 0; + uint32_t virtual_op = 0; + operation op; +}; + +using block_operations = std::vector; + +struct annotated_signed_block : public signed_block { + + annotated_signed_block(); + + annotated_signed_block(const signed_block& block); + + annotated_signed_block(const signed_block& block, const block_operations& ops); + + annotated_signed_block(const annotated_signed_block& block) = default; + + block_id_type block_id; + public_key_type signing_key; + vector transaction_ids; + + // name field starting with _ coz it's not directly related to block + optional _virtual_operations; +}; + +} } // golos::api + + +FC_REFLECT((golos::api::block_operation), + (trx_in_block)(op_in_trx)(virtual_op)(op)) +FC_REFLECT_DERIVED((golos::api::annotated_signed_block), ((golos::chain::signed_block)), + (block_id)(signing_key)(transaction_ids)(_virtual_operations)) diff --git a/libraries/wallet/include/golos/wallet/remote_node_api.hpp b/libraries/wallet/include/golos/wallet/remote_node_api.hpp index 8ab1dfd3d5..539119c346 100644 --- a/libraries/wallet/include/golos/wallet/remote_node_api.hpp +++ b/libraries/wallet/include/golos/wallet/remote_node_api.hpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -46,7 +47,7 @@ using namespace golos::api; * Class is used by wallet to send formatted API calls to database_api plugin on remote node. */ struct remote_database_api { - optional< database_api::signed_block > get_block( uint32_t ); + optional< golos::api::annotated_signed_block > get_block( uint32_t ); optional< block_header > get_block_header( uint32_t ); fc::variant_object get_config(); database_api::dynamic_global_property_object get_dynamic_global_properties(); diff --git a/libraries/wallet/include/golos/wallet/wallet.hpp b/libraries/wallet/include/golos/wallet/wallet.hpp index 0fe3175bb4..a827f31052 100644 --- a/libraries/wallet/include/golos/wallet/wallet.hpp +++ b/libraries/wallet/include/golos/wallet/wallet.hpp @@ -119,15 +119,6 @@ namespace golos { namespace wallet { string ws_server = "ws://localhost:8091"; }; - struct signed_block_with_info: public signed_block { - signed_block_with_info(const signed_block& block); - signed_block_with_info(const signed_block_with_info& block) = default; - - block_id_type block_id; - public_key_type signing_key; - vector transaction_ids; - }; - struct key_with_data { std::string account; std::string type; @@ -266,7 +257,7 @@ namespace golos { namespace wallet { * * @returns Public block data on the blockchain */ - optional get_block(uint32_t num); + optional get_block(uint32_t num); /** Returns sequence of operations included/generated in a specified block * @@ -1349,9 +1340,6 @@ namespace golos { namespace wallet { FC_REFLECT((golos::wallet::wallet_data), (cipher_keys)(ws_server)) -FC_REFLECT_DERIVED((golos::wallet::signed_block_with_info), ((golos::chain::signed_block)), - (block_id)(signing_key)(transaction_ids)) - FC_REFLECT( (golos::wallet::brain_key_info), (brain_priv_key)(wif_priv_key) (pub_key)) FC_REFLECT( (golos::wallet::plain_keys), (checksum)(keys) ) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 86814be00a..cc665273c7 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1267,7 +1267,7 @@ namespace golos { namespace wallet { return my->copy_wallet_file(destination_filename); } - optional wallet_api::get_block(uint32_t num) { + optional wallet_api::get_block(uint32_t num) { return my->_remote_database_api->get_block( num ); } @@ -1649,15 +1649,6 @@ fc::ecc::private_key wallet_api::derive_private_key(const std::string& prefix_st return std::make_pair( public_key_type( priv.get_public_key() ), key_to_wif( priv ) ); } - signed_block_with_info::signed_block_with_info(const signed_block& block): signed_block(block) { - block_id = id(); - signing_key = signee(); - transaction_ids.reserve(transactions.size()); - for (const signed_transaction& tx : transactions) { - transaction_ids.push_back(tx.id()); - } - } - witness_api::feed_history_api_object wallet_api::get_feed_history()const { return my->_remote_witness_api->get_feed_history(); } diff --git a/plugins/database_api/api.cpp b/plugins/database_api/api.cpp index 63d8b60683..2d5152d44a 100755 --- a/plugins/database_api/api.cpp +++ b/plugins/database_api/api.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -15,6 +16,8 @@ namespace golos { namespace plugins { namespace database_api { +using golos::api::annotated_signed_block; +using golos::api::block_operations; template struct callback_info { @@ -49,31 +52,6 @@ using pending_tx_callback_info = callback_info; using pending_tx_callback = pending_tx_callback_info::callback_t; -// block_operation used in block_applied_callback to represent virtual operations. -// default operation type have no position info (trx, op_in_trx) -struct block_operation { - block_operation(const operation_notification& o) : - trx_in_block(o.trx_in_block), - op_in_trx(o.op_in_trx), - virtual_op(o.virtual_op), - op(o.op) {}; - - uint32_t trx_in_block = 0; - uint16_t op_in_trx = 0; - uint32_t virtual_op = 0; - operation op; -}; - -using block_operations = std::vector; - -struct block_with_vops : public signed_block { - block_with_vops(signed_block b, block_operations ops): signed_block(b), _virtual_operations(ops) { - }; - - // name field starting with _ coz it's not directly related to block - block_operations _virtual_operations; -}; - struct virtual_operations { virtual_operations(uint32_t block_num, block_operations ops): block_num(block_num), operations(ops) { }; @@ -243,7 +221,7 @@ DEFINE_API(plugin, set_block_applied_callback) { r = fc::variant(virtual_operations(block.block_num(), my->get_block_vops())); break; case full: - r = fc::variant(block_with_vops(block, my->get_block_vops())); + r = fc::variant(annotated_signed_block(block, my->get_block_vops())); break; default: break; @@ -926,9 +904,5 @@ void plugin::plugin_startup() { } } } // golos::plugins::database_api FC_REFLECT((golos::plugins::database_api::virtual_operations), (block_num)(operations)) -FC_REFLECT((golos::plugins::database_api::block_operation), - (trx_in_block)(op_in_trx)(virtual_op)(op)) -FC_REFLECT_DERIVED((golos::plugins::database_api::block_with_vops), ((golos::protocol::signed_block)), - (_virtual_operations)) FC_REFLECT_ENUM(golos::plugins::database_api::block_applied_callback_result_type, (block)(header)(virtual_ops)(full)) diff --git a/plugins/operation_history/CMakeLists.txt b/plugins/operation_history/CMakeLists.txt index 1253a4518c..23050f2b20 100644 --- a/plugins/operation_history/CMakeLists.txt +++ b/plugins/operation_history/CMakeLists.txt @@ -35,6 +35,7 @@ target_link_libraries ( graphene_time chainbase fc + golos::api ) target_include_directories(golos_${CURRENT_TARGET} diff --git a/plugins/operation_history/include/golos/plugins/operation_history/plugin.hpp b/plugins/operation_history/include/golos/plugins/operation_history/plugin.hpp index 662f90fa99..1f5bba95d7 100644 --- a/plugins/operation_history/include/golos/plugins/operation_history/plugin.hpp +++ b/plugins/operation_history/include/golos/plugins/operation_history/plugin.hpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -38,10 +39,15 @@ namespace golos { namespace plugins { namespace operation_history { using namespace chain; + using golos::api::annotated_signed_block; + using golos::api::block_operation; + using golos::api::block_operations; + using plugins::json_rpc::void_type; using plugins::json_rpc::msg_pack; using plugins::json_rpc::msg_pack_transfer; + DEFINE_API_ARGS(get_block_with_virtual_ops, msg_pack, annotated_signed_block) DEFINE_API_ARGS(get_ops_in_block, msg_pack, std::vector) DEFINE_API_ARGS(get_transaction, msg_pack, annotated_signed_transaction) @@ -70,6 +76,8 @@ namespace golos { namespace plugins { namespace operation_history { void plugin_shutdown() override; DECLARE_API( + (get_block_with_virtual_ops) + /** * @brief Get sequence of operations included/generated within a particular block * @param block_num Height of the block whose generated virtual operations should be returned diff --git a/plugins/operation_history/plugin.cpp b/plugins/operation_history/plugin.cpp index e8effa0755..ad5ae75485 100644 --- a/plugins/operation_history/plugin.cpp +++ b/plugins/operation_history/plugin.cpp @@ -120,6 +120,33 @@ namespace golos { namespace plugins { namespace operation_history { } } + annotated_signed_block get_block_with_virtual_ops(uint32_t block_num) { + + annotated_signed_block result; + + auto sb = database.fetch_block_by_number(block_num); + if (!sb.valid()) { + return result; + } + result = annotated_signed_block(*sb); + + const auto& idx = database.get_index().indices().get(); + auto itr = idx.lower_bound(block_num); + result._virtual_operations = block_operations(); + for (; itr != idx.end() && itr->block == block_num; ++itr) { + if (itr->virtual_op != 0) { + block_operation op; + op.trx_in_block = itr->trx_in_block; + op.op_in_trx = itr->op_in_trx; + op.virtual_op = itr->virtual_op; + op.op = fc::raw::unpack(itr->serialized_op); + (*result._virtual_operations).push_back(op); + } + } + + return result; + } + std::vector get_ops_in_block( uint32_t block_num, bool only_virtual @@ -159,6 +186,15 @@ namespace golos { namespace plugins { namespace operation_history { golos::chain::database& database; }; + DEFINE_API(plugin, get_block_with_virtual_ops) { + PLUGIN_API_VALIDATE_ARGS( + (uint32_t, block_num) + ); + return pimpl->database.with_weak_read_lock([&](){ + return pimpl->get_block_with_virtual_ops(block_num); + }); + } + DEFINE_API(plugin, get_ops_in_block) { PLUGIN_API_VALIDATE_ARGS( (uint32_t, block_num)