Skip to content

Commit

Permalink
Merge branch 'hotfix/sidechain-address' into 'master'
Browse files Browse the repository at this point in the history
Hotfix: Fix sidechain address editing

See merge request PBSA/peerplays!171
  • Loading branch information
bobinson committed Nov 4, 2022
2 parents 611a630 + da73e31 commit 058937a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
7 changes: 7 additions & 0 deletions libraries/chain/hardfork.d/SIDECHAIN.hf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef HARDFORK_SIDECHAIN_DELETE_TIME
#ifdef BUILD_PEERPLAYS_TESTNET
#define HARDFORK_SIDECHAIN_DELETE_TIME (fc::time_point_sec::from_iso_string("2022-11-16T02:00:00"))
#else
#define HARDFORK_SIDECHAIN_DELETE_TIME (fc::time_point_sec::from_iso_string("2022-11-16T02:00:00"))
#endif
#endif
26 changes: 17 additions & 9 deletions libraries/chain/sidechain_address_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ object_id_type add_sidechain_address_evaluator::do_apply(const sidechain_address
const auto &sidechain_addresses_idx = db().get_index_type<sidechain_address_index>().indices().get<by_account_and_sidechain_and_expires>();
const auto &addr_itr = sidechain_addresses_idx.find(std::make_tuple(op.sidechain_address_account, op.sidechain, time_point_sec::maximum()));

if (addr_itr != sidechain_addresses_idx.end())
{
db().modify(*addr_itr, [&](sidechain_address_object &sao) {
sao.expires = db().head_block_time();
});
if (addr_itr != sidechain_addresses_idx.end()) {
if (db().head_block_time() >= HARDFORK_SIDECHAIN_DELETE_TIME) {
db().remove(*addr_itr);
} else {
db().modify(*addr_itr, [&](sidechain_address_object &sao) {
sao.expires = db().head_block_time();
});
}
}

const auto& new_sidechain_address_object = db().create<sidechain_address_object>( [&]( sidechain_address_object& obj ){
Expand Down Expand Up @@ -105,10 +108,15 @@ void_result delete_sidechain_address_evaluator::do_apply(const sidechain_address
{ try {
const auto& idx = db().get_index_type<sidechain_address_index>().indices().get<by_id>();
auto sidechain_address = idx.find(op.sidechain_address_id);
if(sidechain_address != idx.end()) {
db().modify(*sidechain_address, [&](sidechain_address_object &sao) {
sao.expires = db().head_block_time();
});

if (sidechain_address != idx.end()) {
if (db().head_block_time() >= HARDFORK_SIDECHAIN_DELETE_TIME) {
db().remove(*sidechain_address);
} else {
db().modify(*sidechain_address, [&](sidechain_address_object &sao) {
sao.expires = db().head_block_time();
});
}
}
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
Expand Down
61 changes: 55 additions & 6 deletions tests/tests/sidechain_addresses_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ BOOST_AUTO_TEST_CASE( sidechain_address_delete_test ) {

BOOST_TEST_MESSAGE("sidechain_address_delete_test");

generate_blocks(HARDFORK_SIDECHAIN_DELETE_TIME);
generate_block();

INVOKE(sidechain_address_add_test);

GET_ACTOR(alice);
Expand All @@ -151,18 +154,64 @@ BOOST_AUTO_TEST_CASE( sidechain_address_delete_test ) {
sign(trx, alice_private_key);
PUSH_TX(db, trx, ~0);
}
time_point_sec now = db.head_block_time();
//time_point_sec now = db.head_block_time();
generate_block();

{
BOOST_TEST_MESSAGE("Check sidechain_address_delete_operation results");

const auto& idx = db.get_index_type<sidechain_address_index>().indices().get<by_account_and_sidechain_and_expires>();
BOOST_REQUIRE( idx.size() == 1 );
auto obj = idx.find( boost::make_tuple( alice_id, sidechain_type::bitcoin, time_point_sec::maximum() ) );
BOOST_REQUIRE( obj == idx.end() );
auto expired_obj = idx.find( boost::make_tuple( alice_id, sidechain_type::bitcoin, now ) );
BOOST_REQUIRE( expired_obj != idx.end() );
BOOST_REQUIRE( idx.size() == 0 );
}
}

BOOST_AUTO_TEST_CASE(sidechain_address_delete_create_test) {

BOOST_TEST_MESSAGE("sidechain_address_delete_create_test");

generate_blocks(HARDFORK_SIDECHAIN_DELETE_TIME);
generate_block();

INVOKE(sidechain_address_add_test);

GET_ACTOR(alice);

const auto &idx = db.get_index_type<sidechain_address_index>().indices().get<by_account_and_sidechain_and_expires>();
BOOST_REQUIRE(idx.size() == 1);
auto obj = idx.find(boost::make_tuple(alice_id, sidechain_type::bitcoin, time_point_sec::maximum()));
BOOST_REQUIRE(obj != idx.end());

{
BOOST_TEST_MESSAGE("Delete and create sidechain address");
sidechain_address_delete_operation op_del;
op_del.payer = alice_id;
op_del.sidechain_address_id = sidechain_address_id_type(0);
op_del.sidechain_address_account = alice_id;
op_del.sidechain = obj->sidechain;

sidechain_address_add_operation op_create;
op_create.payer = alice_id;
op_create.sidechain_address_account = alice_id;
op_create.sidechain = sidechain_type::bitcoin;
op_create.deposit_public_key = "deposit_public_key";
op_create.withdraw_public_key = "withdraw_public_key";
op_create.withdraw_address = "withdraw_address";

trx.operations.push_back(op_del);
trx.operations.push_back(op_create);
sign(trx, alice_private_key);
PUSH_TX(db, trx, ~0);
}

// both transactions should goes in one block (delete + create)
generate_block();

{
BOOST_TEST_MESSAGE("Check sidechain_address_delete_add_operation results");
const auto &idx = db.get_index_type<sidechain_address_index>().indices().get<by_account_and_sidechain_and_expires>();
BOOST_REQUIRE(idx.size() == 1);
auto obj = idx.find(boost::make_tuple(alice_id, sidechain_type::bitcoin, time_point_sec::maximum()));
BOOST_REQUIRE(obj != idx.end());
}
}

Expand Down

0 comments on commit 058937a

Please sign in to comment.