Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Added annotated_signed_block #936 #945

Merged
merged 1 commit into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libraries/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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
account_api_object.cpp
discussion_helper.cpp
chain_api_properties.cpp
witness_api_object.cpp
block_objects.cpp
)

if(BUILD_SHARED_LIBRARIES)
Expand Down
24 changes: 24 additions & 0 deletions libraries/api/block_objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <golos/api/block_objects.hpp>

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
58 changes: 58 additions & 0 deletions libraries/api/include/golos/api/block_objects.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include <golos/chain/account_object.hpp>
#include <golos/chain/database.hpp>
#include <golos/chain/operation_notification.hpp>
#include <golos/chain/steem_object_types.hpp>
#include <golos/protocol/types.hpp>

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<block_operation>;

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_id_type> transaction_ids;

// name field starting with _ coz it's not directly related to block
optional<block_operations> _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))
3 changes: 2 additions & 1 deletion libraries/wallet/include/golos/wallet/remote_node_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <golos/api/account_api_object.hpp>
#include <golos/api/account_vote.hpp>
#include <golos/api/block_objects.hpp>
#include <golos/api/discussion.hpp>
#include <golos/api/vote_state.hpp>

Expand Down Expand Up @@ -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();
Expand Down
14 changes: 1 addition & 13 deletions libraries/wallet/include/golos/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,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_id_type> transaction_ids;
};

struct key_with_data {
std::string account;
std::string type;
Expand Down Expand Up @@ -262,7 +253,7 @@ namespace golos { namespace wallet {
*
* @returns Public block data on the blockchain
*/
optional<signed_block_with_info> get_block(uint32_t num);
optional<golos::api::annotated_signed_block> get_block(uint32_t num);

/** Returns sequence of operations included/generated in a specified block
*
Expand Down Expand Up @@ -1345,9 +1336,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) )
Expand Down
11 changes: 1 addition & 10 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ namespace golos { namespace wallet {
return my->copy_wallet_file(destination_filename);
}

optional<signed_block_with_info> wallet_api::get_block(uint32_t num) {
optional<golos::api::annotated_signed_block> wallet_api::get_block(uint32_t num) {
return my->_remote_database_api->get_block( num );
}

Expand Down Expand Up @@ -1644,15 +1644,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();
}
Expand Down
34 changes: 4 additions & 30 deletions plugins/database_api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <golos/protocol/get_config.hpp>
#include <golos/protocol/exceptions.hpp>
#include <golos/chain/operation_notification.hpp>
#include <golos/api/block_objects.hpp>

#include <fc/smart_ref_impl.hpp>

Expand All @@ -15,6 +16,8 @@

namespace golos { namespace plugins { namespace database_api {

using golos::api::annotated_signed_block;
using golos::api::block_operations;

template<typename arg>
struct callback_info {
Expand Down Expand Up @@ -49,31 +52,6 @@ using pending_tx_callback_info = callback_info<const signed_transaction&>;
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<block_operation>;

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) {
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
1 change: 1 addition & 0 deletions plugins/operation_history/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ target_link_libraries (
graphene_time
chainbase
fc
golos::api
)

target_include_directories(golos_${CURRENT_TARGET}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <appbase/application.hpp>
#include <golos/plugins/chain/plugin.hpp>

#include <golos/api/block_objects.hpp>
#include <golos/chain/database.hpp>
#include <boost/program_options.hpp>

Expand All @@ -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<applied_operation>)
DEFINE_API_ARGS(get_transaction, msg_pack, annotated_signed_transaction)

Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions plugins/operation_history/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<operation_index>().indices().get<by_location>();
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<protocol::operation>(itr->serialized_op);
(*result._virtual_operations).push_back(op);
}
}

return result;
}

std::vector<applied_operation> get_ops_in_block(
uint32_t block_num,
bool only_virtual
Expand Down Expand Up @@ -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)
Expand Down