Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize operations in account history #447

Merged
merged 3 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 18 additions & 4 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
boost::signals2::scoped_connection _block_applied_connection;
};

full_operation_object::full_operation_object() {}

full_operation_object::full_operation_object( const operation_object& op_obj )
: trx_id( op_obj.trx_id ),
block( op_obj.block ),
trx_in_block( op_obj.trx_in_block ),
op_in_trx( op_obj.op_in_trx ),
virtual_op( op_obj.virtual_op ),
timestamp( op_obj.timestamp )
{
// fc::raw::unpack( op_obj.serialized_op, op ); // g++ refuses to compile this as ambiguous
op = fc::raw::unpack<operation>( op_obj.serialized_op );
}

void find_accounts( set<string>& accounts, const discussion& d ) {
accounts.insert( d.author );
}
Expand Down Expand Up @@ -1042,8 +1056,7 @@ vector<discussion> database_api::get_replies_by_last_update( string start_parent
return result;
}


map<uint32_t,operation_object> database_api::get_account_history( string account, uint64_t from, uint32_t limit )const {
map< uint32_t, full_operation_object > database_api::get_account_history( string account, uint64_t from, uint32_t limit )const {
FC_ASSERT( limit <= 2000, "Limit of ${l} is greater than maxmimum allowed", ("l",limit) );
FC_ASSERT( from >= limit, "From must be greater than limit" );
// idump((account)(from)(limit));
Expand All @@ -1053,8 +1066,9 @@ map<uint32_t,operation_object> database_api::get_account_history( string account
auto end = idx.upper_bound( boost::make_tuple( account, std::max( int64_t(0), int64_t(itr->sequence)-limit ) ) );
// if( end != idx.end() ) idump((*end));

map<uint32_t,operation_object> result;
while( itr != end ) {
map<uint32_t, full_operation_object> result;
while( itr != end )
{
result[itr->sequence] = itr->op(my->_db);
++itr;
}
Expand Down
3 changes: 2 additions & 1 deletion libraries/app/include/steemit/app/database_api.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <steemit/app/full_operation_object.hpp>
#include <steemit/app/state.hpp>
#include <steemit/chain/protocol/types.hpp>

Expand Down Expand Up @@ -383,7 +384,7 @@ class database_api
* @param from - the absolute sequence number, -1 means most recent, limit is the number of operations before from.
* @param limit - the maximum number of items that can be queried (0 to 1000], must be less than from
*/
map<uint32_t,operation_object> get_account_history( string account, uint64_t from, uint32_t limit )const;
map<uint32_t, full_operation_object> get_account_history( string account, uint64_t from, uint32_t limit )const;

////////////////////////////
// Handlers - not exposed //
Expand Down
32 changes: 32 additions & 0 deletions libraries/app/include/steemit/app/full_operation_object.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <steemit/chain/protocol/operations.hpp>
#include <steemit/chain/protocol/types.hpp>

namespace steemit { namespace app {

struct full_operation_object
{
full_operation_object();
full_operation_object( const steemit::chain::operation_object& op_obj );

steemit::chain::transaction_id_type trx_id;
uint32_t block = 0;
uint32_t trx_in_block = 0;
uint16_t op_in_trx = 0;
uint64_t virtual_op = 0;
fc::time_point_sec timestamp;
steemit::chain::operation op;
};

} }

FC_REFLECT( steemit::app::full_operation_object,
(trx_id)
(block)
(trx_in_block)
(op_in_trx)
(virtual_op)
(timestamp)
(op)
)
30 changes: 16 additions & 14 deletions libraries/app/include/steemit/app/state.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include <steemit/app/full_operation_object.hpp>

#include <steemit/chain/global_property_object.hpp>
#include <steemit/chain/account_object.hpp>
#include <steemit/chain/steem_objects.hpp>
Expand Down Expand Up @@ -77,22 +79,22 @@ namespace steemit { namespace app {
extended_account(){}
extended_account( const account_object& a ):account_object(a){}

asset vesting_balance; /// convert vesting_shares to vesting steem
share_type reputation = 0;
map<uint64_t,operation_object> transfer_history; /// transfer to/from vesting
map<uint64_t,operation_object> market_history; /// limit order / cancel / fill
map<uint64_t,operation_object> post_history;
map<uint64_t,operation_object> vote_history;
map<uint64_t,operation_object> other_history;
set<string> witness_votes;
asset vesting_balance; /// convert vesting_shares to vesting steem
share_type reputation = 0;
map<uint64_t,full_operation_object> transfer_history; /// transfer to/from vesting
map<uint64_t,full_operation_object> market_history; /// limit order / cancel / fill
map<uint64_t,full_operation_object> post_history;
map<uint64_t,full_operation_object> vote_history;
map<uint64_t,full_operation_object> other_history;
set<string> witness_votes;

optional<map<uint32_t,extended_limit_order>> open_orders;
optional<vector<string>> posts; /// permlinks for this user
optional<vector<string>> blog; /// blog posts for this user
optional<vector<string>> feed; /// feed posts for this user
optional<vector<string>> recent_replies; /// blog posts for this user
map<string,vector<string>> blog_category; /// blog posts for this user
optional<vector<string>> recommended; /// posts recommened for this user
optional<vector<string>> posts; /// permlinks for this user
optional<vector<string>> blog; /// blog posts for this user
optional<vector<string>> feed; /// feed posts for this user
optional<vector<string>> recent_replies; /// blog posts for this user
map<string,vector<string>> blog_category; /// blog posts for this user
optional<vector<string>> recommended; /// posts recommened for this user
};


Expand Down
40 changes: 20 additions & 20 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <steemit/chain/steem_objects.hpp>
#include <steemit/chain/transaction_object.hpp>

#include <steemit/chain/operation_notification.hpp>

#include <graphene/db/flat_index.hpp>

#include <fc/smart_ref_impl.hpp>
Expand Down Expand Up @@ -904,33 +906,28 @@ void database::clear_pending()
FC_CAPTURE_AND_RETHROW()
}

const operation_object database::notify_pre_apply_operation( const operation& op )
void database::notify_pre_apply_operation( operation_notification& note )
{
operation_object obj;
obj.trx_id = _current_trx_id;
obj.block = _current_block_num;
obj.trx_in_block = _current_trx_in_block;
obj.op_in_trx = _current_op_in_trx;
obj.op = op;

//pre_apply_operation( obj );
STEEMIT_TRY_NOTIFY( pre_apply_operation, obj )
note.trx_id = _current_trx_id;
note.block = _current_block_num;
note.trx_in_block = _current_trx_in_block;
note.op_in_trx = _current_op_in_trx;

return obj;
STEEMIT_TRY_NOTIFY( pre_apply_operation, note )
}

void database::notify_post_apply_operation( const operation_object& obj )
void database::notify_post_apply_operation( const operation_notification& note )
{
//post_apply_operation( obj );
STEEMIT_TRY_NOTIFY( post_apply_operation, obj )
STEEMIT_TRY_NOTIFY( post_apply_operation, note )
}

inline const void database::push_virtual_operation( const operation& op )
{
#if ! defined( IS_LOW_MEM ) || defined( IS_TEST_NET )
FC_ASSERT( is_virtual_operation( op ) );
auto obj = notify_pre_apply_operation( op );
notify_post_apply_operation( obj );
operation_notification note(op);
notify_pre_apply_operation( note );
notify_post_apply_operation( note );
#endif
}

Expand Down Expand Up @@ -3039,9 +3036,10 @@ void database::_apply_transaction(const signed_transaction& trx)

void database::apply_operation(const operation& op)
{
auto obj = notify_pre_apply_operation( op );
operation_notification note(op);
notify_pre_apply_operation( note );
_my->_evaluator_registry.get_evaluator( op ).apply( op );
notify_post_apply_operation( obj );
notify_post_apply_operation( note );
}

const witness_object& database::validate_block_header( uint32_t skip, const signed_block& next_block )const
Expand Down Expand Up @@ -3760,8 +3758,10 @@ void database::apply_hardfork( uint32_t hardfork )
string op_msg = "Testnet: Hardfork applied";
test_op.data = vector< char >( op_msg.begin(), op_msg.end() );
test_op.required_auths.insert( STEEMIT_INIT_MINER_NAME );
auto obj = notify_pre_apply_operation( test_op );
notify_post_apply_operation( obj );
operation op = test_op; // we need the operation object to live to the end of this scope
operation_notification note( op );
notify_pre_apply_operation( note );
notify_post_apply_operation( note );
}
break;
#endif
Expand Down
20 changes: 8 additions & 12 deletions libraries/chain/include/steemit/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace steemit { namespace chain {

class database_impl;
class generic_json_evaluator_registry;
struct operation_notification;

/**
* @class database
Expand Down Expand Up @@ -156,17 +157,13 @@ namespace steemit { namespace chain {
void clear_pending();

/**
* This method is used to track appied operations during the evaluation of a block, these
* This method is used to track applied operations during the evaluation of a block, these
* operations should include any operation actually included in a transaction as well
* as any implied/virtual operations that resulted, such as filling an order. The
* applied operations is cleared after applying each block and calling the block
* observers which may want to index these operations.
*
* @return the op_id which can be used to set the result after it has finished being applied.
* @todo rename this method notify_pre_apply_operation( op )
* as any implied/virtual operations that resulted, such as filling an order.
* The applied operations are cleared after post_apply_operation.
*/
const operation_object notify_pre_apply_operation( const operation& op );
void notify_post_apply_operation( const operation_object& op );
void notify_pre_apply_operation( operation_notification& note );
void notify_post_apply_operation( const operation_notification& note );
inline const void push_virtual_operation( const operation& op );
void notify_applied_block( const signed_block& block );
void notify_on_pending_transaction( const signed_transaction& tx );
Expand All @@ -175,8 +172,8 @@ namespace steemit { namespace chain {
/**
* This signal is emitted for plugins to process every operation after it has been fully applied.
*/
fc::signal<void(const operation_object&)> pre_apply_operation;
fc::signal<void(const operation_object&)> post_apply_operation;
fc::signal<void(const operation_notification&)> pre_apply_operation;
fc::signal<void(const operation_notification&)> post_apply_operation;

/**
* This signal is emitted after all operations and virtual operation for a
Expand Down Expand Up @@ -457,5 +454,4 @@ namespace steemit { namespace chain {
flat_map< std::string, std::shared_ptr< generic_json_evaluator_registry > > _custom_json_evaluators;
};


} }
4 changes: 2 additions & 2 deletions libraries/chain/include/steemit/chain/history_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace steemit { namespace chain {
uint16_t op_in_trx = 0;
uint64_t virtual_op = 0;
time_point_sec timestamp;
operation op;
std::vector<char> serialized_op;
};

struct by_location;
Expand Down Expand Up @@ -83,5 +83,5 @@ namespace steemit { namespace chain {
typedef generic_index< account_history_object, account_history_multi_index_type > account_history_index;
} }

FC_REFLECT_DERIVED( steemit::chain::operation_object, (graphene::db::object), (trx_id)(block)(trx_in_block)(op_in_trx)(virtual_op)(timestamp)(op) )
FC_REFLECT_DERIVED( steemit::chain::operation_object, (graphene::db::object), (trx_id)(block)(trx_in_block)(op_in_trx)(virtual_op)(timestamp)(serialized_op) )
FC_REFLECT_DERIVED( steemit::chain::account_history_object, (graphene::db::object), (account)(sequence)(op) )
20 changes: 20 additions & 0 deletions libraries/chain/include/steemit/chain/operation_notification.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <steemit/chain/protocol/operations.hpp>
#include <steemit/chain/protocol/types.hpp>

namespace steemit { namespace chain {

struct operation_notification
{
operation_notification( const operation& o ) : op(o) {}

transaction_id_type trx_id;
uint32_t block = 0;
uint32_t trx_in_block = 0;
uint16_t op_in_trx = 0;
uint64_t virtual_op = 0;
const operation& op;
};

} }
Loading