#include "ClientDataModel.hpp" #include #include #include #include using namespace graphene::app; template 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())); } ChainDataModel::ChainDataModel(fc::thread& t, QObject* parent) :QObject(parent),m_rpc_thread(&t){} Asset* ChainDataModel::getAsset(ObjectId id) { auto& by_id_idx = m_assets.get(); auto itr = by_id_idx.find(id); if (itr == by_id_idx.end()) { 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); }); return *result.first; } return *itr; } Asset* ChainDataModel::getAsset(QString symbol) { auto& by_symbol_idx = m_assets.get(); auto itr = by_symbol_idx.find(symbol); if (itr == by_symbol_idx.end()) { 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); }); 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()["id"].as(); 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(); 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 accountPackage; if (result.count(accountIdentifier.toStdString())) { accountPackage = result.at(accountIdentifier.toStdString()); // Fetch all necessary assets QList assetsToFetch; QList 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(); // 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&, 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 { m_accounts.modify(itr, [=](Account* a){ a->setProperty("id", ObjectId(accountPackage->account.id.instance())); a->setProperty("name", QString::fromStdString(accountPackage->account.name)); // Set balances QList 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())); } } Account* ChainDataModel::getAccount(ObjectId id) { auto& by_id_idx = m_accounts.get(); 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);}); return *result.first; } return *itr; } Account* ChainDataModel::getAccount(QString name) { auto& by_name_idx = m_accounts.get(); auto itr = by_name_idx.find(name); if( itr == by_name_idx.end() ) { 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);}); return *result.first; } return *itr; } QQmlListProperty Account::balances() { auto count = [](QQmlListProperty* list) { return reinterpret_cast(list->data)->m_balances.size(); }; auto at = [](QQmlListProperty* list, int index) { return reinterpret_cast(list->data)->m_balances[index]; }; return QQmlListProperty(this, this, count, at); } 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(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") { connect(this, &GrapheneApplication::queueExecute, this, &GrapheneApplication::execute); m_model = new ChainDataModel(m_thread, this); connect(m_model, &ChainDataModel::queueExecute, this, &GrapheneApplication::execute); connect(m_model, &ChainDataModel::exceptionThrown, this, &GrapheneApplication::exceptionThrown); } GrapheneApplication::~GrapheneApplication() { } void GrapheneApplication::setIsConnected(bool v) { if (v != m_isConnected) { m_isConnected = v; Q_EMIT isConnectedChanged(m_isConnected); } } void GrapheneApplication::start(QString apiurl, QString user, QString pass) { if (!m_thread.is_current()) { m_done = m_thread.async([=](){ return start(apiurl, user, pass); }); return; } try { m_client = std::make_shared(); ilog("connecting...${s}", ("s",apiurl.toStdString())); auto con = m_client->connect(apiurl.toStdString()); m_connectionClosed = con->closed.connect([this]{queueExecute([this]{setIsConnected(false);});}); auto apic = std::make_shared(*con); auto remote_api = apic->get_remote_api(1); auto db_api = apic->get_remote_api(0); if (!remote_api->login(user.toStdString(), pass.toStdString())) { elog("login failed"); Q_EMIT loginFailed(); return; } ilog("connecting..."); queueExecute([=](){ m_model->setDatabaseAPI(db_api); }); queueExecute([=](){ setIsConnected(true); }); } catch (const fc::exception& e) { Q_EMIT exceptionThrown(QString::fromStdString(e.to_string())); } } Q_SLOT void GrapheneApplication::execute(const std::function& func)const { func(); } void Balance::update(const account_balance_object& update) { if (update.balance != amount) { amount = update.balance.value; emit amountChanged(); } }