CurrentBlockchainStatus::monitor_blockchain added

pull/93/merge
moneroexamples 6 years ago
parent de45edd41c
commit 65cd066b19

1
.gitignore vendored

@ -4,6 +4,7 @@
*.user
*.user.*
.idea/
*.gcov
cmake-build-debug/
ext/restbed/dependency/catch/
ext/restbed/distribution/

@ -41,6 +41,8 @@
# 2017-06-02, Lars Bilke
# - Merged with modified version from github.com/ufz/ogs
#
# 2018-08-02, moneroexamples
# - Modified to use gcovr rather than lcov
#
# USAGE:
#

@ -124,7 +124,12 @@ if (!current_bc_status->init_monero_blockchain())
// info, e.g., current height. Information from this thread is used
// by tx searching threads that are launched for each user independently,
// when they log back or create new account.
current_bc_status->start_monitor_blockchain_thread();
xmreg::ThreadRAII blockchain_monitoring_thread(
std::thread([current_bc_status]
{current_bc_status->monitor_blockchain();}),
xmreg::ThreadRAII::DtorAction::join);
OMINFO << "Blockchain monitoring thread started";
@ -166,7 +171,7 @@ xmreg::MysqlPing mysql_ping {mysql_accounts->get_connection()};
xmreg::ThreadRAII mysql_ping_thread(
std::thread(std::ref(mysql_ping)),
xmreg::ThreadRAII::DtorAction::detach);
xmreg::ThreadRAII::DtorAction::join);
OMINFO << "MySQL ping thread started";

@ -25,34 +25,29 @@ CurrentBlockchainStatus::CurrentBlockchainStatus(
}
void
CurrentBlockchainStatus::start_monitor_blockchain_thread()
CurrentBlockchainStatus::monitor_blockchain()
{
TxSearch::set_search_thread_life(
bc_setup.search_thread_life_in_seconds);
if (!is_running)
{
m_thread = std::thread{[this]()
{
while (true)
{
update_current_blockchain_height();
read_mempool();
OMINFO << "Current blockchain height: " << current_height
<< ", no of mempool txs: " << mempool_txs.size();
clean_search_thread_map();
std::this_thread::sleep_for(
std::chrono::seconds(
bc_setup
.refresh_block_status_every_seconds));
}
}};
is_running = true;
is_running = true;
while (is_running)
{
update_current_blockchain_height();
read_mempool();
OMINFO << "Current blockchain height: " << current_height
<< ", no of mempool txs: " << mempool_txs.size();
clean_search_thread_map();
std::this_thread::sleep_for(
std::chrono::seconds(
bc_setup.refresh_block_status_every_seconds));
}
}
}
uint64_t
CurrentBlockchainStatus::get_current_blockchain_height()
{

@ -59,7 +59,7 @@ public:
std::unique_ptr<RPCCalls> _rpc);
virtual void
start_monitor_blockchain_thread();
monitor_blockchain();
virtual uint64_t
get_current_blockchain_height();
@ -234,6 +234,12 @@ public:
return bc_setup;
}
inline virtual void
set_bc_setup(BlockchainSetup const& bs)
{
bc_setup = bs;
}
virtual void
init_txs_data_vector(vector<block> const& blocks,
vector<crypto::hash>& txs_to_get,

@ -1557,7 +1557,43 @@ TEST_P(BCSTATUS_TEST, GetTxsInBlocks)
txs, txs_data));
}
TEST_P(BCSTATUS_TEST, MonitorBlockchain)
{
uint64_t mock_current_height {1619148};
EXPECT_CALL(*mcore_ptr, get_current_blockchain_height())
.WillRepeatedly(Return(mock_current_height));
vector<tx_info> mempool_txs_to_return;
mempool_txs_to_return.push_back(tx_info{});
mempool_txs_to_return.push_back(tx_info{});
mempool_txs_to_return.push_back(tx_info{});
EXPECT_CALL(*mcore_ptr, get_mempool_txs(_, _))
.WillRepeatedly(DoAll(
SetArgReferee<0>(mempool_txs_to_return),
Return(true)));
// set refresh rate to 1 second as we dont wont to wait long
xmreg::BlockchainSetup bs = bcs->get_bc_setup();
bs.refresh_block_status_every_seconds = 1;
bcs->set_bc_setup(bs);
auto thread_function = std::bind(
&xmreg::CurrentBlockchainStatus::monitor_blockchain,
bcs.get());
xmreg::ThreadRAII blockchain_monitoring_thread(
std::thread(thread_function),
xmreg::ThreadRAII::DtorAction::join);
// wait few seconds so that several loops are done
std::this_thread::sleep_for(4s);
bcs->is_running = false;
}
INSTANTIATE_TEST_CASE_P(

@ -283,7 +283,8 @@ TEST_F(MYSQL_TEST, UpdateAccount)
auto updated_account = acc;
updated_account.scanned_block_height = 555;
updated_account.scanned_block_timestamp = DateTime(static_cast<time_t>(5555555));
updated_account.scanned_block_timestamp
= DateTime(static_cast<time_t>(5555555));
EXPECT_TRUE(xmr_accounts->update(acc, updated_account));
@ -308,7 +309,8 @@ TEST_F(MYSQL_TEST, InsertAndGetAccount)
uint64_t mock_current_blockchain_timestamp = 1529302789;
DateTime blk_timestamp_mysql_format
= mysqlpp::DateTime(static_cast<time_t>(mock_current_blockchain_timestamp));
= mysqlpp::DateTime(static_cast<time_t>(
mock_current_blockchain_timestamp));
// address to insert
string xmr_addr {"4AKNvHrLG6KKtKYRaSPWtSZSnAcaBZ2UzeJg7guFNFK46EN93FnBDS5eiFgxH87Sb7ZcSFxMyBhhgKBJiG5kKBBmCY5tbgw"};
@ -316,7 +318,8 @@ TEST_F(MYSQL_TEST, InsertAndGetAccount)
string view_key_hash {"cdd3ae89cbdae1d14b178c7e7c6ba380630556cb9892bd24eb61a9a517e478cd"};
uint64_t expected_primary_id = xmr_accounts->get_next_primary_id(xmreg::XmrAccount());
uint64_t expected_primary_id
= xmr_accounts->get_next_primary_id(xmreg::XmrAccount());
xmreg::XmrAccount new_account(mysqlpp::null,
xmr_addr,
@ -569,7 +572,8 @@ TEST_F(MYSQL_TEST, DeleteNoNExistingTx)
{
uint64_t some_non_existing_tx_id = 7774748483;
uint64_t no_of_deleted_rows = xmr_accounts->delete_tx(some_non_existing_tx_id);
uint64_t no_of_deleted_rows
= xmr_accounts->delete_tx(some_non_existing_tx_id);
EXPECT_EQ(no_of_deleted_rows, 0);
}
@ -587,7 +591,8 @@ TEST_F(MYSQL_TEST, MarkTxSpendableAndNonSpendable)
// this particular tx is marked as spendable in mysql
EXPECT_TRUE(static_cast<bool>(tx_data.spendable));
uint64_t no_of_changed_rows = xmr_accounts->mark_tx_nonspendable(tx_data.id.data);
uint64_t no_of_changed_rows
= xmr_accounts->mark_tx_nonspendable(tx_data.id.data);
EXPECT_EQ(no_of_changed_rows, 1);
@ -742,7 +747,8 @@ TEST_F(MYSQL_TEST, InsertOneOutput)
mock_output_data.account_id = acc.id.data;
uint64_t expected_primary_id = xmr_accounts->get_next_primary_id(mock_output_data);
uint64_t expected_primary_id
= xmr_accounts->get_next_primary_id(mock_output_data);
uint64_t inserted_output_id = xmr_accounts->insert(mock_output_data);
EXPECT_EQ(inserted_output_id, expected_primary_id);
@ -751,7 +757,8 @@ TEST_F(MYSQL_TEST, InsertOneOutput)
xmreg::XmrOutput out_data2;
EXPECT_TRUE(xmr_accounts->select_by_primary_id(inserted_output_id, out_data2));
EXPECT_TRUE(xmr_accounts
->select_by_primary_id(inserted_output_id, out_data2));
EXPECT_EQ(out_data2.tx_id, mock_output_data.tx_id);
EXPECT_EQ(out_data2.out_pub_key, mock_output_data.out_pub_key);
@ -826,7 +833,8 @@ TEST_F(MYSQL_TEST, InsertSeverlOutputsAtOnce)
mock_outputs_data.back().account_id = acc.id.data;
}
uint64_t expected_primary_id = xmr_accounts->get_next_primary_id(xmreg::XmrOutput());
uint64_t expected_primary_id
= xmr_accounts->get_next_primary_id(xmreg::XmrOutput());
// first time insert should be fine
uint64_t no_inserted_rows = xmr_accounts->insert(mock_outputs_data);
@ -834,7 +842,8 @@ TEST_F(MYSQL_TEST, InsertSeverlOutputsAtOnce)
EXPECT_EQ(no_inserted_rows, mock_outputs_data.size());
// after inserting 10 rows, the expected ID should be before + 11
uint64_t expected_primary_id2 = xmr_accounts->get_next_primary_id(xmreg::XmrOutput());
uint64_t expected_primary_id2
= xmr_accounts->get_next_primary_id(xmreg::XmrOutput());
EXPECT_EQ(expected_primary_id2, expected_primary_id + mock_outputs_data.size());
@ -957,7 +966,8 @@ TEST_F(MYSQL_TEST, InsertOneInput)
mock_input_data.account_id = acc.id.data;
uint64_t expected_primary_id = xmr_accounts->get_next_primary_id(mock_input_data);
uint64_t expected_primary_id
= xmr_accounts->get_next_primary_id(mock_input_data);
uint64_t inserted_id = xmr_accounts->insert(mock_input_data);
EXPECT_EQ(expected_primary_id, inserted_id);
@ -991,7 +1001,8 @@ TEST_F(MYSQL_TEST, InsertSeverlInputsAtOnce)
mock_data.back().account_id = acc.id.data;
}
uint64_t expected_primary_id = xmr_accounts->get_next_primary_id(xmreg::XmrInput());
uint64_t expected_primary_id
= xmr_accounts->get_next_primary_id(xmreg::XmrInput());
// first time insert should be fine
uint64_t no_inserted_rows = xmr_accounts->insert(mock_data);
@ -999,7 +1010,8 @@ TEST_F(MYSQL_TEST, InsertSeverlInputsAtOnce)
EXPECT_EQ(no_inserted_rows, mock_data.size());
// after inserting 10 rows, the expected ID should be before + 11
uint64_t expected_primary_id2 = xmr_accounts->get_next_primary_id(xmreg::XmrInput());
uint64_t expected_primary_id2
= xmr_accounts->get_next_primary_id(xmreg::XmrInput());
EXPECT_EQ(expected_primary_id2, expected_primary_id + mock_data.size());
@ -1245,7 +1257,8 @@ TEST_F(MYSQL_TEST, SelectTxsIfAllAreSpendableAndExist)
txs.clear();
ASSERT_TRUE(this->xmr_accounts->select(acc.id.data, txs));
EXPECT_TRUE(this->xmr_accounts->select_txs_for_account_spendability_check(acc.id.data, txs));
EXPECT_TRUE(this->xmr_accounts
->select_txs_for_account_spendability_check(acc.id.data, txs));
// we check if non of the input txs got filtere out
EXPECT_EQ(txs.size(), no_of_original_txs);
@ -1293,7 +1306,8 @@ TEST_F(MYSQL_TEST, SelectTxsIfAllAreNonspendableButUnlockedAndExist)
for (auto const& tx: txs)
ASSERT_FALSE(bool {tx.spendable});
EXPECT_TRUE(this->xmr_accounts->select_txs_for_account_spendability_check(acc.id.data, txs));
EXPECT_TRUE(this->xmr_accounts
->select_txs_for_account_spendability_check(acc.id.data, txs));
// we check if non of the input txs got filtere out
EXPECT_EQ(txs.size(), no_of_original_txs);
@ -1364,7 +1378,8 @@ TEST_F(MYSQL_TEST, SelectTxsIfAllAreNonspendableLockedButExist)
ASSERT_EQ(tx.unlock_time, 0);
}
EXPECT_TRUE(this->xmr_accounts->select_txs_for_account_spendability_check(acc.id.data, txs));
EXPECT_TRUE(this->xmr_accounts
->select_txs_for_account_spendability_check(acc.id.data, txs));
// we check if non of the input txs got filtere out
EXPECT_EQ(txs.size(), no_of_original_txs);
@ -1375,7 +1390,8 @@ TEST_F(MYSQL_TEST, SelectTxsIfAllAreNonspendableLockedButExist)
{
ASSERT_FALSE(bool {tx.spendable});
if (!bool {tx.coinbase})
ASSERT_EQ(tx.unlock_time, tx.height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE);
ASSERT_EQ(tx.unlock_time,
tx.height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE);
}
}
@ -1404,8 +1420,6 @@ TEST_F(MYSQL_TEST, SelectTxsIfAllAreNonspendableUnlockedAndDontExist)
// so that we have something to work with in this test
ASSERT_TRUE(this->xmr_accounts->select(acc.id.data, txs));
auto no_of_original_txs = txs.size();
for (size_t i = 0; i < txs.size(); ++i)
this->xmr_accounts->mark_tx_nonspendable(txs[i].id.data);
@ -1508,7 +1522,8 @@ TEST_F(MYSQL_TEST, MysqlPingThreadStopsOnPingFailure)
}
// since we waiting 4s, we expect that there were at about 3-4 pings
EXPECT_EQ(mysql_ping.get_stop_reason(), xmreg::MysqlPing::StopReason::PingFailed);
EXPECT_EQ(mysql_ping.get_stop_reason(),
xmreg::MysqlPing::StopReason::PingFailed);
EXPECT_THAT(mysql_ping.get_counter(), AllOf(Ge(3), Le(5)));
}
@ -1549,7 +1564,8 @@ TEST_F(MYSQL_TEST, MysqlPingThreadStopsOnPointerExpiry)
}
// since we waiting 4s, we expect that there were at about 3-4 pings
EXPECT_EQ(mysql_ping.get_stop_reason(), xmreg::MysqlPing::StopReason::PointerExpired);
EXPECT_EQ(mysql_ping.get_stop_reason(),
xmreg::MysqlPing::StopReason::PointerExpired);
EXPECT_THAT(mysql_ping.get_counter(), AllOf(Ge(3), Le(5)));
}

Loading…
Cancel
Save