From 91e4cb2686755c298676fb5a4a5f10d9158f9d31 Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 24 Jun 2019 16:26:41 -0400 Subject: [PATCH] MoneroWallet::getTxs() impl ready to filter txs using request --- src/wallet/MoneroWallet.cpp | 59 ++++++++++++++++++++++++++++++++----- src/wallet/MoneroWallet.h | 4 ++- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index dfab017..a745612 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -528,18 +528,63 @@ namespace monero { // createSubaddress - vector> MoneroWallet::getTxs(const MoneroTxRequest& request) const { + vector> MoneroWallet::getTxs(MoneroTxRequest& request) const { cout << "getTxs(request)" << endl; - // print for debug - std::stringstream ss; - //boost::property_tree::write_json(ss, MoneroUtils::txRequestToPropertyTree(request), false); - //string requestStr = ss.str(); - //cout << "Tx request: " << requestStr << endl; + // print request + cout << "Tx request: " << request.serialize() << endl; + if (request.block != boost::none) cout << "Tx request's rooted at [block]: " << request.block.get()->serialize() << endl; + // normalize request + // TODO: this modifies original request so given req cannot be constant, make given request constant + if (request.transferRequest == boost::none) request.transferRequest = shared_ptr(new MoneroTransferRequest()); + shared_ptr transferRequest = request.transferRequest.get(); + // temporarily disable transfer request + request.transferRequest = boost::none; + // fetch all transfers that meet tx request + MoneroTransferRequest tempTransferReq; + tempTransferReq.txRequest = shared_ptr(&request); + vector> transfers = getTransfers(tempTransferReq); + // collect unique txs from transfers while retaining order + vector> txs = vector>(); + unordered_set> txsSet; + for (const shared_ptr& transfer : transfers) { + if (txsSet.find(transfer->tx) == txsSet.end()) { + txs.push_back(transfer->tx); + txsSet.insert(transfer->tx); + } + } + + // fetch and merge outputs if requested + MoneroOutputRequest tempOutputReq; + tempOutputReq.txRequest = shared_ptr(&request); + if (request.includeOutputs != boost::none && *request.includeOutputs) { + + // fetch outputs + vector> outputs = getOutputs(tempOutputReq); + + // build types for merging + map> txMap; + map> blockMap; + for (const shared_ptr& tx : txs) { + mergeTx(tx, txMap, blockMap, false); + } + + // merge output txs one time while retaining order + unordered_set> outputTxs; + for (const shared_ptr& output : outputs) { + shared_ptr tx = static_pointer_cast(output->tx); + if (outputTxs.find(tx) == outputTxs.end()) { + mergeTx(tx, txMap, blockMap, true); + outputTxs.insert(tx); + } + } + } + + // filter txs that don't meet transfer request throw runtime_error("getTxs() not implemented"); @@ -651,7 +696,7 @@ namespace monero { // TODO: this will modify original request, construct copy? add test MoneroTxRequest txReq = *(request.txRequest != boost::none ? *request.txRequest : shared_ptr(new MoneroTxRequest())); - // get outputs from wallet2 (referred to as "transfers" in wallet2 terminology) + // get output data from wallet2 tools::wallet2::transfer_container outputsW2; wallet2->get_transfers(outputsW2); diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index 45fb3e4..c63d80a 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -500,11 +500,13 @@ namespace monero { * Transactions must meet every criteria defined in the request in order to * be returned. All filtering is optional and no filtering is applied when * not defined. + * + * TODO: request should be const * * @param request filters query results (optional) * @return wallet transactions per the request */ - vector> getTxs(const MoneroTxRequest& request) const; + vector> getTxs(MoneroTxRequest& request) const; // /** // * Get all incoming and outgoing transfers to and from this wallet. An