account for spendings in mempool txs search

pull/4/head
moneroexamples 7 years ago
parent 199a8dbf60
commit a80dbb8680

@ -1788,7 +1788,7 @@ var cnUtil = (function(initConfig) {
oe.index = out.global_index.toString();
oe.key = out.public_key;
console.log('outputs[',i,']: ', outputs[i]);
//console.log('outputs[',i,']: ', outputs[i]);
if (rct){
if (out.rct){
@ -1835,7 +1835,7 @@ var cnUtil = (function(initConfig) {
}
sources.push(src);
}
console.log('sources: ', sources);
//console.log('sources: ', sources);
var change = {
amount: JSBigInt.ZERO
};

@ -147,24 +147,31 @@ thinwalletCtrls.controller('AccountCtrl', function($scope, $rootScope, $http, $q
$scope.blockchain_height = data.blockchain_height || 0;
var transactions = data.transactions || [];
for (var i = 0; i < transactions.length; ++i) {
if ((transactions[i].spent_outputs || []).length > 0) {
for (var j = 0; j < transactions[i].spent_outputs.length; ++j) {
if ((transactions[i].spent_outputs || []).length > 0)
{
for (var j = 0; j < transactions[i].spent_outputs.length; ++j)
{
var key_image = AccountService.cachedKeyImage(
transactions[i].spent_outputs[j].tx_pub_key,
transactions[i].spent_outputs[j].out_index
);
if (transactions[i].spent_outputs[j].key_image !== key_image) {
if (transactions[i].spent_outputs[j].key_image !== key_image)
{
transactions[i].total_sent = new JSBigInt(transactions[i].total_sent).subtract(transactions[i].spent_outputs[j].amount).toString();
transactions[i].spent_outputs.splice(j, 1);
j--;
}
}
}
if (new JSBigInt(transactions[i].total_received || 0).add(transactions[i].total_sent || 0).compare(0) <= 0) {
if (new JSBigInt(transactions[i].total_received || 0).add(transactions[i].total_sent || 0).compare(0) <= 0)
{
transactions.splice(i, 1);
i--;
continue;
}
//console.log(transactions[i].total_received, transactions[i].total_sent);
transactions[i].amount = new JSBigInt(transactions[i].total_received || 0).subtract(transactions[i].total_sent || 0).toString();
transactions[i].approx_float_amount = parseFloat(cnUtil.formatMoney(transactions[i].amount));
transactions[i].timestamp = new Date(transactions[i].timestamp);

@ -115,7 +115,8 @@
<div class="w-col w-col-7">
<div class="transaction-detail transaction-page move-text-div">
<div class="transaction-address">
<span class="bold">Payment ID:</span> &nbsp;&nbsp;{{tx.payment_id || "N/A"}}
<span class="bold">Block no:</span> &nbsp;&nbsp;{{tx_is_mempool(tx) ? 'N/A (tx in mempool)' : tx.height}}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="bold">Payment ID:</span> &nbsp;&nbsp;{{tx.payment_id || "N/A"}}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="bold">Is coinbase?:</span> &nbsp;&nbsp;{{tx.coinbase}}
</div>
</div>

@ -107,7 +107,8 @@
<div class="w-col w-col-7">
<div class="transaction-detail transaction-page move-text-div">
<div class="transaction-address transaction-page">
<span class="bold">Payment ID:</span> &nbsp;&nbsp;{{tx.payment_id || "N/A"}}
<span class="bold">Block no:</span> &nbsp;&nbsp;{{tx_is_mempool(tx) ? 'N/A (tx in mempool)' : tx.height}}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="bold">Payment ID:</span> &nbsp;&nbsp;{{tx.payment_id || "N/A"}}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="bold">Is coinbase?:</span> &nbsp;&nbsp;{{tx.coinbase}}
</div>
</div>

@ -256,8 +256,11 @@ TxSearch::search()
//throw TxSearchException("out_mysql_id is zero!");
}
// add the new output to our cash of known outputs
known_outputs_keys.push_back(make_pair(out_info.pub_key, out_info.amount));
{
std::lock_guard<std::mutex> lck (getting_known_outputs_keys);
known_outputs_keys.push_back(make_pair(out_info.pub_key, out_info.amount));
}
} // for (auto &out_k_idx: found_mine_outputs)
@ -279,6 +282,8 @@ TxSearch::search()
// SECOND component: Checking for our key images, i.e., inputs.
// no need mutex here, as this will be exectued only after
// the above. there is no threads here.
oi_identification.identify_inputs(known_outputs_keys);
@ -473,6 +478,17 @@ TxSearch::find_txs_in_mempool(
uint64_t current_height = CurrentBlockchainStatus::get_current_blockchain_height();
vector<pair<string, uint64_t>> known_outputs_keys_copy;
{
// copy known ouputs. mutex is needed as known_outputs_keys can
// be updated at the same time as used in this here.
// we make its copy as to keep mutex for short time,
// and read only on copy safely.
std::lock_guard<std::mutex> lck (getting_known_outputs_keys);
known_outputs_keys_copy = known_outputs_keys;
}
for (const pair<uint64_t, transaction>& mtx: mempool_txs)
{
@ -517,6 +533,103 @@ TxSearch::find_txs_in_mempool(
j_transactions.push_back(j_tx);
}
// SECOND step: Checking for our key images, i.e., inputs.
// no need mutex here, as this will be exectued only after
// the above. there is no threads here.
oi_identification.identify_inputs(known_outputs_keys_copy);
if (!oi_identification.identified_inputs.empty())
{
// if we find something we need to construct spent_outputs json array
// that will be appended into j_tx above. or in case this is
// only spending tx, i.e., no outputs were found, we need to custruct
// new j_tx.
json spend_keys;
uint64_t total_sent {0};
for (auto& in_info: oi_identification.identified_inputs)
{
// need to get output info from mysql, as we need
// to know output's amount, its orginal
// tx public key and its index in that tx
XmrOutput out;
if (xmr_accounts->output_exists(in_info.out_pub_key, out))
{
uint64_t output_amount = out.amount;
string tx_pub_key = out.tx_pub_key;
uint64_t out_index = out.out_index;
uint64_t mixin = out.mixin; // mixin not used but get it anyway
// as in mymonero
total_sent += output_amount;
spend_keys.push_back({
{"key_image" , in_info.key_img},
{"amount" , output_amount},
{"tx_pub_key", tx_pub_key},
{"out_index" , out_index},
{"mixin" , mixin},
});
}
}
if (!spend_keys.empty())
{
// check if we got outputs in this tx. if yes
// we use exising j_tx. If not, we need to construct new
// j_tx object.
if (!oi_identification.identified_outputs.empty())
{
// we have outputs in this tx as well, so use
// exisiting j_tx. we add spending info
// to j_tx created before.
json& j_tx = j_transactions.back();
j_tx["total_sent"] = total_sent;
j_tx["spent_outputs"] = spend_keys;
}
else
{
// we dont have any outputs in this tx as
// this is spend only tx, so we need to create new
// j_tx.
json j_tx;
j_tx["id"] = 0; // dont have any database id for tx in mempool
// this id is used for sorting txs in the frontend.
j_tx["hash"] = oi_identification.tx_hash_str;
j_tx["timestamp"] = timestamp_to_str(recieve_time); // when it got into mempool
j_tx["total_received"] = 0; // we did not recive any outputs/xmr
j_tx["total_sent"] = total_sent; // to be set later when looking for key images
j_tx["unlock_time"] = 0; // for mempool we set it to zero
// since we dont have block_height to work with
j_tx["height"] = current_height; // put current blockchain height,
// just to indicate to frontend that this
// tx is younger than 10 blocks so that
// it shows unconfirmed message.
j_tx["payment_id"] = CurrentBlockchainStatus::get_payment_id_as_string(tx);
j_tx["coinbase"] = false; // mempool tx are not coinbase, so always false
j_tx["mixin"] = get_mixin_no(tx) - 1;
j_tx["mempool"] = true;
j_tx["spent_outputs"] = spend_keys;
j_transactions.push_back(j_tx);
} // else of if (!oi_identification.identified_outputs.empty())
} // if (!spend_keys.empty())
} // if (!oi_identification.identified_inputs.empty())
} // for (const transaction& tx: txs_to_check)
return j_transactions;

@ -41,6 +41,7 @@ class TxSearch
bool continue_search {true};
mutex getting_known_outputs_keys;
uint64_t last_ping_timestamp;

Loading…
Cancel
Save