From 89ec6344226ac7836d5319605dcde95d241b692a Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Fri, 3 Mar 2017 17:09:49 +0800 Subject: [PATCH] fix: no more parsing json of each tx in mempool 5 times! --- src/page.h | 29 ++++++++++++++++------------ src/tools.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/tools.h | 12 ++++++++++++ 3 files changed, 82 insertions(+), 12 deletions(-) diff --git a/src/page.h b/src/page.h index bc92a67..057d70d 100644 --- a/src/page.h +++ b/src/page.h @@ -557,30 +557,35 @@ public: delta_time[3], delta_time[4]); } - //cout << _tx_info.tx_json << endl; - // sum xmr in inputs and ouputs in the given tx - pair sum_inputs = xmreg::sum_money_in_inputs(_tx_info.tx_json); - pair sum_outputs = xmreg::sum_money_in_outputs(_tx_info.tx_json); - uint64_t num_nonrct_inputs = xmreg::count_nonrct_inputs(_tx_info.tx_json); + pair sum_inputs; + pair sum_outputs; + uint64_t num_nonrct_inputs; // get mixin number in each transaction - vector mixin_numbers = xmreg::get_mixin_no(_tx_info.tx_json); - - uint64_t mixin_no = 0; + vector mixin_numbers; - if (!mixin_numbers.empty()) - mixin_no = mixin_numbers.at(0) - 1; - - json j_tx; + uint64_t mixin_no {0}; string is_ringct_str {"N/A"}; string rct_type_str {"N/A"}; try { + json j_tx; + j_tx = json::parse(_tx_info.tx_json); + // sum xmr in inputs and ouputs in the given tx + sum_inputs = xmreg::sum_money_in_inputs(j_tx); + sum_outputs = xmreg::sum_money_in_outputs(j_tx); + num_nonrct_inputs = xmreg::count_nonrct_inputs(j_tx); + mixin_numbers = xmreg::get_mixin_no(j_tx); + + if (!mixin_numbers.empty()) + mixin_no = mixin_numbers.at(0) - 1; + + if (j_tx["version"].get() > 1) { is_ringct_str = "yes"; diff --git a/src/tools.cpp b/src/tools.cpp index a6e0da3..f641dd5 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -343,7 +343,19 @@ sum_money_in_outputs(const string& json_str) return sum_xmr; }; +pair +sum_money_in_outputs(const json& _json) +{ + pair sum_xmr {0ULL, 0ULL}; + for (const json& vout: _json["vout"]) + { + sum_xmr.first += vout["amount"].get(); + ++sum_xmr.second; + } + + return sum_xmr; +}; uint64_t sum_money_in_inputs(const transaction& tx) @@ -395,6 +407,21 @@ sum_money_in_inputs(const string& json_str) return sum_xmr; }; + +pair +sum_money_in_inputs(const json& _json) +{ + pair sum_xmr {0, 0}; + + for (const json& vin: _json["vin"]) + { + sum_xmr.first += vin["key"]["amount"].get(); + ++sum_xmr.second; + } + + return sum_xmr; +}; + uint64_t count_nonrct_inputs(const transaction& tx) { @@ -447,6 +474,22 @@ count_nonrct_inputs(const string& json_str) return num; }; + +uint64_t +count_nonrct_inputs(const json& _json) +{ + uint64_t num {0}; + + for (const json& vin: _json["vin"]) + { + uint64_t amount = vin["key"]["amount"].get(); + if (amount != 0) + ++num; + } + + return num; +}; + array sum_money_in_tx(const transaction& tx) { @@ -590,6 +633,16 @@ get_mixin_no(const string& json_str) return mixin_no; } +vector +get_mixin_no(const json& _json) +{ + vector mixin_no; + + mixin_no.push_back(_json["vin"].at(0)["key"]["key_offsets"].size()); + + return mixin_no; +} + vector diff --git a/src/tools.h b/src/tools.h index e3b9372..1ad695b 100644 --- a/src/tools.h +++ b/src/tools.h @@ -133,18 +133,27 @@ sum_money_in_outputs(const transaction& tx); pair sum_money_in_outputs(const string& json_str); +pair +sum_money_in_outputs(const json& _json); + uint64_t sum_money_in_inputs(const transaction& tx); pair sum_money_in_inputs(const string& json_str); +pair +sum_money_in_inputs(const json& _json); + uint64_t count_nonrct_inputs(const transaction& tx); uint64_t count_nonrct_inputs(const string& json_str); +uint64_t +count_nonrct_inputs(const json& _json); + array sum_money_in_tx(const transaction& tx); @@ -160,6 +169,9 @@ get_mixin_no(const transaction& tx); vector get_mixin_no(const string& json_str); +vector +get_mixin_no(const json& _json); + vector get_mixin_no_in_txs(const vector& txs);