Skip to content

Commit

Permalink
Merge pull request #1656 from bitshares/jmj_1645
Browse files Browse the repository at this point in the history
Change format of get_htlc output
  • Loading branch information
jmjatlanta authored Mar 22, 2019
2 parents 1a1ba7e + 07d10cb commit 11432f3
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 51 deletions.
4 changes: 2 additions & 2 deletions libraries/chain/db_notify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ void get_relevant_accounts( const object* obj, flat_set<account_id_type>& accoun
} case htlc_object_type:{
const auto& htlc_obj = dynamic_cast<const htlc_object*>(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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/db_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ void database::clear_expired_htlcs()
{
const auto& htlc_idx = get_index_type<htlc_index>().indices().get<by_expiration>();
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 );

Expand Down
24 changes: 13 additions & 11 deletions libraries/chain/htlc_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ namespace graphene {
dbase.adjust_balance( o.from, -o.amount );

const htlc_object& esc = db().create<htlc_object>([&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;

Expand Down Expand Up @@ -100,19 +101,20 @@ namespace graphene {
{
htlc_obj = &db().get<htlc_object>(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();
Expand All @@ -127,7 +129,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();
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/graphene/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
55 changes: 44 additions & 11 deletions libraries/chain/include/graphene/chain/htlc_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 > > >
>

Expand All @@ -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) )
4 changes: 3 additions & 1 deletion libraries/wallet/include/graphene/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
#pragma once

#include <fc/optional.hpp>
#include <graphene/chain/htlc_object.hpp>
#include <graphene/app/api.hpp>
#include <graphene/utilities/key_conversion.hpp>

Expand Down Expand Up @@ -495,7 +497,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;
fc::optional<fc::variant> 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
Expand Down
81 changes: 60 additions & 21 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include <fc/crypto/base58.hpp>

#include <graphene/app/api.hpp>
#include <graphene/app/util.hpp>
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/protocol/fee_schedule.hpp>
#include <graphene/chain/htlc_object.hpp>
Expand Down Expand Up @@ -267,21 +268,21 @@ struct op_prototype_visitor
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();
}
};

Expand Down Expand Up @@ -678,13 +679,16 @@ class wallet_api_impl
return *opt;
}

htlc_object get_htlc(string htlc_id) const
fc::optional<htlc_object> 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<htlc_object>(GRAPHENE_MAX_NESTED_OBJECTS);
return htlc;
if ( !obj.is_null() )
{
return fc::optional<htlc_object>(obj.template as<htlc_object>(GRAPHENE_MAX_NESTED_OBJECTS));
}
return fc::optional<htlc_object>();
}

asset_id_type get_asset_id(string asset_symbol_or_id) const
Expand Down Expand Up @@ -3186,21 +3190,56 @@ 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
fc::optional<fc::variant> wallet_api::get_htlc(std::string htlc_id) const
{
static detail::htlc_hash_to_string_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;
return ret_val;
fc::optional<htlc_object> optional_obj = my->get_htlc(htlc_id);
if ( optional_obj.valid() )
{
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"] = graphene::app::uint128_amount_to_string( obj.transfer.amount.value, asset.precision );
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;
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;
fc::mutable_variant_object result;
result["transfer"] = transfer;
result["conditions"] = conditions;
return fc::optional<fc::variant>(result);
}
return fc::optional<fc::variant>();
}

signed_transaction wallet_api::htlc_redeem( std::string htlc_id, std::string issuer, const std::string& preimage,
Expand Down
2 changes: 1 addition & 1 deletion tests/common/database_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<asset_index>().indices() )
Expand Down

0 comments on commit 11432f3

Please sign in to comment.