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

HTLC Changes #1998

Merged
merged 31 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
895509a
Add memo to htlc
jmjatlanta Sep 20, 2019
04cccbc
Include HASH160 as available option
jmjatlanta Sep 20, 2019
dfbde9c
Add hardfork checks
jmjatlanta Sep 20, 2019
343c667
Bump DB_VERSION (BSIP64)
jmjatlanta Sep 20, 2019
2a5b29b
include preimage in operation history
jmjatlanta Apr 7, 2020
4264348
ref wallet - pretty-print htlc redeemed op
jmjatlanta Apr 7, 2020
a6bed6b
Update jmj_bsip_64 with hardfork changes
jmjatlanta Apr 11, 2020
a59540f
fixes for wallet operation_printer
jmjatlanta Apr 11, 2020
9975948
bsip64 memo fixes and addl testing
jmjatlanta Apr 15, 2020
7eee72c
fix constexpr warnings
jmjatlanta Apr 15, 2020
f161b38
verify hash appears in history
jmjatlanta Apr 15, 2020
f1a2ab0
add per kb fee for htlc memos
jmjatlanta Apr 15, 2020
0a55c64
bump FC for hash160 changes
jmjatlanta Apr 15, 2020
20e43f0
Adjust fee schedule
jmjatlanta Apr 18, 2020
5f68e64
do htlc hf checks in proposals
jmjatlanta Apr 18, 2020
bbab2f9
add preimage serialization
jmjatlanta Apr 18, 2020
b6bb193
adjust transfer_restricted logic
jmjatlanta Apr 18, 2020
1039366
Exclude memo on locked wallets
jmjatlanta Apr 18, 2020
3629d80
put print_memo in proper scope
jmjatlanta Apr 18, 2020
133f7c3
truncate long preimages in wallet output
jmjatlanta Apr 20, 2020
3e3df5a
standardize exact hardfork time
jmjatlanta Apr 20, 2020
ec78f4d
Use current (not default) memo fee
jmjatlanta Apr 20, 2020
fc15857
Detect overflow
jmjatlanta Apr 20, 2020
cf36230
remove decimal from test
jmjatlanta Apr 21, 2020
96112d4
BOOST_TEST_FAIL no longer part of Boost
jmjatlanta Apr 21, 2020
79a7abf
fee_helper refactor
jmjatlanta Apr 23, 2020
11f0b8c
Prevent exception if fee does not exist
jmjatlanta Apr 23, 2020
f2d76ad
merge changes from hardfork
jmjatlanta Apr 23, 2020
d02fd01
permit restricted transfers in proposals
jmjatlanta Apr 24, 2020
7cfcc52
merge of hf into feature branch
jmjatlanta Apr 24, 2020
e4472a9
Remove test spaghetti + minor fixes
jmjatlanta Apr 27, 2020
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
4 changes: 4 additions & 0 deletions libraries/chain/hardfork.d/CORE_BSIP64.hf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// bitshares BSIP 64 HTLC modifications
#ifndef HARDFORK_CORE_BSIP64_TIME
#define HARDFORK_CORE_BSIP64_TIME (fc::time_point_sec( 1600000000 ) ) // Sep 2020
#endif
16 changes: 14 additions & 2 deletions libraries/chain/htlc_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ namespace graphene {
FC_ASSERT( o.preimage_size <= htlc_options->max_preimage_size, "HTLC preimage length exceeds allowed length" );
// make sure the sender has the funds for the HTLC
FC_ASSERT( d.get_balance( o.from, o.amount.asset_id) >= (o.amount), "Insufficient funds") ;
// memo field added at harfork BSIP64
// NOTE: this check can be removed after hardfork time
FC_ASSERT( d.head_block_time() > HARDFORK_CORE_BSIP64_TIME || !o.extensions.value.memo.valid(),
"Memo unavailable until after HARDFORK BSIP64");
jmjatlanta marked this conversation as resolved.
Show resolved Hide resolved
// HASH160 added at hardfork BSIP64
FC_ASSERT( d.head_block_time() > HARDFORK_CORE_BSIP64_TIME || o.preimage_hash.which() !=
htlc_hash(fc::hash160()).which(), "HASH160 unavailable until after HARDFORK BSIP64" );
const auto& asset_to_transfer = o.amount.asset_id( d );
const auto& from_account = o.from( d );
const auto& to_account = o.to( d );
Expand All @@ -74,6 +81,8 @@ namespace graphene {
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;
if ( o.extensions.value.memo.valid() )
esc.memo = o.extensions.value.memo;
});
return esc.id;

Expand All @@ -100,8 +109,11 @@ namespace graphene {
void_result htlc_redeem_evaluator::do_evaluate(const htlc_redeem_operation& o)
{
htlc_obj = &db().get<htlc_object>(o.htlc_id);

FC_ASSERT(o.preimage.size() == htlc_obj->conditions.hash_lock.preimage_size, "Preimage size mismatch.");
// TODO: The hardfork portion of this check can be removed if no HTLC redemptions are
// attempted on an HTLC with a 0 preimage size before the hardfork date.
if ( htlc_obj->conditions.hash_lock.preimage_size > 0U ||
db().head_block_time() <= HARDFORK_CORE_BSIP64_TIME )
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->conditions.hash_lock.preimage_hash.visit( vtor ),
"Provided preimage does not generate correct hash.");
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/graphene/chain/htlc_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace graphene { namespace chain {
} time_lock;
} conditions;

fc::optional<memo_data> memo;

/****
* Index helper for timelock
*/
Expand Down
18 changes: 15 additions & 3 deletions libraries/protocol/include/graphene/protocol/htlc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
#include <fc/time.hpp>
#include <graphene/protocol/base.hpp>
#include <graphene/protocol/asset.hpp>
#include <graphene/protocol/memo.hpp>
#include <algorithm> // std::max

namespace graphene { namespace protocol {
typedef fc::ripemd160 htlc_algo_ripemd160;
typedef fc::sha1 htlc_algo_sha1;
typedef fc::sha256 htlc_algo_sha256;
typedef fc::hash160 htlc_algo_hash160;

typedef fc::static_variant<
htlc_algo_ripemd160,
htlc_algo_sha1,
htlc_algo_sha256
htlc_algo_sha256,
htlc_algo_hash160
> htlc_hash;

struct htlc_create_operation : public base_operation
Expand All @@ -45,6 +48,7 @@ namespace graphene { namespace protocol {
uint64_t fee = 1 * GRAPHENE_BLOCKCHAIN_PRECISION;
uint64_t fee_per_day = 1 * GRAPHENE_BLOCKCHAIN_PRECISION;
};

// paid to network
asset fee;
// where the held monies are to come from
Expand All @@ -59,8 +63,13 @@ namespace graphene { namespace protocol {
uint16_t preimage_size;
// The time the funds will be returned to the source if not claimed
uint32_t claim_period_seconds;
// for future expansion
extensions_type extensions;

// additional extensions
struct additional_options_type
{
fc::optional<memo_data> memo;
};
abitmore marked this conversation as resolved.
Show resolved Hide resolved
extension<additional_options_type> extensions;

/***
* @brief Does simple validation of this object
Expand Down Expand Up @@ -137,6 +146,7 @@ namespace graphene { namespace protocol {
uint16_t htlc_preimage_size;

asset fee;
std::vector<char> preimage;
jmjatlanta marked this conversation as resolved.
Show resolved Hide resolved
};

struct htlc_extend_operation : public base_operation
Expand Down Expand Up @@ -206,6 +216,7 @@ namespace graphene { namespace protocol {
FC_REFLECT_TYPENAME( graphene::protocol::htlc_hash )

FC_REFLECT( graphene::protocol::htlc_create_operation::fee_parameters_type, (fee) (fee_per_day) )
FC_REFLECT( graphene::protocol::htlc_create_operation::additional_options_type, (memo))
FC_REFLECT( graphene::protocol::htlc_redeem_operation::fee_parameters_type, (fee) (fee_per_kb) )
FC_REFLECT( graphene::protocol::htlc_redeemed_operation::fee_parameters_type, ) // VIRTUAL
FC_REFLECT( graphene::protocol::htlc_extend_operation::fee_parameters_type, (fee) (fee_per_day))
Expand All @@ -221,6 +232,7 @@ FC_REFLECT( graphene::protocol::htlc_refund_operation,
(fee)(htlc_id)(to)(original_htlc_recipient)(htlc_amount)(htlc_preimage_hash)(htlc_preimage_size))

GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::htlc_create_operation::fee_parameters_type )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::htlc_create_operation::additional_options_type )
abitmore marked this conversation as resolved.
Show resolved Hide resolved
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::htlc_redeem_operation::fee_parameters_type )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::htlc_extend_operation::fee_parameters_type )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::htlc_create_operation )
Expand Down
1 change: 1 addition & 0 deletions libraries/protocol/include/graphene/protocol/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <fc/crypto/sha1.hpp>
#include <fc/crypto/sha224.hpp>
#include <fc/crypto/sha256.hpp>
#include <fc/crypto/hash160.hpp>
#include <fc/crypto/elliptic.hpp>
#include <fc/reflect/reflect.hpp>
#include <fc/reflect/variant.hpp>
Expand Down
17 changes: 17 additions & 0 deletions libraries/wallet/operation_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class htlc_hash_to_string_visitor
{
return "SHA256 " + hash.str();
}
result_type operator()( const fc::hash160& hash )const
{
return "HASH160 " + hash.str();
}
};

std::string operation_printer::fee(const graphene::protocol::asset& a)const {
Expand Down Expand Up @@ -146,6 +150,19 @@ std::string operation_printer::operator()(const htlc_redeem_operation& op) const
return fee(op.fee);
}

std::string operation_printer::operator()(const htlc_redeemed_operation& op) const
{
out << "Redeem HTLC with database id "
<< std::to_string(op.htlc_id.space_id)
<< "." << std::to_string(op.htlc_id.type_id)
<< "." << std::to_string((uint64_t)op.htlc_id.instance)
<< " with preimage \"";
for (unsigned char c : op.preimage)
out << c;
jmjatlanta marked this conversation as resolved.
Show resolved Hide resolved
out << "\"";
return fee(op.fee);
}

std::string operation_printer::operator()(const htlc_create_operation& op) const
{
static htlc_hash_to_string_visitor vtor;
Expand Down
1 change: 1 addition & 0 deletions libraries/wallet/operation_printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ struct operation_printer
std::string operator()(const graphene::protocol::asset_create_operation& op)const;
std::string operator()(const graphene::protocol::htlc_create_operation& op)const;
std::string operator()(const graphene::protocol::htlc_redeem_operation& op)const;
std::string operator()(const graphene::protocol::htlc_redeemed_operation& op)const;
};

}}} // namespace graphene::wallet::detail
2 changes: 2 additions & 0 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ fc::optional<fc::variant> wallet_api::get_htlc(std::string htlc_id) const
{ return convert("SHA1", obj.str()); }
result_type operator()(const fc::sha256& obj)const
{ return convert("SHA256", obj.str()); }
result_type operator()(const fc::hash160& obj)const
{ return convert("HASH160", obj.str()); }
jmjatlanta marked this conversation as resolved.
Show resolved Hide resolved
private:
result_type convert(const std::string& type, const std::string& hash)const
{
Expand Down
Loading