mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 04:52:59 +01:00
added spork files
This commit is contained in:
parent
38df2bf093
commit
6ab868a3e7
201
src/spork.cpp
Normal file
201
src/spork.cpp
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#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 "spork.h"
|
||||||
|
#include <boost/lexical_cast.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
class CSporkMessage;
|
||||||
|
class CSporkManager;
|
||||||
|
|
||||||
|
std::map<uint256, CSporkMessage> mapSporks;
|
||||||
|
std::map<int, CSporkMessage> mapSporksActive;
|
||||||
|
CSporkManager sporkManager;
|
||||||
|
|
||||||
|
void ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
|
||||||
|
{
|
||||||
|
if(fLiteMode) return; //disable all darksend/masternode related functionality
|
||||||
|
|
||||||
|
if (strCommand == "spork")
|
||||||
|
{
|
||||||
|
//LogPrintf("ProcessSpork::spork\n");
|
||||||
|
CDataStream vMsg(vRecv);
|
||||||
|
CSporkMessage spork;
|
||||||
|
vRecv >> spork;
|
||||||
|
|
||||||
|
if(chainActive.Tip() == NULL) return;
|
||||||
|
|
||||||
|
uint256 hash = spork.GetHash();
|
||||||
|
if(mapSporks.count(hash)) {
|
||||||
|
if(mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned){
|
||||||
|
if(fDebug) LogPrintf("spork - seen %s block %d \n", hash.ToString().c_str(), chainActive.Tip()->nHeight);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
if(fDebug) LogPrintf("spork - got updated spork %s block %d \n", hash.ToString().c_str(), chainActive.Tip()->nHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LogPrintf("spork - new %s ID %d Time %d bestHeight %d\n", hash.ToString().c_str(), spork.nSporkID, spork.nTimeStart, chainActive.Tip()->nHeight);
|
||||||
|
|
||||||
|
if(!sporkManager.CheckSignature(spork)){
|
||||||
|
LogPrintf("spork - invalid signature\n");
|
||||||
|
Misbehaving(pfrom->GetId(), 100);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mapSporksActive[spork.nSporkID] = spork;
|
||||||
|
sporkManager.Relay(spork);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (strCommand == "getsporks")
|
||||||
|
{
|
||||||
|
std::map<int, CSporkMessage>::iterator it = mapSporksActive.begin();
|
||||||
|
|
||||||
|
while(it != mapSporksActive.end()) {
|
||||||
|
pfrom->PushMessage("spork", it->second);
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// grab the spork, otherwise say it's off
|
||||||
|
bool IsSporkActive(int nSporkID)
|
||||||
|
{
|
||||||
|
int64_t r = 0;
|
||||||
|
|
||||||
|
if(mapSporksActive.count(nSporkID)){
|
||||||
|
r = mapSporksActive[nSporkID].nTimeStart;
|
||||||
|
} else {
|
||||||
|
if(nSporkID == SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT) r = SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT_DEFAULT;
|
||||||
|
if(nSporkID == SPORK_2_INSTANTX) r = SPORK_2_INSTANTX_DEFAULT;
|
||||||
|
if(nSporkID == SPORK_3_INSTANTX_BLOCK_FILTERING) r = SPORK_3_INSTANTX_BLOCK_FILTERING_DEFAULT;
|
||||||
|
if(nSporkID == SPORK_4_RECONVERGE) r = SPORK_4_RECONVERGE_DEFAULT;
|
||||||
|
|
||||||
|
if(r == 0) LogPrintf("GetSpork::Unknown Spork %d\n", nSporkID);
|
||||||
|
}
|
||||||
|
if(r == 0) r = 4070908800; //return 2099-1-1 by default
|
||||||
|
|
||||||
|
return r < GetTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool CSporkManager::CheckSignature(CSporkMessage& spork)
|
||||||
|
{
|
||||||
|
//note: need to investigate why this is failing
|
||||||
|
std::string strMessage = boost::lexical_cast<std::string>(spork.nSporkID) + boost::lexical_cast<std::string>(spork.nTimeStart) + boost::lexical_cast<std::string>(spork.nTimeSigned);
|
||||||
|
std::string strPubKey = (Params().NetworkID() == CChainParams::MAIN) ? strMainPubKey : strTestPubKey;
|
||||||
|
CPubKey pubkey(ParseHex(strPubKey));
|
||||||
|
|
||||||
|
std::string errorMessage = "";
|
||||||
|
if(!darkSendSigner.VerifyMessage(pubkey, spork.vchSig, strMessage, errorMessage)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CSporkManager::Sign(CSporkMessage& spork)
|
||||||
|
{
|
||||||
|
std::string strMessage = boost::lexical_cast<std::string>(spork.nSporkID) + boost::lexical_cast<std::string>(spork.nTimeStart) + boost::lexical_cast<std::string>(spork.nTimeSigned);
|
||||||
|
|
||||||
|
CKey key2;
|
||||||
|
CPubKey pubkey2;
|
||||||
|
std::string errorMessage = "";
|
||||||
|
|
||||||
|
if(!darkSendSigner.SetKey(strMasterPrivKey, errorMessage, key2, pubkey2))
|
||||||
|
{
|
||||||
|
LogPrintf("CMasternodePayments::Sign - ERROR: Invalid masternodeprivkey: '%s'\n", errorMessage.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!darkSendSigner.SignMessage(strMessage, errorMessage, spork.vchSig, key2)) {
|
||||||
|
LogPrintf("CMasternodePayments::Sign - Sign message failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!darkSendSigner.VerifyMessage(pubkey2, spork.vchSig, strMessage, errorMessage)) {
|
||||||
|
LogPrintf("CMasternodePayments::Sign - Verify message failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CSporkManager::UpdateSpork(int nSporkID, int64_t nTimeStart)
|
||||||
|
{
|
||||||
|
|
||||||
|
CSporkMessage msg;
|
||||||
|
msg.nSporkID = nSporkID;
|
||||||
|
msg.nTimeStart = nTimeStart;
|
||||||
|
msg.nTimeSigned = GetTime();
|
||||||
|
|
||||||
|
if(Sign(msg)){
|
||||||
|
Relay(msg);
|
||||||
|
mapSporks[msg.GetHash()] = msg;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CSporkManager::Relay(CSporkMessage& msg)
|
||||||
|
{
|
||||||
|
CInv inv(MSG_SPORK, msg.GetHash());
|
||||||
|
|
||||||
|
vector<CInv> vInv;
|
||||||
|
vInv.push_back(inv);
|
||||||
|
LOCK(cs_vNodes);
|
||||||
|
BOOST_FOREACH(CNode* pnode, vNodes){
|
||||||
|
printf("send inv message %s\n", msg.GetHash().ToString().c_str());
|
||||||
|
pnode->PushMessage("inv", vInv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CSporkManager::SetPrivKey(std::string strPrivKey)
|
||||||
|
{
|
||||||
|
CSporkMessage msg;
|
||||||
|
|
||||||
|
// Test signing successful, proceed
|
||||||
|
strMasterPrivKey = strPrivKey;
|
||||||
|
|
||||||
|
Sign(msg);
|
||||||
|
|
||||||
|
if(CheckSignature(msg)){
|
||||||
|
LogPrintf("CSporkManager::SetPrivKey - Successfully initialized as spork signer\n");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int CSporkManager::GetSporkIDByName(std::string strName)
|
||||||
|
{
|
||||||
|
printf("%s\n", strName.c_str());
|
||||||
|
if(strName == "SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT") return SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT;
|
||||||
|
if(strName == "SPORK_2_INSTANTX") return SPORK_2_INSTANTX;
|
||||||
|
if(strName == "SPORK_3_INSTANTX_BLOCK_FILTERING") return SPORK_3_INSTANTX_BLOCK_FILTERING;
|
||||||
|
if(strName == "SPORK_4_RECONVERGE") return SPORK_4_RECONVERGE;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CSporkManager::GetSporkNameByID(int id)
|
||||||
|
{
|
||||||
|
if(id == SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT) return "SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT";
|
||||||
|
if(id == SPORK_2_INSTANTX) return "SPORK_2_INSTANTX";
|
||||||
|
if(id == SPORK_3_INSTANTX_BLOCK_FILTERING) return "SPORK_3_INSTANTX_BLOCK_FILTERING";
|
||||||
|
if(id == SPORK_4_RECONVERGE) return "SPORK_4_RECONVERGE";
|
||||||
|
|
||||||
|
return "Unknown";
|
||||||
|
}
|
107
src/spork.h
Normal file
107
src/spork.h
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
|
||||||
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||||
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
||||||
|
// Distributed under the MIT/X11 software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
#ifndef SPORK_H
|
||||||
|
#define SPORK_H
|
||||||
|
|
||||||
|
#include "bignum.h"
|
||||||
|
#include "sync.h"
|
||||||
|
#include "net.h"
|
||||||
|
#include "key.h"
|
||||||
|
#include "core.h"
|
||||||
|
#include "util.h"
|
||||||
|
#include "script.h"
|
||||||
|
#include "base58.h"
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
// Don't ever reuse these IDs for other sporks
|
||||||
|
#define SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT 10000
|
||||||
|
#define SPORK_2_INSTANTX 10001
|
||||||
|
#define SPORK_3_INSTANTX_BLOCK_FILTERING 10002
|
||||||
|
#define SPORK_4_RECONVERGE 10003
|
||||||
|
|
||||||
|
#define SPORK_1_MASTERNODE_PAYMENTS_ENFORCEMENT_DEFAULT 1424217600 //2015-2-18
|
||||||
|
#define SPORK_2_INSTANTX_DEFAULT 978307200 //2001-1-1
|
||||||
|
#define SPORK_3_INSTANTX_BLOCK_FILTERING_DEFAULT 1424217600 //2015-2-18
|
||||||
|
#define SPORK_4_RECONVERGE_DEFAULT 4070908800 //2099-1-1
|
||||||
|
|
||||||
|
class CSporkMessage;
|
||||||
|
class CSporkManager;
|
||||||
|
|
||||||
|
#include "bignum.h"
|
||||||
|
#include "net.h"
|
||||||
|
#include "key.h"
|
||||||
|
#include "util.h"
|
||||||
|
#include "protocol.h"
|
||||||
|
#include "darksend.h"
|
||||||
|
#include <boost/lexical_cast.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
extern std::map<uint256, CSporkMessage> mapSporks;
|
||||||
|
extern std::map<int, CSporkMessage> mapSporksActive;
|
||||||
|
extern CSporkManager sporkManager;
|
||||||
|
|
||||||
|
void ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
|
||||||
|
bool IsSporkActive(int nSporkID);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Spork Class
|
||||||
|
// Keeps track of all of the network spork settings
|
||||||
|
//
|
||||||
|
|
||||||
|
class CSporkMessage
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::vector<unsigned char> vchSig;
|
||||||
|
int nSporkID;
|
||||||
|
int64_t nTimeStart;
|
||||||
|
int64_t nTimeSigned;
|
||||||
|
|
||||||
|
uint256 GetHash(){
|
||||||
|
uint256 n = HashX11(BEGIN(nSporkID), END(nTimeSigned));
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
IMPLEMENT_SERIALIZE(
|
||||||
|
READWRITE(nSporkID);
|
||||||
|
READWRITE(nTimeStart);
|
||||||
|
READWRITE(nTimeSigned);
|
||||||
|
READWRITE(vchSig);
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class CSporkManager
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::vector<unsigned char> vchSig;
|
||||||
|
|
||||||
|
std::string strMasterPrivKey;
|
||||||
|
std::string strTestPubKey;
|
||||||
|
std::string strMainPubKey;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
CSporkManager() {
|
||||||
|
strMainPubKey = "04549ac134f694c0243f503e8c8a9a986f5de6610049c40b07816809b0d1d06a21b07be27b9bb555931773f62ba6cf35a25fd52f694d4e1106ccd237a7bb899fdd";
|
||||||
|
strTestPubKey = "046f78dcf911fbd61910136f7f0f8d90578f68d0b3ac973b5040fb7afb501b5939f39b108b0569dca71488f5bbf498d92e4d1194f6f941307ffd95f75e76869f0e";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetSporkNameByID(int id);
|
||||||
|
int GetSporkIDByName(std::string strName);
|
||||||
|
bool UpdateSpork(int nSporkID, int64_t nTimeStart);
|
||||||
|
bool SetPrivKey(std::string strPrivKey);
|
||||||
|
bool CheckSignature(CSporkMessage& spork);
|
||||||
|
bool Sign(CSporkMessage& spork);
|
||||||
|
void Relay(CSporkMessage& msg);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user