son_wallet_object operations completed, basic tests added

This commit is contained in:
Srdjan Obucina 2020-01-17 14:56:44 +01:00
parent 6f0c025462
commit 2615c41224
3 changed files with 100 additions and 18 deletions

View file

@ -20,6 +20,9 @@ namespace graphene { namespace chain {
asset fee;
account_id_type payer;
son_wallet_id_type son_wallet_id;
graphene::peerplays_sidechain::sidechain_type sidechain;
string address;
account_id_type fee_payer()const { return payer; }
share_type calculate_fee(const fee_parameters_type& k)const { return 0; }
@ -31,6 +34,7 @@ namespace graphene { namespace chain {
asset fee;
account_id_type payer;
son_wallet_id_type son_wallet_id;
account_id_type fee_payer()const { return payer; }
share_type calculate_fee(const fee_parameters_type& k)const { return 0; }

View file

@ -25,35 +25,45 @@ object_id_type create_son_wallet_evaluator::do_apply(const son_wallet_create_ope
void_result update_son_wallet_evaluator::do_evaluate(const son_wallet_update_operation& op)
{ try{
FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON HARDFORK");
FC_ASSERT(db().get_global_properties().parameters.get_son_btc_account_id() != GRAPHENE_NULL_ACCOUNT, "SON paying account not set.");
const auto& idx = db().get_index_type<son_wallet_index>().indices().get<by_id>();
FC_ASSERT( idx.find(op.son_wallet_id) != idx.end() );
auto itr = idx.find(op.son_wallet_id);
//FC_ASSERT( itr.addresses[op.sidechain] == "", "Sidechain wallet address already set");
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
object_id_type update_son_wallet_evaluator::do_apply(const son_wallet_update_operation& op)
{ try {
const auto& new_son_wallet_object = db().create<son_wallet_object>( [&]( son_wallet_object& obj ){
obj.valid_from = db().head_block_time();
obj.expires = time_point_sec::maximum();
obj.sons = db().get_global_properties().active_sons;
});
return new_son_wallet_object.id;
const auto& idx = db().get_index_type<son_wallet_index>().indices().get<by_id>();
auto itr = idx.find(op.son_wallet_id);
if(itr != idx.end())
{
db().modify(*itr, [&op](son_wallet_object &swo) {
swo.addresses[op.sidechain] = op.address;
});
}
return op.son_wallet_id;
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result close_son_wallet_evaluator::do_evaluate(const son_wallet_close_operation& op)
{ try{
FC_ASSERT(db().head_block_time() >= HARDFORK_SON_TIME, "Not allowed until SON HARDFORK");
FC_ASSERT(db().get_global_properties().parameters.get_son_btc_account_id() != GRAPHENE_NULL_ACCOUNT, "SON paying account not set.");
const auto& idx = db().get_index_type<son_wallet_index>().indices().get<by_id>();
FC_ASSERT( idx.find(op.son_wallet_id) != idx.end() );
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
object_id_type close_son_wallet_evaluator::do_apply(const son_wallet_close_operation& op)
{ try {
const auto& new_son_wallet_object = db().create<son_wallet_object>( [&]( son_wallet_object& obj ){
obj.valid_from = db().head_block_time();
obj.expires = time_point_sec::maximum();
obj.sons = db().get_global_properties().active_sons;
});
return new_son_wallet_object.id;
const auto& idx = db().get_index_type<son_wallet_index>().indices().get<by_id>();
auto itr = idx.find(op.son_wallet_id);
if(itr != idx.end())
{
db().modify(*itr, [&, &op](son_wallet_object &swo) {
swo.expires = db().head_block_time();
});
}
return op.son_wallet_id;
} FC_CAPTURE_AND_RETHROW( (op) ) }
} } // namespace graphene::chain

View file

@ -4,12 +4,11 @@
#include <graphene/chain/hardfork.hpp>
#include <graphene/chain/son_wallet_object.hpp>
#include <graphene/chain/son_wallet_evaluator.hpp>
#include <graphene/peerplays_sidechain/defs.hpp>
using namespace graphene::chain;
using namespace graphene::chain::test;
BOOST_FIXTURE_TEST_SUITE( son_wallet_tests, database_fixture )
BOOST_AUTO_TEST_CASE( son_wallet_create_test ) {
@ -29,7 +28,7 @@ BOOST_AUTO_TEST_CASE( son_wallet_create_test ) {
son_wallet_create_operation op;
op.payer = gpo.parameters.get_son_btc_account_id();
op.payer = db.get_global_properties().parameters.get_son_btc_account_id();
trx.operations.push_back(op);
sign(trx, alice_private_key);
@ -39,8 +38,77 @@ BOOST_AUTO_TEST_CASE( son_wallet_create_test ) {
BOOST_TEST_MESSAGE("Check son_wallet_create_operation results");
const auto& idx = db.get_index_type<son_wallet_index>().indices().get<by_valid_from>();
const auto& idx = db.get_index_type<son_wallet_index>().indices().get<by_id>();
BOOST_REQUIRE( idx.size() == 1 );
auto obj = idx.find(son_wallet_id_type(0));
BOOST_REQUIRE( obj->addresses.at(graphene::peerplays_sidechain::sidechain_type::bitcoin) == "" );
BOOST_REQUIRE( obj->expires == time_point_sec::maximum() );
}
BOOST_AUTO_TEST_CASE( son_wallet_update_test ) {
BOOST_TEST_MESSAGE("son_wallet_update_test");
INVOKE(son_wallet_create_test);
GET_ACTOR(alice);
{
BOOST_TEST_MESSAGE("Send son_wallet_update_operation");
son_wallet_update_operation op;
op.payer = db.get_global_properties().parameters.get_son_btc_account_id();
op.son_wallet_id = son_wallet_id_type(0);
op.sidechain = graphene::peerplays_sidechain::sidechain_type::bitcoin;
op.address = "bitcoin address";
trx.operations.push_back(op);
sign(trx, alice_private_key);
PUSH_TX(db, trx, ~0);
}
generate_block();
{
BOOST_TEST_MESSAGE("Check son_wallet_update_operation results");
const auto& idx = db.get_index_type<son_wallet_index>().indices().get<by_id>();
BOOST_REQUIRE( idx.size() == 1 );
auto obj = idx.find(son_wallet_id_type(0));
BOOST_REQUIRE( obj->addresses.at(graphene::peerplays_sidechain::sidechain_type::bitcoin) == "bitcoin address" );
}
}
BOOST_AUTO_TEST_CASE( son_wallet_close_test ) {
BOOST_TEST_MESSAGE("son_wallet_close_test");
INVOKE(son_wallet_create_test);
GET_ACTOR(alice);
{
BOOST_TEST_MESSAGE("Send son_wallet_close_operation");
son_wallet_close_operation op;
op.payer = db.get_global_properties().parameters.get_son_btc_account_id();
op.son_wallet_id = son_wallet_id_type(0);
trx.operations.push_back(op);
sign(trx, alice_private_key);
PUSH_TX(db, trx, ~0);
}
generate_block();
{
BOOST_TEST_MESSAGE("Check son_wallet_close_operation results");
const auto& idx = db.get_index_type<son_wallet_index>().indices().get<by_id>();
BOOST_REQUIRE( idx.size() == 1 );
auto obj = idx.find(son_wallet_id_type(0));
BOOST_REQUIRE( obj->expires != time_point_sec::maximum() );
}
}
BOOST_AUTO_TEST_SUITE_END()