diff --git a/tests/betting/betting_tests.cpp b/tests/betting/betting_tests.cpp index 92614f46..cae93a97 100644 --- a/tests/betting/betting_tests.cpp +++ b/tests/betting/betting_tests.cpp @@ -68,57 +68,58 @@ BOOST_AUTO_TEST_CASE(generate_block) } #endif -BOOST_AUTO_TEST_CASE(try_create_sport_and_try_again) +BOOST_AUTO_TEST_CASE(try_create_sport) { try { - // "even" witnesses will approve the proposal - // we're expecting only 5 approvals, 6 needed - fc::optional sport_id = try_create_sport({{"en", "Ice Hockey"}, {"zh_Hans", "冰球"}, {"ja", "アイスホッケー"}}); - BOOST_REQUIRE(!sport_id.valid()); + fc::optional result; + sport_create_operation sport_create_op; + sport_create_op.name = {{"en", "Ice Hockey"}, {"zh_Hans", "冰球"}, {"ja", "アイスホッケー"}}; - // adding new active witness - const auto& witnesses = db.get_global_properties().active_witnesses; - ACTOR(nathan); - upgrade_to_lifetime_member(nathan_id); - trx.clear(); - witness_id_type nathan_witness_id = create_witness(nathan_id, nathan_private_key).id; - // give nathan some voting stake - transfer(committee_account, nathan_id, asset(10000000)); - generate_block(); - set_expiration( db, trx ); + proposal_id_type proposal_id = propose_operation(sport_create_op); - // "even" witnesses vote for nathan - account_update_operation op; - op.account = nathan_id; - op.new_options = nathan_id(db).options; - op.new_options->votes.clear(); - uint16_t count = -1; - uint16_t votes = 0; - for (const witness_id_type& witness_id : witnesses) - { - if (++count % 2) - continue; - op.new_options->votes.insert(witness_id(db).vote_id); - ++votes; - } - op.new_options->num_witness = votes; - op.new_options->num_committee = 0; + const auto& active_witnesses = db.get_global_properties().active_witnesses; + int voting_count = active_witnesses.size() / 2; - trx.operations.push_back(op); - sign( trx, nathan_private_key ); - PUSH_TX( db, trx ); - trx.clear(); + // 5 for + std::vector witnesses; + for (const witness_id_type& witness_id : active_witnesses) + { + witnesses.push_back(witness_id); + if (--voting_count == 0) + break; + } + process_proposal_by_witnesses(witnesses, proposal_id); - generate_blocks(db.get_dynamic_global_properties().next_maintenance_time); - // make sure nathan is in active_witnesses - auto itr = std::find(witnesses.begin(), witnesses.end(), nathan_witness_id); - BOOST_CHECK(itr != witnesses.end()); + // 1st out + witnesses.clear(); + auto itr = active_witnesses.begin(); + witnesses.push_back(*itr); + process_proposal_by_witnesses(witnesses, proposal_id, true); - // now we're expecting 6 votes for - sport_id = try_create_sport({{"en", "Ice Hockey"}, {"zh_Hans", "冰球"}, {"ja", "アイスホッケー"}}); - BOOST_REQUIRE(sport_id.valid()); - BOOST_REQUIRE(*sport_id == sport_id_type()); + const auto& sport_index = db.get_index_type().indices().get(); + // not yet approved + BOOST_REQUIRE(sport_index.rbegin() == sport_index.rend()); + + // 6th for + witnesses.clear(); + itr += 5; + witnesses.push_back(*itr); + process_proposal_by_witnesses(witnesses, proposal_id); + + // not yet approved + BOOST_REQUIRE(sport_index.rbegin() == sport_index.rend()); + + // 7th for + witnesses.clear(); + ++itr; + witnesses.push_back(*itr); + process_proposal_by_witnesses(witnesses, proposal_id); + + // done + BOOST_REQUIRE(sport_index.rbegin() != sport_index.rend()); + sport_id_type sport_id = (*sport_index.rbegin()).id; + BOOST_REQUIRE(sport_id == sport_id_type()); } FC_LOG_AND_RETHROW() } diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index a2b653d7..2765e6e3 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -1111,9 +1111,8 @@ vector< operation_history_object > database_fixture::get_operation_history( acco return result; } -void database_fixture::process_operation_by_witnesses(operation op, bool not_all) +void database_fixture::process_operation_by_witnesses(operation op) { - uint16_t count = -1; const flat_set& active_witnesses = db.get_global_properties().active_witnesses; proposal_create_operation proposal_op; @@ -1131,13 +1130,6 @@ void database_fixture::process_operation_by_witnesses(operation op, bool not_all for (const witness_id_type& witness_id : active_witnesses) { - if (not_all) - { - if (++count % 2) - { - continue; - } - } const witness_object& witness = witness_id(db); const account_object& witness_account = witness.witness_account(db); @@ -1158,19 +1150,54 @@ void database_fixture::process_operation_by_witnesses(operation op, bool not_all } } -fc::optional database_fixture::try_create_sport(internationalized_string_type name) -{ try { - fc::optional result; - sport_create_operation sport_create_op; - sport_create_op.name = name; - process_operation_by_witnesses(sport_create_op, true); - const auto& sport_index = db.get_index_type().indices().get(); - if (sport_index.rbegin() != sport_index.rend()) +proposal_id_type database_fixture::propose_operation(operation op) +{ + const flat_set& active_witnesses = db.get_global_properties().active_witnesses; + + proposal_create_operation proposal_op; + proposal_op.fee_paying_account = (*active_witnesses.begin())(db).witness_account; + proposal_op.proposed_ops.emplace_back(op); + proposal_op.expiration_time = db.head_block_time() + fc::days(1); + + signed_transaction tx; + tx.operations.push_back(proposal_op); + set_expiration(db, tx); + sign(tx, init_account_priv_key); + + processed_transaction processed_tx = db.push_transaction(tx); + proposal_id_type proposal_id = processed_tx.operation_results[0].get(); + + return proposal_id; +} + +void database_fixture::process_proposal_by_witnesses(const std::vector& witnesses, proposal_id_type proposal_id, bool remove) +{ + const auto& proposal_idx = db.get_index_type().indices().get(); + + for (const witness_id_type& witness_id : witnesses) { - result = (*sport_index.rbegin()).id; + if (proposal_idx.find(proposal_id) == proposal_idx.end()) + break; + + const witness_object& witness = witness_id(db); + const account_object& witness_account = witness.witness_account(db); + + proposal_update_operation pup; + pup.proposal = proposal_id; + pup.fee_paying_account = witness_account.id; + if (remove) + pup.active_approvals_to_remove.insert(witness_account.id); + else + pup.active_approvals_to_add.insert(witness_account.id); + + signed_transaction tx; + tx.operations.push_back( pup ); + set_expiration( db, tx ); + sign(tx, init_account_priv_key); + + db.push_transaction(tx, ~0); } - return result; -} FC_CAPTURE_AND_RETHROW( (name) ) } +} const sport_object& database_fixture::create_sport(internationalized_string_type name) { try { diff --git a/tests/common/database_fixture.hpp b/tests/common/database_fixture.hpp index c2899d9f..68566881 100644 --- a/tests/common/database_fixture.hpp +++ b/tests/common/database_fixture.hpp @@ -284,8 +284,7 @@ struct database_fixture { account_id_type dividend_holder_account_id, asset_id_type dividend_payout_asset_type) const; vector< operation_history_object > get_operation_history( account_id_type account_id )const; - void process_operation_by_witnesses(operation op, bool not_all = false); - fc::optional try_create_sport(internationalized_string_type name); + void process_operation_by_witnesses(operation op); const sport_object& create_sport(internationalized_string_type name); const event_group_object& create_event_group(internationalized_string_type name, sport_id_type sport_id); const event_object& create_event(internationalized_string_type name, internationalized_string_type season, event_group_id_type event_group_id); @@ -295,6 +294,9 @@ struct database_fixture { void place_bet(account_id_type bettor_id, betting_market_id_type betting_market_id, bet_type back_or_lay, asset amount_to_bet, bet_multiplier_type backer_multiplier, share_type amount_reserved_for_fees); void resolve_betting_market_group(betting_market_group_id_type betting_market_group_id, std::map resolutions); + + proposal_id_type propose_operation(operation op); + void process_proposal_by_witnesses(const std::vector& witnesses, proposal_id_type proposal_id, bool remove = false); }; namespace test {