peerplays_migrated/programs/light_client/ClientDataModel.cpp

374 lines
13 KiB
C++
Raw Normal View History

2015-07-10 21:37:22 +00:00
#include "ClientDataModel.hpp"
#include "Operations.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>
#include <QMetaObject>
2015-07-13 18:14:58 +00:00
using namespace graphene::app;
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
ChainDataModel::ChainDataModel(fc::thread& t, QObject* parent)
: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();
m_db_api->subscribe_to_objects([this](const variant& v) { m_global_properties = v.as<global_property_object>(); },
{m_global_properties.id});
});
}
2015-07-14 21:30:15 +00:00
Asset* ChainDataModel::getAsset(ObjectId id)
2015-07-14 21:30:15 +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);
if (itr == by_id_idx.end())
2015-07-14 21:30:15 +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);
if (itr == by_symbol_idx.end())
2015-07-14 21:30:15 +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;
}
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()) {
default:
wlog("Update procedure for ${update} is not yet implemented.", ("update", update));
break;
}
} else if (id.space() == implementation_ids) {
switch (id.type()) {
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;
}
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()));
}
}
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()));
auto result = m_db_api->get_full_accounts([this](const fc::variant& v) {
processUpdatedObject(v);
}, {accountIdentifier.toStdString()});
fc::optional<full_account> accountPackage;
if (result.count(accountIdentifier.toStdString())) {
accountPackage = result.at(accountIdentifier.toStdString());
// Fetch all necessary assets
QList<asset_id_type> assetsToFetch;
QList<Asset* const *> assetPlaceholders;
assetsToFetch.reserve(accountPackage->balances.size());
// Get list of asset IDs the account has a balance in
std::transform(accountPackage->balances.begin(), accountPackage->balances.end(), std::back_inserter(assetsToFetch),
[](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);
if (!accountPackage.valid()) {
(*itr)->deleteLater();
m_accounts.erase(itr);
} else {
2015-07-21 18:26:47 +00:00
m_accounts.modify(itr, [this,&accountPackage](Account* a){
a->setProperty("id", ObjectId(accountPackage->account.id.instance()));
a->setProperty("name", QString::fromStdString(accountPackage->account.name));
// Set balances
QList<Balance*> balances;
std::transform(accountPackage->balances.begin(), accountPackage->balances.end(), std::back_inserter(balances),
[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
{
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() )
{
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
}
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
{
auto tmp = new Account(--m_account_query_num, name, this);
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
}
QQmlListProperty<Balance> Account::balances()
{
auto count = [](QQmlListProperty<Balance>* list) {
return reinterpret_cast<Account*>(list->data)->m_balances.size();
};
auto at = [](QQmlListProperty<Balance>* list, int index) {
return reinterpret_cast<Account*>(list->data)->m_balances[index];
};
return QQmlListProperty<Balance>(this, this, count, at);
2015-07-10 21:37:22 +00:00
}
2015-07-13 17:24:30 +00:00
void Account::update(const account_balance_object& balance)
{
auto balanceItr = std::find_if(m_balances.begin(), m_balances.end(),
[&balance](Balance* b) { return b->type()->id() == balance.asset_type.instance.value; });
if (balanceItr != m_balances.end()) {
ilog("Updating ${a}'s balance: ${b}", ("a", m_name.toStdString())("b", balance));
(*balanceItr)->update(balance);
Q_EMIT balancesChanged();
} else {
ilog("Adding to ${a}'s new balance: ${b}", ("a", m_name.toStdString())("b", balance));
Balance* newBalance = new Balance;
newBalance->setParent(this);
auto model = qobject_cast<ChainDataModel*>(parent());
newBalance->setProperty("type", QVariant::fromValue(model->getAsset(balance.asset_type.instance.value)));
newBalance->setProperty("amount", QVariant::fromValue(balance.balance.value));
m_balances.append(newBalance);
Q_EMIT balancesChanged();
}
}
GrapheneApplication::GrapheneApplication(QObject* parent)
:QObject(parent),m_thread("app")
2015-07-13 17:56:30 +00:00
{
connect(this, &GrapheneApplication::queueExecute,
this, &GrapheneApplication::execute);
2015-07-13 18:14:58 +00:00
m_model = new ChainDataModel(m_thread, this);
m_operationBuilder = new OperationBuilder(*m_model, this);
2015-07-13 19:41:06 +00:00
connect(m_model, &ChainDataModel::queueExecute,
this, &GrapheneApplication::execute);
2015-07-13 19:41:06 +00:00
connect(m_model, &ChainDataModel::exceptionThrown,
this, &GrapheneApplication::exceptionThrown);
2015-07-13 17:56:30 +00:00
}
2015-07-13 18:14:58 +00:00
2015-07-13 17:56:30 +00:00
GrapheneApplication::~GrapheneApplication()
{
}
void GrapheneApplication::setIsConnected(bool v)
2015-07-13 18:14:58 +00:00
{
if (v != m_isConnected)
2015-07-13 18:14:58 +00:00
{
m_isConnected = v;
Q_EMIT isConnectedChanged(m_isConnected);
2015-07-13 18:14:58 +00:00
}
}
void GrapheneApplication::start(QString apiurl, QString user, QString pass)
2015-07-13 17:24:30 +00:00
{
if (!m_thread.is_current())
2015-07-13 17:56:30 +00:00
{
m_done = m_thread.async([=](){ return start(apiurl, user, pass); });
2015-07-13 18:14:58 +00:00
return;
2015-07-13 17:56:30 +00:00
}
try {
2015-07-13 21:24:25 +00:00
m_client = std::make_shared<fc::http::websocket_client>();
ilog("connecting...${s}", ("s",apiurl.toStdString()));
auto con = m_client->connect(apiurl.toStdString());
m_connectionClosed = con->closed.connect([this]{queueExecute([this]{setIsConnected(false);});});
2015-07-13 17:56:30 +00:00
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
auto remote_api = apic->get_remote_api<login_api>(1);
auto db_api = apic->get_remote_api<database_api>(0);
if (!remote_api->login(user.toStdString(), pass.toStdString()))
2015-07-13 18:14:58 +00:00
{
elog("login failed");
2015-07-13 19:41:06 +00:00
Q_EMIT loginFailed();
2015-07-13 18:14:58 +00:00
return;
}
2015-07-13 17:56:30 +00:00
ilog("connecting...");
queueExecute([=](){
m_model->setDatabaseAPI(db_api);
2015-07-13 19:41:06 +00:00
});
queueExecute([=](){ setIsConnected(true); });
} catch (const fc::exception& e)
2015-07-13 17:56:30 +00:00
{
Q_EMIT exceptionThrown(QString::fromStdString(e.to_string()));
2015-07-13 17:56:30 +00:00
}
2015-07-13 18:14:58 +00:00
}
Q_SLOT void GrapheneApplication::execute(const std::function<void()>& func)const
2015-07-13 18:14:58 +00:00
{
func();
2015-07-13 17:24:30 +00:00
}
void Balance::update(const account_balance_object& update)
{
if (update.balance != amount) {
amount = update.balance.value;
emit amountChanged();
}
}
void Asset::update(const asset_object& asset)
{
if (asset.id.instance() != id())
setProperty("id", QVariant::fromValue(asset.id.instance()));
if (asset.symbol != m_symbol.toStdString()) {
m_symbol = QString::fromStdString(asset.symbol);
Q_EMIT symbolChanged();
}
if (asset.precision != m_precision) {
m_precision = asset.precision;
Q_EMIT precisionChanged();
}
if (asset.options.core_exchange_rate != coreExchangeRate)
coreExchangeRate = asset.options.core_exchange_rate;
}