Skip to content

Commit

Permalink
Move pruner to relayer, fix inf. loop, quorum participation bool return
Browse files Browse the repository at this point in the history
  • Loading branch information
Doy-lee committed Jul 24, 2018
1 parent 1d74138 commit a03a56a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
6 changes: 5 additions & 1 deletion src/cryptonote_core/cryptonote_core.cpp
Expand Up @@ -1449,8 +1449,12 @@ namespace cryptonote
m_deregisters_auto_relayer.do_call(boost::bind(&core::relay_deregister_votes, this));
m_check_updates_interval.do_call(boost::bind(&core::check_updates, this));
m_check_disk_space_interval.do_call(boost::bind(&core::check_disk_space, this));
if (m_service_node)
if (m_service_node && m_service_node_list.is_service_node(m_service_node_pubkey))
{
m_submit_uptime_proof_interval.do_call(boost::bind(&core::submit_uptime_proof, this));
m_uptime_proof_pruner.do_call(boost::bind(&service_nodes::quorum_cop::prune_uptime_proof, &m_quorum_cop));
}

m_miner.on_idle();
m_mempool.on_idle();
return true;
Expand Down
1 change: 1 addition & 0 deletions src/cryptonote_core/cryptonote_core.h
Expand Up @@ -1034,6 +1034,7 @@ namespace cryptonote
epee::math_helper::once_a_time_seconds<60*60*12, true> m_check_updates_interval; //!< interval for checking for new versions
epee::math_helper::once_a_time_seconds<60*10, true> m_check_disk_space_interval; //!< interval for checking for disk space
epee::math_helper::once_a_time_seconds<UPTIME_PROOF_FREQUENCY_IN_SECONDS, true> m_submit_uptime_proof_interval; //!< interval for submitting uptime proof
epee::math_helper::once_a_time_seconds<30, true> m_uptime_proof_pruner;

std::atomic<bool> m_starter_message_showed; //!< has the "daemon will sync now" message been shown?

Expand Down
65 changes: 27 additions & 38 deletions src/cryptonote_core/quorum_cop.cpp
Expand Up @@ -41,14 +41,14 @@ namespace service_nodes

void quorum_cop::blockchain_detached(uint64_t height)
{
uint64_t delta_height = std::abs(static_cast<int64_t>(m_core.get_current_blockchain_height() - height));
uint64_t delta_height = std::abs(static_cast<int64_t>(m_last_height - height));
if (delta_height > REORG_SAFETY_BUFFER_IN_BLOCKS)
{
LOG_ERROR("The blockchain was detached with a change in height: " << delta_height <<
", which is greater than the recommended REORG_SAFETY_BUFFER_IN_BLOCKS: " << REORG_SAFETY_BUFFER_IN_BLOCKS);
LOG_ERROR("The blockchain was detached, quorum cop has processed votes for: " << delta_height <<
" blocks which is greater than the recommended REORG_SAFETY_BUFFER_IN_BLOCKS: " << REORG_SAFETY_BUFFER_IN_BLOCKS);
}

m_last_height = (height < REORG_SAFETY_BUFFER_IN_BLOCKS) ? height : height - REORG_SAFETY_BUFFER_IN_BLOCKS;
m_last_height = height;
}

static bool participates_in_quorum(const std::vector<crypto::public_key>& quorum, crypto::public_key const &my_key, size_t *index_in_quorum = nullptr)
Expand All @@ -63,7 +63,7 @@ namespace service_nodes
if (index_in_quorum)
*index_in_quorum = index;

bool result = index >= quorum.size();
bool result = (index < quorum.size());
return result;
}

Expand All @@ -74,6 +74,13 @@ namespace service_nodes
if (!m_core.get_service_node_keys(my_pubkey, my_seckey))
return;

time_t const now = time(nullptr);
bool been_alive_for_2hrs = (now - m_core.get_start_time()) >= (60 * 60 * 2);
if (!been_alive_for_2hrs)
{
return;
}

uint64_t const height = cryptonote::get_block_height(block);
uint64_t const latest_height = m_core.get_current_blockchain_height();

Expand All @@ -84,19 +91,10 @@ namespace service_nodes
if (height < execute_justice_from_height)
return;

if (height == latest_height)
{
const std::shared_ptr<quorum_state> latest_quorum = m_core.get_quorum_state(latest_height);
if (participates_in_quorum(latest_quorum->nodes_to_test, my_pubkey))
{
m_core.submit_uptime_proof();
}
}

if (m_last_height < execute_justice_from_height)
m_last_height = execute_justice_from_height;

time_t const now = time(nullptr);

for (;m_last_height < (height - REORG_SAFETY_BUFFER_IN_BLOCKS); m_last_height++)
{
const std::shared_ptr<quorum_state> state = m_core.get_quorum_state(m_last_height);
Expand All @@ -116,19 +114,9 @@ namespace service_nodes
for (size_t node_index = 0; node_index < state->nodes_to_test.size(); ++node_index)
{
const crypto::public_key &node_key = state->nodes_to_test[node_index];
bool vote_off_node = false;

CRITICAL_REGION_LOCAL(m_lock);
auto const it = m_uptime_proof_seen.find(node_key);
if (it == m_uptime_proof_seen.end())
{
vote_off_node = true;
}
else
{
size_t time_since_proof = now - it->second;
vote_off_node = (time_since_proof > UPTIME_PROOF_MAX_TIME_IN_SECONDS) ? true : false;
}
bool vote_off_node = (m_uptime_proof_seen.find(node_key) != m_uptime_proof_seen.end());

if (!vote_off_node)
continue;
Expand Down Expand Up @@ -156,8 +144,6 @@ namespace service_nodes
}
}
}

prune_uptime_proof_below(execute_justice_from_height);
}

static crypto::hash make_hash(crypto::public_key const &pubkey, uint64_t timestamp)
Expand All @@ -179,6 +165,8 @@ namespace service_nodes
if ((timestamp < now - UPTIME_PROOF_BUFFER_IN_SECONDS) || (timestamp > now + UPTIME_PROOF_BUFFER_IN_SECONDS))
return false;

// TODO(doyle): the only dependency on m_service_node_lists which could be
// replaced by the lists stored in db when that is implemented - 2018-07-24
if (!m_service_node_list.is_service_node(pubkey))
return false;

Expand All @@ -203,19 +191,20 @@ namespace service_nodes
crypto::generate_signature(hash, pubkey, seckey, req.sig);
}

void quorum_cop::prune_uptime_proof_below(uint64_t height)
bool quorum_cop::prune_uptime_proof()
{
std::list<cryptonote::block> blocks;
if (!m_core.get_blocks(height, 1, blocks))
{
LOG_ERROR("Error quorum cop could not retrieve block with height: " << height << ", from db");
return;
}

const uint64_t prune_from_timestamp = blocks.front().timestamp;
uint64_t now = time(nullptr);
const uint64_t prune_from_timestamp = now - UPTIME_PROOF_MAX_TIME_IN_SECONDS;
CRITICAL_REGION_LOCAL(m_lock);

for (auto it = m_uptime_proof_seen.begin(); it != m_uptime_proof_seen.end();)
it = (it->second < prune_from_timestamp) ? m_uptime_proof_seen.erase(it) : it;
{
if (it->second < prune_from_timestamp)
m_uptime_proof_seen.erase(it);
else
it++;
}

return true;
}
}
2 changes: 1 addition & 1 deletion src/cryptonote_core/quorum_cop.h
Expand Up @@ -57,9 +57,9 @@ namespace service_nodes
static const uint64_t REORG_SAFETY_BUFFER_IN_BLOCKS = 20;
static_assert(REORG_SAFETY_BUFFER_IN_BLOCKS < loki::service_node_deregister::VOTE_LIFETIME_BY_HEIGHT,
"Safety buffer should always be less than the vote lifetime");
bool prune_uptime_proof();

private:
void prune_uptime_proof_below(uint64_t height);

cryptonote::core& m_core;
service_node_list& m_service_node_list;
Expand Down

0 comments on commit a03a56a

Please sign in to comment.