forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmasternode-pos.cpp
253 lines (195 loc) · 8.53 KB
/
masternode-pos.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "sync.h"
#include "net.h"
#include "key.h"
#include "util.h"
#include "base58.h"
#include "protocol.h"
#include "activemasternode.h"
#include "masternodeman.h"
#include "spork.h"
#include <boost/lexical_cast.hpp>
#include "masternodeman.h"
using namespace std;
using namespace boost;
std::map<uint256, CMasternodeScanningError> mapMasternodeScanningErrors;
CMasternodeScanning mnscan;
/*
Masternode - Proof of Service
-- What it checks
1.) Making sure Masternodes have their ports open
2.) Are responding to requests made by the network
-- How it works
When a block comes in, DoMasternodePOS is executed if the client is a
masternode. Using the deterministic ranking algorithm up to 1% of the masternode
network is checked each block.
A port is opened from Masternode A to Masternode B, if successful then nothing happens.
If there is an error, a CMasternodeScanningError object is propagated with an error code.
Errors are applied to the Masternodes and a score is incremented within the masternode object,
after a threshold is met, the masternode goes into an error state. Each cycle the score is
decreased, so if the masternode comes back online it will return to the list.
Masternodes in a error state do not receive payment.
-- Future expansion
We want to be able to prove the nodes have many qualities such as a specific CPU speed, bandwidth,
and dedicated storage. E.g. We could require a full node be a computer running 2GHz with 10GB of space.
*/
void ProcessMessageMasternodePOS(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if(fLiteMode) return; //disable all darksend/masternode related functionality
if(!IsSporkActive(SPORK_7_MASTERNODE_SCANNING)) return;
if(IsInitialBlockDownload()) return;
if (strCommand == "mnse") //Masternode Scanning Error
{
CDataStream vMsg(vRecv);
CMasternodeScanningError mnse;
vRecv >> mnse;
CInv inv(MSG_MASTERNODE_SCANNING_ERROR, mnse.GetHash());
pfrom->AddInventoryKnown(inv);
if(mapMasternodeScanningErrors.count(mnse.GetHash())){
return;
}
mapMasternodeScanningErrors.insert(make_pair(mnse.GetHash(), mnse));
if(!mnse.IsValid())
{
LogPrintf("MasternodePOS::mnse - Invalid object\n");
return;
}
CMasternode* pmnA = mnodeman.Find(mnse.vinMasternodeA);
if(pmnA == NULL) return;
if(pmnA->protocolVersion < MIN_MASTERNODE_POS_PROTO_VERSION) return;
int nBlockHeight = chainActive.Tip()->nHeight;
if(nBlockHeight - mnse.nBlockHeight > 10){
LogPrintf("MasternodePOS::mnse - Too old\n");
return;
}
// Lowest masternodes in rank check the highest each block
int a = mnodeman.GetMasternodeRank(mnse.vinMasternodeA, mnse.nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION);
if(a == -1 || a > GetCountScanningPerBlock())
{
if(a != -1) LogPrintf("MasternodePOS::mnse - MasternodeA ranking is too high\n");
return;
}
int b = mnodeman.GetMasternodeRank(mnse.vinMasternodeB, mnse.nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION, false);
if(b == -1 || b < mnodeman.CountMasternodesAboveProtocol(MIN_MASTERNODE_POS_PROTO_VERSION)-GetCountScanningPerBlock())
{
if(b != -1) LogPrintf("MasternodePOS::mnse - MasternodeB ranking is too low\n");
return;
}
if(!mnse.SignatureValid()){
LogPrintf("MasternodePOS::mnse - Bad masternode message\n");
return;
}
CMasternode* pmnB = mnodeman.Find(mnse.vinMasternodeB);
if(pmnB == NULL) return;
if(fDebug) LogPrintf("ProcessMessageMasternodePOS::mnse - nHeight %d MasternodeA %s MasternodeB %s\n", mnse.nBlockHeight, pmnA->addr.ToString().c_str(), pmnB->addr.ToString().c_str());
pmnB->ApplyScanningError(mnse);
mnse.Relay();
}
}
// Returns how many masternodes are allowed to scan each block
int GetCountScanningPerBlock()
{
return std::max(1, mnodeman.CountMasternodesAboveProtocol(MIN_MASTERNODE_POS_PROTO_VERSION)/100);
}
void CMasternodeScanning::CleanMasternodeScanningErrors()
{
if(chainActive.Tip() == NULL) return;
std::map<uint256, CMasternodeScanningError>::iterator it = mapMasternodeScanningErrors.begin();
while(it != mapMasternodeScanningErrors.end()) {
if(GetTime() > it->second.nExpiration){ //keep them for an hour
LogPrintf("Removing old masternode scanning error %s\n", it->second.GetHash().ToString().c_str());
mapMasternodeScanningErrors.erase(it++);
} else {
it++;
}
}
}
// Check other masternodes to make sure they're running correctly
void CMasternodeScanning::DoMasternodePOSChecks()
{
if(!fMasterNode) return;
if(fLiteMode) return; //disable all darksend/masternode related functionality
if(!IsSporkActive(SPORK_7_MASTERNODE_SCANNING)) return;
if(IsInitialBlockDownload()) return;
int nBlockHeight = chainActive.Tip()->nHeight-5;
int a = mnodeman.GetMasternodeRank(activeMasternode.vin, nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION);
if(a == -1 || a > GetCountScanningPerBlock()){
// we don't need to do anything this block
return;
}
// The lowest ranking nodes (Masternode A) check the highest ranking nodes (Masternode B)
CMasternode* pmn = mnodeman.GetMasternodeByRank(mnodeman.CountMasternodesAboveProtocol(MIN_MASTERNODE_POS_PROTO_VERSION)-a, nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION, false);
if(pmn == NULL) return;
// -- first check : Port is open
if(!ConnectNode((CAddress)pmn->addr, NULL, true)){
// we couldn't connect to the node, let's send a scanning error
CMasternodeScanningError mnse(activeMasternode.vin, pmn->vin, SCANNING_ERROR_NO_RESPONSE, nBlockHeight);
mnse.Sign();
mapMasternodeScanningErrors.insert(make_pair(mnse.GetHash(), mnse));
mnse.Relay();
}
// success
CMasternodeScanningError mnse(activeMasternode.vin, pmn->vin, SCANNING_SUCCESS, nBlockHeight);
mnse.Sign();
mapMasternodeScanningErrors.insert(make_pair(mnse.GetHash(), mnse));
mnse.Relay();
}
bool CMasternodeScanningError::SignatureValid()
{
std::string errorMessage;
std::string strMessage = vinMasternodeA.ToString() + vinMasternodeB.ToString() +
boost::lexical_cast<std::string>(nBlockHeight) + boost::lexical_cast<std::string>(nErrorType);
CMasternode* pmn = mnodeman.Find(vinMasternodeA);
if(pmn == NULL)
{
LogPrintf("CMasternodeScanningError::SignatureValid() - Unknown Masternode\n");
return false;
}
CScript pubkey;
pubkey = GetScriptForDestination(pmn->pubkey2.GetID());
CTxDestination address1;
ExtractDestination(pubkey, address1);
CBitcoinAddress address2(address1);
if(!darkSendSigner.VerifyMessage(pmn->pubkey2, vchMasterNodeSignature, strMessage, errorMessage)) {
LogPrintf("CMasternodeScanningError::SignatureValid() - Verify message failed\n");
return false;
}
return true;
}
bool CMasternodeScanningError::Sign()
{
std::string errorMessage;
CKey key2;
CPubKey pubkey2;
std::string strMessage = vinMasternodeA.ToString() + vinMasternodeB.ToString() +
boost::lexical_cast<std::string>(nBlockHeight) + boost::lexical_cast<std::string>(nErrorType);
if(!darkSendSigner.SetKey(strMasterNodePrivKey, errorMessage, key2, pubkey2))
{
LogPrintf("CMasternodeScanningError::Sign() - ERROR: Invalid masternodeprivkey: '%s'\n", errorMessage.c_str());
return false;
}
CScript pubkey;
pubkey = GetScriptForDestination(pubkey2.GetID());
CTxDestination address1;
ExtractDestination(pubkey, address1);
CBitcoinAddress address2(address1);
//LogPrintf("signing pubkey2 %s \n", address2.ToString().c_str());
if(!darkSendSigner.SignMessage(strMessage, errorMessage, vchMasterNodeSignature, key2)) {
LogPrintf("CMasternodeScanningError::Sign() - Sign message failed");
return false;
}
if(!darkSendSigner.VerifyMessage(pubkey2, vchMasterNodeSignature, strMessage, errorMessage)) {
LogPrintf("CMasternodeScanningError::Sign() - Verify message failed");
return false;
}
return true;
}
void CMasternodeScanningError::Relay()
{
CInv inv(MSG_MASTERNODE_SCANNING_ERROR, GetHash());
vector<CInv> vInv;
vInv.push_back(inv);
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes){
pnode->PushMessage("inv", vInv);
}
}