2015-07-22 15:10:52 +00:00
|
|
|
#include "ChainDataModel.hpp"
|
|
|
|
|
#include "Balance.hpp"
|
2015-07-21 21:09:44 +00:00
|
|
|
#include "Operations.hpp"
|
2015-07-31 21:56:22 +00:00
|
|
|
#include "Transaction.hpp"
|
2015-07-10 21:37:22 +00:00
|
|
|
|
2015-07-13 17:56:30 +00:00
|
|
|
#include <graphene/app/api.hpp>
|
|
|
|
|
#include <graphene/chain/protocol/protocol.hpp>
|
|
|
|
|
|
|
|
|
|
#include <fc/rpc/websocket_api.hpp>
|
|
|
|
|
|
2015-07-17 19:35:24 +00:00
|
|
|
#include <QMetaObject>
|
|
|
|
|
|
2015-07-13 18:14:58 +00:00
|
|
|
using namespace graphene::app;
|
|
|
|
|
|
2015-07-17 19:35:24 +00:00
|
|
|
template<typename T>
|
|
|
|
|
QString idToString(T id) {
|
|
|
|
|
return QString("%1.%2.%3").arg(T::space_id).arg(T::type_id).arg(ObjectId(id.instance));
|
|
|
|
|
}
|
|
|
|
|
QString idToString(graphene::db::object_id_type id) {
|
|
|
|
|
return QString("%1.%2.%3").arg(id.space(), id.type(), ObjectId(id.instance()));
|
|
|
|
|
}
|
2015-07-10 21:37:22 +00:00
|
|
|
|
2015-07-17 19:35:24 +00:00
|
|
|
ChainDataModel::ChainDataModel(fc::thread& t, QObject* parent)
|
2015-07-21 21:09:44 +00:00
|
|
|
:QObject(parent),m_rpc_thread(&t){}
|
|
|
|
|
|
|
|
|
|
void ChainDataModel::setDatabaseAPI(fc::api<database_api> dbapi) {
|
|
|
|
|
m_db_api = dbapi;
|
|
|
|
|
m_rpc_thread->async([this] {
|
|
|
|
|
m_global_properties = m_db_api->get_global_properties();
|
2015-07-31 21:56:22 +00:00
|
|
|
m_db_api->subscribe_to_objects([this](const variant& v) {
|
|
|
|
|
m_global_properties = v.as<global_property_object>();
|
|
|
|
|
}, {m_global_properties.id});
|
|
|
|
|
|
|
|
|
|
m_dynamic_global_properties = m_db_api->get_dynamic_global_properties();
|
|
|
|
|
m_db_api->subscribe_to_objects([this](const variant& d) {
|
|
|
|
|
m_dynamic_global_properties = d.as<dynamic_global_property_object>();
|
|
|
|
|
}, {m_dynamic_global_properties.id});
|
2015-07-21 21:09:44 +00:00
|
|
|
});
|
|
|
|
|
}
|
2015-07-14 21:30:15 +00:00
|
|
|
|
2015-07-31 21:56:22 +00:00
|
|
|
void ChainDataModel::setNetworkAPI(fc::api<network_broadcast_api> napi)
|
|
|
|
|
{
|
|
|
|
|
m_net_api = napi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChainDataModel::broadcast(Transaction* transaction)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
m_net_api->broadcast_transaction_with_callback([transaction](const fc::variant&) {
|
|
|
|
|
transaction->setStatus(Transaction::Complete);
|
|
|
|
|
}, transaction->internalTransaction());
|
|
|
|
|
transaction->setStatus(Transaction::Pending);
|
|
|
|
|
} catch (const fc::exception& e) {
|
|
|
|
|
transaction->setStatus(Transaction::Failed);
|
|
|
|
|
Q_EMIT exceptionThrown(QString::fromStdString(e.to_string()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 21:48:44 +00:00
|
|
|
Asset* ChainDataModel::getAsset(ObjectId id)
|
2015-07-14 21:30:15 +00:00
|
|
|
{
|
2015-07-17 19:35:24 +00:00
|
|
|
auto& by_id_idx = m_assets.get<by_id>();
|
2015-07-14 21:30:15 +00:00
|
|
|
auto itr = by_id_idx.find(id);
|
2015-07-17 19:35:24 +00:00
|
|
|
if (itr == by_id_idx.end())
|
2015-07-14 21:30:15 +00:00
|
|
|
{
|
2015-07-17 19:35:24 +00:00
|
|
|
auto result = m_assets.insert(new Asset(id, QString::number(--m_account_query_num), 0, this));
|
|
|
|
|
assert(result.second);
|
|
|
|
|
|
|
|
|
|
// Run in RPC thread
|
|
|
|
|
m_rpc_thread->async([this,id,result]{ getAssetImpl(idToString(asset_id_type(id)), &*result.first); });
|
2015-07-14 21:30:15 +00:00
|
|
|
return *result.first;
|
|
|
|
|
}
|
|
|
|
|
return *itr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Asset* ChainDataModel::getAsset(QString symbol)
|
|
|
|
|
{
|
|
|
|
|
auto& by_symbol_idx = m_assets.get<by_symbol_name>();
|
|
|
|
|
auto itr = by_symbol_idx.find(symbol);
|
2015-07-17 19:35:24 +00:00
|
|
|
if (itr == by_symbol_idx.end())
|
2015-07-14 21:30:15 +00:00
|
|
|
{
|
2015-07-17 19:35:24 +00:00
|
|
|
auto result = m_assets.insert(new Asset(--m_account_query_num, symbol, 0, this));
|
|
|
|
|
assert(result.second);
|
|
|
|
|
|
|
|
|
|
// Run in RPC thread
|
|
|
|
|
m_rpc_thread->async([this,symbol,result](){ getAssetImpl(symbol, &*result.first); });
|
2015-07-14 21:30:15 +00:00
|
|
|
return *result.first;
|
|
|
|
|
}
|
|
|
|
|
return *itr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 21:50:58 +00:00
|
|
|
void ChainDataModel::processUpdatedObject(const fc::variant& update)
|
|
|
|
|
{
|
|
|
|
|
if (update.is_null())
|
|
|
|
|
return;
|
|
|
|
|
if (&fc::thread::current() == m_rpc_thread)
|
|
|
|
|
{
|
|
|
|
|
ilog("Proxying object update to app thread.");
|
|
|
|
|
Q_EMIT queueExecute([this,update]{processUpdatedObject(update);});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
idump((update));
|
|
|
|
|
try {
|
|
|
|
|
auto id = update.as<variant_object>()["id"].as<object_id_type>();
|
|
|
|
|
if (id.space() == protocol_ids) {
|
|
|
|
|
switch (id.type()) {
|
2015-07-23 14:56:07 +00:00
|
|
|
default:
|
|
|
|
|
wlog("Update procedure for ${update} is not yet implemented.", ("update", update));
|
|
|
|
|
break;
|
2015-07-20 21:50:58 +00:00
|
|
|
}
|
|
|
|
|
} else if (id.space() == implementation_ids) {
|
|
|
|
|
switch (id.type()) {
|
2015-07-23 14:56:07 +00:00
|
|
|
case impl_account_balance_object_type: {
|
|
|
|
|
account_balance_object balance = update.as<account_balance_object>();
|
|
|
|
|
auto owner = m_accounts.find(balance.owner.instance.value);
|
|
|
|
|
if (owner != m_accounts.end())
|
|
|
|
|
(*owner)->update(balance);
|
|
|
|
|
else
|
|
|
|
|
elog("Got unexpected balance update:\n${u}\nfor an account I don't have.",
|
|
|
|
|
("u", update));
|
|
|
|
|
break;
|
2015-07-20 21:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
wlog("Update procedure for ${update} is not yet implemented.", ("update", update));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
wlog("Update procedure for ${update} is not yet implemented.", ("update", update));
|
|
|
|
|
} catch (const fc::exception& e) {
|
|
|
|
|
elog("Caught exception while updating object: ${e}", ("e", e.to_detail_string()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 19:35:24 +00:00
|
|
|
void ChainDataModel::getAssetImpl(QString assetIdentifier, Asset* const * assetInContainer)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
ilog("Fetching asset ${asset}", ("asset", assetIdentifier.toStdString()));
|
|
|
|
|
auto result = m_db_api->lookup_asset_symbols({assetIdentifier.toStdString()});
|
|
|
|
|
|
|
|
|
|
// Run in main thread
|
|
|
|
|
Q_EMIT queueExecute([this,result,assetInContainer](){
|
|
|
|
|
ilog("Processing result ${r}", ("r", result));
|
|
|
|
|
auto itr = m_assets.iterator_to(*assetInContainer);
|
|
|
|
|
|
|
|
|
|
if (result.size() == 0 || !result.front()) {
|
|
|
|
|
(*itr)->deleteLater();
|
|
|
|
|
m_assets.erase(itr);
|
|
|
|
|
} else {
|
|
|
|
|
m_assets.modify(itr,
|
|
|
|
|
[=](Asset* a){
|
|
|
|
|
a->setProperty("symbol", QString::fromStdString(result.front()->symbol));
|
|
|
|
|
a->setProperty("id", ObjectId(result.front()->id.instance()));
|
|
|
|
|
a->setProperty("precision", result.front()->precision);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch ( const fc::exception& e )
|
|
|
|
|
{
|
|
|
|
|
Q_EMIT exceptionThrown(QString::fromStdString(e.to_string()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChainDataModel::getAccountImpl(QString accountIdentifier, Account* const * accountInContainer)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
ilog("Fetching account ${acct}", ("acct", accountIdentifier.toStdString()));
|
2015-07-20 21:50:58 +00:00
|
|
|
auto result = m_db_api->get_full_accounts([this](const fc::variant& v) {
|
2015-07-21 21:56:52 +00:00
|
|
|
vector<variant> updates = v.as<vector<variant>>();
|
|
|
|
|
for (const variant& update : updates) {
|
|
|
|
|
if (update.is_object())
|
|
|
|
|
processUpdatedObject(update);
|
|
|
|
|
else
|
|
|
|
|
elog("Handling object deletions is not yet implemented: ${update}", ("update", update));
|
|
|
|
|
}
|
2015-07-31 13:51:05 +00:00
|
|
|
// TODO: replace true on the next line with a smarter decision as to whether we need status updates or not
|
|
|
|
|
}, {accountIdentifier.toStdString()}, true);
|
2015-07-20 16:21:58 +00:00
|
|
|
fc::optional<full_account> accountPackage;
|
2015-07-17 19:35:24 +00:00
|
|
|
|
|
|
|
|
if (result.count(accountIdentifier.toStdString())) {
|
2015-07-20 16:21:58 +00:00
|
|
|
accountPackage = result.at(accountIdentifier.toStdString());
|
2015-07-17 19:35:24 +00:00
|
|
|
|
|
|
|
|
// Fetch all necessary assets
|
|
|
|
|
QList<asset_id_type> assetsToFetch;
|
|
|
|
|
QList<Asset* const *> assetPlaceholders;
|
2015-07-20 16:21:58 +00:00
|
|
|
assetsToFetch.reserve(accountPackage->balances.size());
|
2015-07-17 19:35:24 +00:00
|
|
|
// Get list of asset IDs the account has a balance in
|
2015-07-20 16:21:58 +00:00
|
|
|
std::transform(accountPackage->balances.begin(), accountPackage->balances.end(), std::back_inserter(assetsToFetch),
|
2015-07-17 19:35:24 +00:00
|
|
|
[](const account_balance_object& b) { return b.asset_type; });
|
|
|
|
|
auto function = [this,&assetsToFetch,&assetPlaceholders] {
|
|
|
|
|
auto itr = assetsToFetch.begin();
|
|
|
|
|
const auto& assets_by_id = m_assets.get<by_id>();
|
|
|
|
|
// Filter out assets I already have, create placeholders for the ones I don't.
|
|
|
|
|
while (itr != assetsToFetch.end()) {
|
|
|
|
|
if (assets_by_id.count(itr->instance))
|
|
|
|
|
itr = assetsToFetch.erase(itr);
|
|
|
|
|
else {
|
|
|
|
|
assetPlaceholders.push_back(&*m_assets.insert(new Asset(itr->instance, QString(), 0, this)).first);
|
|
|
|
|
++itr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
QMetaObject::invokeMethod(parent(), "execute", Qt::BlockingQueuedConnection,
|
|
|
|
|
Q_ARG(const std::function<void()>&, function));
|
|
|
|
|
assert(assetsToFetch.size() == assetPlaceholders.size());
|
|
|
|
|
|
|
|
|
|
// Blocking call to fetch and complete initialization for all the assets
|
|
|
|
|
for (int i = 0; i < assetsToFetch.size(); ++i)
|
|
|
|
|
getAssetImpl(idToString(assetsToFetch[i]), assetPlaceholders[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run in main thread
|
|
|
|
|
Q_EMIT queueExecute([this,accountPackage,accountInContainer](){
|
|
|
|
|
ilog("Processing result ${r}", ("r", accountPackage));
|
|
|
|
|
auto itr = m_accounts.iterator_to(*accountInContainer);
|
|
|
|
|
|
2015-07-20 16:21:58 +00:00
|
|
|
if (!accountPackage.valid()) {
|
2015-07-17 19:35:24 +00:00
|
|
|
(*itr)->deleteLater();
|
|
|
|
|
m_accounts.erase(itr);
|
|
|
|
|
} else {
|
2015-07-21 18:26:47 +00:00
|
|
|
m_accounts.modify(itr, [this,&accountPackage](Account* a){
|
2015-07-20 16:21:58 +00:00
|
|
|
a->setProperty("id", ObjectId(accountPackage->account.id.instance()));
|
2015-07-24 17:57:47 +00:00
|
|
|
a->setAccountObject(accountPackage->account);
|
2015-07-17 19:35:24 +00:00
|
|
|
|
|
|
|
|
// Set balances
|
|
|
|
|
QList<Balance*> balances;
|
2015-07-20 16:21:58 +00:00
|
|
|
std::transform(accountPackage->balances.begin(), accountPackage->balances.end(), std::back_inserter(balances),
|
2015-07-17 19:35:24 +00:00
|
|
|
[this](const account_balance_object& b) {
|
|
|
|
|
Balance* bal = new Balance;
|
|
|
|
|
bal->setParent(this);
|
|
|
|
|
bal->setProperty("amount", QVariant::fromValue(b.balance.value));
|
|
|
|
|
bal->setProperty("type", QVariant::fromValue(getAsset(ObjectId(b.asset_type.instance))));
|
|
|
|
|
return bal;
|
|
|
|
|
});
|
|
|
|
|
a->setBalances(balances);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (const fc::exception& e)
|
|
|
|
|
{
|
|
|
|
|
Q_EMIT exceptionThrown(QString::fromStdString(e.to_string()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 17:06:32 +00:00
|
|
|
Account* ChainDataModel::getAccount(ObjectId id)
|
2015-07-10 21:37:22 +00:00
|
|
|
{
|
2015-07-17 19:35:24 +00:00
|
|
|
auto& by_id_idx = m_accounts.get<by_id>();
|
2015-07-14 13:46:38 +00:00
|
|
|
auto itr = by_id_idx.find(id);
|
|
|
|
|
if( itr == by_id_idx.end() )
|
|
|
|
|
{
|
2015-07-17 19:35:24 +00:00
|
|
|
auto tmp = new Account(id, tr("Account #%1").arg(--m_account_query_num), this);
|
|
|
|
|
auto result = m_accounts.insert(tmp);
|
|
|
|
|
assert(result.second);
|
|
|
|
|
|
|
|
|
|
// Run in RPC thread
|
|
|
|
|
m_rpc_thread->async([this, id, result]{getAccountImpl(idToString(account_id_type(id)), &*result.first);});
|
2015-07-14 13:46:38 +00:00
|
|
|
return *result.first;
|
|
|
|
|
}
|
|
|
|
|
return *itr;
|
2015-07-10 21:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-13 20:21:01 +00:00
|
|
|
Account* ChainDataModel::getAccount(QString name)
|
2015-07-10 21:37:22 +00:00
|
|
|
{
|
2015-07-14 13:46:38 +00:00
|
|
|
auto& by_name_idx = m_accounts.get<by_account_name>();
|
|
|
|
|
auto itr = by_name_idx.find(name);
|
|
|
|
|
if( itr == by_name_idx.end() )
|
2015-07-13 21:24:25 +00:00
|
|
|
{
|
2015-07-15 21:48:44 +00:00
|
|
|
auto tmp = new Account(--m_account_query_num, name, this);
|
2015-07-17 19:35:24 +00:00
|
|
|
auto result = m_accounts.insert(tmp);
|
|
|
|
|
assert(result.second);
|
|
|
|
|
|
|
|
|
|
// Run in RPC thread
|
|
|
|
|
m_rpc_thread->async([this, name, result]{getAccountImpl(name, &*result.first);});
|
2015-07-14 13:46:38 +00:00
|
|
|
return *result.first;
|
2015-07-13 21:24:25 +00:00
|
|
|
}
|
2015-07-14 13:46:38 +00:00
|
|
|
return *itr;
|
2015-07-10 21:37:22 +00:00
|
|
|
}
|