From 674b38910d785d33a3eb233524360df799a70e68 Mon Sep 17 00:00:00 2001 From: serkixenos Date: Wed, 28 Dec 2022 08:23:29 +0100 Subject: [PATCH] Streamline get_block API from database and cli wallet --- libraries/app/database_api.cpp | 33 ++++++++++++++++--- .../app/include/graphene/app/database_api.hpp | 19 +++++++++++ .../wallet/include/graphene/wallet/wallet.hpp | 22 ++++--------- libraries/wallet/wallet.cpp | 22 ++++--------- 4 files changed, 60 insertions(+), 36 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index e22e1dc4..ef48a88d 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -71,6 +71,17 @@ std::string object_id_to_string(object_id_type id) { return object_id; } +signed_block_with_info::signed_block_with_info(){}; + +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 processed_transaction &tx : transactions) + transaction_ids.push_back(tx.id()); +} + class database_api_impl : public std::enable_shared_from_this { public: database_api_impl(graphene::chain::database &db); @@ -89,6 +100,7 @@ public: optional get_block_header(uint32_t block_num) const; map> get_block_header_batch(const vector block_nums) const; optional get_block(uint32_t block_num) const; + optional get_block2(uint32_t block_num) const; vector> get_blocks(uint32_t block_num_from, uint32_t block_num_to) const; processed_transaction get_transaction(uint32_t block_num, uint32_t trx_in_block) const; @@ -532,6 +544,17 @@ optional database_api_impl::get_block(uint32_t block_num) const { return _db.fetch_block_by_number(block_num); } +optional database_api::get_block2(uint32_t block_num) const { + return my->get_block2(block_num); +} + +optional database_api_impl::get_block2(uint32_t block_num) const { + auto result = _db.fetch_block_by_number(block_num); + if (result) + return signed_block_with_info(*result); + return {}; +} + vector> database_api::get_blocks(uint32_t block_num_from, uint32_t block_num_to) const { return my->get_blocks(block_num_from, block_num_to); } @@ -3135,10 +3158,10 @@ vector database_api_impl::nft_get_tokens_by_owner(const account_id_t vector result; result.reserve(limit); auto itr = std::find_if(idx_nft_range.first, idx_nft_range.second, [&lower_id](const nft_object &obj) { - return !(obj.id.instance() < lower_id.instance); - }); + return !(obj.id.instance() < lower_id.instance); + }); while (limit-- && itr != idx_nft_range.second) - result.emplace_back(*itr++); + result.emplace_back(*itr++); return result; } @@ -3155,8 +3178,8 @@ vector database_api_impl::nft_get_metadata_by_owner(const a vector result; result.reserve(limit); auto itr = std::find_if(idx_nft_range.first, idx_nft_range.second, [&lower_id](const nft_metadata_object &obj) { - return !(obj.id.instance() < lower_id.instance); - }); + return !(obj.id.instance() < lower_id.instance); + }); while (limit-- && itr != idx_nft_range.second) result.emplace_back(*itr++); return result; diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index 55da8e5f..948b6d55 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -82,6 +82,15 @@ using namespace std; class database_api_impl; +struct signed_block_with_info : public signed_block { + signed_block_with_info(); + 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 order { double price; double quote; @@ -202,6 +211,13 @@ public: */ optional get_block(uint32_t block_num) const; + /** + * @brief Retrieve a full, signed block, with some extra info + * @param block_num Height of the block to be returned + * @return the referenced block, or null if no matching block was found + */ + optional get_block2(uint32_t block_num) const; + /** * @brief Retrieve a list of signed blocks * @param block_num_from start @@ -1075,6 +1091,8 @@ extern template class fc::api; // clang-format off +FC_REFLECT_DERIVED(graphene::app::signed_block_with_info, (graphene::chain::signed_block), (block_id)(signing_key)(transaction_ids)); + FC_REFLECT(graphene::app::order, (price)(quote)(base)); FC_REFLECT(graphene::app::order_book, (base)(quote)(bids)(asks)); FC_REFLECT(graphene::app::market_ticker, (base)(quote)(latest)(lowest_ask)(highest_bid)(percent_change)(base_volume)(quote_volume)); @@ -1097,6 +1115,7 @@ FC_API(graphene::app::database_api, (get_block_header) (get_block_header_batch) (get_block) + (get_block2) (get_blocks) (get_transaction) (get_recent_transaction_by_id) diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 3d8d5b06..6d97f647 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -191,17 +191,6 @@ struct worker_vote_delta flat_set vote_abstain; }; -struct signed_block_with_info : public signed_block -{ - signed_block_with_info(); - 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_id_type > transaction_ids; -}; - struct vesting_balance_object_with_info : public vesting_balance_object { vesting_balance_object_with_info(); @@ -276,7 +265,12 @@ class wallet_api * @param num height of the block to retrieve * @returns info about the block, or null if not found */ - optional get_block( uint32_t num ); + optional get_block( uint32_t num ); + /** Returns info about a specified block, with some extra info. + * @param num height of the block to retrieve + * @returns info about the block, or null if not found + */ + optional get_block2( uint32_t num ); /** Get signed blocks * @param block_num_from The lowest block number * @param block_num_to The highest block number @@ -2762,9 +2756,6 @@ FC_REFLECT( graphene::wallet::worker_vote_delta, (vote_abstain) ) -FC_REFLECT_DERIVED( graphene::wallet::signed_block_with_info, (graphene::chain::signed_block), - (block_id)(signing_key)(transaction_ids) ) - FC_REFLECT_DERIVED( graphene::wallet::vesting_balance_object_with_info, (graphene::chain::vesting_balance_object), (allowed_withdraw)(allowed_withdraw_time) ) @@ -2882,6 +2873,7 @@ FC_API( graphene::wallet::wallet_api, (get_account) (get_account_id) (get_block) + (get_block2) (get_blocks) (get_account_count) (get_account_history) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 237815c1..794e8692 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -4549,11 +4549,16 @@ bool wallet_api::copy_wallet_file(string destination_filename) 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_db->get_block(num); } +optional wallet_api::get_block2(uint32_t num) +{ + return my->_remote_db->get_block2(num); +} + vector> wallet_api::get_blocks(uint32_t block_num_from, uint32_t block_num_to) const { return my->_remote_db->get_blocks(block_num_from, block_num_to); @@ -7289,10 +7294,6 @@ vector wallet_api::get_account_roles_by_owner(string owner_ account_object owner_account = my->get_account(owner_account_id_or_name); return my->_remote_db->get_account_roles_by_owner(owner_account.id); } -// default ctor necessary for FC_REFLECT -signed_block_with_info::signed_block_with_info() -{ -} order_book wallet_api::get_order_book( const string& base, const string& quote, unsigned limit ) { @@ -7359,17 +7360,6 @@ std::string wallet_api::eth_estimate_withdrawal_transaction_fee() const return my->eth_estimate_withdrawal_transaction_fee(); } -// default ctor necessary for FC_REFLECT -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 processed_transaction& tx : transactions ) - transaction_ids.push_back( tx.id() ); -} - vesting_balance_object_with_info::vesting_balance_object_with_info() : vesting_balance_object() {