diff --git a/libraries/chain/hardfork.d/SIDECHAIN.hf b/libraries/chain/hardfork.d/SIDECHAIN.hf new file mode 100644 index 000000000..3a0a39906 --- /dev/null +++ b/libraries/chain/hardfork.d/SIDECHAIN.hf @@ -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 diff --git a/libraries/chain/sidechain_address_evaluator.cpp b/libraries/chain/sidechain_address_evaluator.cpp index 625ef2f0b..44408d42d 100644 --- a/libraries/chain/sidechain_address_evaluator.cpp +++ b/libraries/chain/sidechain_address_evaluator.cpp @@ -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().indices().get(); 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& obj ){ @@ -105,10 +108,15 @@ void_result delete_sidechain_address_evaluator::do_apply(const sidechain_address { try { const auto& idx = db().get_index_type().indices().get(); 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) ) } diff --git a/tests/tests/sidechain_addresses_test.cpp b/tests/tests/sidechain_addresses_test.cpp index 755ad37f9..cd0fb8769 100644 --- a/tests/tests/sidechain_addresses_test.cpp +++ b/tests/tests/sidechain_addresses_test.cpp @@ -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); @@ -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().indices().get(); - 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().indices().get(); + 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().indices().get(); + 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()); } }