diff --git a/libraries/chain/betting_market_object.cpp b/libraries/chain/betting_market_object.cpp index 4c3eeae5..f5cc9f6d 100644 --- a/libraries/chain/betting_market_object.cpp +++ b/libraries/chain/betting_market_object.cpp @@ -195,6 +195,7 @@ namespace void on_entry(const canceled_event& event, betting_market_state_machine_& fsm) { dlog("betting market ${id} -> canceled", ("id", fsm.betting_market_obj->id)); fsm.betting_market_obj->resolution = betting_market_resolution_type::cancel; + fsm.betting_market_obj->cancel_all_bets(event.db); } }; @@ -375,6 +376,18 @@ void betting_market_object::cancel_all_unmatched_bets(database& db) const db.cancel_bet(*old_book_itr, true); } } + +void betting_market_object::cancel_all_bets(database& db) const +{ + const auto& bets_by_market_id = db.get_index_type().indices().get(); + + auto bet_it = bets_by_market_id.lower_bound(id); + auto bet_it_end = bets_by_market_id.upper_bound(id); + for (; bet_it != bet_it_end; ++bet_it) + { + db.cancel_bet(*bet_it, true); + } +} void betting_market_object::pack_impl(std::ostream& stream) const { diff --git a/libraries/chain/include/graphene/chain/betting_market_object.hpp b/libraries/chain/include/graphene/chain/betting_market_object.hpp index 4f6665c8..772d2219 100644 --- a/libraries/chain/include/graphene/chain/betting_market_object.hpp +++ b/libraries/chain/include/graphene/chain/betting_market_object.hpp @@ -154,6 +154,7 @@ class betting_market_object : public graphene::db::abstract_object< betting_mark betting_market_status get_status() const; void cancel_all_unmatched_bets(database& db) const; + void cancel_all_bets(database& db) const; // serialization functions: // for serializing to raw, go through a temporary sstream object to avoid @@ -585,12 +586,14 @@ struct compare_bet_by_bettor_then_odds { }; struct by_odds {}; +struct by_betting_market_id {}; struct by_bettor_and_odds {}; typedef multi_index_container< bet_object, indexed_by< ordered_unique< tag, member< object, object_id_type, &object::id > >, ordered_unique< tag, identity, compare_bet_by_odds >, + ordered_non_unique< tag, member >, ordered_unique< tag, identity, compare_bet_by_bettor_then_odds > > > bet_object_multi_index_type; typedef generic_index bet_object_index; diff --git a/tests/betting/betting_tests.cpp b/tests/betting/betting_tests.cpp index 1e6d97b5..f77d410f 100644 --- a/tests/betting/betting_tests.cpp +++ b/tests/betting/betting_tests.cpp @@ -1591,8 +1591,15 @@ BOOST_AUTO_TEST_CASE(event_group_delete_test) { try { + ACTORS( (alice)(bob) ) CREATE_ICE_HOCKEY_BETTING_MARKET(false, 0); + const int initialAccountAsset = 10000000; + const int betAsset = 1000000; + + transfer(account_id_type(), alice_id, asset(initialAccountAsset)); + transfer(account_id_type(), bob_id, asset(initialAccountAsset)); + const auto& event_1 = create_event({{"en", "event 1"}}, {{"en", "2016-17"}}, nhl.id); const auto& event_2 = create_event({{"en", "event 2"}}, {{"en", "2016-17"}}, nhl.id); const auto& event_3 = create_event({{"en", "event 3"}}, {{"en", "2016-17"}}, nhl.id); @@ -1601,6 +1608,16 @@ BOOST_AUTO_TEST_CASE(event_group_delete_test) const auto& market_1 = create_betting_market(market_group.id, {{"en", "market 1"}}); const auto& market_2 = create_betting_market(market_group.id, {{"en", "market 2"}}); + //to make bets be not removed immediately + update_betting_market_group_impl(market_group.id, + fc::optional(), + fc::optional(), + betting_market_group_status::in_play, + false); + + const auto& bet_1_id = place_bet(alice_id, market_1.id, bet_type::back, asset(betAsset, asset_id_type()), 2 * GRAPHENE_BETTING_ODDS_PRECISION); + const auto& bet_2_id = place_bet(bob_id, market_1.id, bet_type::lay, asset(betAsset, asset_id_type()), 2 * GRAPHENE_BETTING_ODDS_PRECISION); + delete_event_group(nhl.id); const auto& event_group_by_id = db.get_index_type().indices().get(); @@ -1614,6 +1631,14 @@ BOOST_AUTO_TEST_CASE(event_group_delete_test) BOOST_CHECK(betting_market_status::canceled == market_1.get_status()); BOOST_CHECK(betting_market_status::canceled == market_2.get_status()); + + //check canceled bets and reverted balance changes + const auto& bet_by_id = db.get_index_type().indices().get(); + BOOST_CHECK(bet_by_id.end() == bet_by_id.find(bet_1_id)); + BOOST_CHECK(bet_by_id.end() == bet_by_id.find(bet_2_id)); + + BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), initialAccountAsset); + BOOST_CHECK_EQUAL(get_balance(bob_id, asset_id_type()), initialAccountAsset); } FC_LOG_AND_RETHROW() }