Skip to content

Commit

Permalink
Complete implementation of Proof-of-Service (not debugged)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Duffield committed Mar 14, 2015
1 parent 3fdb010 commit bb7672f
Show file tree
Hide file tree
Showing 13 changed files with 427 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Expand Up @@ -53,6 +53,7 @@ BITCOIN_CORE_H = \
limitedmap.h \
main.h \
masternode.h \
masternode-pos.h \
masternodeman.h \
masternodeconfig.h \
miner.h \
Expand Down Expand Up @@ -154,6 +155,7 @@ libdarkcoin_common_a_SOURCES = \
darksend.cpp \
darksend-relay.cpp \
masternode.cpp \
masternode-pos.cpp \
masternodeman.cpp \
masternodeconfig.cpp \
instantx.cpp \
Expand Down
3 changes: 1 addition & 2 deletions src/darksend.cpp
Expand Up @@ -9,7 +9,6 @@
#include "masternodeman.h"
#include "instantx.h"
#include "ui_interface.h"
;
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
Expand Down Expand Up @@ -45,7 +44,7 @@ int RequestedMasterNodeList = 0;
udjinm6 - udjinm6@darkcoin.io
*/

void CDarksendPool::ProcessMessageDarksend(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
void inline CDarksendPool::ProcessMessageDarksend(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if(fLiteMode) return; //disable all Darksend/Masternode related functionality
if(IsInitialBlockDownload()) return;
Expand Down
21 changes: 21 additions & 0 deletions src/instantx.cpp
Expand Up @@ -252,6 +252,8 @@ int64_t CreateNewLock(CTransaction tx)
if(fDebug) LogPrintf("CreateNewLock - Transaction Lock Exists %s !\n", tx.GetHash().ToString().c_str());
}



return nBlockHeight;
}

Expand Down Expand Up @@ -449,6 +451,25 @@ void CleanTransactionLocksList()
if(GetTime() > it->second.nExpiration){ //keep them for an hour
LogPrintf("Removing old transaction lock %s\n", it->second.txHash.ToString().c_str());

// loop through masternodes that responded
for(int nRank = 0; nRank <= INSTANTX_SIGNATURES_TOTAL; nRank++)
{
CMasternode* pmn = mnodeman.GetMasternodeByRank(nRank, it->second.nBlockHeight, MIN_INSTANTX_PROTO_VERSION);
bool fFound = false;
BOOST_FOREACH(CConsensusVote& v, it->second.vecConsensusVotes)
{
if(pmn->vin == v.vinMasternode){ //Masternode responded
fFound = true;
}
}

if(!fFound){
//increment a scanning error
CMasternodeScanningError mnse(pmn->vin, SCANNING_ERROR_IX_NO_RESPONSE, it->second.nBlockHeight);
pmn->ApplyScanningError(mnse);
}
}

if(mapTxLockReq.count(it->second.txHash)){
CTransaction& tx = mapTxLockReq[it->second.txHash];

Expand Down
13 changes: 13 additions & 0 deletions src/main.cpp
Expand Up @@ -3242,6 +3242,7 @@ bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBl
if (!fImporting && !fReindex && chainActive.Height() > Checkpoints::GetTotalBlocksEstimate()){
darkSendPool.NewBlock();
masternodePayments.ProcessBlock(GetHeight()+10);
mnscan.DoMasternodePOSChecks();
}
}

Expand Down Expand Up @@ -3903,6 +3904,8 @@ bool static AlreadyHave(const CInv& inv)
return mapSporks.count(inv.hash);
case MSG_MASTERNODE_WINNER:
return mapSeenMasternodeVotes.count(inv.hash);
case MSG_MASTERNODE_SCANNING_ERROR:
return mapMasternodeScanningErrors.count(inv.hash);
}
// Don't know what it is, just say we already got one
return true;
Expand Down Expand Up @@ -4063,6 +4066,15 @@ void static ProcessGetData(CNode* pfrom)
pushed = true;
}
}
if (!pushed && inv.type == MSG_MASTERNODE_SCANNING_ERROR) {
if(mapMasternodeScanningErrors.count(inv.hash)){
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << mapMasternodeScanningErrors[inv.hash];
pfrom->PushMessage("mnse", ss);
pushed = true;
}
}

if (!pushed) {
vNotFound.push_back(inv);
Expand Down Expand Up @@ -4824,6 +4836,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
ProcessMessageMasternodePayments(pfrom, strCommand, vRecv);
ProcessMessageInstantX(pfrom, strCommand, vRecv);
ProcessSpork(pfrom, strCommand, vRecv);
ProcessMessageMasternodePOS(pfrom, strCommand, vRecv);
}


Expand Down
237 changes: 237 additions & 0 deletions src/masternode-pos.cpp
@@ -0,0 +1,237 @@



#include "bignum.h"
#include "sync.h"
#include "net.h"
#include "key.h"
#include "util.h"
#include "script.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
{
LogPrintf("ProcessMessageMasternodePOS::mnse\n");
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;
}

// Lowest masternodes in rank check the highest each block
int a = mnodeman.GetMasternodeRank(mnse.vinMasternodeA, mnse.nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION);
if(a > GetCountScanningPerBlock())
{
LogPrintf("MasternodePOS::mnse - MasternodeA ranking is too high\n");
return;
}

int b = mnodeman.GetMasternodeRank(mnse.vinMasternodeB, mnse.nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION);
if(b < mnodeman.CountMasternodesAboveProtocol(MIN_MASTERNODE_POS_PROTO_VERSION)-GetCountScanningPerBlock())
{
LogPrintf("MasternodePOS::mnse - MasternodeB ranking is too low\n");
return;
}

if(!mnse.SignatureValid()){
LogPrintf("MasternodePOS::mnse - Bad masternode message\n");
return;
}

CMasternode* pmn = mnodeman.Find(mnse.vinMasternodeB);
if(pmn == NULL) return;


pmn->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;

int a = mnodeman.GetMasternodeRank(activeMasternode.vin, chainActive.Tip()->nHeight, MIN_MASTERNODE_POS_PROTO_VERSION);
if(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, chainActive.Tip()->nHeight, MIN_MASTERNODE_POS_PROTO_VERSION);
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, chainActive.Tip()->nHeight);
mnse.Sign();
mnse.Relay();
}

// success
CMasternodeScanningError mnse(activeMasternode.vin, pmn->vin, SCANNING_SUCCESS, chainActive.Tip()->nHeight);
mnse.Sign();
mnse.Relay();
}

bool CMasternodeScanningError::SignatureValid()
{
std::string errorMessage;
std::string strMessage = vinMasternodeB.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.SetDestination(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 = vinMasternodeB.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.SetDestination(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);
}
}

0 comments on commit bb7672f

Please sign in to comment.