From fba1d6889c569d2ec1fd57c27e53e196536f3b8c Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 3 Sep 2019 13:51:20 -0400 Subject: [PATCH] get txs with output query --- src/wallet/monero_wallet.cpp | 14 ++++++++---- src/wallet/monero_wallet_model.cpp | 36 +++++++++++++++++++++++------- src/wallet/monero_wallet_model.h | 5 ++++- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/wallet/monero_wallet.cpp b/src/wallet/monero_wallet.cpp index 0eab003..077eee3 100644 --- a/src/wallet/monero_wallet.cpp +++ b/src/wallet/monero_wallet.cpp @@ -1399,14 +1399,17 @@ namespace monero { shared_ptr query_ptr = make_shared(query); // convert to shared pointer for copy shared_ptr _query = query_ptr->copy(query_ptr, make_shared()); // deep copy if (_query->m_transfer_query == boost::none) _query->m_transfer_query = make_shared(); - shared_ptr transfer_query = _query->m_transfer_query.get(); + if (_query->m_output_query == boost::none) _query->m_output_query = make_shared(); // print query if (_query->m_block != boost::none) MTRACE("Tx query's rooted at [block]: " << _query->m_block.get()->serialize()); else MTRACE("Tx _query: " << _query->serialize()); - - // temporarily disable transfer query + + // temporarily disable transfer and output queries in order to collect all tx information + shared_ptr transfer_query = _query->m_transfer_query.get(); + shared_ptr output_query = _query->m_output_query.get(); _query->m_transfer_query = boost::none; + _query->m_output_query = boost::none; // fetch all transfers that meet tx query monero_transfer_query temp_transfer_query; @@ -1449,8 +1452,11 @@ namespace monero { } } - // filter txs that don't meet transfer query // TODO: port this updated version to js + // restore transfer and output queries _query->m_transfer_query = transfer_query; + _query->m_output_query = output_query; + + // filter txs that don't meet transfer query vector> queried_txs; vector>::iterator tx_iter = txs.begin(); while (tx_iter != txs.end()) { diff --git a/src/wallet/monero_wallet_model.cpp b/src/wallet/monero_wallet_model.cpp index f11a38d..1604e86 100644 --- a/src/wallet/monero_wallet_model.cpp +++ b/src/wallet/monero_wallet_model.cpp @@ -244,10 +244,10 @@ namespace monero { // at least one transfer must meet transfer query if defined if (m_transfer_query != boost::none) { bool matchFound = false; - if (tx->m_outgoing_transfer != boost::none && (*m_transfer_query)->meets_criteria((*tx->m_outgoing_transfer).get())) matchFound = true; + if (tx->m_outgoing_transfer != boost::none && m_transfer_query.get()->meets_criteria(tx->m_outgoing_transfer.get().get())) matchFound = true; else if (!tx->m_incoming_transfers.empty()) { - for (const shared_ptr& incomingTransfer : tx->m_incoming_transfers) { - if ((*m_transfer_query)->meets_criteria(incomingTransfer.get())) { + for (const shared_ptr& incoming_transfer : tx->m_incoming_transfers) { + if (m_transfer_query.get()->meets_criteria(incoming_transfer.get())) { matchFound = true; break; } @@ -256,6 +256,20 @@ namespace monero { if (!matchFound) return false; } + // at least one output must meet output query if defined + if (m_output_query != boost::none && !m_output_query.get()->is_default()) { + if (tx->m_vouts.empty()) return false; + bool matchFound = false; + for (const shared_ptr& vout : tx->m_vouts) { + shared_ptr vout_wallet = static_pointer_cast(vout); + if (m_output_query.get()->meets_criteria(vout_wallet.get())) { + matchFound = true; + break; + } + } + if (!matchFound) return false; + } + // filter on having a payment id if (m_has_payment_id != boost::none) { if (*m_has_payment_id && tx->m_payment_id == boost::none) return false; @@ -581,6 +595,12 @@ namespace monero { // ------------------------ MONERO OUTPUT REQUEST --------------------------- + // initialize static empty output for is_default() check + const unique_ptr monero_output_query::M_EMPTY_OUTPUT = unique_ptr(new monero_output_wallet()); + bool monero_output_query::is_default() const { + return meets_criteria(monero_output_query::M_EMPTY_OUTPUT.get()); + } + shared_ptr monero_output_query::copy(const shared_ptr& src, const shared_ptr& tgt) const { return monero_output_query::copy(static_pointer_cast(src), static_pointer_cast(tgt)); }; @@ -610,11 +630,11 @@ namespace monero { bool monero_output_query::meets_criteria(monero_output_wallet* output) const { // filter on output - if (m_account_index != boost::none && *m_account_index != *output->m_account_index) return false; - if (m_subaddress_index != boost::none && *m_subaddress_index != *output->m_subaddress_index) return false; - if (m_amount != boost::none && *m_amount != *output->m_amount) return false; - if (m_is_spent != boost::none && *m_is_spent != *output->m_is_spent) return false; - if (m_is_unlocked != boost::none && *m_is_unlocked != *output->m_is_unlocked) return false; + if (m_account_index != boost::none && (output->m_account_index == boost::none || *m_account_index != *output->m_account_index)) return false; + if (m_subaddress_index != boost::none && (output->m_subaddress_index == boost::none || *m_subaddress_index != *output->m_subaddress_index)) return false; + if (m_amount != boost::none && (output->m_amount == boost::none || *m_amount != *output->m_amount)) return false; + if (m_is_spent != boost::none && (output->m_is_spent == boost::none || *m_is_spent != *output->m_is_spent)) return false; + if (m_is_unlocked != boost::none && (output->m_is_unlocked == boost::none || *m_is_unlocked != *output->m_is_unlocked)) return false; // filter on output key image if (m_key_image != boost::none) { diff --git a/src/wallet/monero_wallet_model.h b/src/wallet/monero_wallet_model.h index 88d2476..3a8698b 100644 --- a/src/wallet/monero_wallet_model.h +++ b/src/wallet/monero_wallet_model.h @@ -242,13 +242,16 @@ namespace monero { boost::optional> m_tx_query; // TODO: necessary to override all super classes? + bool is_default() const; shared_ptr copy(const shared_ptr& src, const shared_ptr& tgt) const; shared_ptr copy(const shared_ptr& src, const shared_ptr& tgt) const; shared_ptr copy(const shared_ptr& src, const shared_ptr& tgt) const; boost::property_tree::ptree to_property_tree() const; bool meets_criteria(monero_output_wallet* output) const; - }; + private: + static const unique_ptr M_EMPTY_OUTPUT; + }; /** * Models a Monero transaction in the context of a wallet.