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"
|
2016-11-25 20:01:56 +01:00
|
|
|
#include "netmessagemaker.h"
|
2017-12-01 19:53:34 +01:00
|
|
|
#include "script/interpreter.h"
|
2017-05-05 13:26:27 +02:00
|
|
|
#include "txmempool.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "utilmoneystr.h"
|
|
|
|
|
|
|
|
CPrivateSendServer privateSendServer;
|
|
|
|
|
2017-02-06 14:31:37 +01:00
|
|
|
void CPrivateSendServer::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
if(fLiteMode) return; // ignore all Dash related functionality
|
|
|
|
if(!masternodeSync.IsBlockchainSynced()) return;
|
|
|
|
|
|
|
|
if(strCommand == NetMsgType::DSACCEPT) {
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
2018-03-15 10:21:43 +01:00
|
|
|
LogPrint("privatesend", "DSACCEPT -- peer=%d using obsolete version %i\n", pfrom->id, pfrom->nVersion);
|
|
|
|
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
|
|
|
|
strprintf("Version must be %d or greater", MIN_PRIVATESEND_PEER_PROTO_VERSION)));
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_VERSION, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(IsSessionReady()) {
|
|
|
|
// too many users in this session already, reject new ones
|
|
|
|
LogPrintf("DSACCEPT -- queue is already full!\n");
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_ACCEPTED, ERR_QUEUE_FULL, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
CDarksendAccept dsa;
|
|
|
|
vRecv >> dsa;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
LogPrint("privatesend", "DSACCEPT -- nDenom %d (%s) txCollateral %s", dsa.nDenom, CPrivateSend::GetDenominationsToString(dsa.nDenom), dsa.txCollateral.ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-05-26 20:03:23 +02:00
|
|
|
if(dsa.nInputCount < 0 || dsa.nInputCount > PRIVATESEND_ENTRY_MAX_SIZE) return;
|
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
masternode_info_t mnInfo;
|
|
|
|
if(!mnodeman.GetMasternodeInfo(activeMasternode.outpoint, mnInfo)) {
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MN_LIST, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
if(vecSessionCollaterals.size() == 0 && mnInfo.nLastDsq != 0 &&
|
|
|
|
mnInfo.nLastDsq + mnodeman.CountEnabled(MIN_PRIVATESEND_PEER_PROTO_VERSION)/5 > mnodeman.nDsqCount)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
|
|
|
LogPrintf("DSACCEPT -- last dsq too recent, must wait: addr=%s\n", pfrom->addr.ToString());
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_RECENT, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolMessage nMessageID = MSG_NOERR;
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
bool fResult = nSessionID == 0 ? CreateNewSession(dsa, nMessageID, connman)
|
|
|
|
: AddUserToExistingSession(dsa, nMessageID);
|
2017-05-05 13:26:27 +02:00
|
|
|
if(fResult) {
|
|
|
|
LogPrintf("DSACCEPT -- is compatible, please submit!\n");
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_ACCEPTED, nMessageID, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
LogPrintf("DSACCEPT -- not compatible with existing transactions!\n");
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, nMessageID, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(strCommand == NetMsgType::DSQUEUE) {
|
|
|
|
TRY_LOCK(cs_darksend, lockRecv);
|
|
|
|
if(!lockRecv) return;
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
2018-03-15 10:21:43 +01:00
|
|
|
LogPrint("privatesend", "DSQUEUE -- peer=%d using obsolete version %i\n", pfrom->id, pfrom->nVersion);
|
|
|
|
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
|
|
|
|
strprintf("Version must be %d or greater", MIN_PRIVATESEND_PEER_PROTO_VERSION)));
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDarksendQueue dsq;
|
|
|
|
vRecv >> dsq;
|
|
|
|
|
|
|
|
// process every dsq only once
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& q : vecDarksendQueue) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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;
|
2018-05-26 20:03:23 +02:00
|
|
|
if(dsq.nInputCount < 0 || dsq.nInputCount > PRIVATESEND_ENTRY_MAX_SIZE) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
masternode_info_t mnInfo;
|
2018-02-15 08:29:44 +01:00
|
|
|
if(!mnodeman.GetMasternodeInfo(dsq.masternodeOutpoint, mnInfo)) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
if(!dsq.CheckSignature(mnInfo.pubKeyMasternode)) {
|
2017-05-05 13:26:27 +02:00
|
|
|
// we probably have outdated info
|
2018-02-15 08:29:44 +01:00
|
|
|
mnodeman.AskForMN(pfrom, dsq.masternodeOutpoint, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!dsq.fReady) {
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& q : vecDarksendQueue) {
|
2018-02-15 08:29:44 +01:00
|
|
|
if(q.masternodeOutpoint == dsq.masternodeOutpoint) {
|
2017-05-05 13:26:27 +02:00
|
|
|
// no way same mn can send another "not yet ready" dsq this soon
|
2017-09-11 16:13:48 +02:00
|
|
|
LogPrint("privatesend", "DSQUEUE -- Masternode %s is sending WAY too many dsq messages\n", mnInfo.addr.ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
int nThreshold = mnInfo.nLastDsq + mnodeman.CountEnabled(MIN_PRIVATESEND_PEER_PROTO_VERSION)/5;
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- nLastDsq: %d threshold: %d nDsqCount: %d\n", mnInfo.nLastDsq, nThreshold, mnodeman.nDsqCount);
|
2017-05-05 13:26:27 +02:00
|
|
|
//don't allow a few nodes to dominate the queuing process
|
2017-09-11 16:13:48 +02:00
|
|
|
if(mnInfo.nLastDsq != 0 && nThreshold > mnodeman.nDsqCount) {
|
|
|
|
LogPrint("privatesend", "DSQUEUE -- Masternode %s is sending too many dsq messages\n", mnInfo.addr.ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-02-15 08:29:44 +01:00
|
|
|
mnodeman.AllowMixing(dsq.masternodeOutpoint);
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
LogPrint("privatesend", "DSQUEUE -- new PrivateSend queue (%s) from masternode %s\n", dsq.ToString(), mnInfo.addr.ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
vecDarksendQueue.push_back(dsq);
|
2017-09-19 16:51:38 +02:00
|
|
|
dsq.Relay(connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if(strCommand == NetMsgType::DSVIN) {
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
2018-03-15 10:21:43 +01:00
|
|
|
LogPrint("privatesend", "DSVIN -- peer=%d using obsolete version %i\n", pfrom->id, pfrom->nVersion);
|
|
|
|
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
|
|
|
|
strprintf("Version must be %d or greater", MIN_PRIVATESEND_PEER_PROTO_VERSION)));
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_VERSION, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//do we have enough users in the current session?
|
|
|
|
if(!IsSessionReady()) {
|
|
|
|
LogPrintf("DSVIN -- session not complete!\n");
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_SESSION, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDarkSendEntry entry;
|
|
|
|
vRecv >> entry;
|
|
|
|
|
2017-09-20 14:02:53 +02:00
|
|
|
LogPrint("privatesend", "DSVIN -- txCollateral %s", entry.txCollateral->ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
if(entry.vecTxDSIn.size() > PRIVATESEND_ENTRY_MAX_SIZE) {
|
|
|
|
LogPrintf("DSVIN -- ERROR: too many inputs! %d/%d\n", entry.vecTxDSIn.size(), PRIVATESEND_ENTRY_MAX_SIZE);
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MAXIMUM, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-04 07:06:07 +01:00
|
|
|
if(entry.vecTxOut.size() > PRIVATESEND_ENTRY_MAX_SIZE) {
|
|
|
|
LogPrintf("DSVIN -- ERROR: too many outputs! %d/%d\n", entry.vecTxOut.size(), PRIVATESEND_ENTRY_MAX_SIZE);
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MAXIMUM, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-26 20:03:23 +02:00
|
|
|
if(nSessionInputCount != 0 && entry.vecTxDSIn.size() != nSessionInputCount) {
|
|
|
|
LogPrintf("DSVIN -- ERROR: incorrect number of inputs! %d/%d\n", entry.vecTxDSIn.size(), nSessionInputCount);
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_INVALID_INPUT_COUNT, connman);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nSessionInputCount != 0 && entry.vecTxOut.size() != nSessionInputCount) {
|
|
|
|
LogPrintf("DSVIN -- ERROR: incorrect number of outputs! %d/%d\n", entry.vecTxOut.size(), nSessionInputCount);
|
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_INVALID_INPUT_COUNT, connman);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:26:27 +02:00
|
|
|
//do we have the same denominations as the current session?
|
2017-12-04 07:06:07 +01:00
|
|
|
if(!IsOutputsCompatibleWithSessionDenom(entry.vecTxOut)) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrintf("DSVIN -- not compatible with existing transactions!\n");
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_EXISTING_TX, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check it like a transaction
|
|
|
|
{
|
|
|
|
CAmount nValueIn = 0;
|
|
|
|
CAmount nValueOut = 0;
|
|
|
|
|
|
|
|
CMutableTransaction tx;
|
|
|
|
|
2017-12-04 07:06:07 +01:00
|
|
|
for (const auto& txout : entry.vecTxOut) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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));
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_NON_STANDARD_PUBKEY, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-07 02:33:51 +01:00
|
|
|
if(!txout.scriptPubKey.IsPayToPublicKeyHash()) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrintf("DSVIN -- invalid script! scriptPubKey=%s\n", ScriptToAsmStr(txout.scriptPubKey));
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_INVALID_SCRIPT, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txin : entry.vecTxDSIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
tx.vin.push_back(txin);
|
|
|
|
|
|
|
|
LogPrint("privatesend", "DSVIN -- txin=%s\n", txin.ToString());
|
|
|
|
|
2017-09-26 16:33:46 +02:00
|
|
|
Coin coin;
|
|
|
|
if(GetUTXOCoin(txin.prevout, coin)) {
|
|
|
|
nValueIn += coin.out.nValue;
|
2017-05-05 13:26:27 +02:00
|
|
|
} else {
|
2018-02-26 12:10:20 +01:00
|
|
|
LogPrintf("DSVIN -- missing input! txin=%s\n", txin.ToString());
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_MISSING_TX, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
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());
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, ERR_FEES, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
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)) {
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_ACCEPTED, nMessageID, connman);
|
|
|
|
CheckPool(connman);
|
|
|
|
RelayStatus(STATUS_ACCEPTED, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
} else {
|
2017-09-19 16:51:38 +02:00
|
|
|
PushStatus(pfrom, STATUS_REJECTED, nMessageID, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(strCommand == NetMsgType::DSSIGNFINALTX) {
|
|
|
|
|
|
|
|
if(pfrom->nVersion < MIN_PRIVATESEND_PEER_PROTO_VERSION) {
|
2018-03-15 10:21:43 +01:00
|
|
|
LogPrint("privatesend", "DSSIGNFINALTX -- peer=%d using obsolete version %i\n", pfrom->id, pfrom->nVersion);
|
|
|
|
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
|
|
|
|
strprintf("Version must be %d or greater", MIN_PRIVATESEND_PEER_PROTO_VERSION)));
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CTxIn> vecTxIn;
|
|
|
|
vRecv >> vecTxIn;
|
|
|
|
|
|
|
|
LogPrint("privatesend", "DSSIGNFINALTX -- vecTxIn.size() %s\n", vecTxIn.size());
|
|
|
|
|
|
|
|
int nTxInIndex = 0;
|
|
|
|
int nTxInsCount = (int)vecTxIn.size();
|
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txin : vecTxIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
nTxInIndex++;
|
|
|
|
if(!AddScriptSig(txin)) {
|
|
|
|
LogPrint("privatesend", "DSSIGNFINALTX -- AddScriptSig() failed at %d/%d, session: %d\n", nTxInIndex, nTxInsCount, nSessionID);
|
2017-09-19 16:51:38 +02:00
|
|
|
RelayStatus(STATUS_REJECTED, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LogPrint("privatesend", "DSSIGNFINALTX -- AddScriptSig() %d/%d success\n", nTxInIndex, nTxInsCount);
|
|
|
|
}
|
|
|
|
// all is good
|
2017-09-19 16:51:38 +02:00
|
|
|
CheckPool(connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
//
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::CheckPool(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-03-21 12:10:01 +01:00
|
|
|
if (!fMasternodeMode) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-03-21 12:10:01 +01:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckPool -- entries count %lu\n", GetEntriesCount());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-03-21 12:10:01 +01:00
|
|
|
// If entries are full, create finalized transaction
|
|
|
|
if (nState == POOL_STATE_ACCEPTING_ENTRIES && GetEntriesCount() >= CPrivateSend::GetMaxPoolTransactions()) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckPool -- FINALIZE TRANSACTIONS\n");
|
|
|
|
CreateFinalTransaction(connman);
|
|
|
|
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(connman);
|
|
|
|
return;
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::CreateFinalTransaction(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CreateFinalTransaction -- FINALIZE TRANSACTIONS\n");
|
|
|
|
|
|
|
|
CMutableTransaction txNew;
|
|
|
|
|
|
|
|
// make our new transaction
|
|
|
|
for(int i = 0; i < GetEntriesCount(); i++) {
|
2017-12-04 07:06:07 +01:00
|
|
|
for (const auto& txout : vecEntries[i].vecTxOut)
|
|
|
|
txNew.vout.push_back(txout);
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txdsin : vecEntries[i].vecTxDSIn)
|
2017-05-05 13:26:27 +02:00
|
|
|
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
|
2017-09-19 16:51:38 +02:00
|
|
|
RelayFinalTransaction(finalMutableTransaction, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
SetState(POOL_STATE_SIGNING);
|
|
|
|
}
|
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::CommitFinalTransaction(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return; // check and relay final tx only on masternode
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2017-01-04 12:22:49 +01:00
|
|
|
CTransactionRef finalTransaction = MakeTransactionRef(finalMutableTransaction);
|
|
|
|
uint256 hashTx = finalTransaction->GetHash();
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2017-01-04 12:22:49 +01:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CommitFinalTransaction -- finalTransaction=%s", finalTransaction->ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
// See if the transaction is valid
|
|
|
|
TRY_LOCK(cs_main, lockMain);
|
|
|
|
CValidationState validationState;
|
|
|
|
mempool.PrioritiseTransaction(hashTx, hashTx.ToString(), 1000, 0.1*COIN);
|
Backport compact blocks functionality from bitcoin (#1966)
* Merge #8068: Compact Blocks
48efec8 Fix some minor compact block issues that came up in review (Matt Corallo)
ccd06b9 Elaborate bucket size math (Pieter Wuille)
0d4cb48 Use vTxHashes to optimize InitData significantly (Matt Corallo)
8119026 Provide a flat list of txid/terators to txn in CTxMemPool (Matt Corallo)
678ee97 Add BIP 152 to implemented BIPs list (Matt Corallo)
56ba516 Add reconstruction debug logging (Matt Corallo)
2f34a2e Get our "best three" peers to announce blocks using cmpctblocks (Matt Corallo)
927f8ee Add ability to fetch CNode by NodeId (Matt Corallo)
d25cd3e Add receiver-side protocol implementation for CMPCTBLOCK stuff (Matt Corallo)
9c837d5 Add sender-side protocol implementation for CMPCTBLOCK stuff (Matt Corallo)
00c4078 Add protocol messages for short-ids blocks (Matt Corallo)
e3b2222 Add some blockencodings tests (Matt Corallo)
f4f8f14 Add TestMemPoolEntryHelper::FromTx version for CTransaction (Matt Corallo)
85ad31e Add partial-block block encodings API (Matt Corallo)
5249dac Add COMPACTSIZE wrapper similar to VARINT for serialization (Matt Corallo)
cbda71c Move context-required checks from CheckBlockHeader to Contextual... (Matt Corallo)
7c29ec9 If AcceptBlockHeader returns true, pindex will be set. (Matt Corallo)
96806c3 Stop trimming when mapTx is empty (Pieter Wuille)
* Merge #8408: Prevent fingerprinting, disk-DoS with compact blocks
1d06e49 Ignore CMPCTBLOCK messages for pruned blocks (Suhas Daftuar)
1de2a46 Ignore GETBLOCKTXN requests for unknown blocks (Suhas Daftuar)
* Merge #8418: Add tests for compact blocks
45c7ddd Add p2p test for BIP 152 (compact blocks) (Suhas Daftuar)
9a22a6c Add support for compactblocks to mininode (Suhas Daftuar)
a8689fd Tests: refactor compact size serialization in mininode (Suhas Daftuar)
9c8593d Implement SipHash in Python (Pieter Wuille)
56c87e9 Allow changing BIP9 parameters on regtest (Suhas Daftuar)
* Merge #8505: Trivial: Fix typos in various files
1aacfc2 various typos (leijurv)
* Merge #8449: [Trivial] Do not shadow local variable, cleanup
a159f25 Remove redundand (and shadowing) declaration (Pavel Janík)
cce3024 Do not shadow local variable, cleanup (Pavel Janík)
* Merge #8739: [qa] Fix broken sendcmpct test in p2p-compactblocks.py
157254a Fix broken sendcmpct test in p2p-compactblocks.py (Suhas Daftuar)
* Merge #8854: [qa] Fix race condition in p2p-compactblocks test
b5fd666 [qa] Fix race condition in p2p-compactblocks test (Suhas Daftuar)
* Merge #8393: Support for compact blocks together with segwit
27acfc1 [qa] Update p2p-compactblocks.py for compactblocks v2 (Suhas Daftuar)
422fac6 [qa] Add support for compactblocks v2 to mininode (Suhas Daftuar)
f5b9b8f [qa] Fix bug in mininode witness deserialization (Suhas Daftuar)
6aa28ab Use cmpctblock type 2 for segwit-enabled transfer (Pieter Wuille)
be7555f Fix overly-prescriptive p2p-segwit test for new fetch logic (Matt Corallo)
06128da Make GetFetchFlags always request witness objects from witness peers (Matt Corallo)
* Merge #8882: [qa] Fix race conditions in p2p-compactblocks.py and sendheaders.py
b55d941 [qa] Fix race condition in sendheaders.py (Suhas Daftuar)
6976db2 [qa] Another attempt to fix race condition in p2p-compactblocks.py (Suhas Daftuar)
* Merge #8904: [qa] Fix compact block shortids for a test case
4cdece4 [qa] Fix compact block shortids for a test case (Dagur Valberg Johannsson)
* Merge #8637: Compact Block Tweaks (rebase of #8235)
3ac6de0 Align constant names for maximum compact block / blocktxn depth (Pieter Wuille)
b2e93a3 Add cmpctblock to debug help list (instagibbs)
fe998e9 More agressively filter compact block requests (Matt Corallo)
02a337d Dont remove a "preferred" cmpctblock peer if they provide a block (Matt Corallo)
* Merge #8975: Chainparams: Trivial: In AppInit2(), s/Params()/chainparams/
6f2f639 Chainparams: Trivial: In AppInit2(), s/Params()/chainparams/ (Jorge Timón)
* Merge #8968: Don't hold cs_main when calling ProcessNewBlock from a cmpctblock
72ca7d9 Don't hold cs_main when calling ProcessNewBlock from a cmpctblock (Matt Corallo)
* Merge #8995: Add missing cs_main lock to ::GETBLOCKTXN processing
dfe7906 Add missing cs_main lock to ::GETBLOCKTXN processing (Matt Corallo)
* Merge #8515: A few mempool removal optimizations
0334430 Add some missing includes (Pieter Wuille)
4100499 Return shared_ptr<CTransaction> from mempool removes (Pieter Wuille)
51f2783 Make removed and conflicted arguments optional to remove (Pieter Wuille)
f48211b Bypass removeRecursive in removeForReorg (Pieter Wuille)
* Merge #9026: Fix handling of invalid compact blocks
d4833ff Bump the protocol version to distinguish new banning behavior. (Suhas Daftuar)
88c3549 Fix compact block handling to not ban if block is invalid (Suhas Daftuar)
c93beac [qa] Test that invalid compactblocks don't result in ban (Suhas Daftuar)
* Merge #9039: Various serialization simplifcations and optimizations
d59a518 Use fixed preallocation instead of costly GetSerializeSize (Pieter Wuille)
25a211a Add optimized CSizeComputer serializers (Pieter Wuille)
a2929a2 Make CSerAction's ForRead() constexpr (Pieter Wuille)
a603925 Avoid -Wshadow errors (Pieter Wuille)
5284721 Get rid of nType and nVersion (Pieter Wuille)
657e05a Make GetSerializeSize a wrapper on top of CSizeComputer (Pieter Wuille)
fad9b66 Make nType and nVersion private and sometimes const (Pieter Wuille)
c2c5d42 Make streams' read and write return void (Pieter Wuille)
50e8a9c Remove unused ReadVersion and WriteVersion (Pieter Wuille)
* Merge #9058: Fixes for p2p-compactblocks.py test timeouts on travis (#8842)
dac53b5 Modify getblocktxn handler not to drop requests for old blocks (Russell Yanofsky)
55bfddc [qa] Fix stale data bug in test_compactblocks_not_at_tip (Russell Yanofsky)
47e9659 [qa] Fix bug in compactblocks v2 merge (Russell Yanofsky)
* Merge #9160: [trivial] Fix hungarian variable name
ec34648 [trivial] Fix hungarian variable name (Russell Yanofsky)
* Merge #9159: [qa] Wait for specific block announcement in p2p-compactblocks
dfa44d1 [qa] Wait for specific block announcement in p2p-compactblocks (Russell Yanofsky)
* Merge #9125: Make CBlock a vector of shared_ptr of CTransactions
b4e4ba4 Introduce convenience type CTransactionRef (Pieter Wuille)
1662b43 Make CBlock::vtx a vector of shared_ptr<CTransaction> (Pieter Wuille)
da60506 Add deserializing constructors to CTransaction and CMutableTransaction (Pieter Wuille)
0e85204 Add serialization for unique_ptr and shared_ptr (Pieter Wuille)
* Merge #8872: Remove block-request logic from INV message processing
037159c Remove block-request logic from INV message processing (Matt Corallo)
3451203 [qa] Respond to getheaders and do not assume a getdata on inv (Matt Corallo)
d768f15 [qa] Make comptool push blocks instead of relying on inv-fetch (mrbandrews)
* Merge #9199: Always drop the least preferred HB peer when adding a new one.
ca8549d Always drop the least preferred HB peer when adding a new one. (Gregory Maxwell)
* Merge #9233: Fix some typos
15fa95d Fix some typos (fsb4000)
* Merge #9260: Mrs Peacock in The Library with The Candlestick (killed main.{h,cpp})
76faa3c Rename the remaining main.{h,cpp} to validation.{h,cpp} (Matt Corallo)
e736772 Move network-msg-processing code out of main to its own file (Matt Corallo)
87c35f5 Remove orphan state wipe from UnloadBlockIndex. (Matt Corallo)
* Merge #9014: Fix block-connection performance regression
dd0df81 Document ConnectBlock connectTrace postconditions (Matt Corallo)
2d6e561 Switch pblock in ProcessNewBlock to a shared_ptr (Matt Corallo)
2736c44 Make the optional pblock in ActivateBestChain a shared_ptr (Matt Corallo)
ae4db44 Create a shared_ptr for the block we're connecting in ActivateBCS (Matt Corallo)
fd9d890 Keep blocks as shared_ptrs, instead of copying txn in ConnectTip (Matt Corallo)
6fdd43b Add struct to track block-connect-time-generated info for callbacks (Matt Corallo)
* Merge #9240: Remove txConflicted
a874ab5 remove internal tracking of mempool conflicts for reporting to wallet (Alex Morcos)
bf663f8 remove external usage of mempool conflict tracking (Alex Morcos)
* Merge #9344: Do not run functions with necessary side-effects in assert()
da9cdd2 Do not run functions with necessary side-effects in assert() (Gregory Maxwell)
* Merge #9273: Remove unused CDiskBlockPos* argument from ProcessNewBlock
a13fa4c Remove unused CDiskBlockPos* argument from ProcessNewBlock (Matt Corallo)
* Merge #9352: Attempt reconstruction from all compact block announcements
813ede9 [qa] Update compactblocks test for multi-peer reconstruction (Suhas Daftuar)
7017298 Allow compactblock reconstruction when block is in flight (Suhas Daftuar)
* Merge #9252: Release cs_main before calling ProcessNewBlock, or processing headers (cmpctblock handling)
bd02bdd Release cs_main before processing cmpctblock as header (Suhas Daftuar)
680b0c0 Release cs_main before calling ProcessNewBlock (cmpctblock handling) (Suhas Daftuar)
* Merge #9283: A few more CTransactionRef optimizations
91335ba Remove unused MakeTransactionRef overloads (Pieter Wuille)
6713f0f Make FillBlock consume txn_available to avoid shared_ptr copies (Pieter Wuille)
62607d7 Convert COrphanTx to keep a CTransactionRef (Pieter Wuille)
c44e4c4 Make AcceptToMemoryPool take CTransactionRef (Pieter Wuille)
* Merge #9375: Relay compact block messages prior to full block connection
02ee4eb Make most_recent_compact_block a pointer to a const (Matt Corallo)
73666ad Add comment to describe callers to ActivateBestChain (Matt Corallo)
962f7f0 Call ActivateBestChain without cs_main/with most_recent_block (Matt Corallo)
0df777d Use a temp pindex to avoid a const_cast in ProcessNewBlockHeaders (Matt Corallo)
c1ae4fc Avoid holding cs_most_recent_block while calling ReadBlockFromDisk (Matt Corallo)
9eb67f5 Ensure we meet the BIP 152 old-relay-types response requirements (Matt Corallo)
5749a85 Cache most-recently-connected compact block (Matt Corallo)
9eaec08 Cache most-recently-announced block's shared_ptr (Matt Corallo)
c802092 Relay compact block messages prior to full block connection (Matt Corallo)
6987219 Add a CValidationInterface::NewPoWValidBlock callback (Matt Corallo)
180586f Call AcceptBlock with the block's shared_ptr instead of CBlock& (Matt Corallo)
8baaba6 [qa] Avoid race in preciousblock test. (Matt Corallo)
9a0b2f4 [qa] Make compact blocks test construction using fetch methods (Matt Corallo)
8017547 Make CBlockIndex*es in net_processing const (Matt Corallo)
* Merge #9486: Make peer=%d log prints consistent
e6111b2 Make peer id logging consistent ("peer=%d" instead of "peer %d") (Matt Corallo)
* Merge #9400: Set peers as HB peers upon full block validation
d4781ac Set peers as HB peers upon full block validation (Gregory Sanders)
* Merge #9499: Use recent-rejects, orphans, and recently-replaced txn for compact-block-reconstruction
c594580 Add braces around AddToCompactExtraTransactions (Matt Corallo)
1ccfe9b Clarify comment about mempool/extra conflicts (Matt Corallo)
fac4c78 Make PartiallyDownloadedBlock::InitData's second param const (Matt Corallo)
b55b416 Add extra_count lower bound to compact reconstruction debug print (Matt Corallo)
863edb4 Consider all (<100k memusage) txn for compact-block-extra-txn cache (Matt Corallo)
7f8c8ca Consider all orphan txn for compact-block-extra-txn cache (Matt Corallo)
93380c5 Use replaced transactions in compact block reconstruction (Matt Corallo)
1531652 Keep shared_ptrs to recently-replaced txn for compact blocks (Matt Corallo)
edded80 Make ATMP optionally return the CTransactionRefs it replaced (Matt Corallo)
c735540 Move ORPHAN constants from validation.h to net_processing.h (Matt Corallo)
* Merge #9587: Do not shadow local variable named `tx`.
44f2baa Do not shadow local variable named `tx`. (Pavel Janík)
* Merge #9510: [trivial] Fix typos in comments
cc16d99 [trivial] Fix typos in comments (practicalswift)
* Merge #9604: [Trivial] add comment about setting peer as HB peer.
dd5b011 [Trivial] add comment about setting peer as HB peer. (John Newbery)
* Fix using of AcceptToMemoryPool in PrivateSend code
* add `override`
* fSupportsDesiredCmpctVersion
* bring back tx ressurection in DisconnectTip
* Fix delayed headers
* Remove unused CConnman::FindNode overload
* Fix typos and comments
* Fix minor code differences
* Don't use rejection cache for corrupted transactions
Partly based on https://github.com/bitcoin/bitcoin/pull/8525
* Backport missed cs_main locking changes
Missed from https://github.com/bitcoin/bitcoin/commit/58a215ce8c13b900cf982c39f8ee4879290d1a95
* Backport missed comments and mapBlockSource.emplace call
Missed from two commits:
https://github.com/bitcoin/bitcoin/commit/88c35491ab19f9afdf9b3fa9356a072f70ef2f55
https://github.com/bitcoin/bitcoin/commit/7c98ce584ec23bcddcba8cdb33efa6547212f6ef
* Add CheckPeerHeaders() helper and check in (nCount == 0) too
2018-04-11 13:06:01 +02:00
|
|
|
if(!lockMain || !AcceptToMemoryPool(mempool, validationState, finalTransaction, false, NULL, NULL, false, maxTxFee, true))
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
|
|
|
LogPrintf("CPrivateSendServer::CommitFinalTransaction -- AcceptToMemoryPool() error: Transaction not valid\n");
|
|
|
|
SetNull();
|
|
|
|
// not much we can do in this case, just notify clients
|
2017-09-19 16:51:38 +02:00
|
|
|
RelayCompletedTransaction(ERR_INVALID_TX, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
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-09-11 16:13:48 +02:00
|
|
|
CDarksendBroadcastTx dstxNew(finalTransaction, activeMasternode.outpoint, GetAdjustedTime());
|
2017-06-30 20:30:16 +02:00
|
|
|
dstxNew.Sign();
|
|
|
|
CPrivateSend::AddDSTX(dstxNew);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LogPrintf("CPrivateSendServer::CommitFinalTransaction -- TRANSMITTING DSTX\n");
|
|
|
|
|
|
|
|
CInv inv(MSG_DSTX, hashTx);
|
2017-09-19 16:51:38 +02:00
|
|
|
connman.RelayInv(inv);
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
// Tell the clients it was successful
|
2017-09-19 16:51:38 +02:00
|
|
|
RelayCompletedTransaction(MSG_SUCCESS, connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
// Randomly charge clients
|
2017-09-19 16:51:38 +02:00
|
|
|
ChargeRandomFees(connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
// 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
|
2018-02-08 06:46:44 +01:00
|
|
|
// would be able to do this over and over again and bring the mixing to a halt.
|
2017-05-05 13:26:27 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::ChargeFees(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
//we don't need to charge collateral for every offence.
|
|
|
|
if(GetRandInt(100) > 33) return;
|
|
|
|
|
2017-09-20 14:02:53 +02:00
|
|
|
std::vector<CTransactionRef> vecOffendersCollaterals;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
if(nState == POOL_STATE_ACCEPTING_ENTRIES) {
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txCollateral : vecSessionCollaterals) {
|
2017-05-05 13:26:27 +02:00
|
|
|
bool fFound = false;
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries)
|
2017-09-20 14:02:53 +02:00
|
|
|
if(*entry.txCollateral == *txCollateral)
|
2017-05-05 13:26:27 +02:00
|
|
|
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?
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries) {
|
|
|
|
for (const auto& txdsin : entry.vecTxDSIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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",
|
2017-09-20 14:02:53 +02:00
|
|
|
(nState == POOL_STATE_SIGNING) ? "sign" : "send", vecOffendersCollaterals[0]->ToString());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-02-12 13:47:35 +01:00
|
|
|
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.
|
|
|
|
*/
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::ChargeRandomFees(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txCollateral : vecSessionCollaterals) {
|
2017-05-05 13:26:27 +02:00
|
|
|
if(GetRandInt(100) > 10) return;
|
2017-09-20 14:02:53 +02:00
|
|
|
LogPrintf("CPrivateSendServer::ChargeRandomFees -- charging random fees, txCollateral=%s", txCollateral->ToString());
|
2018-02-12 13:47:35 +01:00
|
|
|
connman.RelayTransaction(*txCollateral);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check for various timeouts (queue objects, mixing, etc)
|
|
|
|
//
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::CheckTimeout(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-03-21 12:10:01 +01:00
|
|
|
CheckQueue();
|
|
|
|
|
2017-05-05 13:26:27 +02:00
|
|
|
int nTimeout = (nState == POOL_STATE_SIGNING) ? PRIVATESEND_SIGNING_TIMEOUT : PRIVATESEND_QUEUE_TIMEOUT;
|
2018-02-12 13:47:53 +01:00
|
|
|
bool fTimeout = GetTime() - nTimeLastSuccessfulStep >= nTimeout;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
if(nState != POOL_STATE_IDLE && fTimeout) {
|
2018-02-08 06:46:44 +01:00
|
|
|
LogPrint("privatesend", "CPrivateSendServer::CheckTimeout -- %s timed out (%ds) -- resetting\n",
|
2017-05-05 13:26:27 +02:00
|
|
|
(nState == POOL_STATE_SIGNING) ? "Signing" : "Session", nTimeout);
|
2017-09-19 16:51:38 +02:00
|
|
|
ChargeFees(connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::CheckForCompleteQueue(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
if(nState == POOL_STATE_QUEUE && IsSessionReady()) {
|
|
|
|
SetState(POOL_STATE_ACCEPTING_ENTRIES);
|
|
|
|
|
2018-05-26 20:03:23 +02:00
|
|
|
CDarksendQueue dsq(nSessionDenom, nSessionInputCount, activeMasternode.outpoint, 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();
|
2017-09-19 16:51:38 +02:00
|
|
|
dsq.Relay(connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries) {
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2017-12-04 07:06:07 +01:00
|
|
|
for (const auto& txout : entry.vecTxOut)
|
|
|
|
txNew.vout.push_back(txout);
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txdsin : entry.vecTxDSIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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)
|
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txin : entryNew.vecTxDSIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
if(txin.prevout.IsNull()) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddEntry -- input not valid!\n");
|
|
|
|
nMessageIDRet = ERR_INVALID_INPUT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:02:53 +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;
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& txin : entryNew.vecTxDSIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrint("privatesend", "looking for txin -- %s\n", txin.ToString());
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries) {
|
|
|
|
for (const auto& txdsin : entry.vecTxDSIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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;
|
2018-02-12 13:47:53 +01:00
|
|
|
nTimeLastSuccessfulStep = GetTime();
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPrivateSendServer::AddScriptSig(const CTxIn& txinNew)
|
|
|
|
{
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::AddScriptSig -- scriptSig=%s\n", ScriptToAsmStr(txinNew.scriptSig).substr(0,24));
|
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries) {
|
|
|
|
for (const auto& txdsin : entry.vecTxDSIn) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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));
|
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (auto& txin : finalMutableTransaction.vin) {
|
2018-04-20 12:53:23 +02:00
|
|
|
if(txin.prevout == txinNew.prevout && txin.nSequence == txinNew.nSequence) {
|
2017-05-05 13:26:27 +02:00
|
|
|
txin.scriptSig = txinNew.scriptSig;
|
|
|
|
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()
|
|
|
|
{
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries)
|
|
|
|
for (const auto& txdsin : entry.vecTxDSIn)
|
2017-05-05 13:26:27 +02:00
|
|
|
if(!txdsin.fHasSig) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-04 07:06:07 +01:00
|
|
|
bool CPrivateSendServer::IsOutputsCompatibleWithSessionDenom(const std::vector<CTxOut>& vecTxOut)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2017-12-04 07:06:07 +01:00
|
|
|
if(CPrivateSend::GetDenominations(vecTxOut) == 0) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries) {
|
2017-12-04 07:06:07 +01:00
|
|
|
LogPrintf("CPrivateSendServer::IsOutputsCompatibleWithSessionDenom -- vecTxOut denom %d, entry.vecTxOut denom %d\n",
|
|
|
|
CPrivateSend::GetDenominations(vecTxOut), CPrivateSend::GetDenominations(entry.vecTxOut));
|
|
|
|
if(CPrivateSend::GetDenominations(vecTxOut) != CPrivateSend::GetDenominations(entry.vecTxOut)) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
bool CPrivateSendServer::IsAcceptableDSA(const CDarksendAccept& dsa, PoolMessage& nMessageIDRet)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
// is denom even smth legit?
|
|
|
|
std::vector<int> vecBits;
|
2018-01-26 02:11:15 +01:00
|
|
|
if(!CPrivateSend::GetDenominationsBits(dsa.nDenom, vecBits)) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::%s -- denom not valid!\n", __func__);
|
2017-05-05 13:26:27 +02:00
|
|
|
nMessageIDRet = ERR_DENOM;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check collateral
|
2018-01-26 02:11:15 +01:00
|
|
|
if(!fUnitTest && !CPrivateSend::IsCollateralValid(dsa.txCollateral)) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::%s -- collateral not valid!\n", __func__);
|
2017-05-05 13:26:27 +02:00
|
|
|
nMessageIDRet = ERR_INVALID_COLLATERAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-26 20:03:23 +02:00
|
|
|
if(dsa.nInputCount < 0 || dsa.nInputCount > PRIVATESEND_ENTRY_MAX_SIZE) {
|
|
|
|
LogPrint("privatesend", "CPrivateSendServer::%s -- requested count is not valid!\n", __func__);
|
|
|
|
nMessageIDRet = ERR_INVALID_INPUT_COUNT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:26:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
bool CPrivateSendServer::CreateNewSession(const CDarksendAccept& dsa, PoolMessage& nMessageIDRet, CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode || nSessionID != 0) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
if(!IsAcceptableDSA(dsa, nMessageIDRet)) {
|
2017-05-05 13:26:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start new session
|
|
|
|
nMessageIDRet = MSG_NOERR;
|
|
|
|
nSessionID = GetRandInt(999999)+1;
|
2018-01-26 02:11:15 +01:00
|
|
|
nSessionDenom = dsa.nDenom;
|
2018-05-26 20:03:23 +02:00
|
|
|
// nInputCount is not covered by legacy signature, require SPORK_6_NEW_SIGS to activate to use new algo
|
|
|
|
// (to make sure nInputCount wasn't modified by some intermediary node)
|
|
|
|
nSessionInputCount = sporkManager.IsSporkActive(SPORK_6_NEW_SIGS) ? dsa.nInputCount : 0;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
SetState(POOL_STATE_QUEUE);
|
2018-02-12 13:47:53 +01:00
|
|
|
nTimeLastSuccessfulStep = GetTime();
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
if(!fUnitTest) {
|
|
|
|
//broadcast that I'm accepting entries, only if it's the first entry through
|
2018-05-26 20:03:23 +02:00
|
|
|
CDarksendQueue dsq(dsa.nDenom, dsa.nInputCount, activeMasternode.outpoint, 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();
|
2017-09-19 16:51:38 +02:00
|
|
|
dsq.Relay(connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
vecDarksendQueue.push_back(dsq);
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
vecSessionCollaterals.push_back(MakeTransactionRef(dsa.txCollateral));
|
2017-05-05 13:26:27 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
bool CPrivateSendServer::AddUserToExistingSession(const CDarksendAccept& dsa, PoolMessage& nMessageIDRet)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
2018-01-26 02:11:01 +01:00
|
|
|
if(!fMasternodeMode || nSessionID == 0 || IsSessionReady()) return false;
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
if(!IsAcceptableDSA(dsa, nMessageIDRet)) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:11:15 +01:00
|
|
|
if(dsa.nDenom != nSessionDenom) {
|
2017-05-05 13:26:27 +02:00
|
|
|
LogPrintf("CPrivateSendServer::AddUserToExistingSession -- incompatible denom %d (%s) != nSessionDenom %d (%s)\n",
|
2018-01-26 02:11:15 +01:00
|
|
|
dsa.nDenom, CPrivateSend::GetDenominationsToString(dsa.nDenom), nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom));
|
2017-05-05 13:26:27 +02:00
|
|
|
nMessageIDRet = ERR_DENOM;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-26 20:03:23 +02:00
|
|
|
if(dsa.nInputCount != nSessionInputCount) {
|
|
|
|
LogPrintf("CPrivateSendServer::AddUserToExistingSession -- incompatible count %d != nSessionInputCount %d\n",
|
|
|
|
dsa.nInputCount, nSessionInputCount);
|
|
|
|
nMessageIDRet = ERR_INVALID_INPUT_COUNT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:26:27 +02:00
|
|
|
// count new user as accepted to an existing session
|
|
|
|
|
|
|
|
nMessageIDRet = MSG_NOERR;
|
2018-02-12 13:47:53 +01:00
|
|
|
nTimeLastSuccessfulStep = GetTime();
|
2018-01-26 02:11:15 +01:00
|
|
|
vecSessionCollaterals.push_back(MakeTransactionRef(dsa.txCollateral));
|
2017-05-05 13:26:27 +02:00
|
|
|
|
2018-05-26 20:03:23 +02:00
|
|
|
LogPrintf("CPrivateSendServer::AddUserToExistingSession -- new user accepted, nSessionID: %d nSessionDenom: %d (%s) nSessionInputCount: %d vecSessionCollaterals.size(): %d\n",
|
|
|
|
nSessionID, nSessionDenom, CPrivateSend::GetDenominationsToString(nSessionDenom), nSessionInputCount, vecSessionCollaterals.size());
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::RelayFinalTransaction(const CTransaction& txFinal, CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
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) {
|
2017-09-19 16:51:38 +02:00
|
|
|
bool fOk = connman.ForNode(entry.addr, [&txFinal, &connman, this](CNode* pnode) {
|
2016-11-25 20:01:56 +01:00
|
|
|
CNetMsgMaker msgMaker(pnode->GetSendVersion());
|
|
|
|
connman.PushMessage(pnode, msgMaker.Make(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
|
2017-09-19 16:51:38 +02:00
|
|
|
RelayStatus(STATUS_REJECTED, connman);
|
2017-07-25 12:57:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::PushStatus(CNode* pnode, PoolStatusUpdate nStatusUpdate, PoolMessage nMessageID, CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
|
|
|
if(!pnode) return;
|
2016-11-25 20:01:56 +01:00
|
|
|
CNetMsgMaker msgMaker(pnode->GetSendVersion());
|
|
|
|
connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSSTATUSUPDATE, nSessionID, (int)nState, (int)vecEntries.size(), (int)nStatusUpdate, (int)nMessageID));
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::RelayStatus(PoolStatusUpdate nStatusUpdate, CConnman& connman, PoolMessage nMessageID)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
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
|
2017-09-19 16:51:38 +02:00
|
|
|
bool fOk = connman.ForNode(entry.addr, [&nStatusUpdate, &nMessageID, &connman, this](CNode* pnode) {
|
|
|
|
PushStatus(pnode, nStatusUpdate, nMessageID, connman);
|
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
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries) {
|
2017-09-19 16:51:38 +02:00
|
|
|
connman.ForNode(entry.addr, [&connman, this](CNode* pnode) {
|
|
|
|
PushStatus(pnode, STATUS_REJECTED, MSG_NOERR, connman);
|
2017-07-25 12:57:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void CPrivateSendServer::RelayCompletedTransaction(PoolMessage nMessageID, CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
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
|
2018-02-06 12:09:33 +01:00
|
|
|
for (const auto& entry : vecEntries) {
|
2017-09-19 16:51:38 +02:00
|
|
|
bool fOk = connman.ForNode(entry.addr, [&nMessageID, &connman, this](CNode* pnode) {
|
2016-11-25 20:01:56 +01:00
|
|
|
CNetMsgMaker msgMaker(pnode->GetSendVersion());
|
|
|
|
connman.PushMessage(pnode, msgMaker.Make(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
|
2017-09-19 16:51:38 +02:00
|
|
|
RelayStatus(STATUS_REJECTED, connman);
|
2017-07-25 12:57:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPrivateSendServer::SetState(PoolState nStateNew)
|
|
|
|
{
|
2018-03-21 12:10:01 +01:00
|
|
|
if(!fMasternodeMode) return;
|
|
|
|
|
|
|
|
if(nStateNew == POOL_STATE_ERROR || nStateNew == POOL_STATE_SUCCESS) {
|
2017-05-05 13:26:27 +02:00
|
|
|
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
|
2017-09-19 16:51:38 +02:00
|
|
|
void ThreadCheckPrivateSendServer(CConnman& connman)
|
2017-05-05 13:26:27 +02:00
|
|
|
{
|
|
|
|
if(fLiteMode) return; // disable all Dash specific functionality
|
2018-03-21 12:10:01 +01:00
|
|
|
if(!fMasternodeMode) return; // only run on masternodes
|
2017-05-05 13:26:27 +02:00
|
|
|
|
|
|
|
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++;
|
2017-09-19 16:51:38 +02:00
|
|
|
privateSendServer.CheckTimeout(connman);
|
|
|
|
privateSendServer.CheckForCompleteQueue(connman);
|
2017-05-05 13:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|