From 78100f9fb5c148b10996d99c8bdf940eb829aee6 Mon Sep 17 00:00:00 2001 From: Roman Olearski Date: Thu, 20 Jul 2017 14:28:12 +0200 Subject: [PATCH] added betting_market_rules_update_operation --- libraries/app/impacted.cpp | 1 + libraries/chain/betting_market_evaluator.cpp | 22 +++++++++++++++ libraries/chain/db_init.cpp | 1 + libraries/chain/db_notify.cpp | 1 + .../chain/betting_market_evaluator.hpp | 9 ++++++ .../chain/protocol/betting_market.hpp | 22 +++++++++++++++ .../graphene/chain/protocol/operations.hpp | 1 + libraries/chain/protocol/betting_market.cpp | 5 ++++ tests/betting/betting_tests.cpp | 28 +++++++++++++++++++ tests/common/database_fixture.cpp | 11 ++++++++ tests/common/database_fixture.hpp | 3 ++ 11 files changed, 104 insertions(+) diff --git a/libraries/app/impacted.cpp b/libraries/app/impacted.cpp index 455883e0..c723c1fe 100644 --- a/libraries/app/impacted.cpp +++ b/libraries/app/impacted.cpp @@ -216,6 +216,7 @@ struct get_impacted_account_visitor void operator()( const event_create_operation& op ) {} void operator()( const event_update_operation& op ) {} void operator()( const betting_market_rules_create_operation& op ) {} + void operator()( const betting_market_rules_update_operation& op ) {} void operator()( const betting_market_group_create_operation& op ) {} void operator()( const betting_market_create_operation& op ) {} void operator()( const betting_market_group_resolve_operation& op ) {} diff --git a/libraries/chain/betting_market_evaluator.cpp b/libraries/chain/betting_market_evaluator.cpp index 7a832df5..c74f650b 100644 --- a/libraries/chain/betting_market_evaluator.cpp +++ b/libraries/chain/betting_market_evaluator.cpp @@ -49,6 +49,28 @@ object_id_type betting_market_rules_create_evaluator::do_apply(const betting_mar return new_betting_market_rules.id; } FC_CAPTURE_AND_RETHROW( (op) ) } +void_result betting_market_rules_update_evaluator::do_evaluate(const betting_market_rules_update_operation& op) +{ try { + FC_ASSERT(trx_state->_is_proposed_trx); + FC_ASSERT(op.new_name.valid() || op.new_description.valid()); + return void_result(); +} FC_CAPTURE_AND_RETHROW( (op) ) } + +void_result betting_market_rules_update_evaluator::do_apply(const betting_market_rules_update_operation& op) +{ try { + database& _db = db(); + _db.modify( + _db.get(op.betting_market_rules_id), + [&]( betting_market_rules_object& bmro ) + { + if( op.new_name.valid() ) + bmro.name = *op.new_name; + if( op.new_description.valid() ) + bmro.description = *op.new_description; + }); + return void_result(); +} FC_CAPTURE_AND_RETHROW( (op) ) } + void_result betting_market_group_create_evaluator::do_evaluate(const betting_market_group_create_operation& op) { try { FC_ASSERT(trx_state->_is_proposed_trx); diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index d0c3d6dc..54344a1b 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -221,6 +221,7 @@ void database::initialize_evaluators() register_evaluator(); register_evaluator(); register_evaluator(); + register_evaluator(); register_evaluator(); register_evaluator(); register_evaluator(); diff --git a/libraries/chain/db_notify.cpp b/libraries/chain/db_notify.cpp index e6c67df5..90d27618 100644 --- a/libraries/chain/db_notify.cpp +++ b/libraries/chain/db_notify.cpp @@ -198,6 +198,7 @@ struct get_impacted_account_visitor void operator()(const event_create_operation&){} void operator()(const event_update_operation& op ) {} void operator()(const betting_market_rules_create_operation&){} + void operator()(const betting_market_rules_update_operation& op ) {} void operator()(const betting_market_group_create_operation&){} void operator()(const betting_market_create_operation&){} void operator()(const bet_place_operation&){} diff --git a/libraries/chain/include/graphene/chain/betting_market_evaluator.hpp b/libraries/chain/include/graphene/chain/betting_market_evaluator.hpp index 6b75773a..d07b6696 100644 --- a/libraries/chain/include/graphene/chain/betting_market_evaluator.hpp +++ b/libraries/chain/include/graphene/chain/betting_market_evaluator.hpp @@ -38,6 +38,15 @@ namespace graphene { namespace chain { object_id_type do_apply( const betting_market_rules_create_operation& o ); }; + class betting_market_rules_update_evaluator : public evaluator + { + public: + typedef betting_market_rules_update_operation operation_type; + + void_result do_evaluate( const betting_market_rules_update_operation& o ); + void_result do_apply( const betting_market_rules_update_operation& o ); + }; + class betting_market_group_create_evaluator : public evaluator { public: diff --git a/libraries/chain/include/graphene/chain/protocol/betting_market.hpp b/libraries/chain/include/graphene/chain/protocol/betting_market.hpp index 9ce77b1b..b02603b0 100644 --- a/libraries/chain/include/graphene/chain/protocol/betting_market.hpp +++ b/libraries/chain/include/graphene/chain/protocol/betting_market.hpp @@ -51,6 +51,24 @@ struct betting_market_rules_create_operation : public base_operation void validate()const; }; + +struct betting_market_rules_update_operation : public base_operation +{ + struct fee_parameters_type { uint64_t fee = GRAPHENE_BLOCKCHAIN_PRECISION; }; + asset fee; + + betting_market_rules_id_type betting_market_rules_id; + + fc::optional new_name; + + fc::optional new_description; + + extensions_type extensions; + + account_id_type fee_payer()const { return GRAPHENE_WITNESS_ACCOUNT; } + void validate()const; +}; + struct betting_market_group_create_operation : public base_operation { struct fee_parameters_type { uint64_t fee = GRAPHENE_BLOCKCHAIN_PRECISION; }; @@ -311,6 +329,10 @@ FC_REFLECT( graphene::chain::betting_market_rules_create_operation::fee_paramete FC_REFLECT( graphene::chain::betting_market_rules_create_operation, (fee)(name)(description)(extensions) ) +FC_REFLECT( graphene::chain::betting_market_rules_update_operation::fee_parameters_type, (fee) ) +FC_REFLECT( graphene::chain::betting_market_rules_update_operation, + (fee)(new_name)(new_description)(extensions)(betting_market_rules_id) ) + FC_REFLECT( graphene::chain::betting_market_group_create_operation::fee_parameters_type, (fee) ) FC_REFLECT( graphene::chain::betting_market_group_create_operation, (fee)(description)(event_id)(rules_id)(asset_id)(extensions) ) diff --git a/libraries/chain/include/graphene/chain/protocol/operations.hpp b/libraries/chain/include/graphene/chain/protocol/operations.hpp index 7e9e156a..97e74eb9 100644 --- a/libraries/chain/include/graphene/chain/protocol/operations.hpp +++ b/libraries/chain/include/graphene/chain/protocol/operations.hpp @@ -106,6 +106,7 @@ namespace graphene { namespace chain { event_create_operation, event_update_operation, betting_market_rules_create_operation, + betting_market_rules_update_operation, betting_market_group_create_operation, betting_market_create_operation, bet_place_operation, diff --git a/libraries/chain/protocol/betting_market.cpp b/libraries/chain/protocol/betting_market.cpp index 079fba15..553d546f 100644 --- a/libraries/chain/protocol/betting_market.cpp +++ b/libraries/chain/protocol/betting_market.cpp @@ -30,6 +30,11 @@ void betting_market_rules_create_operation::validate() const FC_ASSERT( fee.amount >= 0 ); } +void betting_market_rules_update_operation::validate() const +{ + FC_ASSERT( fee.amount >= 0 ); +} + void betting_market_group_create_operation::validate() const { FC_ASSERT( fee.amount >= 0 ); diff --git a/tests/betting/betting_tests.cpp b/tests/betting/betting_tests.cpp index 89871a67..96d745c1 100644 --- a/tests/betting/betting_tests.cpp +++ b/tests/betting/betting_tests.cpp @@ -241,6 +241,34 @@ BOOST_AUTO_TEST_CASE(peerplays_event_update_test) } FC_LOG_AND_RETHROW() } +BOOST_AUTO_TEST_CASE(peerplays_betting_market_rules_update_test) +{ + try + { + ACTORS( (alice) ); + CREATE_ICE_HOCKEY_BETTING_MARKET(); + + internationalized_string_type n = {{"en", "NHL Rules v1.1"}}; + internationalized_string_type d = {{"en", "The winner will be the team with the most points at the end of the game. The team with fewer points will not be the winner."}}; + + fc::optional empty; + fc::optional name = n; + fc::optional desc = d; + + update_betting_market_rules(betting_market_rules.id, name, empty); + update_betting_market_rules(betting_market_rules.id, empty, desc); + update_betting_market_rules(betting_market_rules.id, name, desc); + + transfer(account_id_type(), alice_id, asset(10000000)); + place_bet(alice_id, capitals_win_market.id, bet_type::back, asset(1000000, asset_id_type()), 2 * GRAPHENE_BETTING_ODDS_PRECISION, 1000000 / 50 /* chain defaults to 2% fees */); + + BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), 10000000 - 1000000 - 20000); + + //GRAPHENE_REQUIRE_THROW(update_betting_market_rules(betting_market_rules.id, empty, empty), fc::exception); + + } FC_LOG_AND_RETHROW() +} + BOOST_AUTO_TEST_CASE( cancel_unmatched_in_betting_group_test ) { diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index 5af9b1a2..a4a4a3eb 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -1265,6 +1265,17 @@ const betting_market_rules_object& database_fixture::create_betting_market_rules return *betting_market_rules_index.rbegin(); } FC_CAPTURE_AND_RETHROW( (name) ) } +void database_fixture::update_betting_market_rules(betting_market_rules_id_type rules_id, + fc::optional name, + fc::optional description) +{ try { + betting_market_rules_update_operation betting_market_rules_update_op; + betting_market_rules_update_op.betting_market_rules_id = rules_id; + betting_market_rules_update_op.new_name = name; + betting_market_rules_update_op.new_description = description; + process_operation_by_witnesses(betting_market_rules_update_op); +} FC_CAPTURE_AND_RETHROW( (name)(description) ) } + const betting_market_group_object& database_fixture::create_betting_market_group(internationalized_string_type description, event_id_type event_id, betting_market_rules_id_type rules_id, asset_id_type asset_id) { try { betting_market_group_create_operation betting_market_group_create_op; diff --git a/tests/common/database_fixture.hpp b/tests/common/database_fixture.hpp index 28b7f7f8..ae320420 100644 --- a/tests/common/database_fixture.hpp +++ b/tests/common/database_fixture.hpp @@ -292,6 +292,9 @@ struct database_fixture { const event_object& create_event(internationalized_string_type name, internationalized_string_type season, event_group_id_type event_group_id); void update_event(event_id_type event_id, fc::optional name, fc::optional season); const betting_market_rules_object& create_betting_market_rules(internationalized_string_type name, internationalized_string_type description); + void update_betting_market_rules(betting_market_rules_id_type rules_id, + fc::optional name, + fc::optional description); const betting_market_group_object& create_betting_market_group(internationalized_string_type description, event_id_type event_id, betting_market_rules_id_type rules_id, asset_id_type asset_id); const betting_market_object& create_betting_market(betting_market_group_id_type group_id, internationalized_string_type payout_condition);