Code improvements.

This commit is contained in:
Ravin
2019-11-07 07:47:33 +05:30
committed by Ravin Perera
parent d16b43406b
commit 8d31075b7b
27 changed files with 155 additions and 171 deletions

View File

@@ -52,9 +52,9 @@ void consensus()
}
LOG_DBG << "Started stage " << std::to_string(ctx.stage);
for (auto p : ctx.candidate_proposals)
for (const auto p : ctx.candidate_proposals)
{
bool self = p.pubkey == conf::cfg.pubkey;
const bool self = p.pubkey == conf::cfg.pubkey;
LOG_DBG << "[stage" << std::to_string(p.stage)
<< "] users:" << p.users.size()
<< " hinp:" << p.hash_inputs.size()
@@ -121,7 +121,7 @@ void consensus()
}
if (is_lcl_desync)
{
bool should_reset = (ctx.time_now - ctx.novel_proposal_time) < floor(conf::cfg.roundtime / 4);
const bool should_reset = (ctx.time_now - ctx.novel_proposal_time) < floor(conf::cfg.roundtime / 4);
//for now we are resetting to stage 0 to avoid possible deadlock situations
timewait_stage(true);
return;
@@ -206,7 +206,7 @@ void verify_and_populate_candidate_user_inputs()
for (const usr::user_submitted_message &umsg : umsgs)
{
std::string sig_hash = crypto::get_hash(umsg.sig);
const std::string sig_hash = crypto::get_hash(umsg.sig);
// Check for duplicate messages using hash of the signature.
if (ctx.recent_userinput_hashes.try_emplace(sig_hash))
@@ -311,7 +311,7 @@ p2p::proposal create_stage123_proposal(vote_counter &votes)
// todo: repeat above for state
}
float_t vote_threshold = get_stage_threshold(ctx.stage);
const float_t vote_threshold = get_stage_threshold(ctx.stage);
// todo: check if inputs being proposed by another node are actually spoofed inputs
// from a user locally connected to this node.
@@ -319,17 +319,17 @@ p2p::proposal create_stage123_proposal(vote_counter &votes)
// if we're at proposal stage 1 we'll accept any input and connection that has 1 or more vote.
// Add user pubkeys which have votes over stage threshold to proposal.
for (auto &[pubkey, numvotes] : votes.users)
for (const auto &[pubkey, numvotes] : votes.users)
if (numvotes >= vote_threshold || (ctx.stage == 1 && numvotes > 0))
stg_prop.users.emplace(pubkey);
// Add inputs which have votes over stage threshold to proposal.
for (auto &[hash, numvotes] : votes.inputs)
for (const auto &[hash, numvotes] : votes.inputs)
if (numvotes >= vote_threshold || (ctx.stage == 1 && numvotes > 0))
stg_prop.hash_inputs.emplace(hash);
// Add outputs which have votes over stage threshold to proposal.
for (auto &[hash, numvotes] : votes.outputs)
for (const auto &[hash, numvotes] : votes.outputs)
if (numvotes >= vote_threshold)
stg_prop.hash_outputs.emplace(hash);
@@ -337,7 +337,7 @@ p2p::proposal create_stage123_proposal(vote_counter &votes)
// time is voted on a simple sorted and majority basis, since there will always be disagreement.
int32_t highest_votes = 0;
for (auto [time, numvotes] : votes.time)
for (const auto [time, numvotes] : votes.time)
{
if (numvotes > highest_votes)
{
@@ -474,7 +474,7 @@ void check_lcl_votes(bool &is_desync, bool &should_request_history, std::string
* Returns the consensus percentage threshold for the specified stage.
* @param stage The consensus stage [1, 2, 3]
*/
float_t get_stage_threshold(uint8_t stage)
float_t get_stage_threshold(const uint8_t stage)
{
switch (stage)
{
@@ -488,7 +488,7 @@ float_t get_stage_threshold(uint8_t stage)
return -1;
}
void timewait_stage(bool reset)
void timewait_stage(const bool reset)
{
if (reset)
ctx.stage = 0;
@@ -539,8 +539,8 @@ void dispatch_user_outputs(const p2p::proposal &cons_prop)
for (const std::string &hash : cons_prop.hash_outputs)
{
auto cu_itr = ctx.candidate_user_outputs.find(hash);
bool hashfound = (cu_itr != ctx.candidate_user_outputs.end());
const auto cu_itr = ctx.candidate_user_outputs.find(hash);
const bool hashfound = (cu_itr != ctx.candidate_user_outputs.end());
if (!hashfound)
{
LOG_ERR << "Output required but wasn't in our candidate outputs map, this will potentially cause desync.";
@@ -553,10 +553,10 @@ void dispatch_user_outputs(const p2p::proposal &cons_prop)
candidate_user_output &cand_output = cu_itr->second;
// Find the user session by user pubkey.
auto sess_itr = usr::ctx.sessionids.find(cand_output.userpubkey);
const auto sess_itr = usr::ctx.sessionids.find(cand_output.userpubkey);
if (sess_itr != usr::ctx.sessionids.end()) // match found
{
auto user_itr = usr::ctx.users.find(sess_itr->second); // sess_itr->second is the session id.
const auto user_itr = usr::ctx.users.find(sess_itr->second); // sess_itr->second is the session id.
if (user_itr != usr::ctx.users.end()) // match found
{
std::string outputtosend;
@@ -589,8 +589,8 @@ void feed_inputs_to_contract_bufmap(proc::contract_bufmap_t &bufmap, const p2p::
for (const std::string &hash : cons_prop.hash_inputs)
{
// For each consensus input hash, we need to find the actual input content to feed the contract.
auto itr = ctx.candidate_user_inputs.find(hash);
bool hashfound = (itr != ctx.candidate_user_inputs.end());
const auto itr = ctx.candidate_user_inputs.find(hash);
const bool hashfound = (itr != ctx.candidate_user_inputs.end());
if (!hashfound)
{
LOG_ERR << "input required but wasn't in our candidate inputs map, this will potentially cause desync.";
@@ -628,7 +628,7 @@ void extract_outputs_from_contract_bufmap(proc::contract_bufmap_t &bufmap)
std::string output;
output.swap(bufpair.output);
std::string hash = crypto::get_hash(pubkey, output);
const std::string hash = crypto::get_hash(pubkey, output);
ctx.candidate_user_outputs.try_emplace(
std::move(hash),
candidate_user_output(pubkey, std::move(output)));
@@ -641,14 +641,14 @@ void extract_outputs_from_contract_bufmap(proc::contract_bufmap_t &bufmap)
* @param time_now The time that must be passed on to the contract.
* @param useriobufmap The contract bufmap which holds user I/O buffers.
*/
void run_contract_binary(int64_t time_now, proc::contract_bufmap_t &useriobufmap)
void run_contract_binary(const int64_t time_now, proc::contract_bufmap_t &useriobufmap)
{
// todo:implement exchange of npl and hpsc bufs
proc::contract_bufmap_t nplbufmap;
proc::contract_iobuf_pair hpscbufpair;
proc::contract_exec_args eargs(time_now, useriobufmap, nplbufmap, hpscbufpair);
proc::exec_contract(eargs);
proc::exec_contract(
proc::contract_exec_args(time_now, useriobufmap, nplbufmap, hpscbufpair));
}
/**