From a7c88be7c72ee8c3619b07c617cd9f13cd3f364c Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Tue, 15 Mar 2016 00:38:55 -0400 Subject: [PATCH] debug_api: Add API call to stream JSON objects to file --- libraries/plugins/debug_witness/debug_api.cpp | 30 ++++++++++ .../plugins/debug_witness/debug_witness.cpp | 57 +++++++++++++++++-- .../graphene/debug_witness/debug_api.hpp | 13 +++++ .../graphene/debug_witness/debug_witness.hpp | 8 +++ .../wallet/include/graphene/wallet/wallet.hpp | 2 + libraries/wallet/wallet.cpp | 15 +++++ 6 files changed, 120 insertions(+), 5 deletions(-) diff --git a/libraries/plugins/debug_witness/debug_api.cpp b/libraries/plugins/debug_witness/debug_api.cpp index 885ecef7..5c2d9a37 100644 --- a/libraries/plugins/debug_witness/debug_api.cpp +++ b/libraries/plugins/debug_witness/debug_api.cpp @@ -12,6 +12,7 @@ #include #include +#include namespace graphene { namespace debug_witness { @@ -26,6 +27,9 @@ class debug_api_impl void debug_generate_blocks( const std::string& debug_key, uint32_t count ); void debug_update_object( const fc::variant_object& update ); //void debug_save_db( std::string db_path ); + void debug_stream_json_objects( const std::string& filename ); + void debug_stream_json_objects_flush(); + std::shared_ptr< graphene::debug_witness_plugin::debug_witness_plugin > get_plugin(); graphene::app::application& app; }; @@ -102,6 +106,21 @@ void debug_api_impl::debug_update_object( const fc::variant_object& update ) db->debug_update( update ); } +std::shared_ptr< graphene::debug_witness_plugin::debug_witness_plugin > debug_api_impl::get_plugin() +{ + return app.get_plugin< graphene::debug_witness_plugin::debug_witness_plugin >( "debug_witness" ); +} + +void debug_api_impl::debug_stream_json_objects( const std::string& filename ) +{ + get_plugin()->set_json_object_stream( filename ); +} + +void debug_api_impl::debug_stream_json_objects_flush() +{ + get_plugin()->flush_json_object_stream(); +} + } // detail debug_api::debug_api( graphene::app::application& app ) @@ -124,4 +143,15 @@ void debug_api::debug_update_object( fc::variant_object update ) my->debug_update_object( update ); } +void debug_api::debug_stream_json_objects( std::string filename ) +{ + my->debug_stream_json_objects( filename ); +} + +void debug_api::debug_stream_json_objects_flush() +{ + my->debug_stream_json_objects_flush(); +} + + } } // graphene::debug_witness diff --git a/libraries/plugins/debug_witness/debug_witness.cpp b/libraries/plugins/debug_witness/debug_witness.cpp index 9d6601c4..7bb5562d 100644 --- a/libraries/plugins/debug_witness/debug_witness.cpp +++ b/libraries/plugins/debug_witness/debug_witness.cpp @@ -98,29 +98,76 @@ void debug_witness_plugin::plugin_startup() // connect needed signals - db.applied_block.connect([this](const graphene::chain::signed_block& b){ on_applied_block(b); }); - db.changed_objects.connect([this](const std::vector& ids){ on_changed_objects(ids); }); - db.removed_objects.connect([this](const std::vector& objs){ on_removed_objects(objs); }); + _applied_block_conn = db.applied_block.connect([this](const graphene::chain::signed_block& b){ on_applied_block(b); }); + _changed_objects_conn = db.changed_objects.connect([this](const std::vector& ids){ on_changed_objects(ids); }); + _removed_objects_conn = db.removed_objects.connect([this](const std::vector& objs){ on_removed_objects(objs); }); return; } void debug_witness_plugin::on_changed_objects( const std::vector& ids ) { - + if( _json_object_stream && (ids.size() > 0) ) + { + const chain::database& db = database(); + for( const graphene::db::object_id_type& oid : ids ) + { + const graphene::db::object* obj = db.find_object( oid ); + if( obj == nullptr ) + { + (*_json_object_stream) << "{\"id\":" << fc::json::to_string( oid ) << "}\n"; + } + else + { + (*_json_object_stream) << fc::json::to_string( obj->to_variant() ) << '\n'; + } + } + } } void debug_witness_plugin::on_removed_objects( const std::vector objs ) { - + /* + if( _json_object_stream ) + { + for( const graphene::db::object* obj : objs ) + { + (*_json_object_stream) << "{\"id\":" << fc::json::to_string( obj->id ) << "}\n"; + } + } + */ } void debug_witness_plugin::on_applied_block( const graphene::chain::signed_block& b ) { + if( _json_object_stream ) + { + (*_json_object_stream) << "{\"bn\":" << fc::to_string( b.block_num() ) << "}\n"; + } +} +void debug_witness_plugin::set_json_object_stream( const std::string& filename ) +{ + if( _json_object_stream ) + { + _json_object_stream->close(); + _json_object_stream.reset(); + } + _json_object_stream = std::make_shared< std::ofstream >( filename ); +} + +void debug_witness_plugin::flush_json_object_stream() +{ + if( _json_object_stream ) + _json_object_stream->flush(); } void debug_witness_plugin::plugin_shutdown() { + if( _json_object_stream ) + { + _json_object_stream->close(); + _json_object_stream.reset(); + } return; } diff --git a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_api.hpp b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_api.hpp index 9306e4be..7985f27f 100644 --- a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_api.hpp +++ b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_api.hpp @@ -71,6 +71,17 @@ class debug_api // not implemented //void save_db( std::string db_path ); + /** + * Stream objects to file. (Hint: Create with mkfifo and pipe it to a script) + */ + + void debug_stream_json_objects( std::string filename ); + + /** + * Flush streaming file. + */ + void debug_stream_json_objects_flush(); + std::shared_ptr< detail::debug_api_impl > my; }; @@ -80,4 +91,6 @@ FC_API(graphene::debug_witness::debug_api, (debug_push_blocks) (debug_generate_blocks) (debug_update_object) + (debug_stream_json_objects) + (debug_stream_json_objects_flush) ) diff --git a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp index 1dde26cc..0e5c173f 100644 --- a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp +++ b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp @@ -45,6 +45,9 @@ public: virtual void plugin_startup() override; virtual void plugin_shutdown() override; + void set_json_object_stream( const std::string& filename ); + void flush_json_object_stream(); + private: void on_changed_objects( const std::vector& ids ); @@ -54,6 +57,11 @@ private: boost::program_options::variables_map _options; std::map _private_keys; + + std::shared_ptr< std::ofstream > _json_object_stream; + boost::signals2::scoped_connection _applied_block_conn; + boost::signals2::scoped_connection _changed_objects_conn; + boost::signals2::scoped_connection _removed_objects_conn; }; } } //graphene::debug_witness_plugin diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 847f2d6e..9f5d26eb 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -1406,6 +1406,7 @@ class wallet_api void dbg_make_mia(string creator, string symbol); void dbg_push_blocks( std::string src_filename, uint32_t count ); void dbg_generate_blocks( std::string debug_wif_key, uint32_t count ); + void dbg_stream_json_objects( const std::string& filename ); void dbg_update_object( fc::variant_object update ); void flood_network(string prefix, uint32_t number_of_transactions); @@ -1580,6 +1581,7 @@ FC_API( graphene::wallet::wallet_api, (dbg_make_mia) (dbg_push_blocks) (dbg_generate_blocks) + (dbg_stream_json_objects) (dbg_update_object) (flood_network) (network_add_nodes) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 3dbeda82..3904967e 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -2343,18 +2343,28 @@ public: { use_debug_api(); (*_remote_debug)->debug_push_blocks( src_filename, count ); + (*_remote_debug)->debug_stream_json_objects_flush(); } void dbg_generate_blocks( const std::string& debug_wif_key, uint32_t count ) { use_debug_api(); (*_remote_debug)->debug_generate_blocks( debug_wif_key, count ); + (*_remote_debug)->debug_stream_json_objects_flush(); + } + + void dbg_stream_json_objects( const std::string& filename ) + { + use_debug_api(); + (*_remote_debug)->debug_stream_json_objects( filename ); + (*_remote_debug)->debug_stream_json_objects_flush(); } void dbg_update_object( const fc::variant_object& update ) { use_debug_api(); (*_remote_debug)->debug_update_object( update ); + (*_remote_debug)->debug_stream_json_objects_flush(); } void use_network_node_api() @@ -3264,6 +3274,11 @@ void wallet_api::dbg_generate_blocks( std::string debug_wif_key, uint32_t count my->dbg_generate_blocks( debug_wif_key, count ); } +void wallet_api::dbg_stream_json_objects( const std::string& filename ) +{ + my->dbg_stream_json_objects( filename ); +} + void wallet_api::dbg_update_object( fc::variant_object update ) { my->dbg_update_object( update );