/
infinitynodelockreward.cpp
470 lines (401 loc) · 18.6 KB
/
infinitynodelockreward.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright (c) 2018-2019 SIN developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <infinitynodelockreward.h>
#include <infinitynodeman.h>
#include <infinitynodepeer.h>
#include <infinitynodemeta.h>
#include <messagesigner.h>
#include <net_processing.h>
#include <netmessagemaker.h>
#include <utilstrencodings.h>
#include <boost/lexical_cast.hpp>
/** Object for who's going to get paid on which blocks */
CInfinityNodeLockReward inflockreward;
/*************************************************************/
/***** CLockRewardRequest ************************************/
/*************************************************************/
CLockRewardRequest::CLockRewardRequest()
{}
CLockRewardRequest::CLockRewardRequest(int Height, COutPoint outpoint, int sintype, int loop){
nRewardHeight = Height;
burnTxIn = CTxIn(outpoint);
nSINtype = sintype;
nLoop = loop;
}
bool CLockRewardRequest::Sign(const CKey& keyInfinitynode, const CPubKey& pubKeyInfinitynode)
{
std::string strError;
std::string strSignMessage;
std::string strMessage = boost::lexical_cast<std::string>(nRewardHeight) + burnTxIn.ToString()
+ boost::lexical_cast<std::string>(nSINtype)
+ boost::lexical_cast<std::string>(nLoop);
if(!CMessageSigner::SignMessage(strMessage, vchSig, keyInfinitynode)) {
LogPrintf("CLockRewardRequest::Sign -- SignMessage() failed\n");
return false;
}
if(!CMessageSigner::VerifyMessage(pubKeyInfinitynode, vchSig, strMessage, strError)) {
LogPrintf("CLockRewardRequest::Sign -- VerifyMessage() failed, error: %s\n", strError);
return false;
}
return true;
}
bool CLockRewardRequest::CheckSignature(CPubKey& pubKeyInfinitynode, int &nDos)
{
std::string strMessage = boost::lexical_cast<std::string>(nRewardHeight) + burnTxIn.ToString()
+ boost::lexical_cast<std::string>(nSINtype)
+ boost::lexical_cast<std::string>(nLoop);
std::string strError = "";
if(!CMessageSigner::VerifyMessage(pubKeyInfinitynode, vchSig, strMessage, strError)) {
LogPrintf("CLockRewardRequest::CheckSignature -- Got bad Infinitynode LockReward signature, ID=%s, error: %s\n",
burnTxIn.prevout.ToStringShort(), strError);
/*TODO: set ban value befor release*/
nDos = 0;
return false;
}
return true;
}
bool CLockRewardRequest::IsValid(CNode* pnode, int nValidationHeight, std::string& strError, CConnman& connman)
{
CInfinitynode inf;
if(!infnodeman.deterministicRewardAtHeight(nRewardHeight, nSINtype, inf)){
strError = strprintf("Cannot find candidate for Height of LockRequest: %d\n", nRewardHeight);
return false;
}
if(inf.vinBurnFund != burnTxIn){
strError = strprintf("Node %s is not a Candidate for height: %d, SIN type: %d\n", burnTxIn.prevout.ToStringShort(), nRewardHeight, nSINtype);
return false;
}
int nDos = 20;
CMetadata meta = infnodemeta.Find(inf.getMetaID());
std::string metaPublicKey = meta.getMetaPublicKey();
std::vector<unsigned char> tx_data = DecodeBase64(metaPublicKey.c_str());
CPubKey pubKey(tx_data.begin(), tx_data.end());
if(!CheckSignature(pubKey, nDos)){
LOCK(cs_main);
Misbehaving(pnode->GetId(), nDos);
strError = strprintf("ERROR: invalid signature\n");
return false;
}
return true;
}
void CLockRewardRequest::Relay(CConnman& connman)
{
CInv inv(MSG_LOCKREWARD_INIT, GetHash());
connman.RelayInv(inv);
}
/*************************************************************/
/***** CLockRewardCommitment *********************************/
/*************************************************************/
CLockRewardCommitment::CLockRewardCommitment()
{}
CLockRewardCommitment::CLockRewardCommitment(uint256 nRequestIn, uint256 nCommitmentIn){
nHashRequest = nRequestIn;
nHashCommitment = nHashCommitment;
}
/*************************************************************/
/***** CInfinityNodeLockReward *******************************/
/*************************************************************/
void CInfinityNodeLockReward::Clear()
{
LOCK(cs);
mapLockRewardRequest.clear();
mapInfinityNodeRandom.clear();
mapInfinityNodeCommitment.clear();
}
bool CInfinityNodeLockReward::AlreadyHave(const uint256& hash)
{
LOCK(cs);
return mapLockRewardRequest.count(hash) ||
mapInfinityNodeRandom.count(hash) ||
mapInfinityNodeCommitment.count(hash);
}
bool CInfinityNodeLockReward::AddLockRewardRequest(const CLockRewardRequest& lockRewardRequest)
{
AssertLockHeld(cs);
//LogPrintf("CInfinityNodeLockReward::AddLockRewardRequest -- lockRewardRequest from %s, SIN type: %d, loop: %d\n",
// lockRewardRequest.burnTxIn.prevout.ToStringShort(), lockRewardRequest.nSINtype, lockRewardRequest.nLoop);
//if we hash this request => don't add it
if(mapLockRewardRequest.count(lockRewardRequest.GetHash())) return false;
if(lockRewardRequest.nLoop == 0){
mapLockRewardRequest.insert(make_pair(lockRewardRequest.GetHash(), lockRewardRequest));
} else if (lockRewardRequest.nLoop > 0){
//new request from candidate, so remove old requrest
RemoveLockRewardRequest(lockRewardRequest);
mapLockRewardRequest.insert(make_pair(lockRewardRequest.GetHash(), lockRewardRequest));
}
return true;
}
bool CInfinityNodeLockReward::GetLockRewardRequest(const uint256& reqHash, CLockRewardRequest& lockRewardRequestRet)
{
LOCK(cs);
std::map<uint256, CLockRewardRequest>::iterator it = mapLockRewardRequest.find(reqHash);
if(it == mapLockRewardRequest.end()) return false;
lockRewardRequestRet = it->second;
return true;
}
void CInfinityNodeLockReward::RemoveLockRewardRequest(const CLockRewardRequest& lockRewardRequest)
{
AssertLockHeld(cs);
std::map<uint256, CLockRewardRequest>::iterator itRequest = mapLockRewardRequest.begin();
while(itRequest != mapLockRewardRequest.end()) {
if(itRequest->second.nRewardHeight == lockRewardRequest.nRewardHeight
&& itRequest->second.burnTxIn == lockRewardRequest.burnTxIn
&& itRequest->second.nSINtype == lockRewardRequest.nSINtype
&& itRequest->second.nLoop < lockRewardRequest.nLoop)
{
mapLockRewardRequest.erase(itRequest++);
}else{
++itRequest;
}
}
}
/*
* STEP 1: get a new LockRewardRequest, check it and send verify message
*
* STEP 1.1 check LockRewardRequest is valid or NOT
* - the nHeight of request cannot be lower than current height
* - the nHeight of request cannot so far in future ( currentHeight + Params().GetConsensus().nInfinityNodeCallLockRewardDeepth + 2)
* - the LockRewardRequest must be valid
* + from (nHeight, SINtype) => find candidate
* + CTxIn of request and CTxIn of candidate must be identical
* + Find publicKey of Candidate in metadata and check the signature in request
*/
bool CInfinityNodeLockReward::CheckLockRewardRequest(CNode* pfrom, CLockRewardRequest& lockRewardRequestRet, CConnman& connman, int nBlockHeight)
{
AssertLockHeld(cs);
if(lockRewardRequestRet.nRewardHeight > nBlockHeight + Params().GetConsensus().nInfinityNodeCallLockRewardDeepth + 2
|| lockRewardRequestRet.nRewardHeight < nBlockHeight){
LogPrintf("CInfinityNodeLockReward::CheckLockRewardRequest -- LockRewardRequest for invalid height: %d, current height: %d\n",
lockRewardRequestRet.nRewardHeight, nBlockHeight);
return false;
}
std::string strError = "";
if(!lockRewardRequestRet.IsValid(pfrom, nBlockHeight, strError, connman)){
LogPrintf("CInfinityNodeLockReward::CheckLockRewardRequest -- LockRewardRequest is invalid. ERROR: %s\n",strError);
return false;
}
return true;
}
/*
* STEP 1: get a new LockRewardRequest, check it and send verify message
*
* STEP 1.2 verify candidate
*/
bool CInfinityNodeLockReward::VerifyLockRewardCandidate(CLockRewardRequest& lockRewardRequestRet, CConnman& connman)
{
AssertLockHeld(cs);
//step 1.2.1: chech if mypeer is good candidate to make Musig
CInfinitynode infRet;
if(!infnodeman.Get(infinitynodePeer.burntx, infRet)){
LogPrintf("CInfinityNodeLockReward::VerifyLockRewardCandidate -- Can not identify mypeer in list, height: %d\n");
return false;
}
int nScore;
if(!infnodeman.getNodeScoreAtHeight(infinitynodePeer.burntx, lockRewardRequestRet.nRewardHeight - 101, nScore)) {
LogPrintf("CMasternodePaymentVote::VerifyLockRewardCandidate -- Can't calculate score for Infinitynode %s\n",
infinitynodePeer.burntx.ToStringShort());
return false;
}
//step 1.2.2: if my score is in Top, i will verify that candidate is online at IP
//step 1.2.2.1: get InfCandidate from request
CInfinitynode infCandidate;
if(!infnodeman.Get(lockRewardRequestRet.burnTxIn.prevout, infCandidate)){
LogPrintf("CInfinityNodeLockReward::VerifyLockRewardCandidate -- Cannot identify candidate in list\n");
return false;
}
CMetadata metaCandidate = infnodemeta.Find(infCandidate.getMetaID());
if(metaCandidate.getMetadataHeight() == 0){
LogPrintf("CInfinityNodeLockReward::VerifyLockRewardCandidate -- Cannot get metadata of candidate %s\n", infCandidate.getBurntxOutPoint().ToStringShort());
return false;
}
CService candidateService = metaCandidate.getService();
if(!SendVerifyRequest(CAddress(candidateService, NODE_NETWORK), infinitynodePeer.burntx, lockRewardRequestRet, connman)){
LogPrintf("CInfinityNodeLockReward::VerifyLockRewardCandidate -- Cannot send verify message to candidate %s\n", infCandidate.getBurntxOutPoint().ToStringShort());
return false;
}
return true;
//TODO:
/*
if(nScore < Params().GetConsensus().nInfinityNodeLockRewardTop) {
//step 2.1: verify node which send the request is online at IP in metadata
//SendVerifyRequest()
//step 2.2: send commitment
return true;
} else {
LogPrintf("CMasternodePaymentVote::VerifyLockRewardCandidate -- I am not in Top score\n");
return false;
}*/
}
/*
* STEP 1: get a new LockRewardRequest, check it and send verify message
*
* STEP 1.3 send verify request to Candidate A at IP(addr) for Request(lockRewardRequestRet)
*/
bool CInfinityNodeLockReward::SendVerifyRequest(const CAddress& addr, COutPoint myPeerBurnTx, CLockRewardRequest& lockRewardRequestRet, CConnman& connman)
{
CVerifyRequest vrequest(addr, myPeerBurnTx, GetRandInt(999999), lockRewardRequestRet.nRewardHeight, lockRewardRequestRet.GetHash());
std::string strMessage = strprintf("%s%d%s", myPeerBurnTx.ToString(), vrequest.nonce, lockRewardRequestRet.GetHash().ToString());
if(!CMessageSigner::SignMessage(strMessage, vrequest.vchSig1, infinitynodePeer.keyInfinitynode)) {
LogPrintf("CInfinityNodeLockReward::SendVerifyRequest -- SignMessage() failed\n");
return false;
}
std::string strError;
if(!CMessageSigner::VerifyMessage(infinitynodePeer.pubKeyInfinitynode, vrequest.vchSig1, strMessage, strError)) {
LogPrintf("CInfinityNodeLockReward::SendVerifyRequest -- VerifyMessage() failed, error: %s\n", strError);
return false;
}
// use random nonce, store it and require node to reply with correct one later
CNode* pnode = connman.OpenNetworkConnection(addr, false, nullptr, NULL, false, false, false, true);
if(pnode == NULL) {
LogPrintf("CInfinityNodeLockReward::SendVerifyRequest -- can't connect to node to verify it, addr=%s\n", addr.ToString());
return false;
}
LogPrintf("CInfinityNodeLockReward::SendVerifyRequest -- verifying node using nonce %d addr=%s\n", vrequest.nonce, addr.ToString());
connman.PushMessage(pnode, CNetMsgMaker(pnode->GetSendVersion()).Make(NetMsgType::INFVERIFY, vrequest));
return true;
}
/*
* STEP 2: get a verify message, check and try to answer the message
*
* check:
* - VerifyRequest was sent from Top Node
* - Signature of VerifyRequest is correct
* answer:
* - create the 2nd signature - my signature and send back to node
* processVerifyRequestReply:
* - check 2nd signature
* - disconnect to node ( Candidate )
*/
//TODO: ban pnode if false
bool CInfinityNodeLockReward::SendVerifyReply(CNode* pnode, CVerifyRequest& vrequest, CConnman& connman)
{
// only Infinitynode will answer the verify requrest
if(!fInfinityNode) {return false;}
AssertLockHeld(cs);
//step 2.0 get publicKey of sender and check signature
infinitynode_info_t infoInf;
if(!infnodeman.GetInfinitynodeInfo(vrequest.vin1.prevout, infoInf)){
LogPrintf("CInfinityNodeLockReward::SendVerifyReply -- Cannot find sender from list %s\n");
return false;
}
CMetadata metaSender = infnodemeta.Find(infoInf.metadataID);
if (metaSender.getMetadataHeight() == 0){
LogPrintf("CInfinityNodeLockReward::SendVerifyReply -- Cannot find sender from list %s\n");
return false;
}
std::string metaPublicKey = metaSender.getMetaPublicKey();
std::vector<unsigned char> tx_data = DecodeBase64(metaPublicKey.c_str());
CPubKey pubKey(tx_data.begin(), tx_data.end());
std::string strError;
std::string strMessage = strprintf("%s%d%s", vrequest.vin1.prevout.ToString(), vrequest.nonce, vrequest.nHashRequest.ToString());
if(!CMessageSigner::VerifyMessage(pubKey, vrequest.vchSig1, strMessage, strError)) {
LogPrintf("CInfinityNodeLockReward::SendVerifyReply -- VerifyMessage() failed, error: %s, message: \n", strError, strMessage);
return false;
}
//step 2.1 check if sender in Top (skip this step for now)
//step 2.2 sign a new message and send it back to sender
vrequest.addr = infinitynodePeer.service;
vrequest.vin2 = CTxIn(infinitynodePeer.burntx);
std::string strMessage2 = strprintf("%s%d%s%s%s", vrequest.addr.ToString(false), vrequest.nonce, vrequest.nHashRequest.ToString(),
vrequest.vin1.prevout.ToStringShort(), vrequest.vin2.prevout.ToStringShort());
if(!CMessageSigner::SignMessage(strMessage2, vrequest.vchSig2, infinitynodePeer.keyInfinitynode)) {
LogPrintf("CInfinityNodeLockReward::ProcessVerifyReply -- SignMessage() failed\n");
return false;
}
if(!CMessageSigner::VerifyMessage(infinitynodePeer.pubKeyInfinitynode, vrequest.vchSig2, strMessage2, strError)) {
LogPrintf("CInfinityNodeLockReward::ProcessVerifyReply -- VerifyMessage() failed, error: %s\n", strError);
return false;
}
connman.PushMessage(pnode, CNetMsgMaker(pnode->GetSendVersion()).Make(NetMsgType::INFVERIFY, vrequest));
return true;
}
/*
* STEP 3: check return VerifyRequest, if it is OK then
* - send the commitment
* - disconect node
*/
bool CInfinityNodeLockReward::CheckVerifyReply(CNode* pnode, CVerifyRequest& vrequest)
{
pnode->fDisconnect = true;
}
/*
* STEP 0: create LockRewardRequest if i am a candidate at nHeight
*/
bool CInfinityNodeLockReward::ProcessBlock(int nBlockHeight, CConnman& connman)
{
if(fLiteMode || !fInfinityNode) return false;
//step 0.1: Check if this InfinitynodePeer is a candidate at nBlockHeight
CInfinitynode infRet;
if(!infnodeman.Get(infinitynodePeer.burntx, infRet)){
LogPrintf("CInfinityNodeLockReward::ProcessBlock -- Can not identify mypeer in list, height: %d\n", nBlockHeight);
return false;
}
int nRewardHeight = infnodeman.isPossibleForLockReward(infRet.getCollateralAddress());
if(nRewardHeight == 0 || (nRewardHeight < (nCachedBlockHeight + Params().GetConsensus().nInfinityNodeCallLockRewardLoop))){
LogPrintf("CInfinityNodeLockReward::ProcessBlock -- Try to LockReward false at height %d\n", nBlockHeight);
return false;
}
//step 0.2 we know that nRewardHeight >= nCachedBlockHeight + Params().GetConsensus().nInfinityNodeCallLockRewardLoop
int loop = (Params().GetConsensus().nInfinityNodeCallLockRewardDeepth / (nRewardHeight - nCachedBlockHeight)) - 1;
CLockRewardRequest newRequest(nRewardHeight, infRet.getBurntxOutPoint(), infRet.getSINType(), loop);
if (newRequest.Sign(infinitynodePeer.keyInfinitynode, infinitynodePeer.pubKeyInfinitynode)) {
LOCK(cs);
if (AddLockRewardRequest(newRequest)) {
LogPrintf("CInfinityNodeLockReward::ProcessBlock -- Relay LockRequest loop: %d\n", loop);
newRequest.Relay(connman);
return true;
}
}
return true;
}
void CInfinityNodeLockReward::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman)
{
if(fLiteMode) return; // disable all SIN specific functionality
if (strCommand == NetMsgType::INFLOCKREWARDINIT) {
CLockRewardRequest lockReq;
vRecv >> lockReq;
//dont ask pfrom for this Request anymore
uint256 nHash = lockReq.GetHash();
pfrom->setAskFor.erase(nHash);
{
LOCK(cs);
if(mapLockRewardRequest.count(nHash)) return;
if(!CheckLockRewardRequest(pfrom, lockReq, connman, nCachedBlockHeight)){
return;
}
LogPrintf("CInfinityNodeLockReward::ProcessMessage -- add new LockRewardRequest from %d\n",pfrom->GetId());
if(AddLockRewardRequest(lockReq)){
lockReq.Relay(connman);
if(!VerifyLockRewardCandidate(lockReq, connman)){
LogPrintf("CInfinityNodeLockReward::ProcessMessage -- can not send commitment. May be i am not in Top score.\n");
return;
}
}
return;
}
} else if (strCommand == NetMsgType::INFVERIFY) {
CVerifyRequest vrequest;
vRecv >> vrequest;
pfrom->setAskFor.erase(vrequest.GetHash());
{
LOCK(cs);
if(!vrequest.vchSig1.empty() && vrequest.vchSig2.empty()) {
SendVerifyReply(pfrom, vrequest, connman);
}
if(!vrequest.vchSig1.empty() && !vrequest.vchSig2.empty()) {
CheckVerifyReply(pfrom, vrequest);
}
return;
}
}
}
void CInfinityNodeLockReward::UpdatedBlockTip(const CBlockIndex *pindex, CConnman& connman)
{
if(!pindex) return;
nCachedBlockHeight = pindex->nHeight;
LogPrintf("CInfinityNodeLockReward::UpdatedBlockTip -- nCachedBlockHeight=%d\n", nCachedBlockHeight);
int nFutureBlock = nCachedBlockHeight + Params().GetConsensus().nInfinityNodeCallLockRewardDeepth;
//CheckPreviousBlockVotes(nFutureBlock);
ProcessBlock(nFutureBlock, connman);
}