Skip to content

Commit ae4658b

Browse files
Refactory and reduce code duplication
1 parent 39e3d53 commit ae4658b

12 files changed

Lines changed: 153 additions & 287 deletions

src/cpp/common/py_monero_common.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#include "py_monero_common.h"
22
#include "utils/monero_utils.h"
33

4+
boost::property_tree::ptree json_to_property_node(const std::string& json) {
5+
// deserialize json to property node
6+
std::istringstream iss = json.empty() ? std::istringstream() : std::istringstream(json);
7+
boost::property_tree::ptree node;
8+
boost::property_tree::read_json(iss, node);
9+
return node;
10+
}
11+
412
PyThreadPoller::~PyThreadPoller() {
513
set_is_polling(false);
614
}
@@ -237,11 +245,8 @@ boost::optional<py::object> PyMoneroJsonResponse::get_result() const {
237245
}
238246

239247
std::shared_ptr<PyMoneroJsonResponse> PyMoneroJsonResponse::deserialize(const std::string& response_json) {
240-
// deserialize json to property node
241-
std::istringstream iss = response_json.empty() ? std::istringstream() : std::istringstream(response_json);
242-
boost::property_tree::ptree node;
243-
boost::property_tree::read_json(iss, node);
244-
248+
// parse json to property node
249+
boost::property_tree::ptree node = json_to_property_node(response_json);
245250
auto response = std::make_shared<PyMoneroJsonResponse>();
246251

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

285290
std::shared_ptr<PyMoneroPathResponse> PyMoneroPathResponse::deserialize(const std::string& response_json) {
286-
// deserialize json to property node
287-
std::istringstream iss = response_json.empty() ? std::istringstream() : std::istringstream(response_json);
288-
boost::property_tree::ptree node;
289-
boost::property_tree::read_json(iss, node);
291+
// parse json to property node
290292
auto response = std::make_shared<PyMoneroPathResponse>();
291-
response->m_response = node;
293+
response->m_response = json_to_property_node(response_json);
292294
return response;
293295
}
294296

src/cpp/daemon/py_monero_daemon_model.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PyMoneroDownloadUpdateParams::PyMoneroDownloadUpdateParams(const std::string& co
66
}
77

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

393-
if (key == std::string("transactions")) {
394-
auto node2 = it->second;
395-
396-
for(boost::property_tree::ptree::const_iterator it2 = node2.begin(); it2 != node2.end(); ++it2) {
397-
auto node3 = it2->second;
398-
auto tx = std::make_shared<monero::monero_tx>();
399-
tx->m_is_confirmed = false;
400-
tx->m_is_miner_tx = false;
401-
tx->m_in_tx_pool = true;
402-
tx->m_num_confirmations = 0;
403-
from_property_tree(node3, tx);
404-
txs.push_back(tx);
405-
}
406-
407-
return;
408-
}
409-
else if (key == std::string("txs")) {
394+
if (pool_txs || key == std::string("txs")) {
410395
auto node2 = it->second;
411396

412397
for(boost::property_tree::ptree::const_iterator it2 = node2.begin(); it2 != node2.end(); ++it2) {
413398
auto node3 = it2->second;
414399
auto tx = std::make_shared<monero::monero_tx>();
415400
tx->m_is_miner_tx = false;
401+
if (pool_txs) {
402+
tx->m_is_confirmed = false;
403+
tx->m_in_tx_pool = true;
404+
tx->m_num_confirmations = 0;
405+
}
416406
from_property_tree(node3, tx);
417407
txs.push_back(tx);
418408
}
419-
420-
return;
421409
}
422410
}
423411
}
@@ -851,7 +839,7 @@ rapidjson::Value PyMoneroStartMiningParams::to_rapidjson_val(rapidjson::Document
851839
rapidjson::Value root(rapidjson::kObjectType);
852840
rapidjson::Value value_str(rapidjson::kStringType);
853841
rapidjson::Value value_num(rapidjson::kNumberType);
854-
if (m_address != boost::none) monero_utils::add_json_member("miner_address", m_address.get(), allocator, root, value_str);
842+
if (m_miner_address != boost::none) monero_utils::add_json_member("miner_address", m_miner_address.get(), allocator, root, value_str);
855843
if (m_num_threads != boost::none) monero_utils::add_json_member("threads_count", m_num_threads.get(), allocator, root, value_num);
856844
if (m_is_background != boost::none) monero_utils::add_json_member("do_background_mining", m_is_background.get(), allocator, root);
857845
if (m_ignore_battery != boost::none) monero_utils::add_json_member("ignore_battery", m_ignore_battery.get(), allocator, root);

src/cpp/daemon/py_monero_daemon_model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class PyMoneroDownloadUpdateParams : public PyMoneroRequestParams {
5252

5353
class PyMoneroStartMiningParams : public PyMoneroJsonRequestParams {
5454
public:
55-
boost::optional<std::string> m_address;
55+
boost::optional<std::string> m_miner_address;
5656
boost::optional<int> m_num_threads;
5757
boost::optional<bool> m_is_background;
5858
boost::optional<bool> m_ignore_battery;

src/cpp/py_monero.cpp

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,47 +1137,17 @@ PYBIND11_MODULE(monero, m) {
11371137
.def_property("transfer_query",
11381138
[](const monero::monero_tx_query& self) { return self.m_transfer_query; },
11391139
[](std::shared_ptr<monero::monero_tx_query>& self, const boost::optional<std::shared_ptr<monero::monero_transfer_query>>& val) {
1140-
const auto old_query = self->m_transfer_query;
1141-
self->m_transfer_query = val;
1142-
if (self->m_transfer_query != boost::none) {
1143-
self->m_transfer_query.get()->m_tx_query = self;
1144-
}
1145-
if (old_query != boost::none) {
1146-
if (val != boost::none && val.get() == old_query.get()) {
1147-
return;
1148-
}
1149-
old_query.get()->m_tx_query = boost::none;
1150-
}
1140+
set_query(self, self->m_transfer_query, val);
11511141
})
11521142
.def_property("input_query",
11531143
[](const monero::monero_tx_query& self) { return self.m_input_query; },
11541144
[](std::shared_ptr<monero::monero_tx_query>& self, const boost::optional<std::shared_ptr<monero::monero_output_query>>& val) {
1155-
const auto old_query = self->m_input_query;
1156-
self->m_input_query = val;
1157-
if (self->m_input_query != boost::none) {
1158-
self->m_input_query.get()->m_tx_query = self;
1159-
}
1160-
if (old_query != boost::none) {
1161-
if (val != boost::none && val.get() == old_query.get()) {
1162-
return;
1163-
}
1164-
old_query.get()->m_tx_query = boost::none;
1165-
}
1145+
set_query(self, self->m_input_query, val);
11661146
})
11671147
.def_property("output_query",
11681148
[](const monero::monero_tx_query& self) { return self.m_output_query; },
11691149
[](std::shared_ptr<monero::monero_tx_query>& self, const boost::optional<std::shared_ptr<monero::monero_output_query>>& val) {
1170-
const auto old_query = self->m_output_query;
1171-
self->m_output_query = val;
1172-
if (self->m_output_query != boost::none) {
1173-
self->m_output_query.get()->m_tx_query = self;
1174-
}
1175-
if (old_query != boost::none) {
1176-
if (val != boost::none && val.get() == old_query.get()) {
1177-
return;
1178-
}
1179-
old_query.get()->m_tx_query = boost::none;
1180-
}
1150+
set_query(self, self->m_output_query, val);
11811151
})
11821152
.def("copy", [](const std::shared_ptr<monero::monero_tx_query>& self) {
11831153
auto tgt = std::make_shared<monero::monero_tx_query>();
@@ -1774,34 +1744,10 @@ PYBIND11_MODULE(monero, m) {
17741744
MONERO_CATCH_AND_RETHROW(self.get_txs());
17751745
})
17761746
.def("get_txs", [](PyMoneroWallet& self, const monero::monero_tx_query& query) {
1777-
try {
1778-
auto txs = self.get_txs(query);
1779-
PyMoneroUtils::sort_txs_wallet(txs, query.m_hashes);
1780-
return txs;
1781-
} catch (const PyMoneroRpcError& e) {
1782-
throw;
1783-
} catch (const PyMoneroError& e) {
1784-
throw;
1785-
}
1786-
catch (const std::exception& e) {
1787-
throw PyMoneroError(e.what());
1788-
}
1747+
MONERO_CATCH_AND_RETHROW(PyMoneroUtils::get_and_sort_txs(self, query));
17891748
}, py::arg("query"))
17901749
.def("get_txs", [](PyMoneroWallet& self, const std::vector<std::string>& tx_hashes) {
1791-
try {
1792-
monero_tx_query query;
1793-
query.m_hashes = tx_hashes;
1794-
auto txs = self.get_txs(query);
1795-
PyMoneroUtils::sort_txs_wallet(txs, query.m_hashes);
1796-
return txs;
1797-
} catch (const PyMoneroRpcError& e) {
1798-
throw;
1799-
} catch (const PyMoneroError& e) {
1800-
throw;
1801-
}
1802-
catch (const std::exception& e) {
1803-
throw PyMoneroError(e.what());
1804-
}
1750+
MONERO_CATCH_AND_RETHROW(PyMoneroUtils::get_and_sort_txs(self, tx_hashes));
18051751
}, py::arg("tx_hashes"))
18061752
.def("get_transfers", [](PyMoneroWallet& self, const monero::monero_transfer_query& query) {
18071753
MONERO_CATCH_AND_RETHROW(self.get_transfers(query));
@@ -2130,49 +2076,13 @@ PYBIND11_MODULE(monero, m) {
21302076
.def(py::init<const std::shared_ptr<PyMoneroRpcConnection>&>(), py::arg("rpc_connection"))
21312077
.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)
21322078
.def("create_wallet", [](PyMoneroWalletRpc& self, const std::shared_ptr<PyMoneroWalletConfig>& config) {
2133-
try {
2134-
self.create_wallet(config);
2135-
return &self;
2136-
}
2137-
catch(const PyMoneroRpcError& ex) {
2138-
throw;
2139-
}
2140-
catch(const PyMoneroError& ex) {
2141-
throw;
2142-
}
2143-
catch(const std::exception& ex) {
2144-
throw PyMoneroError(ex.what());
2145-
}
2079+
MONERO_CATCH_AND_RETHROW(self.create_wallet(config));
21462080
}, py::arg("config"))
21472081
.def("open_wallet", [](PyMoneroWalletRpc& self, const std::shared_ptr<PyMoneroWalletConfig>& config) {
2148-
try {
2149-
self.open_wallet(config);
2150-
return &self;
2151-
}
2152-
catch(const PyMoneroRpcError& ex) {
2153-
throw;
2154-
}
2155-
catch(const PyMoneroError& ex) {
2156-
throw;
2157-
}
2158-
catch(const std::exception& ex) {
2159-
throw PyMoneroError(ex.what());
2160-
}
2082+
MONERO_CATCH_AND_RETHROW(self.open_wallet(config));
21612083
}, py::arg("config"))
21622084
.def("open_wallet", [](PyMoneroWalletRpc& self, const std::string& name, const std::string& password) {
2163-
try {
2164-
self.open_wallet(name, password);
2165-
return &self;
2166-
}
2167-
catch(const PyMoneroRpcError& ex) {
2168-
throw;
2169-
}
2170-
catch(const PyMoneroError& ex) {
2171-
throw;
2172-
}
2173-
catch(const std::exception& ex) {
2174-
throw PyMoneroError(ex.what());
2175-
}
2085+
MONERO_CATCH_AND_RETHROW(self.open_wallet(name, password));
21762086
}, py::arg("name"), py::arg("password"))
21772087
.def("is_closed", [](const PyMoneroWalletRpc& self) {
21782088
MONERO_CATCH_AND_RETHROW(self.is_closed());

src/cpp/utils/py_monero_utils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,16 @@ void PyMoneroUtils::sort_txs_wallet(std::vector<std::shared_ptr<monero::monero_t
308308

309309
txs = std::move(sorted_txs);
310310
}
311+
312+
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) {
313+
auto txs = wallet.get_txs(tx_query);
314+
sort_txs_wallet(txs, tx_query.m_hashes);
315+
return txs;
316+
}
317+
318+
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) {
319+
monero_tx_query tx_query;
320+
tx_query.m_hashes = tx_hashes;
321+
return get_and_sort_txs(wallet, tx_query);
322+
}
323+

src/cpp/utils/py_monero_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "common/py_monero_common.h"
44
#include "utils/monero_utils.h"
5+
#include "wallet/monero_wallet.h"
56

67

78
class PyMoneroUtils {
@@ -45,6 +46,8 @@ class PyMoneroUtils {
4546
static double atomic_units_to_xmr(uint64_t amount_atomic_units);
4647

4748
static void sort_txs_wallet(std::vector<std::shared_ptr<monero::monero_tx_wallet>>& txs, const std::vector<std::string>& hashes);
49+
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);
50+
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);
4851

4952
private:
5053

src/cpp/wallet/py_monero_wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include "py_monero_wallet_model.h"
66
#include "wallet/monero_wallet.h"
77

8+
// TODO sorting is really needed?
9+
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);
10+
811
class PyMoneroWalletConnectionManagerListener : public PyMoneroConnectionManagerListener {
912
public:
1013
PyMoneroWalletConnectionManagerListener(monero::monero_wallet* wallet);

0 commit comments

Comments
 (0)