From 11f9f1be02a7ce67476692ce58a7167c97f5c4ef Mon Sep 17 00:00:00 2001 From: John Jones Date: Fri, 15 Mar 2019 09:50:39 -0500 Subject: [PATCH 1/9] Change format of get_htlc output --- libraries/wallet/wallet.cpp | 61 +++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 938a3e0d99..de91c0a081 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -264,24 +264,54 @@ struct op_prototype_visitor } }; +class htlc_hash_to_mutable_variant_visitor +{ +public: + typedef fc::mutable_variant_object result_type; + + result_type operator()( const fc::ripemd160& hash )const + { + fc::mutable_variant_object ret_val; + ret_val["preimage_hash"] = hash.str(); + ret_val["hash_algo"] = "RIPEMD160"; + return ret_val; + } + + result_type operator()( const fc::sha1& hash )const + { + fc::mutable_variant_object ret_val; + ret_val["preimage_hash"] = hash.str(); + ret_val["hash_algo"] = "SHA1"; + return ret_val; + } + + result_type operator()( const fc::sha256& hash )const + { + fc::mutable_variant_object ret_val; + ret_val["preimage_hash"] = hash.str(); + ret_val["hash_algo"] = "SHA256"; + return ret_val; + } +}; + class htlc_hash_to_string_visitor { public: - typedef string result_type; + typedef std::string result_type; result_type operator()( const fc::ripemd160& hash )const { - return "RIPEMD160 " + hash.str(); + return "RIPEMD160 " + hash.str(); } result_type operator()( const fc::sha1& hash )const { - return "SHA1 " + hash.str(); + return "SHA1 " + hash.str(); } result_type operator()( const fc::sha256& hash )const { - return "SHA256 " + hash.str(); + return "SHA256 " + hash.str(); } }; @@ -3188,18 +3218,25 @@ signed_transaction wallet_api::htlc_create( string source, string destination, s variant wallet_api::get_htlc(std::string htlc_id) const { - static detail::htlc_hash_to_string_visitor vtor; + static detail::htlc_hash_to_mutable_variant_visitor vtor; graphene::chain::htlc_object obj = my->get_htlc(htlc_id); fc::mutable_variant_object ret_val; ret_val["database_id"] = (std::string)obj.id; - ret_val["from"] = (std::string)((graphene::db::object_id_type)obj.from); - ret_val["to"] = (std::string)((graphene::db::object_id_type)obj.to); - ret_val["amount"] = obj.amount.amount.value; - ret_val["asset"] = (std::string)((graphene::db::object_id_type)obj.amount.asset_id); - ret_val["expiration"] = fc::get_approximate_relative_time_string(obj.expiration); - ret_val["preimage_hash"] = obj.preimage_hash.visit( vtor ); - ret_val["preimage_size"] = obj.preimage_size; + fc::mutable_variant_object transfer; + transfer["from"] = (std::string)((graphene::db::object_id_type)obj.from); + transfer["to"] = (std::string)((graphene::db::object_id_type)obj.to); + transfer["amount"] = obj.amount.amount.value; + transfer["asset"] = (std::string)((graphene::db::object_id_type)obj.amount.asset_id); + ret_val["transfer"] = transfer; + fc::mutable_variant_object conditions; + fc::mutable_variant_object hash_lock = obj.preimage_hash.visit( vtor ); + hash_lock["preimage_length"] = obj.preimage_size; + conditions["hash_lock"] = hash_lock; + fc::mutable_variant_object time_lock; + time_lock["expiration"] = obj.expiration.to_iso_string(); + conditions["time_lock"] = time_lock; + ret_val["conditions"] = conditions; return ret_val; } From bd7d0122f3fd6291f4988f4127f6d74476eeab7a Mon Sep 17 00:00:00 2001 From: John Jones Date: Fri, 15 Mar 2019 14:02:35 -0500 Subject: [PATCH 2/9] allow for no results --- .../wallet/include/graphene/wallet/wallet.hpp | 2 +- libraries/wallet/wallet.cpp | 51 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index d99d8bf0b5..1e7d90c234 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -495,7 +495,7 @@ class wallet_api * @param htlc_id the id of the HTLC object. * @returns the information about the HTLC object */ - variant get_htlc(string htlc_id) const; + optional get_htlc(string htlc_id) const; /** Lookup the id of a named account. * @param account_name_or_id the name of the account to look up diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index de91c0a081..75577983df 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -708,13 +708,14 @@ class wallet_api_impl return *opt; } - htlc_object get_htlc(string htlc_id) const + optional get_htlc(string htlc_id) const { htlc_id_type id; fc::from_variant(htlc_id, id); auto obj = _remote_db->get_objects( { id }).front(); - htlc_object htlc = obj.template as(GRAPHENE_MAX_NESTED_OBJECTS); - return htlc; + if (!obj.is_null()) + return optional(obj.template as(GRAPHENE_MAX_NESTED_OBJECTS)); + return optional(); } asset_id_type get_asset_id(string asset_symbol_or_id) const @@ -3216,28 +3217,34 @@ signed_transaction wallet_api::htlc_create( string source, string destination, s claim_period_seconds, broadcast); } -variant wallet_api::get_htlc(std::string htlc_id) const +optional wallet_api::get_htlc(std::string htlc_id) const { static detail::htlc_hash_to_mutable_variant_visitor vtor; - graphene::chain::htlc_object obj = my->get_htlc(htlc_id); - fc::mutable_variant_object ret_val; - ret_val["database_id"] = (std::string)obj.id; - fc::mutable_variant_object transfer; - transfer["from"] = (std::string)((graphene::db::object_id_type)obj.from); - transfer["to"] = (std::string)((graphene::db::object_id_type)obj.to); - transfer["amount"] = obj.amount.amount.value; - transfer["asset"] = (std::string)((graphene::db::object_id_type)obj.amount.asset_id); - ret_val["transfer"] = transfer; - fc::mutable_variant_object conditions; - fc::mutable_variant_object hash_lock = obj.preimage_hash.visit( vtor ); - hash_lock["preimage_length"] = obj.preimage_size; - conditions["hash_lock"] = hash_lock; - fc::mutable_variant_object time_lock; - time_lock["expiration"] = obj.expiration.to_iso_string(); - conditions["time_lock"] = time_lock; - ret_val["conditions"] = conditions; - return ret_val; + optional opt_obj = my->get_htlc(htlc_id); + optional return_val; + if (opt_obj) + { + return_val = fc::mutable_variant_object(); + const graphene::chain::htlc_object& obj = *opt_obj; + fc::mutable_variant_object& ret_val = *return_val; + ret_val["database_id"] = (std::string)obj.id; + fc::mutable_variant_object transfer; + transfer["from"] = (std::string)((graphene::db::object_id_type)obj.from); + transfer["to"] = (std::string)((graphene::db::object_id_type)obj.to); + transfer["amount"] = obj.amount.amount.value; + transfer["asset"] = (std::string)((graphene::db::object_id_type)obj.amount.asset_id); + ret_val["transfer"] = transfer; + fc::mutable_variant_object conditions; + fc::mutable_variant_object hash_lock = obj.preimage_hash.visit( vtor ); + hash_lock["preimage_length"] = obj.preimage_size; + conditions["hash_lock"] = hash_lock; + fc::mutable_variant_object time_lock; + time_lock["expiration"] = obj.expiration.to_iso_string(); + conditions["time_lock"] = time_lock; + ret_val["conditions"] = conditions; + } + return return_val; } signed_transaction wallet_api::htlc_redeem( std::string htlc_id, std::string issuer, const std::string& preimage, From 4ca72d8257b14e4c1307c9f39b7ea0d2c515108a Mon Sep 17 00:00:00 2001 From: John Jones Date: Fri, 15 Mar 2019 14:03:10 -0500 Subject: [PATCH 3/9] fix long to int compiler warning --- tests/cli/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli/main.cpp b/tests/cli/main.cpp index d3e26943cf..7075a8a84f 100644 --- a/tests/cli/main.cpp +++ b/tests/cli/main.cpp @@ -680,7 +680,7 @@ BOOST_AUTO_TEST_CASE( cli_create_htlc ) std::string preimage_string = "My Secret"; fc::sha256 preimage_md = fc::sha256::hash(preimage_string); std::stringstream ss; - for(int i = 0; i < preimage_md.data_size(); i++) + for(size_t i = 0; i < preimage_md.data_size(); i++) { char d = preimage_md.data()[i]; unsigned char uc = static_cast(d); From cfba7ade6474f981b1d1dfdddba30ab966c6698e Mon Sep 17 00:00:00 2001 From: John Jones Date: Sat, 16 Mar 2019 15:50:44 -0500 Subject: [PATCH 4/9] adjust htlc_object struct --- libraries/chain/db_notify.cpp | 4 +- libraries/chain/db_update.cpp | 6 +- libraries/chain/htlc_evaluator.cpp | 24 ++++---- .../include/graphene/chain/htlc_object.hpp | 55 +++++++++++++---- .../wallet/include/graphene/wallet/wallet.hpp | 4 +- libraries/wallet/wallet.cpp | 59 +------------------ tests/common/database_fixture.cpp | 2 +- 7 files changed, 68 insertions(+), 86 deletions(-) diff --git a/libraries/chain/db_notify.cpp b/libraries/chain/db_notify.cpp index fafaa3bf00..1347026428 100644 --- a/libraries/chain/db_notify.cpp +++ b/libraries/chain/db_notify.cpp @@ -369,8 +369,8 @@ void get_relevant_accounts( const object* obj, flat_set& accoun } case htlc_object_type:{ const auto& htlc_obj = dynamic_cast(obj); FC_ASSERT( htlc_obj != nullptr ); - accounts.insert( htlc_obj->from ); - accounts.insert( htlc_obj->to ); + accounts.insert( htlc_obj->transfer.from ); + accounts.insert( htlc_obj->transfer.to ); break; } } diff --git a/libraries/chain/db_update.cpp b/libraries/chain/db_update.cpp index 29856165d7..48dea9fb49 100644 --- a/libraries/chain/db_update.cpp +++ b/libraries/chain/db_update.cpp @@ -581,12 +581,12 @@ void database::clear_expired_htlcs() { const auto& htlc_idx = get_index_type().indices().get(); while ( htlc_idx.begin() != htlc_idx.end() - && htlc_idx.begin()->expiration <= head_block_time() ) + && htlc_idx.begin()->conditions.time_lock.expiration <= head_block_time() ) { const htlc_object& obj = *htlc_idx.begin(); - adjust_balance( obj.from, obj.amount ); + adjust_balance( obj.transfer.from, asset(obj.transfer.amount, obj.transfer.asset_id) ); // virtual op - htlc_refund_operation vop( obj.id, obj.from ); + htlc_refund_operation vop( obj.id, obj.transfer.from ); vop.htlc_id = htlc_idx.begin()->id; push_applied_operation( vop ); diff --git a/libraries/chain/htlc_evaluator.cpp b/libraries/chain/htlc_evaluator.cpp index 7ecc8ac98e..fc02dc234c 100644 --- a/libraries/chain/htlc_evaluator.cpp +++ b/libraries/chain/htlc_evaluator.cpp @@ -56,12 +56,13 @@ namespace graphene { dbase.adjust_balance( o.from, -o.amount ); const htlc_object& esc = db().create([&dbase,&o]( htlc_object& esc ) { - esc.from = o.from; - esc.to = o.to; - esc.amount = o.amount; - esc.preimage_hash = o.preimage_hash; - esc.preimage_size = o.preimage_size; - esc.expiration = dbase.head_block_time() + o.claim_period_seconds; + esc.transfer.from = o.from; + esc.transfer.to = o.to; + esc.transfer.amount = o.amount.amount; + esc.transfer.asset_id = o.amount.asset_id; + esc.conditions.hash_lock.preimage_hash = o.preimage_hash; + esc.conditions.hash_lock.preimage_size = o.preimage_size; + esc.conditions.time_lock.expiration = dbase.head_block_time() + o.claim_period_seconds; }); return esc.id; @@ -89,19 +90,20 @@ namespace graphene { { htlc_obj = &db().get(o.htlc_id); - FC_ASSERT(o.preimage.size() == htlc_obj->preimage_size, "Preimage size mismatch."); + FC_ASSERT(o.preimage.size() == htlc_obj->conditions.hash_lock.preimage_size, "Preimage size mismatch."); const htlc_redeem_visitor vtor( o.preimage ); - FC_ASSERT( htlc_obj->preimage_hash.visit( vtor ), "Provided preimage does not generate correct hash."); + FC_ASSERT( htlc_obj->conditions.hash_lock.preimage_hash.visit( vtor ), "Provided preimage does not generate correct hash."); return void_result(); } void_result htlc_redeem_evaluator::do_apply(const htlc_redeem_operation& o) { - db().adjust_balance(htlc_obj->to, htlc_obj->amount); + db().adjust_balance(htlc_obj->transfer.to, asset(htlc_obj->transfer.amount, htlc_obj->transfer.asset_id) ); // notify related parties - htlc_redeemed_operation virt_op( htlc_obj->id, htlc_obj->from, htlc_obj->to, htlc_obj->amount ); + htlc_redeemed_operation virt_op( htlc_obj->id, htlc_obj->transfer.from, htlc_obj->transfer.to, + asset(htlc_obj->transfer.amount, htlc_obj->transfer.asset_id ) ); db().push_applied_operation( virt_op ); db().remove(*htlc_obj); return void_result(); @@ -116,7 +118,7 @@ namespace graphene { void_result htlc_extend_evaluator::do_apply(const htlc_extend_operation& o) { db().modify(*htlc_obj, [&o](htlc_object& db_obj) { - db_obj.expiration += o.seconds_to_add; + db_obj.conditions.time_lock.expiration += o.seconds_to_add; }); return void_result(); diff --git a/libraries/chain/include/graphene/chain/htlc_object.hpp b/libraries/chain/include/graphene/chain/htlc_object.hpp index e9c4515042..5d41eda392 100644 --- a/libraries/chain/include/graphene/chain/htlc_object.hpp +++ b/libraries/chain/include/graphene/chain/htlc_object.hpp @@ -45,12 +45,38 @@ namespace graphene { namespace chain { static const uint8_t space_id = protocol_ids; static const uint8_t type_id = htlc_object_type; - account_id_type from; - account_id_type to; - asset amount; - fc::time_point_sec expiration; - htlc_hash preimage_hash; - uint16_t preimage_size; + struct transfer_info { + account_id_type from; + account_id_type to; + share_type amount; + asset_id_type asset_id; + } transfer; + struct condition_info { + struct hash_lock_info { + htlc_hash preimage_hash; + uint16_t preimage_size; + } hash_lock; + struct time_lock_info { + fc::time_point_sec expiration; + } time_lock; + } conditions; + + /**** + * Index helper for timelock + */ + struct timelock_extractor { + typedef fc::time_point_sec result_type; + const result_type& operator()(const htlc_object& o)const { return o.conditions.time_lock.expiration; } + }; + + /***** + * Index helper for from + */ + struct from_extractor { + typedef account_id_type result_type; + const result_type& operator()(const htlc_object& o)const { return o.transfer.from; } + }; + }; struct by_from_id; @@ -61,13 +87,13 @@ namespace graphene { namespace chain { ordered_unique< tag< by_id >, member< object, object_id_type, &object::id > >, ordered_unique< tag< by_expiration >, - composite_key< htlc_object, - member< htlc_object, fc::time_point_sec, &htlc_object::expiration >, + composite_key< htlc_object, + htlc_object::timelock_extractor, member< object, object_id_type, &object::id > > >, ordered_unique< tag< by_from_id >, composite_key< htlc_object, - member< htlc_object, account_id_type, &htlc_object::from >, + htlc_object::from_extractor, member< object, object_id_type, &object::id > > > > @@ -77,6 +103,13 @@ namespace graphene { namespace chain { } } // namespace graphene::chain +FC_REFLECT( graphene::chain::htlc_object::transfer_info, + (from) (to) (amount) (asset_id) ) +FC_REFLECT( graphene::chain::htlc_object::condition_info::hash_lock_info, + (preimage_hash) (preimage_size) ) +FC_REFLECT( graphene::chain::htlc_object::condition_info::time_lock_info, + (expiration) ) +FC_REFLECT( graphene::chain::htlc_object::condition_info, + (hash_lock)(time_lock) ) FC_REFLECT_DERIVED( graphene::chain::htlc_object, (graphene::db::object), - (from)(to)(amount)(expiration) - (preimage_hash)(preimage_size) ); + (transfer) (conditions) ) diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 1e7d90c234..14404c47a8 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -23,6 +23,8 @@ */ #pragma once +#include +#include #include #include @@ -495,7 +497,7 @@ class wallet_api * @param htlc_id the id of the HTLC object. * @returns the information about the HTLC object */ - optional get_htlc(string htlc_id) const; + optional get_htlc(string htlc_id) const; /** Lookup the id of a named account. * @param account_name_or_id the name of the account to look up diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 75577983df..2902bbc871 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -264,36 +264,6 @@ struct op_prototype_visitor } }; -class htlc_hash_to_mutable_variant_visitor -{ -public: - typedef fc::mutable_variant_object result_type; - - result_type operator()( const fc::ripemd160& hash )const - { - fc::mutable_variant_object ret_val; - ret_val["preimage_hash"] = hash.str(); - ret_val["hash_algo"] = "RIPEMD160"; - return ret_val; - } - - result_type operator()( const fc::sha1& hash )const - { - fc::mutable_variant_object ret_val; - ret_val["preimage_hash"] = hash.str(); - ret_val["hash_algo"] = "SHA1"; - return ret_val; - } - - result_type operator()( const fc::sha256& hash )const - { - fc::mutable_variant_object ret_val; - ret_val["preimage_hash"] = hash.str(); - ret_val["hash_algo"] = "SHA256"; - return ret_val; - } -}; - class htlc_hash_to_string_visitor { public: @@ -3217,34 +3187,9 @@ signed_transaction wallet_api::htlc_create( string source, string destination, s claim_period_seconds, broadcast); } -optional wallet_api::get_htlc(std::string htlc_id) const +optional wallet_api::get_htlc(std::string htlc_id) const { - static detail::htlc_hash_to_mutable_variant_visitor vtor; - - optional opt_obj = my->get_htlc(htlc_id); - optional return_val; - if (opt_obj) - { - return_val = fc::mutable_variant_object(); - const graphene::chain::htlc_object& obj = *opt_obj; - fc::mutable_variant_object& ret_val = *return_val; - ret_val["database_id"] = (std::string)obj.id; - fc::mutable_variant_object transfer; - transfer["from"] = (std::string)((graphene::db::object_id_type)obj.from); - transfer["to"] = (std::string)((graphene::db::object_id_type)obj.to); - transfer["amount"] = obj.amount.amount.value; - transfer["asset"] = (std::string)((graphene::db::object_id_type)obj.amount.asset_id); - ret_val["transfer"] = transfer; - fc::mutable_variant_object conditions; - fc::mutable_variant_object hash_lock = obj.preimage_hash.visit( vtor ); - hash_lock["preimage_length"] = obj.preimage_size; - conditions["hash_lock"] = hash_lock; - fc::mutable_variant_object time_lock; - time_lock["expiration"] = obj.expiration.to_iso_string(); - conditions["time_lock"] = time_lock; - ret_val["conditions"] = conditions; - } - return return_val; + return my->get_htlc(htlc_id); } signed_transaction wallet_api::htlc_redeem( std::string htlc_id, std::string issuer, const std::string& preimage, diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index 47d5755f89..b58ba374ca 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -318,7 +318,7 @@ void database_fixture::verify_asset_supplies( const database& db ) const auto& htlc_idx = db.get_index_type< htlc_index >().indices().get< by_id >(); for( auto itr = htlc_idx.begin(); itr != htlc_idx.end(); ++itr ) { - total_balances[itr->amount.asset_id] += itr->amount.amount; + total_balances[itr->transfer.asset_id] += itr->transfer.amount; } for( const asset_object& asset_obj : db.get_index_type().indices() ) From c63d21d7ea89fcbb506321a89f1f8da5851798e0 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 18 Mar 2019 09:49:55 -0500 Subject: [PATCH 5/9] Pretty-format HTLC output --- .../wallet/include/graphene/wallet/wallet.hpp | 2 +- libraries/wallet/wallet.cpp | 58 +++++++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 14404c47a8..8ec275c60f 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -497,7 +497,7 @@ class wallet_api * @param htlc_id the id of the HTLC object. * @returns the information about the HTLC object */ - optional get_htlc(string htlc_id) const; + fc::optional get_htlc(string htlc_id) const; /** Lookup the id of a named account. * @param account_name_or_id the name of the account to look up diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 2902bbc871..5e9690b261 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -678,14 +678,16 @@ class wallet_api_impl return *opt; } - optional get_htlc(string htlc_id) const + fc::optional get_htlc(string htlc_id) const { htlc_id_type id; fc::from_variant(htlc_id, id); auto obj = _remote_db->get_objects( { id }).front(); if (!obj.is_null()) - return optional(obj.template as(GRAPHENE_MAX_NESTED_OBJECTS)); - return optional(); + { + return fc::optional(obj.template as(GRAPHENE_MAX_NESTED_OBJECTS)); + } + return fc::optional(); } asset_id_type get_asset_id(string asset_symbol_or_id) const @@ -3187,9 +3189,55 @@ signed_transaction wallet_api::htlc_create( string source, string destination, s claim_period_seconds, broadcast); } -optional wallet_api::get_htlc(std::string htlc_id) const +fc::optional wallet_api::get_htlc(std::string htlc_id) const { - return my->get_htlc(htlc_id); + fc::optional optional_obj = my->get_htlc(htlc_id); + if (optional_obj) + { + const htlc_object& obj = *optional_obj; + // convert to formatted variant + fc::mutable_variant_object transfer; + const auto& from = my->get_account( obj.transfer.from ); + transfer["from"] = from.name; + const auto& to = my->get_account( obj.transfer.to ); + transfer["to"] = to.name; + const auto& asset = my->get_asset( obj.transfer.asset_id ); + transfer["asset"] = asset.symbol; + transfer["amount"] = obj.transfer.amount.value; + class htlc_hash_to_variant_visitor + { + public: + typedef fc::mutable_variant_object result_type; + + result_type operator()(const fc::ripemd160& obj)const + { return convert("RIPEMD160", obj.str()); } + result_type operator()(const fc::sha1& obj)const + { return convert("SHA1", obj.str()); } + result_type operator()(const fc::sha256& obj)const + { return convert("SHA256", obj.str()); } + private: + result_type convert(const std::string& type, const std::string& hash)const + { + fc::mutable_variant_object ret_val; + ret_val["hash_algo"] = type; + ret_val["preimage_hash"] = hash; + return ret_val; + } + }; + static htlc_hash_to_variant_visitor hash_visitor; + fc::mutable_variant_object htlc_lock = obj.conditions.hash_lock.preimage_hash.visit(hash_visitor); + htlc_lock["preimage_size"] = obj.conditions.hash_lock.preimage_size; + fc::mutable_variant_object time_lock; + time_lock["expiration"] = obj.conditions.time_lock.expiration; + fc::mutable_variant_object conditions; + conditions["htlc_lock"] = htlc_lock; + conditions["time_lock"] = time_lock; + fc::mutable_variant_object result; + result["transfer"] = transfer; + result["conditions"] = conditions; + return fc::optional(result); + } + return fc::optional(); } signed_transaction wallet_api::htlc_redeem( std::string htlc_id, std::string issuer, const std::string& preimage, From 5f0e40016b0384c1b784e5d324d711aa8994b81e Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 19 Mar 2019 09:16:13 -0500 Subject: [PATCH 6/9] format of amount to include decimal --- libraries/wallet/wallet.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 5e9690b261..f45f30230f 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -63,6 +63,7 @@ #include #include +#include #include #include #include @@ -3203,7 +3204,7 @@ fc::optional wallet_api::get_htlc(std::string htlc_id) const transfer["to"] = to.name; const auto& asset = my->get_asset( obj.transfer.asset_id ); transfer["asset"] = asset.symbol; - transfer["amount"] = obj.transfer.amount.value; + transfer["amount"] = graphene::app::uint128_amount_to_string( obj.transfer.amount.value, asset.precision ); class htlc_hash_to_variant_visitor { public: From db1383858a25d3ccf6d3dcb6fee8f2c56d2b7536 Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 19 Mar 2019 10:02:06 -0500 Subject: [PATCH 7/9] bump DB_VERSION --- libraries/chain/include/graphene/chain/config.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/chain/include/graphene/chain/config.hpp b/libraries/chain/include/graphene/chain/config.hpp index 867bc5adc4..5b0811e968 100644 --- a/libraries/chain/include/graphene/chain/config.hpp +++ b/libraries/chain/include/graphene/chain/config.hpp @@ -121,7 +121,7 @@ #define GRAPHENE_RECENTLY_MISSED_COUNT_INCREMENT 4 #define GRAPHENE_RECENTLY_MISSED_COUNT_DECREMENT 3 -#define GRAPHENE_CURRENT_DB_VERSION "BTS2.190225" +#define GRAPHENE_CURRENT_DB_VERSION "20190319" #define GRAPHENE_IRREVERSIBLE_THRESHOLD (70 * GRAPHENE_1_PERCENT) From c51220beac1f23a500f6440d2ce1424d776d0a2f Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 20 Mar 2019 11:02:42 -0500 Subject: [PATCH 8/9] Added time left to data returned from get_htlc --- libraries/wallet/wallet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index f45f30230f..308b5be7c0 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -3230,6 +3230,7 @@ fc::optional wallet_api::get_htlc(std::string htlc_id) const htlc_lock["preimage_size"] = obj.conditions.hash_lock.preimage_size; fc::mutable_variant_object time_lock; time_lock["expiration"] = obj.conditions.time_lock.expiration; + time_lock["time_left"] = fc::get_approximate_relative_time_string(obj.conditions.time_lock.expiration); fc::mutable_variant_object conditions; conditions["htlc_lock"] = htlc_lock; conditions["time_lock"] = time_lock; From 07d10cb95dbe40ca70a720533d381a8d8ed3e384 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 21 Mar 2019 13:16:07 -0500 Subject: [PATCH 9/9] using the more expresive optional<>.valid() --- libraries/wallet/wallet.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 308b5be7c0..462a5b91c5 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -684,7 +684,7 @@ class wallet_api_impl htlc_id_type id; fc::from_variant(htlc_id, id); auto obj = _remote_db->get_objects( { id }).front(); - if (!obj.is_null()) + if ( !obj.is_null() ) { return fc::optional(obj.template as(GRAPHENE_MAX_NESTED_OBJECTS)); } @@ -3193,7 +3193,7 @@ signed_transaction wallet_api::htlc_create( string source, string destination, s fc::optional wallet_api::get_htlc(std::string htlc_id) const { fc::optional optional_obj = my->get_htlc(htlc_id); - if (optional_obj) + if ( optional_obj.valid() ) { const htlc_object& obj = *optional_obj; // convert to formatted variant