Skip to content

Commit

Permalink
Merge pull request #340 from pmconrad/bsip-0018
Browse files Browse the repository at this point in the history
Bsip 0018
  • Loading branch information
oxarbitrage authored Sep 6, 2017
2 parents 46f791c + 36e8b77 commit dd8da84
Show file tree
Hide file tree
Showing 28 changed files with 1,109 additions and 346 deletions.
144 changes: 0 additions & 144 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,150 +268,6 @@ namespace graphene { namespace app {
return *_debug_api;
}

vector<account_id_type> get_relevant_accounts( const object* obj )
{
vector<account_id_type> result;
if( obj->id.space() == protocol_ids )
{
switch( (object_type)obj->id.type() )
{
case null_object_type:
case base_object_type:
case OBJECT_TYPE_COUNT:
return result;
case account_object_type:{
result.push_back( obj->id );
break;
} case asset_object_type:{
const auto& aobj = dynamic_cast<const asset_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->issuer );
break;
} case force_settlement_object_type:{
const auto& aobj = dynamic_cast<const force_settlement_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->owner );
break;
} case committee_member_object_type:{
const auto& aobj = dynamic_cast<const committee_member_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->committee_member_account );
break;
} case witness_object_type:{
const auto& aobj = dynamic_cast<const witness_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->witness_account );
break;
} case limit_order_object_type:{
const auto& aobj = dynamic_cast<const limit_order_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->seller );
break;
} case call_order_object_type:{
const auto& aobj = dynamic_cast<const call_order_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->borrower );
break;
} case custom_object_type:{
break;
} case proposal_object_type:{
const auto& aobj = dynamic_cast<const proposal_object*>(obj);
assert( aobj != nullptr );
flat_set<account_id_type> impacted;
transaction_get_impacted_accounts( aobj->proposed_transaction, impacted );
result.reserve( impacted.size() );
for( auto& item : impacted ) result.emplace_back(item);
break;
} case operation_history_object_type:{
const auto& aobj = dynamic_cast<const operation_history_object*>(obj);
assert( aobj != nullptr );
flat_set<account_id_type> impacted;
operation_get_impacted_accounts( aobj->op, impacted );
result.reserve( impacted.size() );
for( auto& item : impacted ) result.emplace_back(item);
break;
} case withdraw_permission_object_type:{
const auto& aobj = dynamic_cast<const withdraw_permission_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->withdraw_from_account );
result.push_back( aobj->authorized_account );
break;
} case vesting_balance_object_type:{
const auto& aobj = dynamic_cast<const vesting_balance_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->owner );
break;
} case worker_object_type:{
const auto& aobj = dynamic_cast<const worker_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->worker_account );
break;
} case balance_object_type:{
/** these are free from any accounts */
break;
}
}
}
else if( obj->id.space() == implementation_ids )
{
switch( (impl_object_type)obj->id.type() )
{
case impl_global_property_object_type:
break;
case impl_dynamic_global_property_object_type:
break;
case impl_reserved0_object_type:
break;
case impl_asset_dynamic_data_type:
break;
case impl_asset_bitasset_data_type:
break;
case impl_account_balance_object_type:{
const auto& aobj = dynamic_cast<const account_balance_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->owner );
break;
} case impl_account_statistics_object_type:{
const auto& aobj = dynamic_cast<const account_statistics_object*>(obj);
assert( aobj != nullptr );
result.push_back( aobj->owner );
break;
} case impl_transaction_object_type:{
const auto& aobj = dynamic_cast<const transaction_object*>(obj);
assert( aobj != nullptr );
flat_set<account_id_type> impacted;
transaction_get_impacted_accounts( aobj->trx, impacted );
result.reserve( impacted.size() );
for( auto& item : impacted ) result.emplace_back(item);
break;
} case impl_blinded_balance_object_type:{
const auto& aobj = dynamic_cast<const blinded_balance_object*>(obj);
assert( aobj != nullptr );
result.reserve( aobj->owner.account_auths.size() );
for( const auto& a : aobj->owner.account_auths )
result.push_back( a.first );
break;
} case impl_block_summary_object_type:
break;
case impl_account_transaction_history_object_type:
break;
case impl_chain_property_object_type:
break;
case impl_witness_schedule_object_type:
break;
case impl_budget_record_object_type:
break;
case impl_special_authority_object_type:
break;
case impl_buyback_object_type:
break;
case impl_fba_accumulator_object_type:
break;
}
}
return result;
} // end get_relevant_accounts( obj )

vector<order_history_object> history_api::get_fill_order_history( asset_id_type a, asset_id_type b, uint32_t limit )const
{
FC_ASSERT(_app.chain_database());
Expand Down
27 changes: 27 additions & 0 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
vector<call_order_object> get_call_orders(asset_id_type a, uint32_t limit)const;
vector<force_settlement_object> get_settle_orders(asset_id_type a, uint32_t limit)const;
vector<call_order_object> get_margin_positions( const account_id_type& id )const;
vector<collateral_bid_object> get_collateral_bids(const asset_id_type asset, uint32_t limit, uint32_t start)const;
void subscribe_to_market(std::function<void(const variant&)> callback, asset_id_type a, asset_id_type b);
void unsubscribe_from_market(asset_id_type a, asset_id_type b);
market_ticker get_ticker( const string& base, const string& quote )const;
Expand Down Expand Up @@ -1096,6 +1097,32 @@ vector<call_order_object> database_api_impl::get_margin_positions( const account
} FC_CAPTURE_AND_RETHROW( (id) )
}

vector<collateral_bid_object> database_api::get_collateral_bids(const asset_id_type asset, uint32_t limit, uint32_t start)const
{
return my->get_collateral_bids( asset, limit, start );
}

vector<collateral_bid_object> database_api_impl::get_collateral_bids(const asset_id_type asset_id, uint32_t limit, uint32_t skip)const
{ try {
FC_ASSERT( limit <= 100 );
const asset_object& swan = asset_id(_db);
FC_ASSERT( swan.is_market_issued() );
const asset_bitasset_data_object& bad = swan.bitasset_data(_db);
const asset_object& back = bad.options.short_backing_asset(_db);
const auto& idx = _db.get_index_type<collateral_bid_index>();
const auto& aidx = idx.indices().get<by_price>();
auto start = aidx.lower_bound( boost::make_tuple( asset_id, price::max(back.id, asset_id), collateral_bid_id_type() ) );
auto end = aidx.lower_bound( boost::make_tuple( asset_id, price::min(back.id, asset_id), collateral_bid_id_type(GRAPHENE_DB_MAX_INSTANCE_ID) ) );
vector<collateral_bid_object> result;
while( skip-- > 0 && start != end ) { ++start; }
while( start != end && limit-- > 0)
{
result.push_back(*start);
++start;
}
return result;
} FC_CAPTURE_AND_RETHROW( (asset_id)(limit)(skip) ) }

void database_api::subscribe_to_market(std::function<void(const variant&)> callback, asset_id_type a, asset_id_type b)
{
my->subscribe_to_market( callback, a, b );
Expand Down
8 changes: 8 additions & 0 deletions libraries/app/impacted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ struct get_impacted_account_visitor
_impacted.insert( op.fee_paying_account );
}
void operator()( const call_order_update_operation& op ) {}
void operator()( const bid_collateral_operation& op )
{
_impacted.insert( op.bidder );
}
void operator()( const fill_order_operation& op )
{
_impacted.insert( op.account_id );
}
void operator()( const execute_bid_operation& op )
{
_impacted.insert( op.bidder );
}

void operator()( const account_create_operation& op )
{
Expand Down
12 changes: 11 additions & 1 deletion libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
* Copyright (c) 2017 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
Expand Down Expand Up @@ -367,6 +367,15 @@ class database_api
*/
vector<force_settlement_object> get_settle_orders(asset_id_type a, uint32_t limit)const;

/**
* @brief Get collateral_bid_objects for a given asset
* @param a ID of asset
* @param limit Maximum number of objects to retrieve
* @param start skip that many results
* @return The settle orders, ordered from earliest settlement date to latest
*/
vector<collateral_bid_object> get_collateral_bids(const asset_id_type asset, uint32_t limit, uint32_t start)const;

/**
* @return all open margin positions for a given account id.
*/
Expand Down Expand Up @@ -663,6 +672,7 @@ FC_API(graphene::app::database_api,
(get_call_orders)
(get_settle_orders)
(get_margin_positions)
(get_collateral_bids)
(subscribe_to_market)
(unsubscribe_from_market)
(get_ticker)
Expand Down
29 changes: 24 additions & 5 deletions libraries/chain/asset_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ void_result asset_settle_evaluator::do_evaluate(const asset_settle_evaluator::op
FC_ASSERT(asset_to_settle->can_force_settle() || bitasset.has_settlement() );
if( bitasset.is_prediction_market )
FC_ASSERT( bitasset.has_settlement(), "global settlement must occur before force settling a prediction market" );
else if( bitasset.current_feed.settlement_price.is_null() )
else if( bitasset.current_feed.settlement_price.is_null()
&& ( d.head_block_time() <= HARDFORK_CORE_216_TIME
|| !bitasset.has_settlement() ) )
FC_THROW_EXCEPTION(insufficient_feeds, "Cannot force settle with no price feed.");
FC_ASSERT(d.get_balance(d.get(op.account), *asset_to_settle) >= op.amount);

Expand All @@ -462,17 +464,20 @@ operation_result asset_settle_evaluator::do_apply(const asset_settle_evaluator::
const auto& bitasset = asset_to_settle->bitasset_data(d);
if( bitasset.has_settlement() )
{
const auto& mia_dyn = asset_to_settle->dynamic_asset_data_id(d);

auto settled_amount = op.amount * bitasset.settlement_price;
FC_ASSERT( settled_amount.amount <= bitasset.settlement_fund );
if( op.amount.amount == mia_dyn.current_supply )
settled_amount.amount = bitasset.settlement_fund; // avoid rounding problems
else
FC_ASSERT( settled_amount.amount <= bitasset.settlement_fund ); // should be strictly < except for PM with zero outcome

d.modify( bitasset, [&]( asset_bitasset_data_object& obj ){
obj.settlement_fund -= settled_amount.amount;
});

d.adjust_balance(op.account, settled_amount);

const auto& mia_dyn = asset_to_settle->dynamic_asset_data_id(d);

d.modify( mia_dyn, [&]( asset_dynamic_data_object& obj ){
obj.current_supply -= op.amount.amount;
});
Expand All @@ -498,7 +503,10 @@ void_result asset_publish_feeds_evaluator::do_evaluate(const asset_publish_feed_
FC_ASSERT(base.is_market_issued());

const asset_bitasset_data_object& bitasset = base.bitasset_data(d);
FC_ASSERT( !bitasset.has_settlement(), "No further feeds may be published after a settlement event" );
if( bitasset.is_prediction_market || d.head_block_time() <= HARDFORK_CORE_216_TIME )
{
FC_ASSERT( !bitasset.has_settlement(), "No further feeds may be published after a settlement event" );
}

FC_ASSERT( o.feed.settlement_price.quote.asset_id == bitasset.options.short_backing_asset );
if( d.head_block_time() > HARDFORK_480_TIME )
Expand Down Expand Up @@ -549,7 +557,18 @@ void_result asset_publish_feeds_evaluator::do_apply(const asset_publish_feed_ope
});

if( !(old_feed == bad.current_feed) )
{
if( bad.has_settlement() ) // implies head_block_time > HARDFORK_CORE_216_TIME
{
const auto& mia_dyn = base.dynamic_asset_data_id(d);
if( !bad.current_feed.settlement_price.is_null()
&& ~price::call_price(asset(mia_dyn.current_supply, o.asset_id),
asset(bad.settlement_fund, bad.options.short_backing_asset),
bad.current_feed.maintenance_collateral_ratio ) < bad.current_feed.settlement_price )
d.revive_bitasset(base);
}
db().check_call_orders(base);
}

return void_result();
} FC_CAPTURE_AND_RETHROW((o)) }
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/db_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void database::debug_dump()

const auto& balance_index = db.get_index_type<account_balance_index>().indices();
const simple_index<account_statistics_object>& statistics_index = db.get_index_type<simple_index<account_statistics_object>>();
const auto& bids = db.get_index_type<collateral_bid_index>().indices();
map<asset_id_type,share_type> total_balances;
map<asset_id_type,share_type> total_debts;
share_type core_in_orders;
Expand All @@ -58,6 +59,8 @@ void database::debug_dump()
// idump(("statistics")(s));
reported_core_in_orders += s.total_core_in_orders;
}
for( const collateral_bid_object& b : bids )
total_balances[b.inv_swan_price.base.asset_id] += b.inv_swan_price.base.amount;
for( const limit_order_object& o : db.get_index_type<limit_order_index>().indices() )
{
// idump(("limit_order")(o));
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/db_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void database::initialize_evaluators()
register_evaluator<limit_order_create_evaluator>();
register_evaluator<limit_order_cancel_evaluator>();
register_evaluator<call_order_update_evaluator>();
register_evaluator<bid_collateral_evaluator>();
register_evaluator<transfer_evaluator>();
register_evaluator<override_transfer_evaluator>();
register_evaluator<asset_fund_fee_pool_evaluator>();
Expand Down Expand Up @@ -214,6 +215,7 @@ void database::initialize_indexes()
add_index< primary_index<simple_index<budget_record_object > > >();
add_index< primary_index< special_authority_index > >();
add_index< primary_index< buyback_index > >();
add_index< primary_index<collateral_bid_index > >();

add_index< primary_index< simple_index< fba_accumulator_object > > >();
}
Expand Down
Loading

0 comments on commit dd8da84

Please sign in to comment.