code improvement with edge case handling

This commit is contained in:
pbattu123 2020-04-07 01:31:17 -03:00
parent e445e1a11c
commit cb7429e818
3 changed files with 10 additions and 16 deletions

View file

@ -207,7 +207,8 @@ vector<uint16_t> asset_object::get_ticket_ids( database& db ) const
if( ath->next == account_transaction_history_id_type() ) if( ath->next == account_transaction_history_id_type() )
{ {
ids.insert(ids.end(), balance-1, oho.id.instance()); if(balance > 1 && oho.op.which() == operation::tag<ticket_purchase_operation>::value)
ids.insert(ids.end(), balance-1, oho.id.instance());
ath = nullptr; ath = nullptr;
break; break;
} }
@ -269,9 +270,9 @@ map< account_id_type, vector< uint16_t > > asset_object::distribute_winners_part
reward_op.is_benefactor_reward = false; reward_op.is_benefactor_reward = false;
reward_op.winner = holders[winner_num]; reward_op.winner = holders[winner_num];
time_point_sec now = time_point::now(); time_point_sec now = time_point::now();
if(now > HARDFORK_5050_1_TIME) if(now > HARDFORK_5050_1_TIME && ticket_ids.size() >= winner_num)
{ {
const static_variant<void_t, uint16_t> tkt_id = ticket_ids[winner_num]; const static_variant<uint16_t, void_t> tkt_id = ticket_ids[winner_num];
reward_op.winner_ticket_id = tkt_id; reward_op.winner_ticket_id = tkt_id;
} }
reward_op.win_percentage = tickets[c]; reward_op.win_percentage = tickets[c];

View file

@ -52,7 +52,7 @@ namespace graphene { namespace chain {
share_type calculate_fee( const fee_parameters_type& k )const; share_type calculate_fee( const fee_parameters_type& k )const;
}; };
typedef static_variant<void_t, uint16_t> ticket_num; typedef static_variant<uint16_t, void_t> ticket_num;
/** /**
* @ingroup operations * @ingroup operations

View file

@ -525,21 +525,14 @@ BOOST_AUTO_TEST_CASE( lottery_winner_ticket_id_test )
for( uint8_t win: test_asset.lottery_options->winning_tickets ) for( uint8_t win: test_asset.lottery_options->winning_tickets )
winners_part += win; winners_part += win;
auto participants = test_asset.distribute_winners_part( db ); while( db.head_block_time() < ( test_asset.lottery_options->end_date ) )
test_asset.distribute_benefactors_part( db ); generate_block();
test_asset.distribute_sweeps_holders_part( db );
generate_block(); auto op_history = get_operation_history( account_id_type(1) ); //Can observe operation 79 to verify winner ticket number
for( auto p: participants ) { for( auto h: op_history ) {
idump(( get_operation_history(p.first) ));
}
auto benefactor_history = get_operation_history( account_id_type() );
for( auto h: benefactor_history ) {
idump((h)); idump((h));
} }
while( db.head_block_time() < ( test_asset.lottery_options->end_date + fc::seconds(30) ) )
generate_block();
BOOST_CHECK( db.get_balance( test_asset.get_id() ).amount.value == 0 ); BOOST_CHECK( db.get_balance( test_asset.get_id() ).amount.value == 0 );
uint64_t creator_recieved = db.get_balance( account_id_type(), asset_id_type() ).amount.value - creator_balance_before_end; uint64_t creator_recieved = db.get_balance( account_id_type(), asset_id_type() ).amount.value - creator_balance_before_end;
test_asset = test_asset_id(db); test_asset = test_asset_id(db);