separated main from darksend

This commit is contained in:
Evan Duffield 2014-08-30 07:42:16 -07:00
parent 30211927b0
commit e6e00ba9e4
8 changed files with 2017 additions and 1985 deletions

View File

@ -163,6 +163,7 @@ HEADERS += src/qt/bitcoingui.h \
src/uint256.h \
src/serialize.h \
src/main.h \
src/darksend.h \
src/net.h \
src/key.h \
src/db.h \
@ -259,6 +260,7 @@ SOURCES += src/qt/bitcoin.cpp \
src/key.cpp \
src/script.cpp \
src/main.cpp \
src/darksend.cpp \
src/init.cpp \
src/net.cpp \
src/bloom.cpp \

View File

@ -71,6 +71,7 @@
#include "main.h"
#include "txdb.h"
#include "uint256.h"
#include "darksend.h"
using namespace json_spirit;
using namespace std;

1609
src/darksend.cpp Normal file

File diff suppressed because it is too large Load Diff

393
src/darksend.h Normal file
View File

@ -0,0 +1,393 @@
// 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 DARKSEND_H
#define DARKSEND_H
#include "main.h"
class CTxIn;
class CDarkSendPool;
class CDarkSendSigner;
class CMasterNode;
class CMasterNodeVote;
class CBitcoinAddress;
class CDarksendQueue;
extern CDarkSendPool darkSendPool;
extern CDarkSendSigner darkSendSigner;
extern std::vector<CMasterNode> darkSendMasterNodes;
extern std::vector<int64> darkSendDenominations;
extern std::string strMasterNodePrivKey;
extern std::string strUseMasternode;
extern int64 enforceMasternodePaymentsTime;
extern std::vector<CDarksendQueue> vecDarksendQueue;
extern std::vector<CTxIn> vecMasternodeAskedFor;
class CMasterNode
{
public:
CService addr;
CTxIn vin;
int64 lastTimeSeen;
CPubKey pubkey;
CPubKey pubkey2;
std::vector<unsigned char> sig;
int64 now;
int enabled;
bool unitTest;
CMasterNode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector<unsigned char> newSig, int64 newNow, CPubKey newPubkey2)
{
addr = newAddr;
vin = newVin;
pubkey = newPubkey;
pubkey2 = newPubkey2;
sig = newSig;
now = newNow;
enabled = 1;
lastTimeSeen = 0;
unitTest = false;
}
uint256 CalculateScore(int mod=1, int64 nBlockHeight=0);
void UpdateLastSeen(int64 override=0)
{
if(override == 0){
lastTimeSeen = GetTimeMicros();
} else {
lastTimeSeen = override;
}
}
void Check();
bool UpdatedWithin(int microSeconds)
{
//LogPrintf("UpdatedWithin %"PRI64u", %"PRI64u" -- %d \n", GetTimeMicros() , lastTimeSeen, (GetTimeMicros() - lastTimeSeen) < microSeconds);
return (GetTimeMicros() - lastTimeSeen) < microSeconds;
}
void Disable()
{
lastTimeSeen = 0;
}
bool IsEnabled()
{
return enabled == 1;
}
};
class CDarkSendEntryVin
{
public:
bool isSigSet;
CTxIn vin;
CDarkSendEntryVin()
{
isSigSet = false;
vin = CTxIn();
}
};
class CDarkSendEntry
{
public:
bool isSet;
std::vector<CDarkSendEntryVin> sev;
int64 amount;
CTransaction collateral;
std::vector<CTxOut> vout;
CTransaction txSupporting;
int64 addedTime;
CDarkSendEntry()
{
isSet = false;
collateral = CTransaction();
amount = 0;
}
bool Add(const std::vector<CTxIn> vinIn, int64 amountIn, const CTransaction collateralIn, const std::vector<CTxOut> voutIn)
{
if(isSet){return false;}
BOOST_FOREACH(const CTxIn v, vinIn) {
CDarkSendEntryVin s = CDarkSendEntryVin();
s.vin = v;
sev.push_back(s);
}
vout = voutIn;
amount = amountIn;
collateral = collateralIn;
isSet = true;
addedTime = GetTime();
return true;
}
bool AddSig(const CTxIn& vin)
{
BOOST_FOREACH(CDarkSendEntryVin& s, sev) {
if(s.vin.prevout == vin.prevout && s.vin.nSequence == vin.nSequence){
if(s.isSigSet){return false;}
s.vin.scriptSig = vin.scriptSig;
s.vin.prevPubKey = vin.prevPubKey;
s.isSigSet = true;
return true;
}
}
return false;
}
bool IsExpired()
{
return (GetTime() - addedTime) > 120;// 120 seconds
}
};
class CDarksendQueue
{
public:
CTxIn vin;
int64 time;
int nDenom;
CDarksendQueue()
{
nDenom = 0;
vin = CTxIn();
time = 0;
}
IMPLEMENT_SERIALIZE
(
READWRITE(nDenom);
READWRITE(vin);
READWRITE(time);
)
bool GetAddress(CService &addr)
{
BOOST_FOREACH(CMasterNode mn, darkSendMasterNodes) {
if(mn.vin == vin){
addr = mn.addr;
return true;
}
}
return false;
}
void Relay()
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
pnode->PushMessage("dsq", (*this));
}
bool IsExpired()
{
return (GetTime() - time) > 120;// 120 seconds
}
};
class CDarkSendSigner
{
public:
bool SetKey(std::string strSecret, std::string& errorMessage, CKey& key, CPubKey& pubkey);
bool SignMessage(std::string strMessage, std::string& errorMessage, std::vector<unsigned char>& vchSig, CKey key);
bool VerifyMessage(CPubKey pubkey, std::vector<unsigned char>& vchSig, std::string strMessage, std::string& errorMessage);
};
static const int64 DARKSEND_COLLATERAL = 0.025*COIN;
static const int64 DARKSEND_FEE = 0.0125*COIN;
/** Used to keep track of current status of darksend pool
*/
class CDarkSendPool
{
public:
static const int MIN_PEER_PROTO_VERSION = 70035;
std::vector<CDarkSendEntry> myEntries;
std::vector<CDarkSendEntry> entries;
CTransaction finalTransaction;
int64 lastTimeChanged;
int64 lastAutoDenomination;
unsigned int state;
unsigned int entriesCount;
unsigned int lastEntryAccepted;
unsigned int countEntriesAccepted;
CScript collateralPubKey;
std::vector<CTxIn> lockedCoins;
CTxIn vinMasterNode;
CPubKey pubkeyMasterNode;
CPubKey pubkeyMasterNode2;
std::string strMasterNodeSignMessage;
std::vector<unsigned char> vchMasterNodeSignature;
int isCapableMasterNode;
uint256 masterNodeBlockHash;
std::string masterNodeAddr;
CService masterNodeSignAddr;
int64 masterNodeSignatureTime;
int masternodePortOpen;
std::string lastMessage;
bool completedTransaction;
bool unitTest;
CService submittedToMasternode;
int sessionID;
int64 sessionAmount; //Users must submit an amount compatible with this amount
int sessionUsers; //N Users have said they'll join
bool sessionFoundMasternode; //If we've found a compatible masternode
int sessionTries;
int lastSplitUpBlock;
int cachedLastSuccess;
CDarkSendPool()
{
//LogPrintf("CDarkSendPool::INIT()\n");
/* DarkSend uses collateral addresses to trust parties entering the pool
to behave themselves. If they don't it takes their money. */
std::string strAddress = "";
if(!fTestNet) {
strAddress = "Xq19GqFvajRrEdDHYRKGYjTsQfpV5jyipF";
} else {
strAddress = "mxE2Rp3oYpSEFdsN5TdHWhZvEHm3PJQQVm";
}
isCapableMasterNode = MASTERNODE_NOT_PROCESSED;
masternodePortOpen = 0;
lastSplitUpBlock = 0;
cachedLastSuccess = 0;
unitTest = false;
SetCollateralAddress(strAddress);
SetNull();
}
bool SetCollateralAddress(std::string strAddress);
void SetNull(bool clearEverything=false);
void UnlockCoins();
bool IsNull() const
{
return (state == POOL_STATUS_ACCEPTING_ENTRIES && entries.empty() && myEntries.empty());
}
int GetState() const
{
return state;
}
int GetEntriesCount() const
{
if(fMasterNode){
return entries.size();
} else {
return entriesCount;
}
}
int GetLastEntryAccepted() const
{
return lastEntryAccepted;
}
int GetCountEntriesAccepted() const
{
return countEntriesAccepted;
}
int GetMyTransactionCount() const
{
return myEntries.size();
}
std::string GetMasterNodeAddr()
{
return masterNodeAddr;
}
void UpdateState(unsigned int newState)
{
if (fMasterNode && (newState == POOL_STATUS_ERROR || newState == POOL_STATUS_SUCCESS)){
LogPrintf("CDarkSendPool::UpdateState() - Can't set state to ERROR or SUCCESS as a masternode. \n");
return;
}
LogPrintf("CDarkSendPool::UpdateState() == %d | %d \n", state, newState);
if(state != newState){
lastTimeChanged = GetTimeMillis();
if(fMasterNode) {
RelayDarkSendStatus(darkSendPool.sessionID, darkSendPool.GetState(), darkSendPool.GetEntriesCount(), -1);
}
}
state = newState;
}
bool IsCompatibleWithEntries(std::vector<CTxOut> vout);
bool IsCompatibleWithSession(int64 nAmount);
bool DoAutomaticDenominating(bool fDryRun=false);
int GetCurrentMasterNode(int mod=1, int64 nBlockHeight=0);
int GetMasternodeByVin(CTxIn& vin);
int GetMasternodeRank(CTxIn& vin, int mod);
int GetMasternodeByRank(int findRank);
void Check();
void ChargeFees();
void CheckTimeout();
bool SignatureValid(const CScript& newSig, const CTxIn& newVin);
bool IsCollateralValid(const CTransaction& txCollateral);
bool AddEntry(const std::vector<CTxIn>& newInput, const int64& nAmount, const CTransaction& txCollateral, const std::vector<CTxOut>& newOutput, std::string& error);
bool AddScriptSig(const CTxIn& newVin);
bool SignaturesComplete();
void SendMoney(const CTransaction& collateral, std::vector<CTxIn>& vin, std::vector<CTxOut>& vout, int64& fee, int64 amount);
bool StatusUpdate(int newState, int newEntriesCount, int newAccepted, std::string& error, int newSessionID=0);
bool SignFinalTransaction(CTransaction& finalTransactionNew, CNode* node);
void ProcessMasternodeConnections();
bool ConnectToBestMasterNode(int depth=0);
bool GetMasterNodeVin(CTxIn& vin, CPubKey& pubkey, CKey& secretKey);
void RelayDarkDeclareWinner();
bool EnableHotColdMasterNode(CTxIn& vin, int64 sigTime, CService& addr);
void RegisterAsMasterNode(bool stop);
bool GetLastValidBlockHash(uint256& hash, int mod=1, int nBlockHeight=0);
void NewBlock();
void CompletedTransaction(bool error, std::string lastMessageNew);
void ClearLastMessage();
int GetInputDarksendRounds(CTxIn in, int rounds=0);
bool SplitUpMoney(bool justCollateral=false);
int GetDenominations(const std::vector<CTxOut>& vout);
int GetDenominationsByAmount(int64 nAmount);
};
void ConnectToDarkSendMasterNodeWinner();
void ThreadCheckDarkSendPool();
#endif

File diff suppressed because it is too large Load Diff

View File

@ -16,22 +16,7 @@
#include <list>
#include <algorithm>
#include <boost/lexical_cast.hpp>
//#define static_assert(numeric_limits<double>::max_exponent() > 8, "your double sux");
class CWallet;
class CBlock;
class CBlockIndex;
class CKeyItem;
class CReserveKey;
class CAddress;
class CInv;
class CNode;
class CDarkSendPool;
class CDarkSendSigner;
class CMasterNode;
class CMasterNodeVote;
class CBitcoinAddress;
#define START_MASTERNODE_PAYMENTS_TESTNET 1403568776 //Tue, 24 Jun 2014 00:12:56 GMT
#define START_MASTERNODE_PAYMENTS 1403728576 //Wed, 25 Jun 2014 20:36:16 GMT
@ -60,6 +45,16 @@ class CBitcoinAddress;
#define MASTERNODE_EXPIRATION_MICROSECONDS 35*60*1000*1000
#define MASTERNODE_REMOVAL_MICROSECONDS 35.5*60*1000*1000
class CWallet;
class CBlock;
class CBlockIndex;
class CKeyItem;
class CReserveKey;
class CAddress;
class CInv;
class CNode;
struct CBlockIndexWorkComparator;
/** The maximum allowed size for a serialized block, in bytes (network rule) */
@ -139,13 +134,6 @@ extern int nScriptCheckThreads;
extern int nAskedForBlocks; // Nodes sent a getblocks 0
extern bool fTxIndex;
extern unsigned int nCoinCacheSize;
extern CDarkSendPool darkSendPool;
extern CDarkSendSigner darkSendSigner;
extern std::vector<CMasterNode> darkSendMasterNodes;
extern std::vector<int64> darkSendDenominations;
extern std::string strMasterNodePrivKey;
extern std::string strUseMasternode;
extern int64 enforceMasternodePaymentsTime;
extern CWallet pmainWallet;
extern std::map<uint256, CBlock*> mapOrphanBlocks;
@ -1676,13 +1664,6 @@ public:
}
return false;
}
bool MasterNodePaymentsEnforcing() const
{
if(nTime > enforceMasternodePaymentsTime) return true;
return false;
}
};
@ -2436,369 +2417,6 @@ public:
)
};
class CMasterNode
{
public:
CService addr;
CTxIn vin;
int64 lastTimeSeen;
CPubKey pubkey;
CPubKey pubkey2;
std::vector<unsigned char> sig;
int64 now;
int enabled;
bool unitTest;
CMasterNode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector<unsigned char> newSig, int64 newNow, CPubKey newPubkey2)
{
addr = newAddr;
vin = newVin;
pubkey = newPubkey;
pubkey2 = newPubkey2;
sig = newSig;
now = newNow;
enabled = 1;
lastTimeSeen = 0;
unitTest = false;
}
uint256 CalculateScore(int mod=1, int64 nBlockHeight=0);
void UpdateLastSeen(int64 override=0)
{
if(override == 0){
lastTimeSeen = GetTimeMicros();
} else {
lastTimeSeen = override;
}
}
void Check();
bool UpdatedWithin(int microSeconds)
{
//LogPrintf("UpdatedWithin %"PRI64u", %"PRI64u" -- %d \n", GetTimeMicros() , lastTimeSeen, (GetTimeMicros() - lastTimeSeen) < microSeconds);
return (GetTimeMicros() - lastTimeSeen) < microSeconds;
}
void Disable()
{
lastTimeSeen = 0;
}
bool IsEnabled()
{
return enabled == 1;
}
};
class CDarkSendEntryVin
{
public:
bool isSigSet;
CTxIn vin;
CDarkSendEntryVin()
{
isSigSet = false;
vin = CTxIn();
}
};
class CDarkSendEntry
{
public:
bool isSet;
std::vector<CDarkSendEntryVin> sev;
int64 amount;
CTransaction collateral;
std::vector<CTxOut> vout;
CTransaction txSupporting;
int64 addedTime;
CDarkSendEntry()
{
isSet = false;
collateral = CTransaction();
amount = 0;
}
bool Add(const std::vector<CTxIn> vinIn, int64 amountIn, const CTransaction collateralIn, const std::vector<CTxOut> voutIn)
{
if(isSet){return false;}
BOOST_FOREACH(const CTxIn v, vinIn) {
CDarkSendEntryVin s = CDarkSendEntryVin();
s.vin = v;
sev.push_back(s);
}
vout = voutIn;
amount = amountIn;
collateral = collateralIn;
isSet = true;
addedTime = GetTime();
return true;
}
bool AddSig(const CTxIn& vin)
{
BOOST_FOREACH(CDarkSendEntryVin& s, sev) {
if(s.vin.prevout == vin.prevout && s.vin.nSequence == vin.nSequence){
if(s.isSigSet){return false;}
s.vin.scriptSig = vin.scriptSig;
s.vin.prevPubKey = vin.prevPubKey;
s.isSigSet = true;
return true;
}
}
return false;
}
bool IsExpired()
{
return (GetTime() - addedTime) > 120;// 120 seconds
}
};
class CDarksendQueue
{
public:
CTxIn vin;
int64 time;
int nDenom;
CDarksendQueue()
{
nDenom = 0;
vin = CTxIn();
time = 0;
}
IMPLEMENT_SERIALIZE
(
READWRITE(nDenom);
READWRITE(vin);
READWRITE(time);
)
bool GetAddress(CService &addr)
{
BOOST_FOREACH(CMasterNode mn, darkSendMasterNodes) {
if(mn.vin == vin){
addr = mn.addr;
return true;
}
}
return false;
}
void Relay()
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
pnode->PushMessage("dsq", (*this));
}
bool IsExpired()
{
return (GetTime() - time) > 120;// 120 seconds
}
};
class CDarkSendSigner
{
public:
bool SetKey(std::string strSecret, std::string& errorMessage, CKey& key, CPubKey& pubkey);
bool SignMessage(std::string strMessage, std::string& errorMessage, std::vector<unsigned char>& vchSig, CKey key);
bool VerifyMessage(CPubKey pubkey, std::vector<unsigned char>& vchSig, std::string strMessage, std::string& errorMessage);
};
static const int64 DARKSEND_COLLATERAL = 0.025*COIN;
static const int64 DARKSEND_FEE = 0.0125*COIN;
/** Used to keep track of current status of darksend pool
*/
class CDarkSendPool
{
public:
static const int MIN_PEER_PROTO_VERSION = 70035;
std::vector<CDarkSendEntry> myEntries;
std::vector<CDarkSendEntry> entries;
CTransaction finalTransaction;
int64 lastTimeChanged;
int64 lastAutoDenomination;
unsigned int state;
unsigned int entriesCount;
unsigned int lastEntryAccepted;
unsigned int countEntriesAccepted;
CScript collateralPubKey;
std::vector<CTxIn> lockedCoins;
CTxIn vinMasterNode;
CPubKey pubkeyMasterNode;
CPubKey pubkeyMasterNode2;
std::string strMasterNodeSignMessage;
std::vector<unsigned char> vchMasterNodeSignature;
int isCapableMasterNode;
uint256 masterNodeBlockHash;
std::string masterNodeAddr;
CService masterNodeSignAddr;
int64 masterNodeSignatureTime;
int masternodePortOpen;
std::string lastMessage;
bool completedTransaction;
bool unitTest;
CService submittedToMasternode;
int sessionID;
int64 sessionAmount; //Users must submit an amount compatible with this amount
int sessionUsers; //N Users have said they'll join
bool sessionFoundMasternode; //If we've found a compatible masternode
int sessionTries;
int lastSplitUpBlock;
int cachedLastSuccess;
CDarkSendPool()
{
//LogPrintf("CDarkSendPool::INIT()\n");
/* DarkSend uses collateral addresses to trust parties entering the pool
to behave themselves. If they don't it takes their money. */
std::string strAddress = "";
if(!fTestNet) {
strAddress = "Xq19GqFvajRrEdDHYRKGYjTsQfpV5jyipF";
} else {
strAddress = "mxE2Rp3oYpSEFdsN5TdHWhZvEHm3PJQQVm";
}
isCapableMasterNode = MASTERNODE_NOT_PROCESSED;
masternodePortOpen = 0;
lastSplitUpBlock = 0;
cachedLastSuccess = 0;
unitTest = false;
SetCollateralAddress(strAddress);
SetNull();
}
bool SetCollateralAddress(std::string strAddress);
void SetNull(bool clearEverything=false);
void UnlockCoins();
bool IsNull() const
{
return (state == POOL_STATUS_ACCEPTING_ENTRIES && entries.empty() && myEntries.empty());
}
int GetState() const
{
return state;
}
int GetEntriesCount() const
{
if(fMasterNode){
return entries.size();
} else {
return entriesCount;
}
}
int GetLastEntryAccepted() const
{
return lastEntryAccepted;
}
int GetCountEntriesAccepted() const
{
return countEntriesAccepted;
}
int GetMyTransactionCount() const
{
return myEntries.size();
}
std::string GetMasterNodeAddr()
{
return masterNodeAddr;
}
void UpdateState(unsigned int newState)
{
if (fMasterNode && (newState == POOL_STATUS_ERROR || newState == POOL_STATUS_SUCCESS)){
LogPrintf("CDarkSendPool::UpdateState() - Can't set state to ERROR or SUCCESS as a masternode. \n");
return;
}
LogPrintf("CDarkSendPool::UpdateState() == %d | %d \n", state, newState);
if(state != newState){
lastTimeChanged = GetTimeMillis();
if(fMasterNode) {
RelayDarkSendStatus(darkSendPool.sessionID, darkSendPool.GetState(), darkSendPool.GetEntriesCount(), -1);
}
}
state = newState;
}
bool IsCompatibleWithEntries(std::vector<CTxOut> vout);
bool IsCompatibleWithSession(int64 nAmount);
bool DoAutomaticDenominating(bool fDryRun=false);
int GetCurrentMasterNode(int mod=1, int64 nBlockHeight=0);
int GetMasternodeByVin(CTxIn& vin);
int GetMasternodeRank(CTxIn& vin, int mod);
int GetMasternodeByRank(int findRank);
void Check();
void ChargeFees();
void CheckTimeout();
bool SignatureValid(const CScript& newSig, const CTxIn& newVin);
bool IsCollateralValid(const CTransaction& txCollateral);
bool AddEntry(const std::vector<CTxIn>& newInput, const int64& nAmount, const CTransaction& txCollateral, const std::vector<CTxOut>& newOutput, std::string& error);
bool AddScriptSig(const CTxIn& newVin);
bool SignaturesComplete();
void SendMoney(const CTransaction& collateral, std::vector<CTxIn>& vin, std::vector<CTxOut>& vout, int64& fee, int64 amount);
bool StatusUpdate(int newState, int newEntriesCount, int newAccepted, std::string& error, int newSessionID=0);
bool SignFinalTransaction(CTransaction& finalTransactionNew, CNode* node);
void ProcessMasternodeConnections();
bool ConnectToBestMasterNode(int depth=0);
bool GetMasterNodeVin(CTxIn& vin, CPubKey& pubkey, CKey& secretKey);
void RelayDarkDeclareWinner();
bool EnableHotColdMasterNode(CTxIn& vin, int64 sigTime, CService& addr);
void RegisterAsMasterNode(bool stop);
bool GetLastValidBlockHash(uint256& hash, int mod=1, int nBlockHeight=0);
void NewBlock();
void CompletedTransaction(bool error, std::string lastMessageNew);
void ClearLastMessage();
int GetInputDarksendRounds(CTxIn in, int rounds=0);
bool SplitUpMoney(bool justCollateral=false);
int GetDenominations(const std::vector<CTxOut>& vout);
int GetDenominationsByAmount(int64 nAmount);
};
void ConnectToDarkSendMasterNodeWinner();
void ThreadCheckDarkSendPool();
#endif

View File

@ -578,7 +578,6 @@ Value getblocktemplate(const Array& params, bool fHelp)
result.push_back(Pair("payee", ""));
}
result.push_back(Pair("masternode_payments", pblock->MasterNodePaymentsOn()));
result.push_back(Pair("enforce_masternode_payments", pblock->MasterNodePaymentsEnforcing()));
return result;
}

View File

@ -11,6 +11,7 @@
#include <stdlib.h>
#include "main.h"
#include "darksend.h"
#include "key.h"
#include "keystore.h"
#include "script.h"