This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
candidate.hpp
101 lines (89 loc) · 2.59 KB
/
candidate.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef CANDIDATE_HPP
#define CANDIDATE_HPP
class candidate :
public boost::enable_shared_from_this<candidate>
{
public:
//hash_t shash; // hash of signature hashes, this is the key to this container
uint32_t now;
uint16_t peer; // source of , probably not used
uint64_t score; //
bool failed; // candidate will not be accepted during voting
std::map<uint64_t,hash_s> msg_add; // add to list
std::set<uint64_t> msg_del; // remove from list
std::set<uint64_t> msg_mis; // still waiting
//bool failed_peer; // candidate will not be accepted
//std::map<uint16_t,msidhash_t> svid_have; // hashes missed by peer (processed first)
//std::map<uint16_t,msidhash_t> svid_miss; // hashes missed by server (processed second)
//std::set<uint16_t> waiting_server; //hashes still missing by server during block building
std::set<message_ptr> votes; // messages with votes, probably not used, but could be used for reporting
std::set<uint16_t> peers; // used by save_candidate
candidate() :
now(0),
peer(0),
score(0),
failed(false)
{
}
candidate(uint32_t blk,std::map<uint64_t,hash_s>& add,std::set<uint64_t>& del,std::set<uint64_t>& mis,uint16_t svid,bool myfailed) :
now(blk),
peer(svid),
score(0),
failed(myfailed),
msg_add(add),
msg_del(del),
msg_mis(mis)
{
}
bool accept()
{ if(!failed && msg_mis.size()==0){
return true;}
return false;
}
bool elected_accept()
{ if(msg_mis.size()==0){
return true;}
return false;
}
void update(message_ptr msg)
{ msg_.lock(); //just in case
auto mis=msg_mis.find(msg->hash.num & 0xFFFFFFFFFFFF0000L);
if(mis!=msg_mis.end()){
auto add=msg_add.find(msg->hash.num & 0xFFFFFFFFFFFF0000L);
assert(add!=msg_add.end());
if(!memcmp(add->second.hash,msg->sigh,32)){
msg_mis.erase(mis);}}
msg_.unlock();
}
const char* print_missing(servers* srvs) //TODO, consider having local lock
{ static std::string line;
if(failed){
line="FAILED MISSING: ";}
else{
line="MISSING: ";}
msg_.lock();
for(auto key : msg_mis){
char miss[64];
uint32_t msid=(key>>16) & 0xFFFFFFFFL;
uint16_t svid=(key>>48);
sprintf(miss," %04X:%08X",svid,msid);
line+=miss;}
msg_.unlock();
return(line.c_str());
}
void get_missing(std::vector<uint64_t>& missing)
{ msg_.lock();
for(auto key : msg_mis){
missing.push_back(key);}
msg_.unlock();
}
void del_missing(uint64_t mis)
{ msg_.lock();
msg_mis.erase(mis);
msg_.unlock();
}
private:
boost::mutex msg_;
};
typedef boost::shared_ptr<candidate> candidate_ptr;
#endif // CANDIDATE_HPP