searching for outputs in tx search thread added

pull/1/head
moneroexamples 8 years ago
parent d8abb0efe0
commit 2b55888956

@ -236,7 +236,7 @@ public:
// Query query = conn.query(INSERT_STMT);
// query.parse();
cout << query << endl;
// cout << query << endl;
try
{

@ -112,11 +112,31 @@ struct CurrentBlockchainStatus
}
static bool
get_block(uint64_t height, block blk)
get_block(uint64_t height, block& blk)
{
return mcore.get_block_by_height(height, blk);
}
static bool
get_block_txs(const block& blk, list<transaction>& blk_txs)
{
// get all transactions in the block found
// initialize the first list with transaction for solving
// the block i.e. coinbase tx.
blk_txs.push_back(blk.miner_tx);
list<crypto::hash> missed_txs;
if (!core_storage->get_transactions(blk.tx_hashes, blk_txs, missed_txs))
{
cerr << "Cant get transactions in block: " << get_block_hash(blk) << endl;
return false;
}
return true;
}
};
// initialize static variables
@ -138,6 +158,9 @@ class TxSearch
XmrAccount acc;
// this manages all mysql queries
// its better to when each thread has its own mysql connection object.
// this way if one thread crashes, it want take down
// connection for the entire service
MySqlAccounts xmr_accounts;
// address and viewkey for this search thread.
@ -206,15 +229,123 @@ public:
if (!CurrentBlockchainStatus::get_block(searched_blk_no, blk))
{
cerr << "Cant get block of height: " << searched_blk_no << endl;
cerr << "Cant get block of height: " + to_string(searched_blk_no) << endl;
searched_blk_no =- 2; // just go back one block, and retry
continue;
}
std::lock_guard<std::mutex> lck (mtx);
// for each tx in the given block look, get ouputs
list<cryptonote::transaction> blk_txs;
if (!CurrentBlockchainStatus::get_block_txs(blk, blk_txs))
{
throw TxSearchException("Cant get transactions in block: " + to_string(searched_blk_no));
}
//std::lock_guard<std::mutex> lck (mtx);
fmt::print(" - searching block {:d} of hash {:s} \n",
searched_blk_no,
epee::string_tools::pod_to_hex(get_block_hash(blk)));
searched_blk_no, pod_to_hex(get_block_hash(blk)));
for (transaction& tx: blk_txs)
{
crypto::hash tx_hash = get_transaction_hash(tx);
// cout << pod_to_hex(tx_hash) << endl;
public_key tx_pub_key = xmreg::get_tx_pub_key_from_received_outs(tx);
// <public_key , amount , out idx>
vector<tuple<txout_to_key, uint64_t, uint64_t>> outputs;
outputs = get_ouputs_tuple(tx);
// for each output, in a tx, check if it belongs
// to the given account of specific address and viewkey
// public transaction key is combined with our viewkey
// to create, so called, derived key.
key_derivation derivation;
if (!generate_key_derivation(tx_pub_key, viewkey, derivation))
{
cerr << "Cant get derived key for: " << "\n"
<< "pub_tx_key: " << tx_pub_key << " and "
<< "prv_view_key" << viewkey << endl;
throw TxSearchException("");
}
for (auto& out: outputs)
{
txout_to_key txout_k = std::get<0>(out);
uint64_t amount = std::get<1>(out);
uint64_t output_idx_in_tx = std::get<2>(out);
// get the tx output public key
// that normally would be generated for us,
// if someone had sent us some xmr.
public_key generated_tx_pubkey;
derive_public_key(derivation,
output_idx_in_tx,
address.m_spend_public_key,
generated_tx_pubkey);
// check if generated public key matches the current output's key
bool mine_output = (txout_k.key == generated_tx_pubkey);
// if mine output has RingCT, i.e., tx version is 2
// need to decode its amount. otherwise its zero.
if (mine_output && tx.version == 2)
{
// initialize with regular amount
uint64_t rct_amount = amount;
bool r;
r = decode_ringct(tx.rct_signatures,
tx_pub_key,
viewkey,
output_idx_in_tx,
tx.rct_signatures.ecdhInfo[output_idx_in_tx].mask,
rct_amount);
if (!r)
{
cerr << "Cant decode ringCT!" << endl;
throw TxSearchException("");
}
// cointbase txs have amounts in plain sight.
// so use amount from ringct, only for non-coinbase txs
if (!is_coinbase(tx))
{
rct_amount = amount;
}
amount = rct_amount;
} // if (mine_output && tx.version == 2)
if (mine_output)
{
// found an output associated with the given address and viewkey
string msg = fmt::format("block: {:d}, tx_hash: {:s}, output_pub_key: {:s}\n",
searched_blk_no,
pod_to_hex(tx_hash),
pod_to_hex(txout_k.key));
cout << msg << endl;
}
} // for (const auto& out: outputs)
} // for (const transaction& tx: blk_txs)
++searched_blk_no;

@ -183,8 +183,8 @@ public:
{
json j_request = body_to_json(body);
if (show_logs)
print_json_log("get_address_txs request: ", j_request);
// if (show_logs)
// print_json_log("get_address_txs request: ", j_request);
json j_response {
{ "total_received", "0"},
@ -207,8 +207,8 @@ public:
{
json j_request = body_to_json(body);
if (show_logs)
print_json_log("get_address_info request: ", j_request);
// if (show_logs)
// print_json_log("get_address_info request: ", j_request);
json j_response {
{"locked_funds", "0"},

@ -49,6 +49,8 @@ namespace lt = boost::local_time;
using json = nlohmann::json;
using epee::string_tools::pod_to_hex;
using epee::string_tools::hex_to_pod;
template <typename T>
bool

Loading…
Cancel
Save