peerplays_migrated/libraries/app/api.cpp

1311 lines
52 KiB
C++
Raw Normal View History

2015-06-08 15:50:35 +00:00
/*
* Copyright (c) 2015, Cryptonomex, Inc.
* All rights reserved.
*
* This source code is provided for evaluation in private test networks only, until September 8, 2015. After this date, this license expires and
* the code may not be used, modified or distributed for any purpose. Redistribution and use in source and binary forms, with or without modification,
* are permitted until September 8, 2015, provided that the following conditions are met:
*
* 1. The code and/or derivative works are used only for private test networks consisting of no more than 10 P2P nodes.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cctype>
2015-06-08 15:50:35 +00:00
#include <graphene/app/api.hpp>
#include <graphene/app/api_access.hpp>
2015-06-08 15:50:35 +00:00
#include <graphene/app/application.hpp>
#include <graphene/app/impacted.hpp>
2015-06-08 15:50:35 +00:00
#include <graphene/chain/database.hpp>
#include <graphene/chain/get_config.hpp>
2015-06-08 15:50:35 +00:00
#include <graphene/utilities/key_conversion.hpp>
#include <graphene/chain/protocol/fee_schedule.hpp>
#include <graphene/chain/withdraw_permission_object.hpp>
#include <graphene/chain/worker_evaluator.hpp>
#include <graphene/chain/transaction_object.hpp>
#include <graphene/chain/confidential_evaluator.hpp>
2015-06-08 15:50:35 +00:00
#include <fc/crypto/hex.hpp>
#include <fc/smart_ref_impl.hpp>
2015-06-08 15:50:35 +00:00
namespace graphene { namespace app {
database_api::database_api(graphene::chain::database& db):_db(db)
{
2015-07-30 13:20:49 +00:00
wlog("creating database api ${x}", ("x",int64_t(this)) );
2015-06-08 15:50:35 +00:00
_change_connection = _db.changed_objects.connect([this](const vector<object_id_type>& ids) {
on_objects_changed(ids);
});
_removed_connection = _db.removed_objects.connect([this](const vector<const object*>& objs) {
on_objects_removed(objs);
});
2015-06-08 15:50:35 +00:00
_applied_block_connection = _db.applied_block.connect([this](const signed_block&){ on_applied_block(); });
_pending_trx_connection = _db.on_pending_transaction.connect([this](const signed_transaction& trx ){
if( _pending_trx_callback ) _pending_trx_callback( fc::variant(trx) );
});
2015-06-08 15:50:35 +00:00
}
2015-07-30 13:20:49 +00:00
database_api::~database_api()
{
elog("freeing database api ${x}", ("x",int64_t(this)) );
}
2015-08-26 22:01:48 +00:00
void database_api::set_subscribe_callback( std::function<void(const variant&)> cb, bool clear_filter )
{
edump((clear_filter));
_subscribe_callback = cb;
if( clear_filter || !cb )
{
static fc::bloom_parameters param;
param.projected_element_count = 10000;
param.false_positive_probability = 1.0/10000;
param.maximum_size = 1024*8*8*2;
param.compute_optimal_parameters();
2015-08-26 22:01:48 +00:00
_subscribe_filter = fc::bloom_filter(param);
}
}
2015-06-08 15:50:35 +00:00
fc::variants database_api::get_objects(const vector<object_id_type>& ids)const
{
2015-08-26 22:01:48 +00:00
if( _subscribe_callback ) {
for( auto id : ids )
{
if( id.type() == operation_history_object_type && id.space() == protocol_ids ) continue;
if( id.type() == impl_account_transaction_history_object_type && id.space() == implementation_ids ) continue;
2015-08-28 17:58:49 +00:00
this->subscribe_to_item( id );
2015-08-26 22:01:48 +00:00
}
}
else
{
elog( "getObjects without subscribe callback??" );
}
2015-06-08 15:50:35 +00:00
fc::variants result;
result.reserve(ids.size());
std::transform(ids.begin(), ids.end(), std::back_inserter(result),
[this](object_id_type id) -> fc::variant {
if(auto obj = _db.find_object(id))
return obj->to_variant();
return {};
});
return result;
}
optional<block_header> database_api::get_block_header(uint32_t block_num) const
{
auto result = _db.fetch_block_by_number(block_num);
if(result)
return *result;
return {};
}
optional<signed_block> database_api::get_block(uint32_t block_num)const
{
return _db.fetch_block_by_number(block_num);
}
2015-06-29 22:48:47 +00:00
processed_transaction database_api::get_transaction(uint32_t block_num, uint32_t trx_num)const
{
auto opt_block = _db.fetch_block_by_number(block_num);
FC_ASSERT( opt_block );
FC_ASSERT( opt_block->transactions.size() > trx_num );
return opt_block->transactions[trx_num];
}
2015-06-08 15:50:35 +00:00
vector<optional<account_object>> database_api::lookup_account_names(const vector<string>& account_names)const
{
const auto& accounts_by_name = _db.get_index_type<account_index>().indices().get<by_name>();
vector<optional<account_object> > result;
result.reserve(account_names.size());
std::transform(account_names.begin(), account_names.end(), std::back_inserter(result),
[&accounts_by_name](const string& name) -> optional<account_object> {
auto itr = accounts_by_name.find(name);
return itr == accounts_by_name.end()? optional<account_object>() : *itr;
});
return result;
}
vector<optional<asset_object>> database_api::lookup_asset_symbols(const vector<string>& symbols_or_ids)const
2015-06-08 15:50:35 +00:00
{
const auto& assets_by_symbol = _db.get_index_type<asset_index>().indices().get<by_symbol>();
vector<optional<asset_object> > result;
result.reserve(symbols_or_ids.size());
std::transform(symbols_or_ids.begin(), symbols_or_ids.end(), std::back_inserter(result),
[this, &assets_by_symbol](const string& symbol_or_id) -> optional<asset_object> {
if( !symbol_or_id.empty() && std::isdigit(symbol_or_id[0]) )
{
auto ptr = _db.find(variant(symbol_or_id).as<asset_id_type>());
return ptr == nullptr? optional<asset_object>() : *ptr;
}
auto itr = assets_by_symbol.find(symbol_or_id);
2015-06-08 15:50:35 +00:00
return itr == assets_by_symbol.end()? optional<asset_object>() : *itr;
});
return result;
}
chain_property_object database_api::get_chain_properties()const
{
return _db.get(chain_property_id_type());
}
2015-06-08 15:50:35 +00:00
global_property_object database_api::get_global_properties()const
{
return _db.get(global_property_id_type());
}
fc::variant_object database_api::get_config()const
{
return get_config();
}
2015-08-06 16:41:45 +00:00
chain_id_type database_api::get_chain_id()const
{
return _db.get_chain_id();
}
2015-06-08 15:50:35 +00:00
dynamic_global_property_object database_api::get_dynamic_global_properties()const
{
return _db.get(dynamic_global_property_id_type());
}
vector<optional<account_object>> database_api::get_accounts(const vector<account_id_type>& account_ids)const
{
vector<optional<account_object>> result; result.reserve(account_ids.size());
std::transform(account_ids.begin(), account_ids.end(), std::back_inserter(result),
[this](account_id_type id) -> optional<account_object> {
if(auto o = _db.find(id))
2015-08-26 22:01:48 +00:00
{
2015-08-28 17:58:49 +00:00
subscribe_to_item( id );
2015-06-08 15:50:35 +00:00
return *o;
2015-08-26 22:01:48 +00:00
}
2015-06-08 15:50:35 +00:00
return {};
});
return result;
}
vector<optional<asset_object>> database_api::get_assets(const vector<asset_id_type>& asset_ids)const
{
vector<optional<asset_object>> result; result.reserve(asset_ids.size());
std::transform(asset_ids.begin(), asset_ids.end(), std::back_inserter(result),
[this](asset_id_type id) -> optional<asset_object> {
if(auto o = _db.find(id))
2015-08-26 22:01:48 +00:00
{
2015-08-28 17:58:49 +00:00
subscribe_to_item( id );
2015-06-08 15:50:35 +00:00
return *o;
2015-08-26 22:01:48 +00:00
}
2015-06-08 15:50:35 +00:00
return {};
});
return result;
}
uint64_t database_api::get_account_count()const
{
return _db.get_index_type<account_index>().indices().size();
}
map<string,account_id_type> database_api::lookup_accounts(const string& lower_bound_name, uint32_t limit)const
{
FC_ASSERT( limit <= 1000 );
const auto& accounts_by_name = _db.get_index_type<account_index>().indices().get<by_name>();
map<string,account_id_type> result;
for( auto itr = accounts_by_name.lower_bound(lower_bound_name);
limit-- && itr != accounts_by_name.end();
++itr )
2015-08-26 22:01:48 +00:00
{
2015-06-08 15:50:35 +00:00
result.insert(make_pair(itr->name, itr->get_id()));
2015-08-26 22:01:48 +00:00
if( limit == 1 )
2015-08-28 17:58:49 +00:00
subscribe_to_item( itr->get_id() );
2015-08-26 22:01:48 +00:00
}
2015-06-08 15:50:35 +00:00
return result;
}
2015-08-26 22:01:48 +00:00
std::map<std::string, full_account> database_api::get_full_accounts( const vector<std::string>& names_or_ids, bool subscribe)
{
2015-09-03 21:42:52 +00:00
idump((names_or_ids));
std::map<std::string, full_account> results;
for (const std::string& account_name_or_id : names_or_ids)
{
const account_object* account = nullptr;
if (std::isdigit(account_name_or_id[0]))
account = _db.find(fc::variant(account_name_or_id).as<account_id_type>());
else
{
const auto& idx = _db.get_index_type<account_index>().indices().get<by_name>();
auto itr = idx.find(account_name_or_id);
if (itr != idx.end())
account = &*itr;
}
if (account == nullptr)
continue;
2015-07-30 13:20:49 +00:00
if( subscribe )
{
ilog( "subscribe to ${id}", ("id",account->name) );
2015-08-28 17:58:49 +00:00
subscribe_to_item( account->id );
2015-07-30 13:20:49 +00:00
}
// fc::mutable_variant_object full_account;
full_account acnt;
acnt.account = *account;
acnt.statistics = account->statistics(_db);
acnt.registrar_name = account->registrar(_db).name;
acnt.referrer_name = account->referrer(_db).name;
acnt.lifetime_referrer_name = account->lifetime_referrer(_db).name;
acnt.votes = lookup_vote_ids( vector<vote_id_type>(account->options.votes.begin(),account->options.votes.end()) );
// Add the account itself, its statistics object, cashback balance, and referral account names
/*
full_account("account", *account)("statistics", account->statistics(_db))
("registrar_name", account->registrar(_db).name)("referrer_name", account->referrer(_db).name)
("lifetime_referrer_name", account->lifetime_referrer(_db).name);
*/
if (account->cashback_vb)
{
acnt.cashback_balance = account->cashback_balance(_db);
}
// Add the account's proposals
const auto& proposal_idx = _db.get_index_type<proposal_index>();
const auto& pidx = dynamic_cast<const primary_index<proposal_index>&>(proposal_idx);
const auto& proposals_by_account = pidx.get_secondary_index<graphene::chain::required_approval_index>();
auto required_approvals_itr = proposals_by_account._account_to_proposals.find( account->id );
if( required_approvals_itr != proposals_by_account._account_to_proposals.end() )
{
acnt.proposals.reserve( required_approvals_itr->second.size() );
for( auto proposal_id : required_approvals_itr->second )
acnt.proposals.push_back( proposal_id(_db) );
}
// Add the account's balances
auto balance_range = _db.get_index_type<account_balance_index>().indices().get<by_account>().equal_range(account->id);
//vector<account_balance_object> balances;
std::for_each(balance_range.first, balance_range.second,
[&acnt](const account_balance_object& balance) {
acnt.balances.emplace_back(balance);
});
// Add the account's vesting balances
auto vesting_range = _db.get_index_type<vesting_balance_index>().indices().get<by_account>().equal_range(account->id);
std::for_each(vesting_range.first, vesting_range.second,
[&acnt](const vesting_balance_object& balance) {
acnt.vesting_balances.emplace_back(balance);
});
// Add the account's orders
auto order_range = _db.get_index_type<limit_order_index>().indices().get<by_account>().equal_range(account->id);
std::for_each(order_range.first, order_range.second,
[&acnt] (const limit_order_object& order) {
acnt.limit_orders.emplace_back(order);
});
auto call_range = _db.get_index_type<call_order_index>().indices().get<by_account>().equal_range(account->id);
std::for_each(call_range.first, call_range.second,
[&acnt] (const call_order_object& call) {
acnt.call_orders.emplace_back(call);
});
results[account_name_or_id] = acnt;
}
return results;
}
2015-06-08 15:50:35 +00:00
vector<asset> database_api::get_account_balances(account_id_type acnt, const flat_set<asset_id_type>& assets)const
{
vector<asset> result;
if (assets.empty())
{
// if the caller passes in an empty list of assets, return balances for all assets the account owns
const account_balance_index& balance_index = _db.get_index_type<account_balance_index>();
auto range = balance_index.indices().get<by_account>().equal_range(acnt);
for (const account_balance_object& balance : boost::make_iterator_range(range.first, range.second))
result.push_back(asset(balance.get_balance()));
}
else
{
result.reserve(assets.size());
std::transform(assets.begin(), assets.end(), std::back_inserter(result),
[this, acnt](asset_id_type id) { return _db.get_balance(acnt, id); });
}
2015-06-08 15:50:35 +00:00
return result;
}
vector<asset> database_api::get_named_account_balances(const std::string& name, const flat_set<asset_id_type>& assets) const
{
const auto& accounts_by_name = _db.get_index_type<account_index>().indices().get<by_name>();
auto itr = accounts_by_name.find(name);
FC_ASSERT( itr != accounts_by_name.end() );
return get_account_balances(itr->get_id(), assets);
}
/**
* @return the limit orders for both sides of the book for the two assets specified up to limit number on each side.
*/
vector<limit_order_object> database_api::get_limit_orders(asset_id_type a, asset_id_type b, uint32_t limit)const
{
const auto& limit_order_idx = _db.get_index_type<limit_order_index>();
const auto& limit_price_idx = limit_order_idx.indices().get<by_price>();
vector<limit_order_object> result;
uint32_t count = 0;
auto limit_itr = limit_price_idx.lower_bound(price::max(a,b));
auto limit_end = limit_price_idx.upper_bound(price::min(a,b));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}
count = 0;
limit_itr = limit_price_idx.lower_bound(price::max(b,a));
limit_end = limit_price_idx.upper_bound(price::min(b,a));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}
return result;
}
vector<call_order_object> database_api::get_call_orders(asset_id_type a, uint32_t limit)const
{
const auto& call_index = _db.get_index_type<call_order_index>().indices().get<by_price>();
const asset_object& mia = _db.get(a);
price index_price = price::min(mia.bitasset_data(_db).options.short_backing_asset, mia.get_id());
return vector<call_order_object>(call_index.lower_bound(index_price.min()),
call_index.lower_bound(index_price.max()));
}
vector<force_settlement_object> database_api::get_settle_orders(asset_id_type a, uint32_t limit)const
{
const auto& settle_index = _db.get_index_type<force_settlement_index>().indices().get<by_expiration>();
const asset_object& mia = _db.get(a);
return vector<force_settlement_object>(settle_index.lower_bound(mia.get_id()),
settle_index.upper_bound(mia.get_id()));
}
vector<asset_object> database_api::list_assets(const string& lower_bound_symbol, uint32_t limit)const
{
FC_ASSERT( limit <= 100 );
const auto& assets_by_symbol = _db.get_index_type<asset_index>().indices().get<by_symbol>();
vector<asset_object> result;
result.reserve(limit);
auto itr = assets_by_symbol.lower_bound(lower_bound_symbol);
if( lower_bound_symbol == "" )
itr = assets_by_symbol.begin();
2015-06-08 15:50:35 +00:00
while(limit-- && itr != assets_by_symbol.end())
result.emplace_back(*itr++);
return result;
}
fc::optional<committee_member_object> database_api::get_committee_member_by_account(account_id_type account) const
{
const auto& idx = _db.get_index_type<committee_member_index>().indices().get<by_account>();
auto itr = idx.find(account);
if( itr != idx.end() )
return *itr;
return {};
}
fc::optional<witness_object> database_api::get_witness_by_account(account_id_type account) const
{
const auto& idx = _db.get_index_type<witness_index>().indices().get<by_account>();
auto itr = idx.find(account);
if( itr != idx.end() )
return *itr;
return {};
}
2015-08-13 13:22:05 +00:00
vector<variant> database_api::lookup_vote_ids( const vector<vote_id_type>& votes )const
{
FC_ASSERT( votes.size() < 100, "Only 100 votes can be queried at a time" );
const auto& witness_idx = _db.get_index_type<witness_index>().indices().get<by_vote_id>();
const auto& committee_idx = _db.get_index_type<committee_member_index>().indices().get<by_vote_id>();
vector<variant> result;
result.reserve( votes.size() );
for( auto id : votes )
{
switch( id.type() )
{
case vote_id_type::committee:
{
auto itr = committee_idx.find( id );
if( itr != committee_idx.end() )
result.emplace_back( variant( *itr ) );
else
result.emplace_back( variant() );
break;
}
case vote_id_type::witness:
{
auto itr = witness_idx.find( id );
if( itr != witness_idx.end() )
result.emplace_back( variant( *itr ) );
else
result.emplace_back( variant() );
break;
}
case vote_id_type::worker:
break;
case vote_id_type::VOTE_TYPE_COUNT: break; // supress unused enum value warnings
}
}
return result;
}
uint64_t database_api::get_witness_count()const
{
return _db.get_index_type<witness_index>().indices().size();
}
map<string, witness_id_type> database_api::lookup_witness_accounts(const string& lower_bound_name, uint32_t limit)const
{
FC_ASSERT( limit <= 1000 );
const auto& witnesses_by_id = _db.get_index_type<witness_index>().indices().get<by_id>();
// we want to order witnesses by account name, but that name is in the account object
// so the witness_index doesn't have a quick way to access it.
// get all the names and look them all up, sort them, then figure out what
// records to return. This could be optimized, but we expect the
// number of witnesses to be few and the frequency of calls to be rare
std::map<std::string, witness_id_type> witnesses_by_account_name;
for (const witness_object& witness : witnesses_by_id)
if (auto account_iter = _db.find(witness.witness_account))
if (account_iter->name >= lower_bound_name) // we can ignore anything below lower_bound_name
witnesses_by_account_name.insert(std::make_pair(account_iter->name, witness.id));
auto end_iter = witnesses_by_account_name.begin();
while (end_iter != witnesses_by_account_name.end() && limit--)
++end_iter;
witnesses_by_account_name.erase(end_iter, witnesses_by_account_name.end());
return witnesses_by_account_name;
}
map<string, committee_member_id_type> database_api::lookup_committee_member_accounts(const string& lower_bound_name, uint32_t limit)const
{
FC_ASSERT( limit <= 1000 );
const auto& committee_members_by_id = _db.get_index_type<committee_member_index>().indices().get<by_id>();
// we want to order committee_members by account name, but that name is in the account object
// so the committee_member_index doesn't have a quick way to access it.
// get all the names and look them all up, sort them, then figure out what
// records to return. This could be optimized, but we expect the
// number of committee_members to be few and the frequency of calls to be rare
std::map<std::string, committee_member_id_type> committee_members_by_account_name;
for (const committee_member_object& committee_member : committee_members_by_id)
if (auto account_iter = _db.find(committee_member.committee_member_account))
if (account_iter->name >= lower_bound_name) // we can ignore anything below lower_bound_name
committee_members_by_account_name.insert(std::make_pair(account_iter->name, committee_member.id));
auto end_iter = committee_members_by_account_name.begin();
while (end_iter != committee_members_by_account_name.end() && limit--)
++end_iter;
committee_members_by_account_name.erase(end_iter, committee_members_by_account_name.end());
return committee_members_by_account_name;
}
vector<optional<witness_object>> database_api::get_witnesses(const vector<witness_id_type>& witness_ids)const
{
vector<optional<witness_object>> result; result.reserve(witness_ids.size());
std::transform(witness_ids.begin(), witness_ids.end(), std::back_inserter(result),
[this](witness_id_type id) -> optional<witness_object> {
if(auto o = _db.find(id))
return *o;
return {};
});
return result;
}
vector<optional<committee_member_object>> database_api::get_committee_members(const vector<committee_member_id_type>& committee_member_ids)const
{
vector<optional<committee_member_object>> result; result.reserve(committee_member_ids.size());
std::transform(committee_member_ids.begin(), committee_member_ids.end(), std::back_inserter(result),
[this](committee_member_id_type id) -> optional<committee_member_object> {
if(auto o = _db.find(id))
return *o;
return {};
});
return result;
}
2015-06-08 15:50:35 +00:00
login_api::login_api(application& a)
:_app(a)
{
}
2015-06-08 15:50:35 +00:00
login_api::~login_api()
{
}
bool login_api::login(const string& user, const string& password)
{
optional< api_access_info > acc = _app.get_api_access_info( user );
if( !acc.valid() )
return false;
if( acc->password_hash_b64 != "*" )
{
std::string password_salt = fc::base64_decode( acc->password_salt_b64 );
std::string acc_password_hash = fc::base64_decode( acc->password_hash_b64 );
fc::sha256 hash_obj = fc::sha256::hash( password + password_salt );
if( hash_obj.data_size() != acc_password_hash.length() )
return false;
if( memcmp( hash_obj.data(), acc_password_hash.c_str(), hash_obj.data_size() ) != 0 )
return false;
}
for( const std::string& api_name : acc->allowed_apis )
enable_api( api_name );
2015-06-08 15:50:35 +00:00
return true;
}
void login_api::enable_api( const std::string& api_name )
{
if( api_name == "database_api" )
{
_database_api = std::make_shared< database_api >( std::ref( *_app.chain_database() ) );
}
else if( api_name == "network_broadcast_api" )
{
_network_broadcast_api = std::make_shared< network_broadcast_api >( std::ref( _app ) );
}
else if( api_name == "history_api" )
{
_history_api = std::make_shared< history_api >( _app );
}
else if( api_name == "network_node_api" )
{
_network_node_api = std::make_shared< network_node_api >( std::ref(_app) );
}
return;
}
network_broadcast_api::network_broadcast_api(application& a):_app(a)
{
_applied_block_connection = _app.chain_database()->applied_block.connect([this](const signed_block& b){ on_applied_block(b); });
}
void network_broadcast_api::on_applied_block( const signed_block& b )
{
if( _callbacks.size() )
{
/// we need to ensure the database_api is not deleted for the life of the async operation
auto capture_this = shared_from_this();
for( uint32_t trx_num = 0; trx_num < b.transactions.size(); ++trx_num )
{
const auto& trx = b.transactions[trx_num];
auto id = trx.id();
auto itr = _callbacks.find(id);
if( itr != _callbacks.end() )
{
auto block_num = b.block_num();
auto& callback = _callbacks.find(id)->second;
fc::async( [capture_this,this,id,block_num,trx_num,trx,callback](){ callback( fc::variant(transaction_confirmation{ id, block_num, trx_num, trx}) ); } );
}
}
}
}
void network_broadcast_api::broadcast_transaction(const signed_transaction& trx)
2015-06-08 15:50:35 +00:00
{
trx.validate();
_app.chain_database()->push_transaction(trx);
_app.p2p_node()->broadcast_transaction(trx);
}
void network_broadcast_api::broadcast_block( const signed_block& b )
{
_app.chain_database()->push_block(b);
_app.p2p_node()->broadcast( net::block_message( b ));
}
void network_broadcast_api::broadcast_transaction_with_callback(confirmation_callback cb, const signed_transaction& trx)
{
trx.validate();
_callbacks[trx.id()] = cb;
_app.chain_database()->push_transaction(trx);
_app.p2p_node()->broadcast_transaction(trx);
}
network_node_api::network_node_api( application& a ) : _app( a )
{
}
void network_node_api::add_node(const fc::ip::endpoint& ep)
{
_app.p2p_node()->add_node(ep);
}
2015-06-08 15:50:35 +00:00
std::vector<net::peer_status> network_node_api::get_connected_peers() const
2015-06-08 15:50:35 +00:00
{
return _app.p2p_node()->get_connected_peers();
}
fc::api<network_broadcast_api> login_api::network_broadcast()const
{
FC_ASSERT(_network_broadcast_api);
return *_network_broadcast_api;
}
fc::api<network_node_api> login_api::network_node()const
2015-06-08 15:50:35 +00:00
{
FC_ASSERT(_network_node_api);
return *_network_node_api;
2015-06-08 15:50:35 +00:00
}
fc::api<database_api> login_api::database()const
{
FC_ASSERT(_database_api);
return *_database_api;
}
fc::api<history_api> login_api::history() const
{
FC_ASSERT(_history_api);
return *_history_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:{
} 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 */
}
}
}
else if( obj->id.space() == implementation_ids )
{
switch( (impl_object_type)obj->id.type() )
{
case impl_global_property_object_type:{
} case impl_dynamic_global_property_object_type:{
} case impl_index_meta_object_type:{
} case impl_asset_dynamic_data_type:{
} 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:{
} case impl_account_transaction_history_object_type:{
} case impl_chain_property_object_type: {
}
}
}
return result;
} // end get_relevant_accounts( obj )
2015-09-03 21:42:52 +00:00
void database_api::broadcast_updates( const vector<variant>& updates )
{
if( updates.size() ) {
auto capture_this = shared_from_this();
fc::async([capture_this,updates](){
capture_this->_subscribe_callback( fc::variant(updates) );
});
}
}
void database_api::on_objects_removed( const vector<const object*>& objs )
{
/// we need to ensure the database_api is not deleted for the life of the async operation
2015-08-26 22:01:48 +00:00
if( _subscribe_callback )
{
2015-09-03 21:42:52 +00:00
vector<variant> updates;
updates.reserve(objs.size());
2015-09-03 21:42:52 +00:00
for( auto obj : objs )
updates.emplace_back( obj->id );
broadcast_updates( updates );
}
2015-08-26 22:01:48 +00:00
if( _market_subscriptions.size() )
{
map< pair<asset_id_type, asset_id_type>, vector<variant> > broadcast_queue;
for( const auto& obj : objs )
{
const limit_order_object* order = dynamic_cast<const limit_order_object*>(obj);
if( order )
{
auto sub = _market_subscriptions.find( order->get_market() );
if( sub != _market_subscriptions.end() )
broadcast_queue[order->get_market()].emplace_back( order->id );
}
}
if( broadcast_queue.size() )
{
2015-09-03 21:42:52 +00:00
auto capture_this = shared_from_this();
fc::async([capture_this,this,broadcast_queue](){
for( const auto& item : broadcast_queue )
{
auto sub = _market_subscriptions.find(item.first);
if( sub != _market_subscriptions.end() )
sub->second( fc::variant(item.second ) );
}
});
}
}
}
2015-06-08 15:50:35 +00:00
void database_api::on_objects_changed(const vector<object_id_type>& ids)
{
2015-08-26 22:01:48 +00:00
vector<variant> updates;
map< pair<asset_id_type, asset_id_type>, vector<variant> > market_broadcast_queue;
2015-08-26 22:01:48 +00:00
2015-06-08 15:50:35 +00:00
for(auto id : ids)
{
const object* obj = nullptr;
2015-08-26 22:01:48 +00:00
if( _subscribe_callback )
2015-06-08 15:50:35 +00:00
{
obj = _db.find_object( id );
if( obj )
2015-06-08 15:50:35 +00:00
{
2015-09-03 21:42:52 +00:00
updates.emplace_back( obj->to_variant() );
2015-06-08 15:50:35 +00:00
}
2015-07-30 20:45:14 +00:00
else
2015-08-26 22:01:48 +00:00
{
2015-09-03 21:42:52 +00:00
updates.emplace_back(id); // send just the id to indicate removal
2015-08-26 22:01:48 +00:00
}
}
if( _market_subscriptions.size() )
{
2015-08-26 22:01:48 +00:00
if( !_subscribe_callback )
obj = _db.find_object( id );
if( obj )
{
const limit_order_object* order = dynamic_cast<const limit_order_object*>(obj);
if( order )
{
auto sub = _market_subscriptions.find( order->get_market() );
if( sub != _market_subscriptions.end() )
market_broadcast_queue[order->get_market()].emplace_back( order->id );
}
}
}
}
auto capture_this = shared_from_this();
/// pushing the future back / popping the prior future if it is complete.
/// if a connection hangs then this could get backed up and result in
/// a failure to exit cleanly.
2015-08-26 22:01:48 +00:00
fc::async([capture_this,this,updates,market_broadcast_queue](){
if( _subscribe_callback ) _subscribe_callback( updates );
for( const auto& item : market_broadcast_queue )
{
auto sub = _market_subscriptions.find(item.first);
if( sub != _market_subscriptions.end() )
sub->second( fc::variant(item.second ) );
}
2015-06-08 15:50:35 +00:00
});
}
/** note: this method cannot yield because it is called in the middle of
* apply a block.
*/
void database_api::on_applied_block()
{
if (_block_applied_callback)
{
auto capture_this = shared_from_this();
block_id_type block_id = _db.head_block_id();
fc::async([this,capture_this,block_id](){
_block_applied_callback(fc::variant(block_id));
});
}
2015-06-08 15:50:35 +00:00
if(_market_subscriptions.size() == 0)
return;
const auto& ops = _db.get_applied_operations();
map< std::pair<asset_id_type,asset_id_type>, vector<pair<operation, operation_result>> > subscribed_markets_ops;
for(const auto& op : ops)
{
std::pair<asset_id_type,asset_id_type> market;
switch(op.op.which())
{
/* This is sent via the object_changed callback
2015-06-08 15:50:35 +00:00
case operation::tag<limit_order_create_operation>::value:
market = op.op.get<limit_order_create_operation>().get_market();
break;
*/
2015-06-08 15:50:35 +00:00
case operation::tag<fill_order_operation>::value:
market = op.op.get<fill_order_operation>().get_market();
break;
/*
case operation::tag<limit_order_cancel_operation>::value:
*/
default: break;
}
if(_market_subscriptions.count(market))
subscribed_markets_ops[market].push_back(std::make_pair(op.op, op.result));
}
/// we need to ensure the database_api is not deleted for the life of the async operation
auto capture_this = shared_from_this();
fc::async([this,capture_this,subscribed_markets_ops](){
2015-06-08 15:50:35 +00:00
for(auto item : subscribed_markets_ops)
{
auto itr = _market_subscriptions.find(item.first);
if(itr != _market_subscriptions.end())
itr->second(fc::variant(item.second));
}
});
}
void database_api::subscribe_to_market(std::function<void(const variant&)> callback, asset_id_type a, asset_id_type b)
{
if(a > b) std::swap(a,b);
FC_ASSERT(a != b);
_market_subscriptions[ std::make_pair(a,b) ] = callback;
}
void database_api::unsubscribe_from_market(asset_id_type a, asset_id_type b)
{
if(a > b) std::swap(a,b);
FC_ASSERT(a != b);
_market_subscriptions.erase(std::make_pair(a,b));
}
std::string database_api::get_transaction_hex(const signed_transaction& trx)const
{
return fc::to_hex(fc::raw::pack(trx));
}
vector<operation_history_object> history_api::get_account_history(account_id_type account, operation_history_id_type stop, unsigned limit, operation_history_id_type start) const
2015-06-08 15:50:35 +00:00
{
FC_ASSERT(_app.chain_database());
const auto& db = *_app.chain_database();
FC_ASSERT(limit <= 100);
vector<operation_history_object> result;
const auto& stats = account(db).statistics(db);
if(stats.most_recent_op == account_transaction_history_id_type()) return result;
const account_transaction_history_object* node = &stats.most_recent_op(db);
if(start == operation_history_id_type())
start = node->id;
while(node && node->operation_id.instance.value > stop.instance.value && result.size() < limit)
{
if(node->id.instance() <= start.instance.value)
result.push_back(node->operation_id(db));
if(node->next == account_transaction_history_id_type())
node = nullptr;
else node = db.find(node->next);
}
return result;
}
flat_set<uint32_t> history_api::get_market_history_buckets()const
{
auto hist = _app.get_plugin<market_history_plugin>( "market_history" );
FC_ASSERT( hist );
return hist->tracked_buckets();
}
vector<bucket_object> history_api::get_market_history( asset_id_type a, asset_id_type b,
uint32_t bucket_seconds, fc::time_point_sec start, fc::time_point_sec end )const
2015-06-23 22:23:41 +00:00
{ try {
FC_ASSERT(_app.chain_database());
const auto& db = *_app.chain_database();
vector<bucket_object> result;
result.reserve(100);
if( a > b ) std::swap(a,b);
const auto& bidx = db.get_index_type<bucket_index>();
const auto& by_key_idx = bidx.indices().get<by_key>();
auto itr = by_key_idx.lower_bound( bucket_key( a, b, bucket_seconds, start ) );
while( itr != by_key_idx.end() && itr->key.open <= end && result.size() < 100 )
{
if( !(itr->key.base == a && itr->key.quote == b && itr->key.seconds == bucket_seconds) )
return result;
result.push_back(*itr);
++itr;
}
return result;
} FC_CAPTURE_AND_RETHROW( (a)(b)(bucket_seconds)(start)(end) ) }
2015-06-08 15:50:35 +00:00
/**
* @return all accounts that referr to the key or account id in their owner or active authorities.
*/
vector<account_id_type> database_api::get_account_references( account_id_type account_id )const
{
const auto& idx = _db.get_index_type<account_index>();
const auto& aidx = dynamic_cast<const primary_index<account_index>&>(idx);
const auto& refs = aidx.get_secondary_index<graphene::chain::account_member_index>();
auto itr = refs.account_to_account_memberships.find(account_id);
vector<account_id_type> result;
if( itr != refs.account_to_account_memberships.end() )
{
result.reserve( itr->second.size() );
for( auto item : itr->second ) result.push_back(item);
}
return result;
}
/**
* @return all accounts that referr to the key or account id in their owner or active authorities.
*/
2015-07-16 18:28:23 +00:00
vector<vector<account_id_type>> database_api::get_key_references( vector<public_key_type> keys )const
{
wdump( (keys) );
2015-07-16 18:28:23 +00:00
vector< vector<account_id_type> > final_result;
final_result.reserve(keys.size());
2015-07-16 18:28:23 +00:00
for( auto& key : keys )
{
2015-08-28 17:58:49 +00:00
address a1( pts_address(key, false, 56) );
address a2( pts_address(key, true, 56) );
address a3( pts_address(key, false, 0) );
address a4( pts_address(key, true, 0) );
address a5( key );
subscribe_to_item( key );
subscribe_to_item( a1 );
subscribe_to_item( a2 );
subscribe_to_item( a3 );
subscribe_to_item( a4 );
subscribe_to_item( a5 );
2015-07-16 18:28:23 +00:00
const auto& idx = _db.get_index_type<account_index>();
const auto& aidx = dynamic_cast<const primary_index<account_index>&>(idx);
const auto& refs = aidx.get_secondary_index<graphene::chain::account_member_index>();
auto itr = refs.account_to_key_memberships.find(key);
vector<account_id_type> result;
for( auto& a : {a1,a2,a3,a4,a5} )
{
auto itr = refs.account_to_address_memberships.find(a);
if( itr != refs.account_to_address_memberships.end() )
{
result.reserve( itr->second.size() );
for( auto item : itr->second )
{
wdump((a)(item)(item(_db).name));
result.push_back(item);
}
}
}
2015-07-16 18:28:23 +00:00
if( itr != refs.account_to_key_memberships.end() )
{
result.reserve( itr->second.size() );
for( auto item : itr->second ) result.push_back(item);
}
final_result.emplace_back( std::move(result) );
}
2015-08-28 17:58:49 +00:00
for( auto i : final_result )
subscribe_to_item(i);
2015-07-16 18:28:23 +00:00
return final_result;
}
/** TODO: add secondary index that will accelerate this process */
vector<proposal_object> database_api::get_proposed_transactions( account_id_type id )const
{
const auto& idx = _db.get_index_type<proposal_index>();
vector<proposal_object> result;
idx.inspect_all_objects( [&](const object& obj){
const proposal_object& p = static_cast<const proposal_object&>(obj);
if( p.required_active_approvals.find( id ) != p.required_active_approvals.end() )
result.push_back(p);
else if ( p.required_owner_approvals.find( id ) != p.required_owner_approvals.end() )
result.push_back(p);
else if ( p.available_active_approvals.find( id ) != p.available_active_approvals.end() )
result.push_back(p);
});
return result;
}
vector<call_order_object> database_api::get_margin_positions( const account_id_type& id )const
{ try {
const auto& idx = _db.get_index_type<call_order_index>();
const auto& aidx = idx.indices().get<by_account>();
auto start = aidx.lower_bound( boost::make_tuple( id, 0 ) );
auto end = aidx.lower_bound( boost::make_tuple( id+1, 0 ) );
vector<call_order_object> result;
while( start != end )
{
result.push_back(*start);
++start;
}
return result;
} FC_CAPTURE_AND_RETHROW( (id) ) }
2015-06-23 22:23:41 +00:00
vector<asset> database_api::get_vested_balances( const vector<balance_id_type>& objs )const
{ try {
vector<asset> result;
result.reserve( objs.size() );
auto now = _db.head_block_time();
for( auto obj : objs )
result.push_back( obj(_db).available( now ) );
return result;
} FC_CAPTURE_AND_RETHROW( (objs) ) }
vector<vesting_balance_object> database_api::get_vesting_balances( account_id_type account_id )const
{
try
{
vector<vesting_balance_object> result;
auto vesting_range = _db.get_index_type<vesting_balance_index>().indices().get<by_account>().equal_range(account_id);
std::for_each(vesting_range.first, vesting_range.second,
[&result](const vesting_balance_object& balance) {
result.emplace_back(balance);
});
return result;
}
FC_CAPTURE_AND_RETHROW( (account_id) );
}
vector<balance_object> database_api::get_balance_objects( const vector<address>& addrs )const
{ try {
const auto& bal_idx = _db.get_index_type<balance_index>();
const auto& by_owner_idx = bal_idx.indices().get<by_owner>();
vector<balance_object> result;
for( const auto& owner : addrs )
{
2015-08-28 17:58:49 +00:00
subscribe_to_item( owner );
auto itr = by_owner_idx.lower_bound( boost::make_tuple( owner, asset_id_type(0) ) );
while( itr != by_owner_idx.end() && itr->owner == owner )
{
result.push_back( *itr );
++itr;
}
}
return result;
} FC_CAPTURE_AND_RETHROW( (addrs) ) }
set<public_key_type> database_api::get_required_signatures( const signed_transaction& trx, const flat_set<public_key_type>& available_keys )const
{
2015-09-03 21:42:52 +00:00
wdump((trx)(available_keys));
auto result = trx.get_required_signatures( _db.get_chain_id(),
2015-08-06 16:41:45 +00:00
available_keys,
[&]( account_id_type id ){ return &id(_db).active; },
[&]( account_id_type id ){ return &id(_db).owner; },
_db.get_global_properties().parameters.max_authority_depth );
2015-09-03 21:42:52 +00:00
wdump((result));
return result;
}
set<public_key_type> database_api::get_potential_signatures( const signed_transaction& trx )const
{
2015-09-03 21:42:52 +00:00
wdump((trx));
set<public_key_type> result;
trx.get_required_signatures( _db.get_chain_id(),
flat_set<public_key_type>(),
[&]( account_id_type id ){
2015-08-06 18:33:34 +00:00
const auto& auth = id(_db).active;
for( const auto& k : auth.get_keys() )
2015-08-06 18:33:34 +00:00
result.insert(k);
return &auth; },
[&]( account_id_type id ){
2015-08-06 18:33:34 +00:00
const auto& auth = id(_db).owner;
for( const auto& k : auth.get_keys() )
2015-08-06 18:33:34 +00:00
result.insert(k);
return &auth;
},
_db.get_global_properties().parameters.max_authority_depth );
2015-09-03 21:42:52 +00:00
wdump((result));
return result;
}
/**
* Validates a transaction against the current state without broadcast it on the network.
*/
processed_transaction database_api::validate_transaction( const signed_transaction& trx )const
{
return _db.validate_transaction(trx);
}
2015-08-06 16:41:45 +00:00
bool database_api::verify_authority( const signed_transaction& trx )const
{
2015-08-06 16:41:45 +00:00
trx.verify_authority( _db.get_chain_id(),
[&]( account_id_type id ){ return &id(_db).active; },
[&]( account_id_type id ){ return &id(_db).owner; },
_db.get_global_properties().parameters.max_authority_depth );
return true;
}
2015-09-07 14:19:51 +00:00
bool database_api::verify_account_authority( const string& name_or_id, const flat_set<public_key_type>& keys )const
{
FC_ASSERT( name_or_id.size() > 0);
const account_object* account = nullptr;
if (std::isdigit(name_or_id[0]))
account = _db.find(fc::variant(name_or_id).as<account_id_type>());
else
{
const auto& idx = _db.get_index_type<account_index>().indices().get<by_name>();
auto itr = idx.find(name_or_id);
if (itr != idx.end())
account = &*itr;
}
FC_ASSERT( account, "no such account" );
/// reuse trx.verify_authority by creating a dummy transfer
signed_transaction trx;
transfer_operation op;
op.from = account->id;
trx.operations.emplace_back(op);
return verify_authority( trx );
}
vector<blinded_balance_object> database_api::get_blinded_balances( const flat_set<commitment_type>& commitments )const
{
vector<blinded_balance_object> result; result.reserve(commitments.size());
const auto& bal_idx = _db.get_index_type<blinded_balance_index>();
const auto& by_commitment_idx = bal_idx.indices().get<by_commitment>();
for( const auto& c : commitments )
{
auto itr = by_commitment_idx.find( c );
if( itr != by_commitment_idx.end() )
result.push_back( *itr );
}
return result;
}
vector<asset> database_api::get_required_fees( const vector<operation>& ops, asset_id_type id )const
{
vector<asset> result;
result.reserve(ops.size());
const asset_object& a = id(_db);
for( const auto& op : ops )
result.push_back( _db.current_fee_schedule().calculate_fee( op, a.options.core_exchange_rate ) );
return result;
}
2015-08-03 20:22:19 +00:00
optional<account_object> database_api::get_account_by_name( string name )const
{
const auto& idx = _db.get_index_type<account_index>().indices().get<by_name>();
auto itr = idx.find(name);
if (itr != idx.end())
return *itr;
return optional<account_object>();
}
2015-06-08 15:50:35 +00:00
} } // graphene::app