From c1b5eb95e11a661306f9b175217ad497989dbaf5 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Fri, 28 Aug 2015 13:58:49 -0400 Subject: [PATCH] update subscriptions to work with keys --- libraries/app/api.cpp | 63 +++++++++++++++------- libraries/app/include/graphene/app/api.hpp | 15 +++++- libraries/chain/db_witness_schedule.cpp | 2 +- 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index b0fab7fe..dff9626d 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -67,14 +67,6 @@ namespace graphene { namespace app { } } - void database_api::subscribe_to_id( object_id_type id )const - { - idump((id)); - if( _subscribe_callback ) - _subscribe_filter.insert( (const unsigned char*)&id, sizeof(id) ); - else - elog( "unable to subscribe to id because there is no subscribe callback set" ); - } fc::variants database_api::get_objects(const vector& ids)const { if( _subscribe_callback ) { @@ -83,7 +75,7 @@ namespace graphene { namespace app { if( id.type() == operation_history_object_type && id.space() == protocol_ids ) continue; if( id.type() == impl_account_transaction_history_object_type && id.space() == implementation_ids ) continue; - subscribe_to_id( id ); + this->subscribe_to_item( id ); } } else @@ -188,7 +180,7 @@ namespace graphene { namespace app { [this](account_id_type id) -> optional { if(auto o = _db.find(id)) { - subscribe_to_id( id ); + subscribe_to_item( id ); return *o; } return {}; @@ -203,7 +195,7 @@ namespace graphene { namespace app { [this](asset_id_type id) -> optional { if(auto o = _db.find(id)) { - subscribe_to_id( id ); + subscribe_to_item( id ); return *o; } return {}; @@ -228,7 +220,7 @@ namespace graphene { namespace app { { result.insert(make_pair(itr->name, itr->get_id())); if( limit == 1 ) - subscribe_to_id( itr->get_id() ); + subscribe_to_item( itr->get_id() ); } return result; @@ -256,7 +248,7 @@ namespace graphene { namespace app { if( subscribe ) { ilog( "subscribe to ${id}", ("id",account->name) ); - subscribe_to_id( account->id ); + subscribe_to_item( account->id ); } // fc::mutable_variant_object full_account; @@ -881,6 +873,28 @@ namespace graphene { namespace app { obj = _db.find_object( id ); if( obj ) { + auto acnt = dynamic_cast(obj); + if( acnt ) + { + bool added_account = false; + for( const auto& key : acnt->owner.key_auths ) + if( is_subscribed_to_item( key.first ) ) + { + updates.emplace_back( obj->to_variant() ); + added_account = true; + break; + } + for( const auto& key : acnt->active.key_auths ) + if( is_subscribed_to_item( key.first ) ) + { + updates.emplace_back( obj->to_variant() ); + added_account = true; + break; + } + if( added_account ) + continue; + } + vector relevant = get_relevant_accounts( obj ); for( const auto& r : relevant ) { @@ -1080,11 +1094,19 @@ namespace graphene { namespace app { for( auto& key : keys ) { - address a1( pts_address(key, false, 56) ); - address a2( pts_address(key, true, 56) ); - address a3( pts_address(key, false, 0) ); - address a4( pts_address(key, true, 0) ); - address a5( key ); + + address a1( pts_address(key, false, 56) ); + address a2( pts_address(key, true, 56) ); + address a3( pts_address(key, false, 0) ); + address a4( pts_address(key, true, 0) ); + address a5( key ); + + subscribe_to_item( key ); + subscribe_to_item( a1 ); + subscribe_to_item( a2 ); + subscribe_to_item( a3 ); + subscribe_to_item( a4 ); + subscribe_to_item( a5 ); const auto& idx = _db.get_index_type(); const auto& aidx = dynamic_cast&>(idx); @@ -1113,6 +1135,10 @@ namespace graphene { namespace app { } final_result.emplace_back( std::move(result) ); } + + for( auto i : final_result ) + subscribe_to_item(i); + return final_result; } @@ -1169,6 +1195,7 @@ namespace graphene { namespace app { for( const auto& owner : addrs ) { + subscribe_to_item( owner ); auto itr = by_owner_idx.lower_bound( boost::make_tuple( owner, asset_id_type(0) ) ); while( itr != by_owner_idx.end() && itr->owner == owner ) { diff --git a/libraries/app/include/graphene/app/api.hpp b/libraries/app/include/graphene/app/api.hpp index 0d459bf5..82c82cae 100644 --- a/libraries/app/include/graphene/app/api.hpp +++ b/libraries/app/include/graphene/app/api.hpp @@ -297,7 +297,6 @@ namespace graphene { namespace app { */ void cancel_all_subscriptions() { set_subscribe_callback( std::function(), true); _market_subscriptions.clear(); } - ///@} /// @brief Get a hexdump of the serialized binary form of a transaction std::string get_transaction_hex(const signed_transaction& trx)const; @@ -357,7 +356,19 @@ namespace graphene { namespace app { void set_subscribe_callback( std::function cb, bool clear_filter ); private: - void subscribe_to_id( object_id_type id )const; + template + void subscribe_to_item( const T& i )const + { + if( !_subscribe_callback ) return; + _subscribe_filter.insert( (const char*)&i, sizeof(i) ); + } + + template + bool is_subscribed_to_item( const T& i )const + { + if( !_subscribe_callback ) return false; + return _subscribe_filter.contains( i ); + } /** called every time a block is applied to report the objects that were changed */ void on_objects_changed(const vector& ids); diff --git a/libraries/chain/db_witness_schedule.cpp b/libraries/chain/db_witness_schedule.cpp index ebfbcae4..adc7b315 100644 --- a/libraries/chain/db_witness_schedule.cpp +++ b/libraries/chain/db_witness_schedule.cpp @@ -46,7 +46,7 @@ witness_id_type database::get_scheduled_witness( uint32_t slot_num )const const flat_set< witness_id_type >& active_witnesses = get_global_properties().active_witnesses; uint32_t n = active_witnesses.size(); uint64_t min_witness_separation; - if( BOOST_UNLIKELY( n < 5 ) ) + if( GRAPHENE_DEFAULT_MIN_WITNESS_COUNT < 5 && BOOST_UNLIKELY( n < 5 ) ) { // special-case 0 and 1. // for 2 give a value which results in witnesses alternating slots