From 62dd8482d93126d3ad46703557a2a9f9216f8326 Mon Sep 17 00:00:00 2001 From: kstdl Date: Wed, 3 Jan 2018 15:34:15 +0300 Subject: [PATCH] added get_account_lotteries method to db_api and cli, lottery end_date and ticket_price verification --- libraries/app/database_api.cpp | 38 +++++++++++++++++-- .../app/include/graphene/app/database_api.hpp | 6 ++- libraries/chain/asset_evaluator.cpp | 4 +- .../include/graphene/chain/asset_object.hpp | 4 ++ libraries/chain/protocol/asset_ops.cpp | 1 + .../wallet/include/graphene/wallet/wallet.hpp | 5 +++ libraries/wallet/wallet.cpp | 16 ++++++-- 7 files changed, 65 insertions(+), 9 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index e18e61f7..d2bfacc2 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -118,6 +118,10 @@ class database_api_impl : public std::enable_shared_from_this vector get_lotteries( asset_id_type stop = asset_id_type(), unsigned limit = 100, asset_id_type start = asset_id_type() )const; + vector get_account_lotteries( account_id_type issuer, + asset_id_type stop, + unsigned limit, + asset_id_type start )const; asset get_lottery_balance( asset_id_type lottery_id )const; sweeps_vesting_balance_object get_sweeps_vesting_balance_object( account_id_type account )const; asset get_sweeps_vesting_balance_available_for_claim( account_id_type account )const; @@ -1032,9 +1036,9 @@ vector database_api::get_lotteries( asset_id_type stop, { return my->get_lotteries( stop, limit, start ); } -vector database_api_impl::get_lotteries(asset_id_type stop, - unsigned limit, - asset_id_type start )const +vector database_api_impl::get_lotteries( asset_id_type stop, + unsigned limit, + asset_id_type start )const { vector result; if( limit > 100 ) limit = 100; @@ -1051,6 +1055,34 @@ vector database_api_impl::get_lotteries(asset_id_type stop, return result; } +vector database_api::get_account_lotteries( account_id_type issuer, + asset_id_type stop, + unsigned limit, + asset_id_type start )const +{ + return my->get_account_lotteries( issuer, stop, limit, start ); +} + +vector database_api_impl::get_account_lotteries( account_id_type issuer, + asset_id_type stop, + unsigned limit, + asset_id_type start )const +{ + vector result; + if( limit > 100 ) limit = 100; + const auto& assets = _db.get_index_type().indices().get(); + + const auto range = assets.equal_range( boost::make_tuple( true, issuer.instance.value ) ); + for( const auto& a : boost::make_iterator_range( range.first, range.second ) ) + { + if( start == asset_id_type() || (a.get_id().instance.value <= start.instance.value) ) + result.push_back( a ); + if( a.get_id().instance.value < stop.instance.value || result.size() >= limit ) + break; + } + + return result; +} asset database_api::get_lottery_balance( asset_id_type lottery_id )const { diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index ca02180e..a1f83a42 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -359,7 +359,10 @@ class database_api vector get_lotteries( asset_id_type stop = asset_id_type(), unsigned limit = 100, asset_id_type start = asset_id_type() )const; - + vector get_account_lotteries( account_id_type issuer, + asset_id_type stop, + unsigned limit, + asset_id_type start )const; sweeps_vesting_balance_object get_sweeps_vesting_balance_object( account_id_type account )const; asset get_sweeps_vesting_balance_available_for_claim( account_id_type account )const; /** @@ -755,6 +758,7 @@ FC_API(graphene::app::database_api, // Sweeps (get_lotteries) + (get_account_lotteries) (get_lottery_balance) (get_sweeps_vesting_balance_object) (get_sweeps_vesting_balance_available_for_claim) diff --git a/libraries/chain/asset_evaluator.cpp b/libraries/chain/asset_evaluator.cpp index 4a3a2b1c..cda100f5 100644 --- a/libraries/chain/asset_evaluator.cpp +++ b/libraries/chain/asset_evaluator.cpp @@ -120,7 +120,9 @@ void_result asset_create_evaluator::do_evaluate( const asset_create_operation& o if( op.extensions.which() == asset_extension::tag::value ) { FC_ASSERT( op.common_options.max_supply >= 5 ); - op.extensions.get().validate(); + auto lottery_options = op.extensions.get(); + lottery_options.validate(); + FC_ASSERT( lottery_options.end_date > d.head_block_time() ); } return void_result(); } FC_CAPTURE_AND_RETHROW( (op) ) } diff --git a/libraries/chain/include/graphene/chain/asset_object.hpp b/libraries/chain/include/graphene/chain/asset_object.hpp index 21129ae4..56d83f5c 100644 --- a/libraries/chain/include/graphene/chain/asset_object.hpp +++ b/libraries/chain/include/graphene/chain/asset_object.hpp @@ -118,6 +118,8 @@ namespace graphene { namespace chain { string amount_to_pretty_string(const asset &amount)const { FC_ASSERT(amount.asset_id == id); return amount_to_pretty_string(amount.amount); } + uint32_t get_issuer_num()const + { return issuer.instance.value; } /// Ticker symbol for this asset, i.e. "USD" string symbol; /// Maximum number of digits after the decimal point (must be <= 12) @@ -280,10 +282,12 @@ namespace graphene { namespace chain { composite_key< asset_object, const_mem_fun, + const_mem_fun, member >, composite_key_compare< std::greater< bool >, + std::greater< uint32_t >, std::greater< object_id_type > > >, diff --git a/libraries/chain/protocol/asset_ops.cpp b/libraries/chain/protocol/asset_ops.cpp index 683e9d89..95b97bfd 100644 --- a/libraries/chain/protocol/asset_ops.cpp +++ b/libraries/chain/protocol/asset_ops.cpp @@ -252,6 +252,7 @@ void asset_claim_fees_operation::validate()const { void lottery_asset_options::validate() const { FC_ASSERT( winning_tickets.size() <= 64 ); + FC_ASSERT( ticket_price.amount >= 1 ); uint16_t total = 0; for( auto benefactor : benefactors ) { total += benefactor.share; diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 1cdb895b..43472ad8 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -353,6 +353,10 @@ class wallet_api vector get_lotteries( asset_id_type stop = asset_id_type(), unsigned limit = 100, asset_id_type start = asset_id_type() )const; + vector get_account_lotteries( account_id_type issuer, + asset_id_type stop = asset_id_type(), + unsigned limit = 100, + asset_id_type start = asset_id_type() )const; asset get_lottery_balance( asset_id_type lottery_id ) const; /** Returns the most recent operations on the named account. @@ -1964,6 +1968,7 @@ FC_API( graphene::wallet::wallet_api, (get_asset) (get_bitasset_data) (get_lotteries) + (get_account_lotteries) (get_lottery_balance) (fund_asset_fee_pool) (reserve_asset) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 587cb9b9..517ef91c 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1816,10 +1816,10 @@ public: witness_create_op.block_signing_key = witness_public_key; witness_create_op.url = url; - secret_hash_type::encoder enc; - fc::raw::pack(enc, witness_private_key); - fc::raw::pack(enc, secret_hash_type()); - witness_create_op.initial_secret = secret_hash_type::hash(enc.result()); + // secret_hash_type::encoder enc; + // fc::raw::pack(enc, witness_private_key); + // fc::raw::pack(enc, secret_hash_type()); + witness_create_op.initial_secret = secret_hash_type(); if (_remote_db->get_witness_by_account(witness_create_op.witness_account)) @@ -3513,6 +3513,14 @@ vector wallet_api::get_lotteries( asset_id_type stop, return my->_remote_db->get_lotteries( stop, limit, start ); } +vector wallet_api::get_account_lotteries( account_id_type issuer, + asset_id_type stop, + unsigned limit, + asset_id_type start )const +{ + return my->_remote_db->get_account_lotteries( issuer, stop, limit, start ); +} + asset wallet_api::get_lottery_balance( asset_id_type lottery_id )const { return my->_remote_db->get_lottery_balance( lottery_id );