2017-05-05 13:26:27 +02:00
|
|
|
// Copyright (c) 2014-2017 The Dash Core developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include "privatesend-server.h"
|
|
|
|
|
|
|
|
#include "activemasternode.h"
|
|
|
|
#include "consensus/validation.h"
|
|
|
|
#include "core_io.h"
|
|
|
|
#include "init.h"
|
|
|
|
#include "masternode-sync.h"
|
|
|
|
#include "masternodeman.h"
|
|
|
|
#include "txmempool.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "utilmoneystr.h"
|
|
|
|
|
|
|
|
CPrivateSendServer privateSendServer;
|
|
|
|
|
|
|
|
void CPrivateSendServer::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
|
|
|
|
{
|
|
|
|
if(!fMasterNode) return;
|
|
|
|
if(fLiteMode) return; // ignore all Dash related functionality
|
|
|
|
if(!masternodeSync.IsBlockchainSynced()) return;
|
|
|
|
|
|
|
|
if(strCommand == NetMsgType::DSACCEPT) {
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
|
|
|
LogPrintf("DSACCEPT -- incompatible version! nVersion: %d\n", pfrom->nVersion);
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_VERSION);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(IsSessionReady()) {
|
|
|
|
// too many users in this session already, reject new ones
|
|
|
|
LogPrintf("DSACCEPT -- queue is already full!\n");
|
|
|
|
PushStatus(pfrom, STATUS_ACCEPTED, ERR_QUEUE_FULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nDenom;
|
|
|
|
CTransaction txCollateral;
|
|
|
|
vRecv >> nDenom >> txCollateral;
|
|
|
|
|
2017-06-30 20:30:16 +02:00
|
|
|
LogPrint("privatesend", "DSACCEPT -- nDenom %d (%s) txCollateral %s", nDenom, CPrivateSend::GetDenominationsToString(nDenom), txCollateral.ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
CMasternode* pmn = mnodeman.Find(activeMasternode.vin);
|
|
|
|
if(pmn == NULL) {
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MN_LIST);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vecSessionCollaterals.size() == 0 && pmn->nLastDsq != 0 &&
|
|
|
|
pmn->nLastDsq + mnodeman.CountEnabled(MIN_PRIVATESEND_PEER_PROTO_VERSION)/5 > mnodeman.nDsqCount)
|
|
|
|
{
|
|
|
|
LogPrintf("DSACCEPT -- last dsq too recent, must wait: addr=%s\n", pfrom->addr.ToString());
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_RECENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolMessage nMessageID = MSG_NOERR;
|
|
|
|
|
|
|
|
bool fResult = nSessionID == 0 ? CreateNewSession(nDenom, txCollateral, nMessageID)
|
|
|
|
: AddUserToExistingSession(nDenom, txCollateral, nMessageID);
|
|
|
|
if(fResult) {
|
|
|
|
LogPrintf("DSACCEPT -- is compatible, please submit!\n");
|
|
|
|
PushStatus(pfrom, STATUS_ACCEPTED, nMessageID);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
LogPrintf("DSACCEPT -- not compatible with existing transactions!\n");
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, nMessageID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(strCommand == NetMsgType::DSQUEUE) {
|
|
|
|
TRY_LOCK(cs_darksend, lockRecv);
|
|
|
|
if(!lockRecv) return;
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- incompatible version! nVersion: %d\n", pfrom->nVersion);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDarksendQueue dsq;
|
|
|
|
vRecv >> dsq;
|
|
|
|
|
|
|
|
// process every dsq only once
|
|
|
|
BOOST_FOREACH(CDarksendQueue q, vecDarksendQueue) {
|
|
|
|
if(q == dsq) {
|
|
|
|
// LogPrint("privatesend", "DSQUEUE -- %s seen\n", dsq.ToString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- %s new\n", dsq.ToString());
|
|
|
|
|
2017-08-29 01:51:44 +02:00
|
|
|
if(dsq.IsExpired()) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
CMasternode* pmn = mnodeman.Find(dsq.vin);
|
|
|
|
if(pmn == NULL) return;
|
|
|
|
|
|
|
|
if(!dsq.CheckSignature(pmn->pubKeyMasternode)) {
|
|
|
|
// we probably have outdated info
|
|
|
|
mnodeman.AskForMN(pfrom, dsq.vin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!dsq.fReady) {
|
|
|
|
BOOST_FOREACH(CDarksendQueue q, vecDarksendQueue) {
|
|
|
|
if(q.vin == dsq.vin) {
|
|
|
|
// no way same mn can send another "not yet ready" dsq this soon
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- Masternode %s is sending WAY too many dsq messages\n", pmn->addr.ToString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nThreshold = pmn->nLastDsq + mnodeman.CountEnabled(MIN_PRIVATESEND_PEER_PROTO_VERSION)/5;
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- nLastDsq: %d threshold: %d nDsqCount: %d\n", pmn->nLastDsq, nThreshold, mnodeman.nDsqCount);
|
|
|
|
//don't allow a few nodes to dominate the queuing process
|
|
|
|
if(pmn->nLastDsq != 0 && nThreshold > mnodeman.nDsqCount) {
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- Masternode %s is sending too many dsq messages\n", pmn->addr.ToString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mnodeman.nDsqCount++;
|
|
|
|
pmn->nLastDsq = mnodeman.nDsqCount;
|
|
|
|
pmn->fAllowMixingTx = true;
|
|
|
|
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- new PrivateSend queue (%s) from masternode %s\n", dsq.ToString(), pmn->addr.ToString());
|
|
|
|
vecDarksendQueue.push_back(dsq);
|
|
|
|
dsq.Relay();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(strCommand == NetMsgType::DSVIN) {
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
|
|
|
LogPrintf("DSVIN -- incompatible version! nVersion: %d\n", pfrom->nVersion);
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_VERSION);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//do we have enough users in the current session?
|
|
|
|
if(!IsSessionReady()) {
|
|
|
|
LogPrintf("DSVIN -- session not complete!\n");
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_SESSION);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDarkSendEntry entry;
|
|
|
|
vRecv >> entry;
|
|
|
|
|
|
|
|
LogPrint("privatesend", "DSVIN -- txCollateral %s", entry.txCollateral.ToString());
|
|
|
|
|
|
|
|
if(entry.vecTxDSIn.size() > PRIVATESEND_ENTRY_MAX_SIZE) {
|
|
|
|
LogPrintf("DSVIN -- ERROR: too many inputs! %d/%d\n", entry.vecTxDSIn.size(), PRIVATESEND_ENTRY_MAX_SIZE);
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MAXIMUM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(entry.vecTxDSOut.size() > PRIVATESEND_ENTRY_MAX_SIZE) {
|
|
|
|
LogPrintf("DSVIN -- ERROR: too many outputs! %d/%d\n", entry.vecTxDSOut.size(), PRIVATESEND_ENTRY_MAX_SIZE);
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MAXIMUM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//do we have the same denominations as the current session?
|
|
|
|
if(!IsOutputsCompatibleWithSessionDenom(entry.vecTxDSOut)) {
|
|
|
|
LogPrintf("DSVIN -- not compatible with existing transactions!\n");
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_EXISTING_TX);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check it like a transaction
|
|
|
|
{
|
|
|
|
CAmount nValueIn = 0;
|
|
|
|
CAmount nValueOut = 0;
|
|
|
|
|
|
|
|
CMutableTransaction tx;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxOut txout, entry.vecTxDSOut) {
|
|
|
|
nValueOut += txout.nValue;
|
|
|
|
tx.vout.push_back(txout);
|
|
|
|
|
|
|
|
if(txout.scriptPubKey.size() != 25) {
|
|
|
|
LogPrintf("DSVIN -- non-standard pubkey detected! scriptPubKey=%s\n", ScriptToAsmStr(txout.scriptPubKey));
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_NON_STANDARD_PUBKEY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!txout.scriptPubKey.IsNormalPaymentScript()) {
|
|
|
|
LogPrintf("DSVIN -- invalid script! scriptPubKey=%s\n", ScriptToAsmStr(txout.scriptPubKey));
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_INVALID_SCRIPT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxIn txin, entry.vecTxDSIn) {
|
|
|
|
tx.vin.push_back(txin);
|
|
|
|
|
|
|
|
LogPrint("privatesend", "DSVIN -- txin=%s\n", txin.ToString());
|
|
|
|
|
2017-08-25 14:56:48 +02:00
|
|
|
CCoins coins;
|
|
|
|
if(GetUTXOCoins(txin.prevout, coins)) {
|
|
|
|
nValueIn += coins.vout[txin.prevout.n].nValue;
|
2017-05-05 13:26:27 +02:00
|
|
|
} else {
|
|
|
|
LogPrintf("DSVIN -- missing input! tx=%s", tx.ToString());
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MISSING_TX);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should be no fee in mixing tx
|
|
|
|
CAmount nFee = nValueIn - nValueOut;
|
|
|
|
if(nFee != 0) {
|
|
|
|
LogPrintf("DSVIN -- there should be no fee in mixing tx! fees: %lld, tx=%s", nFee, tx.ToString());
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_FEES);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
CValidationState validationState;
|
|
|
|
mempool.PrioritiseTransaction(tx.GetHash(), tx.GetHash().ToString(), 1000, 0.1*COIN);
|
|
|
|
if(!AcceptToMemoryPool(mempool, validationState, CTransaction(tx), false, NULL, false, true, true)) {
|
|
|
|
LogPrintf("DSVIN -- transaction not valid! tx=%s", tx.ToString());
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_INVALID_TX);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolMessage nMessageID = MSG_NOERR;
|
|
|
|
|
2017-07-25 12:57:26 +02:00
|
|
|
entry.addr = pfrom->addr;
|
2017-05-05 13:26:27 +02:00
|
|
|
if(AddEntry(entry, nMessageID)) {
|
|
|
|
PushStatus(pfrom, STATUS_ACCEPTED, nMessageID);
|
|
|
|
CheckPool();
|
|
|
|
RelayStatus(STATUS_ACCEPTED);
|
|
|
|
} else {
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, nMessageID);
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(strCommand == NetMsgType::DSSIGNFINALTX) {
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
|
|
|
LogPrintf("DSSIGNFINALTX -- incompatible version! nVersion: %d\n", pfrom->nVersion);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CTxIn> vecTxIn;
|
|
|
|
vRecv >> vecTxIn;
|
|
|
|
|
|
|
|
LogPrint("privatesend", "DSSIGNFINALTX -- vecTxIn.size() %s\n", vecTxIn.size());
|
|
|
|
|
|
|
|
int nTxInIndex = 0;
|
|
|
|
int nTxInsCount = (int)vecTxIn.size();
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxIn txin, vecTxIn) {
|
|
|
|
nTxInIndex++;
|
|
|
|
if(!AddScriptSig(txin)) {
|
|
|
|
LogPrint("privatesend", "DSSIGNFINALTX -- AddScriptSig() failed at %d/%d, session: %d\n", nTxInIndex, nTxInsCount, nSessionID);
|
|
|
|
RelayStatus(STATUS_REJECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LogPrint("privatesend", "DSSIGNFINALTX -- AddScriptSig() %d/%d success\n", nTxInIndex, nTxInsCount);
|
|
|
|
}
|
|
|
|
// all is good
|
|
|
|
CheckPool();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::SetNull()
|
|
|
|
{
|
|
|
|
// MN side
|
|
|
|
vecSessionCollaterals.clear();
|
|
|
|
|
2017-06-30 20:30:16 +02:00
|
|
|
CPrivateSendBase::SetNull();
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check the mixing progress and send client updates if a Masternode
|
|
|
|
//
|
|
|
|
void CPrivateSendServer::CheckPool()
|
|
|
|
{
|
|
|
|
if(fMasterNode) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckPool -- entries count %lu\n", GetEntriesCount());
|
|
|
|
|
|
|
|
// If entries are full, create finalized transaction
|
2017-06-30 20:30:16 +02:00
|
|
|
if(nState == POOL_STATE_ACCEPTING_ENTRIES && GetEntriesCount() >= CPrivateSend::GetMaxPoolTransactions()) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckPool -- FINALIZE TRANSACTIONS\n");
|
|
|
|
CreateFinalTransaction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have all of the signatures, try to compile the transaction
|
|
|
|
if(nState == POOL_STATE_SIGNING && IsSignaturesComplete()) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckPool -- SIGNING\n");
|
|
|
|
CommitFinalTransaction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset if we're here for 10 seconds
|
|
|
|
if((nState == POOL_STATE_ERROR || nState == POOL_STATE_SUCCESS) && GetTimeMillis() - nTimeLastSuccessfulStep >= 10000) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckPool -- timeout, RESETTING\n");
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::CreateFinalTransaction()
|
|
|
|
{
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CreateFinalTransaction -- FINALIZE TRANSACTIONS\n");
|
|
|
|
|
|
|
|
CMutableTransaction txNew;
|
|
|
|
|
|
|
|
// make our new transaction
|
|
|
|
for(int i = 0; i < GetEntriesCount(); i++) {
|
|
|
|
BOOST_FOREACH(const CTxDSOut& txdsout, vecEntries[i].vecTxDSOut)
|
|
|
|
txNew.vout.push_back(txdsout);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxDSIn& txdsin, vecEntries[i].vecTxDSIn)
|
|
|
|
txNew.vin.push_back(txdsin);
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:42:59 +02:00
|
|
|
sort(txNew.vin.begin(), txNew.vin.end(), CompareInputBIP69());
|
|
|
|
sort(txNew.vout.begin(), txNew.vout.end(), CompareOutputBIP69());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
finalMutableTransaction = txNew;
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CreateFinalTransaction -- finalMutableTransaction=%s", txNew.ToString());
|
|
|
|
|
|
|
|
// request signatures from clients
|
|
|
|
RelayFinalTransaction(finalMutableTransaction);
|
|
|
|
SetState(POOL_STATE_SIGNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::CommitFinalTransaction()
|
|
|
|
{
|
|
|
|
if(!fMasterNode) return; // check and relay final tx only on masternode
|
|
|
|
|
|
|
|
CTransaction finalTransaction = CTransaction(finalMutableTransaction);
|
|
|
|
uint256 hashTx = finalTransaction.GetHash();
|
|
|
|
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CommitFinalTransaction -- finalTransaction=%s", finalTransaction.ToString());
|
|
|
|
|
|
|
|
{
|
|
|
|
// See if the transaction is valid
|
|
|
|
TRY_LOCK(cs_main, lockMain);
|
|
|
|
CValidationState validationState;
|
|
|
|
mempool.PrioritiseTransaction(hashTx, hashTx.ToString(), 1000, 0.1*COIN);
|
|
|
|
if(!lockMain || !AcceptToMemoryPool(mempool, validationState, finalTransaction, false, NULL, false, true, true))
|
|
|
|
{
|
|
|
|
LogPrintf("CPrivateSendServer::CommitFinalTransaction -- AcceptToMemoryPool() error: Transaction not valid\n");
|
|
|
|
SetNull();
|
|
|
|
// not much we can do in this case, just notify clients
|
|
|
|
RelayCompletedTransaction(ERR_INVALID_TX);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrintf("CPrivateSendServer::CommitFinalTransaction -- CREATING DSTX\n");
|
|
|
|
|
|
|
|
// create and sign masternode dstx transaction
|
2017-07-19 21:49:48 +02:00
|
|
|
if(!CPrivateSend::GetDSTX(hashTx)) {
|
2017-06-30 20:30:16 +02:00
|
|
|
CDarksendBroadcastTx dstxNew(finalTransaction, activeMasternode.vin, GetAdjustedTime());
|
|
|
|
dstxNew.Sign();
|
|
|
|
CPrivateSend::AddDSTX(dstxNew);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LogPrintf("CPrivateSendServer::CommitFinalTransaction -- TRANSMITTING DSTX\n");
|
|
|
|
|
|
|
|
CInv inv(MSG_DSTX, hashTx);
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
g_connman->RelayInv(inv);
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
// Tell the clients it was successful
|
|
|
|
RelayCompletedTransaction(MSG_SUCCESS);
|
|
|
|
|
|
|
|
// Randomly charge clients
|
|
|
|
ChargeRandomFees();
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CommitFinalTransaction -- COMPLETED -- RESETTING\n");
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Charge clients a fee if they're abusive
|
|
|
|
//
|
|
|
|
// Why bother? PrivateSend uses collateral to ensure abuse to the process is kept to a minimum.
|
|
|
|
// The submission and signing stages are completely separate. In the cases where
|
|
|
|
// a client submits a transaction then refused to sign, there must be a cost. Otherwise they
|
|
|
|
// would be able to do this over and over again and bring the mixing to a hault.
|
|
|
|
//
|
|
|
|
// How does this work? Messages to Masternodes come in via NetMsgType::DSVIN, these require a valid collateral
|
|
|
|
// transaction for the client to be able to enter the pool. This transaction is kept by the Masternode
|
|
|
|
// until the transaction is either complete or fails.
|
|
|
|
//
|
|
|
|
void CPrivateSendServer::ChargeFees()
|
|
|
|
{
|
|
|
|
if(!fMasterNode) return;
|
|
|
|
|
|
|
|
//we don't need to charge collateral for every offence.
|
|
|
|
if(GetRandInt(100) > 33) return;
|
|
|
|
|
|
|
|
std::vector<CTransaction> vecOffendersCollaterals;
|
|
|
|
|
|
|
|
if(nState == POOL_STATE_ACCEPTING_ENTRIES) {
|
|
|
|
BOOST_FOREACH(const CTransaction& txCollateral, vecSessionCollaterals) {
|
|
|
|
bool fFound = false;
|
|
|
|
BOOST_FOREACH(const CDarkSendEntry& entry, vecEntries)
|
|
|
|
if(entry.txCollateral == txCollateral)
|
|
|
|
fFound = true;
|
|
|
|
|
|
|
|
// This queue entry didn't send us the promised transaction
|
|
|
|
if(!fFound) {
|
|
|
|
LogPrintf("CPrivateSendServer::ChargeFees -- found uncooperative node (didn't send transaction), found offence\n");
|
|
|
|
vecOffendersCollaterals.push_back(txCollateral);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nState == POOL_STATE_SIGNING) {
|
|
|
|
// who didn't sign?
|
|
|
|
BOOST_FOREACH(const CDarkSendEntry entry, vecEntries) {
|
|
|
|
BOOST_FOREACH(const CTxDSIn txdsin, entry.vecTxDSIn) {
|
|
|
|
if(!txdsin.fHasSig) {
|
|
|
|
LogPrintf("CPrivateSendServer::ChargeFees -- found uncooperative node (didn't sign), found offence\n");
|
|
|
|
vecOffendersCollaterals.push_back(entry.txCollateral);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no offences found
|
|
|
|
if(vecOffendersCollaterals.empty()) return;
|
|
|
|
|
|
|
|
//mostly offending? Charge sometimes
|
|
|
|
if((int)vecOffendersCollaterals.size() >= Params().PoolMaxTransactions() - 1 && GetRandInt(100) > 33) return;
|
|
|
|
|
|
|
|
//everyone is an offender? That's not right
|
|
|
|
if((int)vecOffendersCollaterals.size() >= Params().PoolMaxTransactions()) return;
|
|
|
|
|
|
|
|
//charge one of the offenders randomly
|
|
|
|
std::random_shuffle(vecOffendersCollaterals.begin(), vecOffendersCollaterals.end());
|
|
|
|
|
|
|
|
if(nState == POOL_STATE_ACCEPTING_ENTRIES || nState == POOL_STATE_SIGNING) {
|
|
|
|
LogPrintf("CPrivateSendServer::ChargeFees -- found uncooperative node (didn't %s transaction), charging fees: %s\n",
|
|
|
|
(nState == POOL_STATE_SIGNING) ? "sign" : "send", vecOffendersCollaterals[0].ToString());
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
|
|
|
|
|
|
|
CValidationState state;
|
|
|
|
bool fMissingInputs;
|
|
|
|
if(!AcceptToMemoryPool(mempool, state, vecOffendersCollaterals[0], false, &fMissingInputs, false, true)) {
|
|
|
|
// should never really happen
|
|
|
|
LogPrintf("CPrivateSendServer::ChargeFees -- ERROR: AcceptToMemoryPool failed!\n");
|
|
|
|
} else {
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
g_connman->RelayTransaction(vecOffendersCollaterals[0]);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Charge the collateral randomly.
|
|
|
|
Mixing is completely free, to pay miners we randomly pay the collateral of users.
|
|
|
|
|
|
|
|
Collateral Fee Charges:
|
|
|
|
|
|
|
|
Being that mixing has "no fees" we need to have some kind of cost associated
|
|
|
|
with using it to stop abuse. Otherwise it could serve as an attack vector and
|
|
|
|
allow endless transaction that would bloat Dash and make it unusable. To
|
|
|
|
stop these kinds of attacks 1 in 10 successful transactions are charged. This
|
|
|
|
adds up to a cost of 0.001DRK per transaction on average.
|
|
|
|
*/
|
|
|
|
void CPrivateSendServer::ChargeRandomFees()
|
|
|
|
{
|
|
|
|
if(!fMasterNode) return;
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CTransaction& txCollateral, vecSessionCollaterals) {
|
|
|
|
|
|
|
|
if(GetRandInt(100) > 10) return;
|
|
|
|
|
|
|
|
LogPrintf("CPrivateSendServer::ChargeRandomFees -- charging random fees, txCollateral=%s", txCollateral.ToString());
|
|
|
|
|
|
|
|
CValidationState state;
|
|
|
|
bool fMissingInputs;
|
|
|
|
if(!AcceptToMemoryPool(mempool, state, txCollateral, false, &fMissingInputs, false, true)) {
|
|
|
|
// should never really happen
|
|
|
|
LogPrintf("CPrivateSendServer::ChargeRandomFees -- ERROR: AcceptToMemoryPool failed!\n");
|
|
|
|
} else {
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
g_connman->RelayTransaction(txCollateral);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check for various timeouts (queue objects, mixing, etc)
|
|
|
|
//
|
|
|
|
void CPrivateSendServer::CheckTimeout()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRY_LOCK(cs_darksend, lockDS);
|
|
|
|
if(!lockDS) return; // it's ok to fail here, we run this quite frequently
|
|
|
|
|
|
|
|
// check mixing queue objects for timeouts
|
|
|
|
std::vector<CDarksendQueue>::iterator it = vecDarksendQueue.begin();
|
|
|
|
while(it != vecDarksendQueue.end()) {
|
|
|
|
if((*it).IsExpired()) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckTimeout -- Removing expired queue (%s)\n", (*it).ToString());
|
|
|
|
it = vecDarksendQueue.erase(it);
|
|
|
|
} else ++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!fMasterNode) return;
|
|
|
|
|
|
|
|
int nLagTime = fMasterNode ? 0 : 10000; // if we're the client, give the server a few extra seconds before resetting.
|
|
|
|
int nTimeout = (nState == POOL_STATE_SIGNING) ? PRIVATESEND_SIGNING_TIMEOUT : PRIVATESEND_QUEUE_TIMEOUT;
|
|
|
|
bool fTimeout = GetTimeMillis() - nTimeLastSuccessfulStep >= nTimeout*1000 + nLagTime;
|
|
|
|
|
|
|
|
if(nState != POOL_STATE_IDLE && fTimeout) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckTimeout -- %s timed out (%ds) -- restting\n",
|
|
|
|
(nState == POOL_STATE_SIGNING) ? "Signing" : "Session", nTimeout);
|
|
|
|
ChargeFees();
|
|
|
|
SetNull();
|
|
|
|
SetState(POOL_STATE_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check to see if we're ready for submissions from clients
|
|
|
|
After receiving multiple dsa messages, the queue will switch to "accepting entries"
|
|
|
|
which is the active state right before merging the transaction
|
|
|
|
*/
|
|
|
|
void CPrivateSendServer::CheckForCompleteQueue()
|
|
|
|
{
|
|
|
|
if(!fMasterNode) return;
|
|
|
|
|
|
|
|
if(nState == POOL_STATE_QUEUE && IsSessionReady()) {
|
|
|
|
SetState(POOL_STATE_ACCEPTING_ENTRIES);
|
|
|
|
|
2017-08-29 01:51:44 +02:00
|
|
|
CDarksendQueue dsq(nSessionDenom, activeMasternode.vin, GetAdjustedTime(), true);
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckForCompleteQueue -- queue is ready, signing and relaying (%s)\n", dsq.ToString());
|
|
|
|
dsq.Sign();
|
|
|
|
dsq.Relay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure a given input matches an input in the pool and its scriptSig is valid
|
|
|
|
bool CPrivateSendServer::IsInputScriptSigValid(const CTxIn& txin)
|
|
|
|
{
|
|
|
|
CMutableTransaction txNew;
|
|
|
|
txNew.vin.clear();
|
|
|
|
txNew.vout.clear();
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
int nTxInIndex = -1;
|
|
|
|
CScript sigPubKey = CScript();
|
|
|
|
|
|
|
|
BOOST_FOREACH(CDarkSendEntry& entry, vecEntries) {
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxDSOut& txdsout, entry.vecTxDSOut)
|
|
|
|
txNew.vout.push_back(txdsout);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxDSIn& txdsin, entry.vecTxDSIn) {
|
|
|
|
txNew.vin.push_back(txdsin);
|
|
|
|
|
|
|
|
if(txdsin.prevout == txin.prevout) {
|
|
|
|
nTxInIndex = i;
|
|
|
|
sigPubKey = txdsin.prevPubKey;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nTxInIndex >= 0) { //might have to do this one input at a time?
|
|
|
|
txNew.vin[nTxInIndex].scriptSig = txin.scriptSig;
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::IsInputScriptSigValid -- verifying scriptSig %s\n", ScriptToAsmStr(txin.scriptSig).substr(0,24));
|
|
|
|
if(!VerifyScript(txNew.vin[nTxInIndex].scriptSig, sigPubKey, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, MutableTransactionSignatureChecker(&txNew, nTxInIndex))) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::IsInputScriptSigValid -- VerifyScript() failed on input %d\n", nTxInIndex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::IsInputScriptSigValid -- Failed to find matching input in pool, %s\n", txin.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::IsInputScriptSigValid -- Successfully validated input and scriptSig\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Add a clients transaction to the pool
|
|
|
|
//
|
|
|
|
bool CPrivateSendServer::AddEntry(const CDarkSendEntry& entryNew, PoolMessage& nMessageIDRet)
|
|
|
|
{
|
|
|
|
if(!fMasterNode) return false;
|
|
|
|
|
|
|
|
BOOST_FOREACH(CTxIn txin, entryNew.vecTxDSIn) {
|
|
|
|
if(txin.prevout.IsNull()) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddEntry -- input not valid!\n");
|
|
|
|
nMessageIDRet = ERR_INVALID_INPUT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:30:16 +02:00
|
|
|
if(!CPrivateSend::IsCollateralValid(entryNew.txCollateral)) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddEntry -- collateral not valid!\n");
|
|
|
|
nMessageIDRet = ERR_INVALID_COLLATERAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:30:16 +02:00
|
|
|
if(GetEntriesCount() >= CPrivateSend::GetMaxPoolTransactions()) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddEntry -- entries is full!\n");
|
|
|
|
nMessageIDRet = ERR_ENTRIES_FULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(CTxIn txin, entryNew.vecTxDSIn) {
|
|
|
|
LogPrint("privatesend", "looking for txin -- %s\n", txin.ToString());
|
|
|
|
BOOST_FOREACH(const CDarkSendEntry& entry, vecEntries) {
|
|
|
|
BOOST_FOREACH(const CTxDSIn& txdsin, entry.vecTxDSIn) {
|
|
|
|
if(txdsin.prevout == txin.prevout) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddEntry -- found in txin\n");
|
|
|
|
nMessageIDRet = ERR_ALREADY_HAVE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vecEntries.push_back(entryNew);
|
|
|
|
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddEntry -- adding entry\n");
|
|
|
|
nMessageIDRet = MSG_ENTRIES_ADDED;
|
|
|
|
nTimeLastSuccessfulStep = GetTimeMillis();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPrivateSendServer::AddScriptSig(const CTxIn& txinNew)
|
|
|
|
{
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddScriptSig -- scriptSig=%s\n", ScriptToAsmStr(txinNew.scriptSig).substr(0,24));
|
|
|
|
|
|
|
|
BOOST_FOREACH(const CDarkSendEntry& entry, vecEntries) {
|
|
|
|
BOOST_FOREACH(const CTxDSIn& txdsin, entry.vecTxDSIn) {
|
|
|
|
if(txdsin.scriptSig == txinNew.scriptSig) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddScriptSig -- already exists\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!IsInputScriptSigValid(txinNew)) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddScriptSig -- Invalid scriptSig\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddScriptSig -- scriptSig=%s new\n", ScriptToAsmStr(txinNew.scriptSig).substr(0,24));
|
|
|
|
|
|
|
|
BOOST_FOREACH(CTxIn& txin, finalMutableTransaction.vin) {
|
|
|
|
if(txinNew.prevout == txin.prevout && txin.nSequence == txinNew.nSequence) {
|
|
|
|
txin.scriptSig = txinNew.scriptSig;
|
|
|
|
txin.prevPubKey = txinNew.prevPubKey;
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddScriptSig -- adding to finalMutableTransaction, scriptSig=%s\n", ScriptToAsmStr(txinNew.scriptSig).substr(0,24));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(int i = 0; i < GetEntriesCount(); i++) {
|
|
|
|
if(vecEntries[i].AddScriptSig(txinNew)) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddScriptSig -- adding to entries, scriptSig=%s\n", ScriptToAsmStr(txinNew.scriptSig).substr(0,24));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrintf("CPrivateSendServer::AddScriptSig -- Couldn't set sig!\n" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure everything is signed
|
|
|
|
bool CPrivateSendServer::IsSignaturesComplete()
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const CDarkSendEntry& entry, vecEntries)
|
|
|
|
BOOST_FOREACH(const CTxDSIn& txdsin, entry.vecTxDSIn)
|
|
|
|
if(!txdsin.fHasSig) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPrivateSendServer::IsOutputsCompatibleWithSessionDenom(const std::vector<CTxDSOut>& vecTxDSOut)
|
|
|
|
{
|
2017-06-30 20:30:16 +02:00
|
|
|
if(CPrivateSend::GetDenominations(vecTxDSOut) == 0) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(const CDarkSendEntry entry, vecEntries) {
|
2017-06-30 20:30:16 +02:00
|
|
|
LogPrintf("CPrivateSendServer::IsOutputsCompatibleWithSessionDenom -- vecTxDSOut denom %d, entry.vecTxDSOut denom %d\n",
|
|
|
|
CPrivateSend::GetDenominations(vecTxDSOut), CPrivateSend::GetDenominations(entry.vecTxDSOut));
|
|
|
|
if(CPrivateSend::GetDenominations(vecTxDSOut) != CPrivateSend::GetDenominations(entry.vecTxDSOut)) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPrivateSendServer::IsAcceptableDenomAndCollateral(int nDenom, CTransaction txCollateral, PoolMessage& nMessageIDRet)
|
|
|
|
{
|
|
|
|
if(!fMasterNode) return false;
|
|
|
|
|
|
|
|
// is denom even smth legit?
|
|
|
|
std::vector<int> vecBits;
|
2017-06-30 20:30:16 +02:00
|
|
|
if(!CPrivateSend::GetDenominationsBits(nDenom, vecBits)) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::IsAcceptableDenomAndCollateral -- denom not valid!\n");
|
|
|
|
nMessageIDRet = ERR_DENOM;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check collateral
|
2017-06-30 20:30:16 +02:00
|
|
|
if(!fUnitTest && !CPrivateSend::IsCollateralValid(txCollateral)) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::IsAcceptableDenomAndCollateral -- collateral not valid!\n");
|
|
|
|
nMessageIDRet = ERR_INVALID_COLLATERAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPrivateSendServer::CreateNewSession(int nDenom, CTransaction txCollateral, PoolMessage& nMessageIDRet)
|
|
|
|
{
|
|
|
|
if(!fMasterNode || nSessionID != 0) return false;
|
|
|
|
|
|
|
|
// new session can only be started in idle mode
|
|
|
|
if(nState != POOL_STATE_IDLE) {
|
|
|
|
nMessageIDRet = ERR_MODE;
|
|
|
|
LogPrintf("CPrivateSendServer::CreateNewSession -- incompatible mode: nState=%d\n", nState);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!IsAcceptableDenomAndCollateral(nDenom, txCollateral, nMessageIDRet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start new session
|
|
|
|
nMessageIDRet = MSG_NOERR;
|
|
|
|
nSessionID = GetRandInt(999999)+1;
|
|
|
|
nSessionDenom = nDenom;
|
|
|
|
|
|
|
|
SetState(POOL_STATE_QUEUE);
|
|
|
|
nTimeLastSuccessfulStep = GetTimeMillis();
|
|
|
|
|
|
|
|
if(!fUnitTest) {
|
|
|
|
//broadcast that I'm accepting entries, only if it's the first entry through
|
2017-08-29 01:51:44 +02:00
|
|
|
CDarksendQueue dsq(nDenom, activeMasternode.vin, GetAdjustedTime(), false);
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CreateNewSession -- signing and relaying new queue: %s\n", dsq.ToString());
|
|
|
|
dsq.Sign();
|
|
|
|
dsq.Relay();
|
|
|
|
vecDarksendQueue.push_back(dsq);
|
|
|
|
}
|
|
|
|
|
|
|
|
vecSessionCollaterals.push_back(txCollateral);
|
|
|
|
LogPrintf("CPrivateSendServer::CreateNewSession -- new session created, nSessionID: %d nSessionDenom: %d (%s) vecSessionCollaterals.size(): %d\n",
|
2017-06-30 20:30:16 +02:00
|
|
|
nSessionID, nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom), vecSessionCollaterals.size());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPrivateSendServer::AddUserToExistingSession(int nDenom, CTransaction txCollateral, PoolMessage& nMessageIDRet)
|
|
|
|
{
|
|
|
|
if(!fMasterNode || nSessionID == 0 || IsSessionReady()) return false;
|
|
|
|
|
|
|
|
if(!IsAcceptableDenomAndCollateral(nDenom, txCollateral, nMessageIDRet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only add new users to an existing session when we are in queue mode
|
|
|
|
if(nState != POOL_STATE_QUEUE) {
|
|
|
|
nMessageIDRet = ERR_MODE;
|
|
|
|
LogPrintf("CPrivateSendServer::AddUserToExistingSession -- incompatible mode: nState=%d\n", nState);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nDenom != nSessionDenom) {
|
|
|
|
LogPrintf("CPrivateSendServer::AddUserToExistingSession -- incompatible denom %d (%s) != nSessionDenom %d (%s)\n",
|
2017-06-30 20:30:16 +02:00
|
|
|
nDenom, CPrivateSend::GetDenominationsToString(nDenom), nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom));
|
2017-05-05 13:26:27 +02:00
|
|
|
nMessageIDRet = ERR_DENOM;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// count new user as accepted to an existing session
|
|
|
|
|
|
|
|
nMessageIDRet = MSG_NOERR;
|
|
|
|
nTimeLastSuccessfulStep = GetTimeMillis();
|
|
|
|
vecSessionCollaterals.push_back(txCollateral);
|
|
|
|
|
|
|
|
LogPrintf("CPrivateSendServer::AddUserToExistingSession -- new user accepted, nSessionID: %d nSessionDenom: %d (%s) vecSessionCollaterals.size(): %d\n",
|
2017-06-30 20:30:16 +02:00
|
|
|
nSessionID, nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom), vecSessionCollaterals.size());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::RelayFinalTransaction(const CTransaction& txFinal)
|
|
|
|
{
|
2017-07-25 12:57:26 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::%s -- nSessionID: %d nSessionDenom: %d (%s)\n",
|
|
|
|
__func__, nSessionID, nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom));
|
|
|
|
|
|
|
|
// final mixing tx with empty signatures should be relayed to mixing participants only
|
|
|
|
for (const auto entry : vecEntries) {
|
|
|
|
bool fOk = g_connman->ForNode(entry.addr, [&txFinal, this](CNode* pnode) {
|
2017-07-27 16:28:05 +02:00
|
|
|
g_connman->PushMessage(pnode, NetMsgType::DSFINALTX, nSessionID, txFinal);
|
2017-07-25 12:57:26 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if(!fOk) {
|
|
|
|
// no such node? maybe this client disconnected or our own connection went down
|
|
|
|
RelayStatus(STATUS_REJECTED);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::PushStatus(CNode* pnode, PoolStatusUpdate nStatusUpdate, PoolMessage nMessageID)
|
|
|
|
{
|
|
|
|
if(!pnode) return;
|
2017-07-27 16:28:05 +02:00
|
|
|
g_connman->PushMessage(pnode, NetMsgType::DSSTATUSUPDATE, nSessionID, (int)nState, (int)vecEntries.size(), (int)nStatusUpdate, (int)nMessageID);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::RelayStatus(PoolStatusUpdate nStatusUpdate, PoolMessage nMessageID)
|
|
|
|
{
|
2017-07-25 12:57:26 +02:00
|
|
|
unsigned int nDisconnected{};
|
|
|
|
// status updates should be relayed to mixing participants only
|
|
|
|
for (const auto entry : vecEntries) {
|
|
|
|
// make sure everyone is still connected
|
|
|
|
bool fOk = g_connman->ForNode(entry.addr, [&nStatusUpdate, &nMessageID, this](CNode* pnode) {
|
2017-05-05 13:26:27 +02:00
|
|
|
PushStatus(pnode, nStatusUpdate, nMessageID);
|
2017-07-25 12:57:26 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if(!fOk) {
|
|
|
|
// no such node? maybe this client disconnected or our own connection went down
|
|
|
|
++nDisconnected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nDisconnected == 0) return; // all is clear
|
|
|
|
|
|
|
|
// smth went wrong
|
|
|
|
LogPrintf("CPrivateSendServer::%s -- can't continue, %llu client(s) disconnected, nSessionID: %d nSessionDenom: %d (%s)\n",
|
|
|
|
__func__, nDisconnected, nSessionID, nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom));
|
|
|
|
|
|
|
|
// notify everyone else that this session should be terminated
|
|
|
|
for (const auto entry : vecEntries) {
|
|
|
|
g_connman->ForNode(entry.addr, [this](CNode* pnode) {
|
|
|
|
PushStatus(pnode, STATUS_REJECTED, MSG_NOERR);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nDisconnected == vecEntries.size()) {
|
|
|
|
// all clients disconnected, there is probably some issues with our own connection
|
|
|
|
// do not charge any fees, just reset the pool
|
|
|
|
SetNull();
|
|
|
|
}
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::RelayCompletedTransaction(PoolMessage nMessageID)
|
|
|
|
{
|
2017-07-25 12:57:26 +02:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::%s -- nSessionID: %d nSessionDenom: %d (%s)\n",
|
|
|
|
__func__, nSessionID, nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom));
|
|
|
|
|
|
|
|
// final mixing tx with empty signatures should be relayed to mixing participants only
|
|
|
|
for (const auto entry : vecEntries) {
|
|
|
|
bool fOk = g_connman->ForNode(entry.addr, [&nMessageID, this](CNode* pnode) {
|
2017-07-27 16:28:05 +02:00
|
|
|
g_connman->PushMessage(pnode, NetMsgType::DSCOMPLETE, nSessionID, (int)nMessageID);
|
2017-07-25 12:57:26 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if(!fOk) {
|
|
|
|
// no such node? maybe client disconnected or our own connection went down
|
|
|
|
RelayStatus(STATUS_REJECTED);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::SetState(PoolState nStateNew)
|
|
|
|
{
|
|
|
|
if(fMasterNode && (nStateNew == POOL_STATE_ERROR || nStateNew == POOL_STATE_SUCCESS)) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::SetState -- Can't set state to ERROR or SUCCESS as a Masternode. \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrintf("CPrivateSendServer::SetState -- nState: %d, nStateNew: %d\n", nState, nStateNew);
|
|
|
|
nState = nStateNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Rename/move to core
|
|
|
|
void ThreadCheckPrivateSendServer()
|
|
|
|
{
|
|
|
|
if(fLiteMode) return; // disable all Dash specific functionality
|
|
|
|
|
|
|
|
static bool fOneThread;
|
|
|
|
if(fOneThread) return;
|
|
|
|
fOneThread = true;
|
|
|
|
|
|
|
|
// Make this thread recognisable as the PrivateSend thread
|
|
|
|
RenameThread("dash-ps-server");
|
|
|
|
|
|
|
|
unsigned int nTick = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
MilliSleep(1000);
|
|
|
|
|
|
|
|
if(masternodeSync.IsBlockchainSynced() && !ShutdownRequested()) {
|
|
|
|
nTick++;
|
|
|
|
privateSendServer.CheckTimeout();
|
|
|
|
privateSendServer.CheckForCompleteQueue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|