Streamline get_block API from database and cli wallet

This commit is contained in:
serkixenos 2022-12-28 08:23:29 +01:00
parent c1d5691ce2
commit 674b38910d
4 changed files with 60 additions and 36 deletions

View file

@ -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<database_api_impl> {
public:
database_api_impl(graphene::chain::database &db);
@ -89,6 +100,7 @@ public:
optional<block_header> get_block_header(uint32_t block_num) const;
map<uint32_t, optional<block_header>> get_block_header_batch(const vector<uint32_t> block_nums) const;
optional<signed_block> get_block(uint32_t block_num) const;
optional<signed_block_with_info> get_block2(uint32_t block_num) const;
vector<optional<signed_block>> 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<signed_block> database_api_impl::get_block(uint32_t block_num) const {
return _db.fetch_block_by_number(block_num);
}
optional<signed_block_with_info> database_api::get_block2(uint32_t block_num) const {
return my->get_block2(block_num);
}
optional<signed_block_with_info> 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<optional<signed_block>> 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<nft_object> database_api_impl::nft_get_tokens_by_owner(const account_id_t
vector<nft_object> 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<nft_metadata_object> database_api_impl::nft_get_metadata_by_owner(const a
vector<nft_metadata_object> 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;

View file

@ -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_id_type> transaction_ids;
};
struct order {
double price;
double quote;
@ -202,6 +211,13 @@ public:
*/
optional<signed_block> 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<signed_block_with_info> 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<graphene::app::database_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)

View file

@ -191,17 +191,6 @@ struct worker_vote_delta
flat_set<worker_id_type> 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<signed_block_with_info> get_block( uint32_t num );
optional<signed_block> 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<signed_block_with_info> 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)

View file

@ -4549,11 +4549,16 @@ bool wallet_api::copy_wallet_file(string destination_filename)
return my->copy_wallet_file(destination_filename);
}
optional<signed_block_with_info> wallet_api::get_block(uint32_t num)
optional<signed_block> wallet_api::get_block(uint32_t num)
{
return my->_remote_db->get_block(num);
}
optional<signed_block_with_info> wallet_api::get_block2(uint32_t num)
{
return my->_remote_db->get_block2(num);
}
vector<optional<signed_block>> 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<account_role_object> 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()
{