Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/cpp/common/py_monero_common.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "py_monero_common.h"
#include "utils/monero_utils.h"

boost::property_tree::ptree json_to_property_node(const std::string& json) {
// deserialize json to property node
std::istringstream iss = json.empty() ? std::istringstream() : std::istringstream(json);
boost::property_tree::ptree node;
boost::property_tree::read_json(iss, node);
return node;
}

PyThreadPoller::~PyThreadPoller() {
set_is_polling(false);
}
Expand Down Expand Up @@ -237,11 +245,8 @@ boost::optional<py::object> PyMoneroJsonResponse::get_result() const {
}

std::shared_ptr<PyMoneroJsonResponse> PyMoneroJsonResponse::deserialize(const std::string& response_json) {
// deserialize json to property node
std::istringstream iss = response_json.empty() ? std::istringstream() : std::istringstream(response_json);
boost::property_tree::ptree node;
boost::property_tree::read_json(iss, node);

// parse json to property node
boost::property_tree::ptree node = json_to_property_node(response_json);
auto response = std::make_shared<PyMoneroJsonResponse>();

for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) {
Expand Down Expand Up @@ -283,12 +288,9 @@ boost::optional<py::object> PyMoneroPathResponse::get_response() const {
}

std::shared_ptr<PyMoneroPathResponse> PyMoneroPathResponse::deserialize(const std::string& response_json) {
// deserialize json to property node
std::istringstream iss = response_json.empty() ? std::istringstream() : std::istringstream(response_json);
boost::property_tree::ptree node;
boost::property_tree::read_json(iss, node);
// parse json to property node
auto response = std::make_shared<PyMoneroPathResponse>();
response->m_response = node;
response->m_response = json_to_property_node(response_json);
return response;
}

Expand Down
30 changes: 9 additions & 21 deletions src/cpp/daemon/py_monero_daemon_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PyMoneroDownloadUpdateParams::PyMoneroDownloadUpdateParams(const std::string& co
}

PyMoneroStartMiningParams::PyMoneroStartMiningParams(const std::string& address, int num_threads, bool is_background, bool ignore_battery):
m_address(address),
m_miner_address(address),
m_num_threads(num_threads),
m_is_background(is_background),
m_ignore_battery(ignore_battery) {
Expand Down Expand Up @@ -389,35 +389,23 @@ void PyMoneroTx::from_property_tree(const boost::property_tree::ptree& node, con
void PyMoneroTx::from_property_tree(const boost::property_tree::ptree& node, std::vector<std::shared_ptr<monero::monero_tx>>& txs) {
for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
bool pool_txs = key == std::string("transactions");

if (key == std::string("transactions")) {
auto node2 = it->second;

for(boost::property_tree::ptree::const_iterator it2 = node2.begin(); it2 != node2.end(); ++it2) {
auto node3 = it2->second;
auto tx = std::make_shared<monero::monero_tx>();
tx->m_is_confirmed = false;
tx->m_is_miner_tx = false;
tx->m_in_tx_pool = true;
tx->m_num_confirmations = 0;
from_property_tree(node3, tx);
txs.push_back(tx);
}

return;
}
else if (key == std::string("txs")) {
if (pool_txs || key == std::string("txs")) {
auto node2 = it->second;

for(boost::property_tree::ptree::const_iterator it2 = node2.begin(); it2 != node2.end(); ++it2) {
auto node3 = it2->second;
auto tx = std::make_shared<monero::monero_tx>();
tx->m_is_miner_tx = false;
if (pool_txs) {
tx->m_is_confirmed = false;
tx->m_in_tx_pool = true;
tx->m_num_confirmations = 0;
}
from_property_tree(node3, tx);
txs.push_back(tx);
}

return;
}
}
}
Expand Down Expand Up @@ -851,7 +839,7 @@ rapidjson::Value PyMoneroStartMiningParams::to_rapidjson_val(rapidjson::Document
rapidjson::Value root(rapidjson::kObjectType);
rapidjson::Value value_str(rapidjson::kStringType);
rapidjson::Value value_num(rapidjson::kNumberType);
if (m_address != boost::none) monero_utils::add_json_member("miner_address", m_address.get(), allocator, root, value_str);
if (m_miner_address != boost::none) monero_utils::add_json_member("miner_address", m_miner_address.get(), allocator, root, value_str);
if (m_num_threads != boost::none) monero_utils::add_json_member("threads_count", m_num_threads.get(), allocator, root, value_num);
if (m_is_background != boost::none) monero_utils::add_json_member("do_background_mining", m_is_background.get(), allocator, root);
if (m_ignore_battery != boost::none) monero_utils::add_json_member("ignore_battery", m_ignore_battery.get(), allocator, root);
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/daemon/py_monero_daemon_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PyMoneroDownloadUpdateParams : public PyMoneroRequestParams {

class PyMoneroStartMiningParams : public PyMoneroJsonRequestParams {
public:
boost::optional<std::string> m_address;
boost::optional<std::string> m_miner_address;
boost::optional<int> m_num_threads;
boost::optional<bool> m_is_background;
boost::optional<bool> m_ignore_battery;
Expand Down
106 changes: 8 additions & 98 deletions src/cpp/py_monero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,47 +1137,17 @@ PYBIND11_MODULE(monero, m) {
.def_property("transfer_query",
[](const monero::monero_tx_query& self) { return self.m_transfer_query; },
[](std::shared_ptr<monero::monero_tx_query>& self, const boost::optional<std::shared_ptr<monero::monero_transfer_query>>& val) {
const auto old_query = self->m_transfer_query;
self->m_transfer_query = val;
if (self->m_transfer_query != boost::none) {
self->m_transfer_query.get()->m_tx_query = self;
}
if (old_query != boost::none) {
if (val != boost::none && val.get() == old_query.get()) {
return;
}
old_query.get()->m_tx_query = boost::none;
}
set_query(self, self->m_transfer_query, val);
})
.def_property("input_query",
[](const monero::monero_tx_query& self) { return self.m_input_query; },
[](std::shared_ptr<monero::monero_tx_query>& self, const boost::optional<std::shared_ptr<monero::monero_output_query>>& val) {
const auto old_query = self->m_input_query;
self->m_input_query = val;
if (self->m_input_query != boost::none) {
self->m_input_query.get()->m_tx_query = self;
}
if (old_query != boost::none) {
if (val != boost::none && val.get() == old_query.get()) {
return;
}
old_query.get()->m_tx_query = boost::none;
}
set_query(self, self->m_input_query, val);
})
.def_property("output_query",
[](const monero::monero_tx_query& self) { return self.m_output_query; },
[](std::shared_ptr<monero::monero_tx_query>& self, const boost::optional<std::shared_ptr<monero::monero_output_query>>& val) {
const auto old_query = self->m_output_query;
self->m_output_query = val;
if (self->m_output_query != boost::none) {
self->m_output_query.get()->m_tx_query = self;
}
if (old_query != boost::none) {
if (val != boost::none && val.get() == old_query.get()) {
return;
}
old_query.get()->m_tx_query = boost::none;
}
set_query(self, self->m_output_query, val);
})
.def("copy", [](const std::shared_ptr<monero::monero_tx_query>& self) {
auto tgt = std::make_shared<monero::monero_tx_query>();
Expand Down Expand Up @@ -1774,34 +1744,10 @@ PYBIND11_MODULE(monero, m) {
MONERO_CATCH_AND_RETHROW(self.get_txs());
})
.def("get_txs", [](PyMoneroWallet& self, const monero::monero_tx_query& query) {
try {
auto txs = self.get_txs(query);
PyMoneroUtils::sort_txs_wallet(txs, query.m_hashes);
return txs;
} catch (const PyMoneroRpcError& e) {
throw;
} catch (const PyMoneroError& e) {
throw;
}
catch (const std::exception& e) {
throw PyMoneroError(e.what());
}
MONERO_CATCH_AND_RETHROW(PyMoneroUtils::get_and_sort_txs(self, query));
}, py::arg("query"))
.def("get_txs", [](PyMoneroWallet& self, const std::vector<std::string>& tx_hashes) {
try {
monero_tx_query query;
query.m_hashes = tx_hashes;
auto txs = self.get_txs(query);
PyMoneroUtils::sort_txs_wallet(txs, query.m_hashes);
return txs;
} catch (const PyMoneroRpcError& e) {
throw;
} catch (const PyMoneroError& e) {
throw;
}
catch (const std::exception& e) {
throw PyMoneroError(e.what());
}
MONERO_CATCH_AND_RETHROW(PyMoneroUtils::get_and_sort_txs(self, tx_hashes));
}, py::arg("tx_hashes"))
.def("get_transfers", [](PyMoneroWallet& self, const monero::monero_transfer_query& query) {
MONERO_CATCH_AND_RETHROW(self.get_transfers(query));
Expand Down Expand Up @@ -2130,49 +2076,13 @@ PYBIND11_MODULE(monero, m) {
.def(py::init<const std::shared_ptr<PyMoneroRpcConnection>&>(), py::arg("rpc_connection"))
.def(py::init<const std::string&, const std::string&, const std::string&, const std::string&, const std::string&, uint64_t>(), py::arg("uri") = "", py::arg("username") = "", py::arg("password") = "", py::arg("proxy_uri") = "", py::arg("zmq_uri") = "", py::arg("timeout") = 20000)
.def("create_wallet", [](PyMoneroWalletRpc& self, const std::shared_ptr<PyMoneroWalletConfig>& config) {
try {
self.create_wallet(config);
return &self;
}
catch(const PyMoneroRpcError& ex) {
throw;
}
catch(const PyMoneroError& ex) {
throw;
}
catch(const std::exception& ex) {
throw PyMoneroError(ex.what());
}
MONERO_CATCH_AND_RETHROW(self.create_wallet(config));
}, py::arg("config"))
.def("open_wallet", [](PyMoneroWalletRpc& self, const std::shared_ptr<PyMoneroWalletConfig>& config) {
try {
self.open_wallet(config);
return &self;
}
catch(const PyMoneroRpcError& ex) {
throw;
}
catch(const PyMoneroError& ex) {
throw;
}
catch(const std::exception& ex) {
throw PyMoneroError(ex.what());
}
MONERO_CATCH_AND_RETHROW(self.open_wallet(config));
}, py::arg("config"))
.def("open_wallet", [](PyMoneroWalletRpc& self, const std::string& name, const std::string& password) {
try {
self.open_wallet(name, password);
return &self;
}
catch(const PyMoneroRpcError& ex) {
throw;
}
catch(const PyMoneroError& ex) {
throw;
}
catch(const std::exception& ex) {
throw PyMoneroError(ex.what());
}
MONERO_CATCH_AND_RETHROW(self.open_wallet(name, password));
}, py::arg("name"), py::arg("password"))
.def("is_closed", [](const PyMoneroWalletRpc& self) {
MONERO_CATCH_AND_RETHROW(self.is_closed());
Expand Down
13 changes: 13 additions & 0 deletions src/cpp/utils/py_monero_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,16 @@ void PyMoneroUtils::sort_txs_wallet(std::vector<std::shared_ptr<monero::monero_t

txs = std::move(sorted_txs);
}

std::vector<std::shared_ptr<monero::monero_tx_wallet>> PyMoneroUtils::get_and_sort_txs(const monero::monero_wallet& wallet, const monero::monero_tx_query& tx_query) {
auto txs = wallet.get_txs(tx_query);
sort_txs_wallet(txs, tx_query.m_hashes);
return txs;
}

std::vector<std::shared_ptr<monero::monero_tx_wallet>> PyMoneroUtils::get_and_sort_txs(const monero::monero_wallet& wallet, const std::vector<std::string>& tx_hashes) {
monero_tx_query tx_query;
tx_query.m_hashes = tx_hashes;
return get_and_sort_txs(wallet, tx_query);
}

3 changes: 3 additions & 0 deletions src/cpp/utils/py_monero_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common/py_monero_common.h"
#include "utils/monero_utils.h"
#include "wallet/monero_wallet.h"


class PyMoneroUtils {
Expand Down Expand Up @@ -45,6 +46,8 @@ class PyMoneroUtils {
static double atomic_units_to_xmr(uint64_t amount_atomic_units);

static void sort_txs_wallet(std::vector<std::shared_ptr<monero::monero_tx_wallet>>& txs, const std::vector<std::string>& hashes);
static std::vector<std::shared_ptr<monero::monero_tx_wallet>> get_and_sort_txs(const monero::monero_wallet& wallet, const std::vector<std::string>& tx_hashes);
static std::vector<std::shared_ptr<monero::monero_tx_wallet>> get_and_sort_txs(const monero::monero_wallet& wallet, const monero::monero_tx_query& tx_query);

private:

Expand Down
3 changes: 3 additions & 0 deletions src/cpp/wallet/py_monero_wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "py_monero_wallet_model.h"
#include "wallet/monero_wallet.h"

// TODO sorting is really needed?
std::vector<std::shared_ptr<monero::monero_tx_wallet>> get_and_sort_txs(const monero::monero_wallet& wallet, const monero::monero_tx_query& tx_query);

class PyMoneroWalletConnectionManagerListener : public PyMoneroConnectionManagerListener {
public:
PyMoneroWalletConnectionManagerListener(monero::monero_wallet* wallet);
Expand Down
Loading
Loading