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

Merge 3.0.1 to Testnet #1709

Merged
merged 8 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 3 additions & 1 deletion libraries/chain/db_notify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ struct get_impacted_account_visitor
}
void operator()( const htlc_redeem_operation& op )
{
_impacted.insert( op.fee_payer() );
_impacted.insert( op.fee_payer() );
}
void operator()( const htlc_redeemed_operation& op )
{
_impacted.insert( op.from );
if ( op.to != op.redeemer )
_impacted.insert( op.to );
}
void operator()( const htlc_extend_operation& op )
{
Expand Down
18 changes: 13 additions & 5 deletions libraries/chain/htlc_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace graphene {
// make sure the expiration is reasonable
FC_ASSERT( o.claim_period_seconds <= htlc_options->max_timeout_secs, "HTLC Timeout exceeds allowed length" );
// make sure the preimage length is reasonable
FC_ASSERT( o.preimage_size <= htlc_options->max_preimage_size, "HTLC preimage length exceeds allowed length" );
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") ;
const auto& asset_to_transfer = o.amount.asset_id( d );
Expand Down Expand Up @@ -102,9 +102,9 @@ namespace graphene {
htlc_obj = &db().get<htlc_object>(o.htlc_id);

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.");
FC_ASSERT( htlc_obj->conditions.hash_lock.preimage_hash.visit( vtor ),
"Provided preimage does not generate correct hash.");

return void_result();
}
Expand All @@ -113,8 +113,8 @@ namespace graphene {
{
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->transfer.from, htlc_obj->transfer.to,
asset(htlc_obj->transfer.amount, htlc_obj->transfer.asset_id ) );
htlc_redeemed_operation virt_op( htlc_obj->id, htlc_obj->transfer.from, htlc_obj->transfer.to,
o.redeemer, 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 @@ -123,6 +123,14 @@ namespace graphene {
void_result htlc_extend_evaluator::do_evaluate(const htlc_extend_operation& o)
{
htlc_obj = &db().get<htlc_object>(o.htlc_id);
FC_ASSERT(o.update_issuer == htlc_obj->transfer.from, "HTLC may only be extended by its creator.");
optional<htlc_options> htlc_options = get_committee_htlc_options(db());
FC_ASSERT( htlc_obj->conditions.time_lock.expiration.sec_since_epoch()
+ static_cast<uint64_t>(o.seconds_to_add) < fc::time_point_sec::maximum().sec_since_epoch(),
"Extension would cause an invalid date");
FC_ASSERT( htlc_obj->conditions.time_lock.expiration + o.seconds_to_add
<= db().head_block_time() + htlc_options->max_timeout_secs,
"Extension pushes contract too far into the future" );
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 "20190323"
#define GRAPHENE_CURRENT_DB_VERSION "20190408"

#define GRAPHENE_IRREVERSIBLE_THRESHOLD (70 * GRAPHENE_1_PERCENT)

Expand Down
8 changes: 4 additions & 4 deletions libraries/chain/include/graphene/chain/protocol/htlc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ namespace graphene {
struct fee_parameters_type {};

htlc_redeemed_operation() {}
htlc_redeemed_operation( htlc_id_type htlc_id, account_id_type from, account_id_type to, asset amount) :
htlc_id(htlc_id), from(from), to(to), amount(amount) {}
htlc_redeemed_operation( htlc_id_type htlc_id, account_id_type from, account_id_type to, account_id_type redeemer, asset amount ) :
htlc_id(htlc_id), from(from), to(to), redeemer(redeemer), amount(amount) {}

account_id_type fee_payer()const { return to; }
void validate()const { FC_ASSERT( !"virtual operation" ); }

share_type calculate_fee(const fee_parameters_type& k)const { return 0; }

htlc_id_type htlc_id;
account_id_type from, to;
account_id_type from, to, redeemer;
asset amount;
asset fee;
};
Expand Down Expand Up @@ -206,6 +206,6 @@ FC_REFLECT( graphene::chain::htlc_refund_operation::fee_parameters_type, ) // VI
FC_REFLECT( graphene::chain::htlc_create_operation,
(fee)(from)(to)(amount)(preimage_hash)(preimage_size)(claim_period_seconds)(extensions))
FC_REFLECT( graphene::chain::htlc_redeem_operation, (fee)(htlc_id)(redeemer)(preimage)(extensions))
FC_REFLECT( graphene::chain::htlc_redeemed_operation, (fee)(htlc_id)(from)(to)(amount) )
FC_REFLECT( graphene::chain::htlc_redeemed_operation, (fee)(htlc_id)(from)(to)(redeemer)(amount) )
FC_REFLECT( graphene::chain::htlc_extend_operation, (fee)(htlc_id)(update_issuer)(seconds_to_add)(extensions))
FC_REFLECT( graphene::chain::htlc_refund_operation, (fee)(htlc_id)(to))
74 changes: 67 additions & 7 deletions tests/tests/htlc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,74 @@ try {
auto obj = db_api.get_objects( {alice_htlc_id }).front();
graphene::chain::htlc_object htlc = obj.template as<graphene::chain::htlc_object>(GRAPHENE_MAX_NESTED_OBJECTS);

// someone else attempts to extend it (bob says he's alice, but he's not)
{
graphene::chain::htlc_extend_operation bad_extend;
bad_extend.htlc_id = alice_htlc_id;
bad_extend.seconds_to_add = 10;
bad_extend.fee = db.get_global_properties().parameters.current_fees->calculate_fee(bad_extend);
bad_extend.update_issuer = alice_id;
trx.operations.push_back(bad_extend);
sign(trx, bob_private_key);
GRAPHENE_CHECK_THROW( PUSH_TX(db, trx, database::skip_nothing ), fc::exception );
trx.clear();
}
// someone else attempts to extend it (bob wants to extend Alice's contract)
{
graphene::chain::htlc_extend_operation bad_extend;
bad_extend.htlc_id = alice_htlc_id;
bad_extend.seconds_to_add = 10;
bad_extend.fee = db.get_global_properties().parameters.current_fees->calculate_fee(bad_extend);
bad_extend.update_issuer = bob_id;
trx.operations.push_back(bad_extend);
sign(trx, bob_private_key);
GRAPHENE_CHECK_THROW( PUSH_TX(db, trx, ~0 ), fc::exception );
trx.clear();
}
// attempt to extend it with too much time
{
graphene::chain::htlc_extend_operation big_extend;
big_extend.htlc_id = alice_htlc_id;
big_extend.seconds_to_add = db.get_global_properties().parameters.extensions.value.updatable_htlc_options->max_timeout_secs + 10;
big_extend.fee = db.get_global_properties().parameters.current_fees->calculate_fee(big_extend);
big_extend.update_issuer = alice_id;
trx.operations.push_back(big_extend);
sign(trx, alice_private_key);
GRAPHENE_CHECK_THROW( PUSH_TX(db, trx, ~0), fc::exception );
trx.clear();
}

// attempt to extend properly
{
graphene::chain::htlc_extend_operation extend;
extend.htlc_id = alice_htlc_id;
extend.seconds_to_add = 10;
extend.fee = db.get_global_properties().parameters.current_fees->calculate_fee(extend);
extend.update_issuer = alice_id;
trx.operations.push_back(extend);
sign(trx, alice_private_key);
PUSH_TX(db, trx, ~0);
trx.clear();
}

// let it expire (wait for timeout)
generate_blocks( db.head_block_time() + fc::seconds(120) );
// verify funds return (minus the fees)
BOOST_CHECK_EQUAL( get_balance(alice_id, graphene::chain::asset_id_type()), 96 * GRAPHENE_BLOCKCHAIN_PRECISION );
BOOST_CHECK_EQUAL( get_balance(alice_id, graphene::chain::asset_id_type()), 92 * GRAPHENE_BLOCKCHAIN_PRECISION );
// verify Bob cannot execute the contract after the fact
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_CASE( htlc_fulfilled )
{
try {
ACTORS((alice)(bob));
ACTORS((alice)(bob)(joker));

int64_t init_balance(100 * GRAPHENE_BLOCKCHAIN_PRECISION);

transfer( committee_account, alice_id, graphene::chain::asset(init_balance) );
transfer( committee_account, bob_id, graphene::chain::asset(init_balance) );
transfer( committee_account, joker_id, graphene::chain::asset(init_balance) );

advance_past_hardfork(this);

Expand Down Expand Up @@ -279,23 +330,32 @@ try {
// make sure Alice's money is still on hold, and account for extra fee
BOOST_CHECK_EQUAL( get_balance( alice_id, graphene::chain::asset_id_type()), 72 * GRAPHENE_BLOCKCHAIN_PRECISION );

// send a redeem operation to claim the funds
// grab number of history objects to make sure everyone gets notified
size_t alice_num_history = get_operation_history(alice_id).size();
size_t bob_num_history = get_operation_history(bob_id).size();
size_t joker_num_history = get_operation_history(joker_id).size();

// joker sends a redeem operation to claim the funds for bob
{
graphene::chain::htlc_redeem_operation update_operation;
update_operation.redeemer = bob_id;
update_operation.redeemer = joker_id;
update_operation.htlc_id = alice_htlc_id;
update_operation.preimage = pre_image;
update_operation.fee = db.current_fee_schedule().calculate_fee( update_operation );
trx.operations.push_back( update_operation );
sign(trx, bob_private_key);
sign(trx, joker_private_key);
PUSH_TX( db, trx, ~0 );
generate_block();
trx.clear();
}
// verify funds end up in Bob's account (100 + 20 - 4(fee) )
BOOST_CHECK_EQUAL( get_balance(bob_id, graphene::chain::asset_id_type()), 116 * GRAPHENE_BLOCKCHAIN_PRECISION );
// verify funds end up in Bob's account (100 + 20 )
BOOST_CHECK_EQUAL( get_balance(bob_id, graphene::chain::asset_id_type()), 120 * GRAPHENE_BLOCKCHAIN_PRECISION );
// verify funds remain out of Alice's acount ( 100 - 20 - 4 )
BOOST_CHECK_EQUAL( get_balance(alice_id, graphene::chain::asset_id_type()), 72 * GRAPHENE_BLOCKCHAIN_PRECISION );
// verify all three get notified
BOOST_CHECK_EQUAL( get_operation_history(alice_id).size(), alice_num_history + 1);
BOOST_CHECK_EQUAL( get_operation_history(bob_id).size(), bob_num_history + 1);
BOOST_CHECK_EQUAL( get_operation_history(joker_id).size(), joker_num_history + 1);
} FC_LOG_AND_RETHROW()
}

Expand Down