From cacd5206ef709d3501d4dbd6d9ec64618dc4b1b9 Mon Sep 17 00:00:00 2001 From: sowle Date: Tue, 7 Apr 2020 14:30:38 +0300 Subject: [PATCH] wallet RPC: search_for_transactions added --- src/wallet/wallet_public_structs_defs.h | 46 ++++++++++++++++++++++ src/wallet/wallet_rpc_server.cpp | 51 +++++++++++++++++++++++++ src/wallet/wallet_rpc_server.h | 2 + 3 files changed, 99 insertions(+) diff --git a/src/wallet/wallet_public_structs_defs.h b/src/wallet/wallet_public_structs_defs.h index 6bebcd37..9b1a7607 100644 --- a/src/wallet/wallet_public_structs_defs.h +++ b/src/wallet/wallet_public_structs_defs.h @@ -871,6 +871,52 @@ namespace wallet_public }; }; + struct COMMAND_RPC_SEARCH_FOR_TRANSACTIONS + { + struct request + { + crypto::hash tx_id; + bool in; + bool out; + //bool pending; + //bool failed; + bool pool; + bool filter_by_height; + uint64_t min_height; + uint64_t max_height; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE_POD_AS_HEX_STRING(tx_id) + KV_SERIALIZE(in) + KV_SERIALIZE(out) + //KV_SERIALIZE(pending) + //KV_SERIALIZE(failed) + KV_SERIALIZE(pool) + KV_SERIALIZE(filter_by_height) + KV_SERIALIZE(min_height) + KV_SERIALIZE(max_height) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + std::list in; + std::list out; + //std::list pending; + //std::list failed; + std::list pool; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(in) + KV_SERIALIZE(out) + //KV_SERIALIZE(pending) + //KV_SERIALIZE(failed) + KV_SERIALIZE(pool) + END_KV_SERIALIZE_MAP() + }; + }; + + inline std::string get_escrow_contract_state_name(uint32_t state) { diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 698a181a..e8fbb098 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -578,6 +578,57 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_search_for_transactions(const wallet_public::COMMAND_RPC_SEARCH_FOR_TRANSACTIONS::request& req, wallet_public::COMMAND_RPC_SEARCH_FOR_TRANSACTIONS::response& res, epee::json_rpc::error& er, connection_context& cntx) + { + bool tx_id_specified = req.tx_id != currency::null_hash; + + // + m_wallet.enumerate_transfers_history([&](const wallet_public::wallet_transfer_info& wti) -> bool { + + if (tx_id_specified) + { + if (wti.tx_hash != req.tx_id) + return true; // continue + } + + if (req.filter_by_height) + { + if (!wti.height) // unconfirmed + return true; // continue + + if (wti.height < req.min_height) + { + // no need to scan more + return false; // stop + } + if (wti.height > req.max_height) + { + return true; // continue + } + } + + if (wti.is_income && req.in) + res.in.push_back(wti); + + if (!wti.is_income && req.out) + res.out.push_back(wti); + + return true; // continue + }, false /* enumerate_forward */); + + + if (req.pool) + { + m_wallet.enumerate_unconfirmed_transfers([&](const wallet_public::wallet_transfer_info& wti) -> bool { + if ((wti.is_income && req.in) || (!wti.is_income && req.out)) + res.pool.push_back(wti); + return true; // continue + }); + } + + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ bool wallet_rpc_server::on_contracts_send_proposal(const wallet_public::COMMAND_CONTRACTS_SEND_PROPOSAL::request& req, wallet_public::COMMAND_CONTRACTS_SEND_PROPOSAL::response& res, epee::json_rpc::error& er, connection_context& cntx) { WALLET_RPC_BEGIN_TRY_ENTRY(); diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index b44b496b..aa4ee8db 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -50,6 +50,7 @@ namespace tools MAP_JON_RPC_WE("sweep_below", on_sweep_below, wallet_public::COMMAND_SWEEP_BELOW) MAP_JON_RPC_WE("sign_transfer", on_sign_transfer, wallet_public::COMMAND_SIGN_TRANSFER) MAP_JON_RPC_WE("submit_transfer", on_submit_transfer, wallet_public::COMMAND_SUBMIT_TRANSFER) + MAP_JON_RPC_WE("search_for_transactions", on_search_for_transactions, wallet_public::COMMAND_RPC_SEARCH_FOR_TRANSACTIONS) //contracts API Skipped block by timestamp, height: 94766, block time 1563035089 MAP_JON_RPC_WE("contracts_send_proposal", on_contracts_send_proposal, wallet_public::COMMAND_CONTRACTS_SEND_PROPOSAL) MAP_JON_RPC_WE("contracts_accept_proposal", on_contracts_accept_proposal, wallet_public::COMMAND_CONTRACTS_ACCEPT_PROPOSAL) @@ -79,6 +80,7 @@ namespace tools bool on_sweep_below(const wallet_public::COMMAND_SWEEP_BELOW::request& req, wallet_public::COMMAND_SWEEP_BELOW::response& res, epee::json_rpc::error& er, connection_context& cntx); bool on_sign_transfer(const wallet_public::COMMAND_SIGN_TRANSFER::request& req, wallet_public::COMMAND_SIGN_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx); bool on_submit_transfer(const wallet_public::COMMAND_SUBMIT_TRANSFER::request& req, wallet_public::COMMAND_SUBMIT_TRANSFER::response& res, epee::json_rpc::error& er, connection_context& cntx); + bool on_search_for_transactions(const wallet_public::COMMAND_RPC_SEARCH_FOR_TRANSACTIONS::request& req, wallet_public::COMMAND_RPC_SEARCH_FOR_TRANSACTIONS::response& res, epee::json_rpc::error& er, connection_context& cntx); bool on_contracts_send_proposal(const wallet_public::COMMAND_CONTRACTS_SEND_PROPOSAL::request& req, wallet_public::COMMAND_CONTRACTS_SEND_PROPOSAL::response& res, epee::json_rpc::error& er, connection_context& cntx); bool on_contracts_accept_proposal(const wallet_public::COMMAND_CONTRACTS_ACCEPT_PROPOSAL::request& req, wallet_public::COMMAND_CONTRACTS_ACCEPT_PROPOSAL::response& res, epee::json_rpc::error& er, connection_context& cntx);