tx search sleeps if all blocks up to the current one have been searched now

pull/1/head
moneroexamples 8 years ago
parent c57bf1e026
commit d8abb0efe0

@ -133,7 +133,7 @@ class MySqlAccounts: public MySqlConnector
)";
static constexpr const char* INSERT_STMT = R"(
INSERT INTO `Accounts` (`address`) VALUES (%0q);
INSERT INTO `Accounts` (`address`, `scanned_block_height`) VALUES (%0q, %1q);
)";
@ -147,22 +147,22 @@ public:
select(const string& address, XmrAccount& account)
{
// static shared_ptr<Query> query;
//
// if (!query)
// {
// Query q = conn.query(SELECT_STMT);
// q.parse();
// query = shared_ptr<Query>(new Query(q));
// }
static shared_ptr<Query> query;
Query query = conn.query(SELECT_STMT);
query.parse();
if (!query)
{
Query q = conn.query(SELECT_STMT);
q.parse();
query = shared_ptr<Query>(new Query(q));
}
// Query query = conn.query(SELECT_STMT);
// query.parse();
try
{
vector<XmrAccount> res;
query.storein(res, address);
query->storein(res, address);
if (!res.empty())
{
@ -186,23 +186,23 @@ public:
bool
select(const int64_t& acc_id, XmrAccount& account)
{
//
// static shared_ptr<Query> query;
//
// if (!query)
// {
// Query q = conn.query(SELECT_STMT2);
// q.parse();
// query = shared_ptr<Query>(new Query(q));
// }
Query query = conn.query(SELECT_STMT2);
query.parse();
static shared_ptr<Query> query;
if (!query)
{
Query q = conn.query(SELECT_STMT2);
q.parse();
query = shared_ptr<Query>(new Query(q));
}
// Query query = conn.query(SELECT_STMT2);
// query.parse();
try
{
vector<XmrAccount> res;
query.storein(res, acc_id);
query->storein(res, acc_id);
if (!res.empty())
{
@ -220,14 +220,27 @@ public:
}
uint64_t
create(const string& address)
create(const string& address, const uint64_t& scanned_block_height = 0)
{
Query query = conn.query(INSERT_STMT);
query.parse();
static shared_ptr<Query> query;
if (!query)
{
Query q = conn.query(INSERT_STMT);
q.parse();
query = shared_ptr<Query>(new Query(q));
}
// Query query = conn.query(INSERT_STMT);
// query.parse();
cout << query << endl;
try
{
SimpleResult sr = query.execute(address);
SimpleResult sr = query->execute(address, scanned_block_height);
if (sr.rows() == 1)
return sr.insert_id();

@ -185,16 +185,29 @@ public:
while(continue_search)
{
if (searched_blk_no > CurrentBlockchainStatus::current_height)
{
fmt::print("searched_blk_no {:d} and current_height {:d}\n",
searched_blk_no, CurrentBlockchainStatus::current_height);
std::this_thread::sleep_for(
std::chrono::seconds(
CurrentBlockchainStatus::refresh_block_status_every_seconds)
);
continue;
}
//
cout << " - searching tx of: " << acc << endl;
// get block cointaining this tx
block blk;
if (!CurrentBlockchainStatus::get_block(++searched_blk_no, blk))
if (!CurrentBlockchainStatus::get_block(searched_blk_no, blk))
{
cerr << "Cant get block of height: " << searched_blk_no << endl;
throw TxSearchException("Cant get block of height: " + to_string(searched_blk_no));
continue;
}
std::lock_guard<std::mutex> lck (mtx);
@ -203,15 +216,9 @@ public:
epee::string_tools::pod_to_hex(get_block_hash(blk)));
std::this_thread::sleep_for(std::chrono::seconds(1));
++searched_blk_no;
if (searched_blk_no == CurrentBlockchainStatus::current_height)
{
std::this_thread::sleep_for(
std::chrono::seconds(
CurrentBlockchainStatus::refresh_block_status_every_seconds)
);
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}

@ -131,7 +131,15 @@ public:
// account does not exist, so create new one
// for this address
if ((acc_id = xmr_accounts->create(xmr_address)) != 0)
// we will save current blockchain height
// in mysql, so that we know from what block
// to start searching txs of this new acount
// make it 1 block lower than current, just in case.
// this variable will be our using to initialize
// `canned_block_height` in mysql Accounts table.
uint64_t current_blkchain_height = get_current_blockchain_height() - 1;
if ((acc_id = xmr_accounts->create(xmr_address, current_blkchain_height)) != 0)
{
// select newly created account
if (xmr_accounts->select(acc_id, acc))
@ -316,6 +324,12 @@ public:
return true;
}
inline uint64_t
get_current_blockchain_height()
{
return CurrentBlockchainStatus::get_current_blockchain_height();
}
private:

Loading…
Cancel
Save