send and confirm transactions to assigned nodes from DS

This commit is contained in:
Stephen Birarda 2014-05-21 10:58:41 -07:00
parent 908eb5cc4f
commit 1dddabb691
5 changed files with 109 additions and 13 deletions

View file

@ -739,12 +739,55 @@ void DomainServer::setupPendingAssignmentCredits() {
}
void DomainServer::sendPendingTransactionsToServer() {
// enumerate the pending transactions and send them to the server to complete payment
TransactionHash::iterator i = _pendingAssignmentCredits.begin();
while (i != _pendingAssignmentCredits.end()) {
AccountManager& accountManager = AccountManager::getInstance();
if (accountManager.hasValidAccessToken()) {
++i;
// enumerate the pending transactions and send them to the server to complete payment
TransactionHash::iterator i = _pendingAssignmentCredits.begin();
JSONCallbackParameters transactionCallbackParams;
transactionCallbackParams.jsonCallbackReceiver = this;
transactionCallbackParams.jsonCallbackMethod = "transactionJSONCallback";
while (i != _pendingAssignmentCredits.end()) {
accountManager.authenticatedRequest("api/v1/transactions", QNetworkAccessManager::PostOperation,
transactionCallbackParams, i.value()->postJson().toJson());
// set this transaction to finalized so we don't add additional credits to it
i.value()->setIsFinalized(true);
++i;
}
}
}
void DomainServer::transactionJSONCallback(const QJsonObject& data) {
// check if this was successful - if so we can remove it from our list of pending
if (data.value("status").toString() == "success") {
// create a dummy wallet transaction to unpack the JSON to
WalletTransaction dummyTransaction;
dummyTransaction.loadFromJson(data);
TransactionHash::iterator i = _pendingAssignmentCredits.find(dummyTransaction.getDestinationUUID());
while (i != _pendingAssignmentCredits.end() && i.key() == dummyTransaction.getDestinationUUID()) {
if (i.value()->getUUID() == dummyTransaction.getUUID()) {
// we have a match - we can remove this from the hash of pending credits
// and delete it for clean up
WalletTransaction* matchingTransaction = i.value();
_pendingAssignmentCredits.erase(i);
delete matchingTransaction;
break;
} else {
++i;
}
}
}
}
@ -907,6 +950,24 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url
connection->respond(HTTPConnection::StatusCode200, assignmentDocument.toJson(), qPrintable(JSON_MIME_TYPE));
// we've processed this request
return true;
} else if (url.path() == "/transactions.json") {
// enumerate our pending transactions and display them in an array
QJsonObject rootObject;
QJsonArray transactionArray;
TransactionHash::iterator i = _pendingAssignmentCredits.begin();
while (i != _pendingAssignmentCredits.end()) {
transactionArray.push_back(i.value()->toJson());
++i;
}
rootObject["pending_transactions"] = transactionArray;
// print out the created JSON
QJsonDocument transactionsDocument(rootObject);
connection->respond(HTTPConnection::StatusCode200, transactionsDocument.toJson(), qPrintable(JSON_MIME_TYPE));
return true;
} else if (url.path() == QString("%1.json").arg(URI_NODES)) {
// setup the JSON

View file

@ -47,6 +47,8 @@ public slots:
/// Called by NodeList to inform us a node has been killed
void nodeKilled(SharedNodePointer node);
void transactionJSONCallback(const QJsonObject& data);
private slots:
void loginFailed();
void readAvailableDatagrams();

View file

@ -15,6 +15,15 @@
#include "WalletTransaction.h"
WalletTransaction::WalletTransaction() :
_uuid(),
_destinationUUID(),
_amount(),
_isFinalized(false)
{
}
WalletTransaction::WalletTransaction(const QUuid& destinationUUID, double amount) :
_uuid(QUuid::createUuid()),
_destinationUUID(destinationUUID),
@ -24,20 +33,35 @@ WalletTransaction::WalletTransaction(const QUuid& destinationUUID, double amount
}
const QString TRANSACTION_ID_KEY = "id";
const QString TRANSACTION_DESTINATION_WALLET_ID_KEY = "destination_wallet_id";
const QString TRANSACTION_AMOUNT_KEY = "amount";
const QString ROOT_OBJECT_TRANSACTION_KEY = "transaction";
QJsonDocument WalletTransaction::postJson() {
QJsonObject rootObject;
rootObject.insert(ROOT_OBJECT_TRANSACTION_KEY, toJson());
return QJsonDocument(rootObject);
}
QJsonObject WalletTransaction::toJson() {
QJsonObject transactionObject;
const QString TRANSCATION_ID_KEY = "id";
const QString TRANSACTION_DESTINATION_WALLET_ID_KEY = "destination_wallet_id";
const QString TRANSACTION_AMOUNT_KEY = "amount";
transactionObject.insert(TRANSCATION_ID_KEY, uuidStringWithoutCurlyBraces(_uuid));
transactionObject.insert(TRANSACTION_ID_KEY, uuidStringWithoutCurlyBraces(_uuid));
transactionObject.insert(TRANSACTION_DESTINATION_WALLET_ID_KEY, uuidStringWithoutCurlyBraces(_destinationUUID));
transactionObject.insert(TRANSACTION_AMOUNT_KEY, _amount);
const QString ROOT_OBJECT_TRANSACTION_KEY = "transaction";
rootObject.insert(ROOT_OBJECT_TRANSACTION_KEY, transactionObject);
return transactionObject;
}
void WalletTransaction::loadFromJson(const QJsonObject& jsonObject) {
// pull the destination wallet and ID of the transaction to match it
QJsonObject transactionObject = jsonObject.value("data").toObject().value(ROOT_OBJECT_TRANSACTION_KEY).toObject();
return QJsonDocument(rootObject);
_uuid = QUuid(transactionObject.value(TRANSACTION_ID_KEY).toString());
_destinationUUID = QUuid(transactionObject.value(TRANSACTION_DESTINATION_WALLET_ID_KEY).toString());
_amount = transactionObject.value(TRANSACTION_AMOUNT_KEY).toDouble();
}

View file

@ -18,6 +18,7 @@
class WalletTransaction : public QObject {
public:
WalletTransaction();
WalletTransaction(const QUuid& destinationUUID, double amount);
const QUuid& getUUID() const { return _uuid; }
@ -33,6 +34,8 @@ public:
void setIsFinalized(bool isFinalized) { _isFinalized = isFinalized; }
QJsonDocument postJson();
QJsonObject toJson();
void loadFromJson(const QJsonObject& jsonObject);
private:
QUuid _uuid;
QUuid _destinationUUID;

View file

@ -136,7 +136,13 @@ void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager::
QNetworkRequest authenticatedRequest;
QUrl requestURL = _authURL;
requestURL.setPath(path);
if (path.startsWith("/")) {
requestURL.setPath(path);
} else {
requestURL.setPath("/" + path);
}
requestURL.setQuery("access_token=" + _accountInfo.getAccessToken().token);
authenticatedRequest.setUrl(requestURL);