2021-04-20 21:33:02 +02:00
// Copyright (c) 2014-2021 The Dash Core developers
2017-05-05 13:26:27 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2018-11-05 10:29:33 +01:00
2021-10-01 21:19:08 +02:00
# include <coinjoin/client.h>
# include <coinjoin/options.h>
2017-05-05 13:26:27 +02:00
2021-10-25 15:55:34 +02:00
# include <chainparams.h>
2020-03-19 23:46:56 +01:00
# include <consensus/validation.h>
# include <core_io.h>
2021-04-16 05:41:16 +02:00
# include <evo/deterministicmns.h>
2021-10-01 21:19:08 +02:00
# include <masternode/meta.h>
2021-10-25 15:55:34 +02:00
# include <masternode/sync.h>
2021-04-16 05:41:16 +02:00
# include <net_processing.h>
2021-10-25 15:55:34 +02:00
# include <netmessagemaker.h>
2020-03-19 23:46:56 +01:00
# include <script/sign.h>
2021-06-21 00:49:59 +02:00
# include <shutdown.h>
2021-06-27 08:33:13 +02:00
# include <util/moneystr.h>
2021-12-21 13:05:29 +01:00
# include <util/ranges.h>
2021-10-25 15:55:34 +02:00
# include <util/system.h>
2022-03-24 05:13:51 +01:00
# include <util/translation.h>
2020-03-19 23:46:56 +01:00
# include <validation.h>
2021-03-19 09:11:41 +01:00
# include <version.h>
2020-03-19 23:46:56 +01:00
# include <wallet/coincontrol.h>
2020-07-07 19:31:33 +02:00
# include <wallet/fees.h>
2017-07-03 15:14:07 +02:00
2017-05-28 15:50:07 +02:00
# include <memory>
2019-10-09 18:48:53 +02:00
# include <univalue.h>
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
std : : map < const std : : string , std : : shared_ptr < CCoinJoinClientManager > > coinJoinClientManagers ;
CCoinJoinClientQueueManager coinJoinClientQueueManager ;
2020-07-17 02:25:11 +02:00
2017-05-05 13:26:27 +02:00
2022-04-18 18:47:26 +02:00
void CCoinJoinClientQueueManager : : ProcessMessage ( CNode * pfrom , const std : : string & msg_type , CDataStream & vRecv , CConnman & connman , bool enable_bip61 )
2017-05-05 13:26:27 +02:00
{
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return ;
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return ;
2018-11-05 10:29:07 +01:00
if ( ! masternodeSync . IsBlockchainSynced ( ) ) return ;
2017-05-05 13:26:27 +02:00
2021-08-12 09:02:29 +02:00
if ( ! CheckDiskSpace ( GetDataDir ( ) ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientQueueManager::ProcessMessage -- Not enough disk space, disabling CoinJoin. \n " ) ;
2018-09-04 12:54:59 +02:00
return ;
}
2017-05-05 13:26:27 +02:00
2022-04-18 18:47:26 +02:00
if ( msg_type = = NetMsgType : : DSQUEUE ) {
2021-03-17 23:36:11 +01:00
CCoinJoinQueue dsq ;
2017-05-05 13:26:27 +02:00
vRecv > > dsq ;
2018-09-04 12:54:59 +02:00
{
TRY_LOCK ( cs_vecqueue , lockRecv ) ;
2018-11-05 10:29:07 +01:00
if ( ! lockRecv ) return ;
2018-09-04 12:54:59 +02:00
// process every dsq only once
2021-03-17 23:36:11 +01:00
for ( const auto & q : vecCoinJoinQueue ) {
2018-11-05 10:29:07 +01:00
if ( q = = dsq ) {
2018-09-04 12:54:59 +02:00
return ;
}
2019-10-09 18:48:32 +02:00
if ( q . fReady = = dsq . fReady & & q . masternodeOutpoint = = dsq . masternodeOutpoint ) {
// no way the same mn can send another dsq with the same readiness this soon
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSQUEUE -- Peer %s is sending WAY too many dsq messages for a masternode with collateral %s \n " , pfrom - > GetLogString ( ) , dsq . masternodeOutpoint . ToStringShort ( ) ) ;
2019-10-09 18:48:32 +02:00
return ;
}
2017-05-05 13:26:27 +02:00
}
2018-09-04 12:54:59 +02:00
} // cs_vecqueue
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSQUEUE -- %s new \n " , dsq . ToString ( ) ) ;
2017-05-05 13:26:27 +02:00
2019-10-09 18:48:32 +02:00
if ( dsq . IsTimeOutOfBounds ( ) ) return ;
2017-05-05 13:26:27 +02:00
2018-12-17 15:45:36 +01:00
auto mnList = deterministicMNManager - > GetListAtChainTip ( ) ;
auto dmn = mnList . GetValidMNByCollateral ( dsq . masternodeOutpoint ) ;
if ( ! dmn ) return ;
2017-05-05 13:26:27 +02:00
2019-06-13 11:01:26 +02:00
if ( ! dsq . CheckSignature ( dmn - > pdmnState - > pubKeyOperator . Get ( ) ) ) {
2018-12-31 14:01:04 +01:00
LOCK ( cs_main ) ;
2017-05-07 09:59:42 +02:00
Misbehaving ( pfrom - > GetId ( ) , 10 ) ;
2017-05-05 13:26:27 +02:00
return ;
}
// if the queue is ready, submit if we can
2021-12-21 13:05:29 +01:00
if ( dsq . fReady & & ranges : : any_of ( coinJoinClientManagers ,
[ & dmn , & connman ] ( const auto & pair ) { return pair . second - > TrySubmitDenominate ( dmn - > pdmnState - > addr , connman ) ; } ) ) {
LogPrint ( BCLog : : COINJOIN , " DSQUEUE -- CoinJoin queue (%s) is ready on masternode %s \n " , dsq . ToString ( ) , dmn - > pdmnState - > addr . ToString ( ) ) ;
return ;
2017-05-05 13:26:27 +02:00
} else {
2019-01-03 21:08:34 +01:00
int64_t nLastDsq = mmetaman . GetMetaInfo ( dmn - > proTxHash ) - > GetLastDsq ( ) ;
2020-05-02 17:50:03 +02:00
int64_t nDsqThreshold = mmetaman . GetDsqThreshold ( dmn - > proTxHash , mnList . GetValidMNsCount ( ) ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSQUEUE -- nLastDsq: %d nDsqThreshold: %d nDsqCount: %d \n " , nLastDsq , nDsqThreshold , mmetaman . GetDsqCount ( ) ) ;
2019-06-18 13:33:05 +02:00
// don't allow a few nodes to dominate the queuing process
2020-05-02 17:50:03 +02:00
if ( nLastDsq ! = 0 & & nDsqThreshold > mmetaman . GetDsqCount ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSQUEUE -- Masternode %s is sending too many dsq messages \n " , dmn - > proTxHash . ToString ( ) ) ;
2017-05-05 13:26:27 +02:00
return ;
}
2019-01-03 21:08:34 +01:00
mmetaman . AllowMixing ( dmn - > proTxHash ) ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSQUEUE -- new CoinJoin queue (%s) from masternode %s \n " , dsq . ToString ( ) , dmn - > pdmnState - > addr . ToString ( ) ) ;
2019-10-09 18:48:32 +02:00
2021-12-21 13:05:29 +01:00
ranges : : any_of ( coinJoinClientManagers ,
[ & dsq ] ( const auto & pair ) { return pair . second - > MarkAlreadyJoinedQueueAsTried ( dsq ) ; } ) ;
2019-10-09 18:48:32 +02:00
2022-04-25 11:28:37 +02:00
{
TRY_LOCK ( cs_vecqueue , lockRecv ) ;
if ( ! lockRecv ) return ;
vecCoinJoinQueue . push_back ( dsq ) ;
}
2017-09-19 16:51:38 +02:00
dsq . Relay ( connman ) ;
2017-05-05 13:26:27 +02:00
}
2020-07-16 14:23:37 +02:00
}
}
2022-04-18 18:47:26 +02:00
void CCoinJoinClientManager : : ProcessMessage ( CNode * pfrom , const std : : string & msg_type , CDataStream & vRecv , CConnman & connman , bool enable_bip61 )
2020-07-16 14:23:37 +02:00
{
if ( fMasternodeMode ) return ;
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return ;
2020-07-16 14:23:37 +02:00
if ( ! masternodeSync . IsBlockchainSynced ( ) ) return ;
2021-08-12 09:02:29 +02:00
if ( ! CheckDiskSpace ( GetDataDir ( ) ) ) {
2020-07-16 14:23:37 +02:00
ResetPool ( ) ;
StopMixing ( ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::ProcessMessage -- Not enough disk space, disabling CoinJoin. \n " ) ;
2020-07-16 14:23:37 +02:00
return ;
}
2022-04-18 18:47:26 +02:00
if ( msg_type = = NetMsgType : : DSSTATUSUPDATE | |
msg_type = = NetMsgType : : DSFINALTX | |
msg_type = = NetMsgType : : DSCOMPLETE ) {
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2018-09-20 14:40:43 +02:00
LOCK ( cs_deqsessions ) ;
for ( auto & session : deqSessions ) {
2022-04-18 18:47:26 +02:00
session . ProcessMessage ( pfrom , msg_type , vRecv , connman , enable_bip61 ) ;
2018-09-04 12:54:59 +02:00
}
}
}
2022-04-18 18:47:26 +02:00
void CCoinJoinClientSession : : ProcessMessage ( CNode * pfrom , const std : : string & msg_type , CDataStream & vRecv , CConnman & connman , bool enable_bip61 )
2018-09-04 12:54:59 +02:00
{
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return ;
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return ;
2018-11-05 10:29:07 +01:00
if ( ! masternodeSync . IsBlockchainSynced ( ) ) return ;
2017-05-05 13:26:27 +02:00
2022-04-18 18:47:26 +02:00
if ( msg_type = = NetMsgType : : DSSTATUSUPDATE ) {
2018-12-17 15:45:36 +01:00
if ( ! mixingMasternode ) return ;
if ( mixingMasternode - > pdmnState - > addr ! = pfrom - > addr ) {
2017-05-05 13:26:27 +02:00
return ;
}
2021-03-17 23:36:11 +01:00
CCoinJoinStatusUpdate psssup ;
2019-06-18 13:33:30 +02:00
vRecv > > psssup ;
2020-05-11 14:30:57 +02:00
ProcessPoolStateUpdate ( psssup ) ;
2017-05-05 13:26:27 +02:00
2022-04-18 18:47:26 +02:00
} else if ( msg_type = = NetMsgType : : DSFINALTX ) {
2018-12-17 15:45:36 +01:00
if ( ! mixingMasternode ) return ;
if ( mixingMasternode - > pdmnState - > addr ! = pfrom - > addr ) {
2017-05-05 13:26:27 +02:00
return ;
}
int nMsgSessionID ;
2016-12-05 08:01:20 +01:00
vRecv > > nMsgSessionID ;
CTransaction txNew ( deserialize , vRecv ) ;
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
if ( nSessionID ! = nMsgSessionID ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSFINALTX -- message doesn't match current CoinJoin session: nSessionID: %d nMsgSessionID: %d \n " , nSessionID , nMsgSessionID ) ;
2017-05-05 13:26:27 +02:00
return ;
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSFINALTX -- txNew %s " , txNew . ToString ( ) ) ; /* Continued */
2017-05-05 13:26:27 +02:00
2019-06-18 13:33:05 +02:00
// check to see if input is spent already? (and probably not confirmed)
2017-09-19 16:51:38 +02:00
SignFinalTransaction ( txNew , pfrom , connman ) ;
2017-05-05 13:26:27 +02:00
2022-04-18 18:47:26 +02:00
} else if ( msg_type = = NetMsgType : : DSCOMPLETE ) {
2018-12-17 15:45:36 +01:00
if ( ! mixingMasternode ) return ;
if ( mixingMasternode - > pdmnState - > addr ! = pfrom - > addr ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSCOMPLETE -- message doesn't match current Masternode: infoMixingMasternode=%s addr=%s \n " , mixingMasternode - > pdmnState - > addr . ToString ( ) , pfrom - > addr . ToString ( ) ) ;
2017-05-05 13:26:27 +02:00
return ;
}
int nMsgSessionID ;
2019-06-18 13:33:30 +02:00
PoolMessage nMsgMessageID ;
2017-05-05 13:26:27 +02:00
vRecv > > nMsgSessionID > > nMsgMessageID ;
2018-11-05 10:29:07 +01:00
if ( nMsgMessageID < MSG_POOL_MIN | | nMsgMessageID > MSG_POOL_MAX ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSCOMPLETE -- nMsgMessageID is out of bounds: %d \n " , nMsgMessageID ) ;
2017-05-05 13:26:27 +02:00
return ;
}
2018-11-05 10:29:07 +01:00
if ( nSessionID ! = nMsgSessionID ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " DSCOMPLETE -- message doesn't match current CoinJoin session: nSessionID: %d nMsgSessionID: %d \n " , nSessionID , nMsgSessionID ) ;
2017-05-05 13:26:27 +02:00
return ;
}
2022-04-06 22:37:20 +02:00
LogPrint ( BCLog : : COINJOIN , " DSCOMPLETE -- nMsgSessionID %d nMsgMessageID %d (%s) \n " , nMsgSessionID , nMsgMessageID , CCoinJoin : : GetMessageByID ( nMsgMessageID ) . translated ) ;
2017-05-05 13:26:27 +02:00
2019-06-18 13:33:30 +02:00
CompletedTransaction ( nMsgMessageID ) ;
2017-05-05 13:26:27 +02:00
}
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : StartMixing ( ) {
2022-01-10 18:31:45 +01:00
bool expected { false } ;
return fMixing . compare_exchange_strong ( expected , true ) ;
2020-07-12 07:52:25 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : StopMixing ( ) {
2020-08-28 11:52:00 +02:00
fMixing = false ;
2020-07-12 07:52:25 +02:00
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : IsMixing ( ) const
2020-07-12 07:52:25 +02:00
{
2020-08-28 11:52:00 +02:00
return fMixing ;
2020-07-12 07:52:25 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientSession : : ResetPool ( )
2017-05-05 13:26:27 +02:00
{
txMyCollateral = CMutableTransaction ( ) ;
UnlockCoins ( ) ;
2017-09-11 16:14:55 +02:00
keyHolderStorage . ReturnAll ( ) ;
2022-04-25 11:28:37 +02:00
WITH_LOCK ( cs_coinjoin , SetNull ( ) ) ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : ResetPool ( )
2018-09-04 12:54:59 +02:00
{
nCachedLastSuccessBlock = 0 ;
vecMasternodesUsed . clear ( ) ;
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2021-09-30 20:58:33 +02:00
LOCK ( cs_deqsessions ) ;
2018-09-20 14:40:43 +02:00
for ( auto & session : deqSessions ) {
2018-09-04 12:54:59 +02:00
session . ResetPool ( ) ;
}
2018-09-20 14:40:43 +02:00
deqSessions . clear ( ) ;
2018-09-04 12:54:59 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientSession : : SetNull ( )
2017-05-05 13:26:27 +02:00
{
2022-04-25 11:28:37 +02:00
AssertLockHeld ( cs_coinjoin ) ;
2017-05-05 13:26:27 +02:00
// Client side
2018-12-17 15:45:36 +01:00
mixingMasternode = nullptr ;
2018-02-01 02:10:52 +01:00
pendingDsaRequest = CPendingDsaRequest ( ) ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
CCoinJoinBaseSession : : SetNull ( ) ;
2017-05-05 13:26:27 +02:00
}
//
// Unlock coins after mixing fails or succeeds
//
2021-03-17 23:36:11 +01:00
void CCoinJoinClientSession : : UnlockCoins ( )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return ;
2018-07-28 15:50:41 +02:00
2018-11-05 10:29:07 +01:00
while ( true ) {
2020-08-28 11:52:00 +02:00
TRY_LOCK ( mixingWallet . cs_wallet , lockWallet ) ;
2018-11-05 10:29:07 +01:00
if ( ! lockWallet ) {
2021-07-13 11:31:17 +02:00
UninterruptibleSleep ( std : : chrono : : milliseconds { 50 } ) ;
2018-11-05 10:29:07 +01:00
continue ;
}
2018-02-06 12:09:33 +01:00
for ( const auto & outpoint : vecOutPointLocked )
2020-08-28 11:52:00 +02:00
mixingWallet . UnlockCoin ( outpoint ) ;
2017-05-05 13:26:27 +02:00
break ;
}
vecOutPointLocked . clear ( ) ;
}
2022-04-06 22:37:20 +02:00
bilingual_str CCoinJoinClientSession : : GetStatus ( bool fWaitForBlock ) const
2017-05-05 13:26:27 +02:00
{
static int nStatusMessageProgress = 0 ;
nStatusMessageProgress + = 10 ;
2020-08-24 17:15:57 +02:00
std : : string strSuffix ;
2017-05-05 13:26:27 +02:00
2019-06-18 13:33:05 +02:00
if ( fWaitForBlock | | ! masternodeSync . IsBlockchainSynced ( ) ) {
2017-05-05 13:26:27 +02:00
return strAutoDenomResult ;
2019-06-18 13:33:05 +02:00
}
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
switch ( nState ) {
case POOL_STATE_IDLE :
2022-04-06 22:37:20 +02:00
return strprintf ( _ ( " %s is idle. " ) , gCoinJoinName ) ;
2018-11-05 10:29:07 +01:00
case POOL_STATE_QUEUE :
if ( nStatusMessageProgress % 70 < = 30 )
strSuffix = " . " ;
else if ( nStatusMessageProgress % 70 < = 50 )
strSuffix = " .. " ;
2021-08-06 23:55:51 +02:00
else
2018-11-05 10:29:07 +01:00
strSuffix = " ... " ;
2022-04-06 22:37:20 +02:00
return strprintf ( _ ( " Submitted to masternode, waiting in queue %s " ) , strSuffix ) ;
2018-11-05 10:29:07 +01:00
case POOL_STATE_ACCEPTING_ENTRIES :
2019-06-18 13:33:30 +02:00
return strAutoDenomResult ;
2018-11-05 10:29:07 +01:00
case POOL_STATE_SIGNING :
if ( nStatusMessageProgress % 70 < = 40 )
2022-04-06 22:37:20 +02:00
return _ ( " Found enough users, signing ... " ) ;
2018-11-05 10:29:07 +01:00
else if ( nStatusMessageProgress % 70 < = 50 )
strSuffix = " . " ;
else if ( nStatusMessageProgress % 70 < = 60 )
strSuffix = " .. " ;
2021-08-06 23:55:51 +02:00
else
2018-11-05 10:29:07 +01:00
strSuffix = " ... " ;
2022-04-06 22:37:20 +02:00
return strprintf ( _ ( " Found enough users, signing ( waiting %s ) " ), strSuffix) ;
2018-11-05 10:29:07 +01:00
case POOL_STATE_ERROR :
2022-04-06 22:37:20 +02:00
return strprintf ( _ ( " %s request incomplete: " ) , gCoinJoinName ) + strLastMessage + Untranslated ( " " ) + _ ( " Will retry... " ) ;
2018-11-05 10:29:07 +01:00
default :
2022-04-06 22:37:20 +02:00
return strprintf ( _ ( " Unknown state: id = %u " ) , nState ) ;
2017-05-05 13:26:27 +02:00
}
}
2022-04-06 22:37:20 +02:00
bilingual_str CCoinJoinClientManager : : GetStatuses ( )
2018-09-04 12:54:59 +02:00
{
2022-04-06 22:37:20 +02:00
bilingual_str strStatus ;
2018-09-04 12:54:59 +02:00
bool fWaitForBlock = WaitForAnotherBlock ( ) ;
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2021-09-30 20:58:33 +02:00
LOCK ( cs_deqsessions ) ;
2021-07-30 02:01:02 +02:00
for ( const auto & session : deqSessions ) {
2022-04-06 22:37:20 +02:00
strStatus = strStatus + session . GetStatus ( fWaitForBlock ) + Untranslated ( " ; " ) ;
2018-09-04 12:54:59 +02:00
}
return strStatus ;
}
2021-03-17 23:36:11 +01:00
std : : string CCoinJoinClientManager : : GetSessionDenoms ( )
2018-09-04 12:54:59 +02:00
{
std : : string strSessionDenoms ;
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2021-09-30 20:58:33 +02:00
LOCK ( cs_deqsessions ) ;
2021-07-30 02:01:02 +02:00
for ( const auto & session : deqSessions ) {
2022-01-11 01:57:37 +01:00
strSessionDenoms + = CCoinJoin : : DenominationToString ( session . nSessionDenom ) ;
strSessionDenoms + = " ; " ;
2018-09-04 12:54:59 +02:00
}
return strSessionDenoms . empty ( ) ? " N/A " : strSessionDenoms ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : GetMixingMasternodeInfo ( CDeterministicMNCPtr & ret ) const
2017-12-04 07:06:07 +01:00
{
2018-12-17 15:45:36 +01:00
ret = mixingMasternode ;
return ret ! = nullptr ;
2017-12-04 07:06:07 +01:00
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : GetMixingMasternodesInfo ( std : : vector < CDeterministicMNCPtr > & vecDmnsRet ) const
2017-12-04 07:06:07 +01:00
{
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2018-09-20 14:40:43 +02:00
LOCK ( cs_deqsessions ) ;
for ( const auto & session : deqSessions ) {
2018-12-17 15:45:36 +01:00
CDeterministicMNCPtr dmn ;
if ( session . GetMixingMasternodeInfo ( dmn ) ) {
vecDmnsRet . push_back ( dmn ) ;
2018-09-04 12:54:59 +02:00
}
}
2018-12-17 15:45:36 +01:00
return ! vecDmnsRet . empty ( ) ;
2017-12-04 07:06:07 +01:00
}
2017-05-05 13:26:27 +02:00
//
2018-09-04 12:54:59 +02:00
// Check session timeouts
2017-05-05 13:26:27 +02:00
//
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : CheckTimeout ( )
2017-05-05 13:26:27 +02:00
{
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return false ;
2017-05-05 13:26:27 +02:00
2020-05-11 14:30:57 +02:00
if ( nState = = POOL_STATE_IDLE ) return false ;
if ( nState = = POOL_STATE_ERROR ) {
if ( GetTime ( ) - nTimeLastSuccessfulStep > = 10 ) {
// reset after being in POOL_STATE_ERROR for 10 or more seconds
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- resetting session %d \n " , __func__ , nSessionID ) ;
2022-04-25 11:28:37 +02:00
WITH_LOCK ( cs_coinjoin , SetNull ( ) ) ;
2020-05-11 14:30:57 +02:00
}
return false ;
2017-05-05 13:26:27 +02:00
}
2018-02-12 13:47:53 +01:00
int nLagTime = 10 ; // give the server a few extra seconds before resetting.
2021-03-17 23:36:11 +01:00
int nTimeout = ( nState = = POOL_STATE_SIGNING ) ? COINJOIN_SIGNING_TIMEOUT : COINJOIN_QUEUE_TIMEOUT ;
2018-02-12 13:47:53 +01:00
bool fTimeout = GetTime ( ) - nTimeLastSuccessfulStep > = nTimeout + nLagTime ;
2017-05-05 13:26:27 +02:00
2020-05-11 14:30:57 +02:00
if ( ! fTimeout ) return false ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- %s %d timed out (%ds) \n " , __func__ ,
2020-05-11 14:30:57 +02:00
( nState = = POOL_STATE_SIGNING ) ? " Signing at session " : " Session " , nSessionID , nTimeout ) ;
2018-09-04 12:54:59 +02:00
2020-05-11 14:30:57 +02:00
SetState ( POOL_STATE_ERROR ) ;
2018-09-04 12:54:59 +02:00
UnlockCoins ( ) ;
keyHolderStorage . ReturnAll ( ) ;
2020-05-11 14:30:57 +02:00
nTimeLastSuccessfulStep = GetTime ( ) ;
2021-03-17 23:36:11 +01:00
strLastMessage = CCoinJoin : : GetMessageByID ( ERR_SESSION ) ;
2018-09-04 12:54:59 +02:00
return true ;
}
//
// Check all queues and sessions for timeouts
//
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : CheckTimeout ( )
2018-09-04 12:54:59 +02:00
{
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return ;
2018-09-04 12:54:59 +02:00
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) | | ! IsMixing ( ) ) return ;
2018-11-25 14:35:52 +01:00
2018-09-20 14:40:43 +02:00
LOCK ( cs_deqsessions ) ;
for ( auto & session : deqSessions ) {
2018-09-04 12:54:59 +02:00
if ( session . CheckTimeout ( ) ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Session timed out. " ) ;
2018-09-04 12:54:59 +02:00
}
2017-05-05 13:26:27 +02:00
}
}
//
// Execute a mixing denomination via a Masternode.
// This is only ran from clients
//
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : SendDenominate ( const std : : vector < std : : pair < CTxDSIn , CTxOut > > & vecPSInOutPairsIn , CConnman & connman )
2017-05-05 13:26:27 +02:00
{
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SendDenominate -- CoinJoin from a Masternode is not supported currently. \n " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2018-06-18 15:19:23 +02:00
if ( CTransaction ( txMyCollateral ) . IsNull ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClient:SendDenominate -- CoinJoin collateral not set \n " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
// we should already be connected to a Masternode
2018-11-05 10:29:07 +01:00
if ( ! nSessionID ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SendDenominate -- No Masternode has been selected yet. \n " ) ;
2017-05-05 13:26:27 +02:00
UnlockCoins ( ) ;
2017-09-11 16:14:55 +02:00
keyHolderStorage . ReturnAll ( ) ;
2022-04-25 11:28:37 +02:00
WITH_LOCK ( cs_coinjoin , SetNull ( ) ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-08-12 09:02:29 +02:00
if ( ! CheckDiskSpace ( GetDataDir ( ) ) ) {
2017-05-05 13:26:27 +02:00
UnlockCoins ( ) ;
2017-09-11 16:14:55 +02:00
keyHolderStorage . ReturnAll ( ) ;
2022-04-25 11:28:37 +02:00
WITH_LOCK ( cs_coinjoin , SetNull ( ) ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SendDenominate -- Not enough disk space. \n " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
SetState ( POOL_STATE_ACCEPTING_ENTRIES ) ;
2022-04-06 22:37:20 +02:00
strLastMessage = Untranslated ( " " ) ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SendDenominate -- Added transaction to pool. \n " ) ;
2017-05-05 13:26:27 +02:00
2018-09-15 12:18:32 +02:00
CMutableTransaction tx ; // for debug purposes only
std : : vector < CTxDSIn > vecTxDSInTmp ;
std : : vector < CTxOut > vecTxOutTmp ;
2017-05-05 13:26:27 +02:00
2018-09-15 12:18:32 +02:00
for ( const auto & pair : vecPSInOutPairsIn ) {
vecTxDSInTmp . emplace_back ( pair . first ) ;
vecTxOutTmp . emplace_back ( pair . second ) ;
tx . vin . emplace_back ( pair . first ) ;
tx . vout . emplace_back ( pair . second ) ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SendDenominate -- Submitting partial tx %s " , tx . ToString ( ) ) ; /* Continued */
2018-09-15 12:18:32 +02:00
2017-05-05 13:26:27 +02:00
// store our entry for later use
2021-09-30 20:58:33 +02:00
LOCK ( cs_coinjoin ) ;
2021-10-11 17:06:49 +02:00
vecEntries . emplace_back ( vecTxDSInTmp , vecTxOutTmp , CTransaction ( txMyCollateral ) ) ;
2018-09-15 12:18:32 +02:00
RelayIn ( vecEntries . back ( ) , connman ) ;
2018-02-12 13:47:53 +01:00
nTimeLastSuccessfulStep = GetTime ( ) ;
2017-05-05 13:26:27 +02:00
return true ;
}
2020-05-11 14:30:57 +02:00
// Process incoming messages from Masternode updating the progress of mixing
2021-03-17 23:36:11 +01:00
void CCoinJoinClientSession : : ProcessPoolStateUpdate ( CCoinJoinStatusUpdate psssup )
2017-05-05 13:26:27 +02:00
{
2020-05-11 14:30:57 +02:00
if ( fMasternodeMode ) return ;
2017-05-05 13:26:27 +02:00
// do not update state when mixing client state is one of these
2020-05-11 14:30:57 +02:00
if ( nState = = POOL_STATE_IDLE | | nState = = POOL_STATE_ERROR ) return ;
2017-05-05 13:26:27 +02:00
2020-05-11 14:30:57 +02:00
if ( psssup . nState < POOL_STATE_MIN | | psssup . nState > POOL_STATE_MAX ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- psssup.nState is out of bounds: %d \n " , __func__ , psssup . nState ) ;
2020-05-11 14:30:57 +02:00
return ;
}
2017-05-05 13:26:27 +02:00
2020-05-11 14:30:57 +02:00
if ( psssup . nMessageID < MSG_POOL_MIN | | psssup . nMessageID > MSG_POOL_MAX ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- psssup.nMessageID is out of bounds: %d \n " , __func__ , psssup . nMessageID ) ;
2020-05-11 14:30:57 +02:00
return ;
2017-05-05 13:26:27 +02:00
}
2022-04-06 22:37:20 +02:00
bilingual_str strMessageTmp = CCoinJoin : : GetMessageByID ( psssup . nMessageID ) ;
strAutoDenomResult = _ ( " Masternode: " ) + Untranslated ( " " ) + strMessageTmp ;
2020-05-11 14:30:57 +02:00
switch ( psssup . nStatusUpdate ) {
case STATUS_REJECTED : {
2022-04-06 22:37:20 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- rejected by Masternode: %s \n " , __func__ , strMessageTmp . translated ) ;
2020-05-11 14:30:57 +02:00
SetState ( POOL_STATE_ERROR ) ;
UnlockCoins ( ) ;
keyHolderStorage . ReturnAll ( ) ;
2018-02-12 13:47:53 +01:00
nTimeLastSuccessfulStep = GetTime ( ) ;
2020-05-11 14:30:57 +02:00
strLastMessage = strMessageTmp ;
break ;
}
case STATUS_ACCEPTED : {
if ( nState = = psssup . nState & & psssup . nState = = POOL_STATE_QUEUE & & nSessionID = = 0 & & psssup . nSessionID ! = 0 ) {
// new session id should be set only in POOL_STATE_QUEUE state
nSessionID = psssup . nSessionID ;
nTimeLastSuccessfulStep = GetTime ( ) ;
2022-04-06 22:37:20 +02:00
strMessageTmp = strMessageTmp + strprintf ( Untranslated ( " Set nSessionID to %d. " ) , nSessionID ) ;
2020-05-11 14:30:57 +02:00
}
2022-04-06 22:37:20 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- accepted by Masternode: %s \n " , __func__ , strMessageTmp . translated ) ;
2020-05-11 14:30:57 +02:00
break ;
}
default : {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- psssup.nStatusUpdate is out of bounds: %d \n " , __func__ , psssup . nStatusUpdate ) ;
2020-05-11 14:30:57 +02:00
break ;
2017-05-05 13:26:27 +02:00
}
}
}
//
// After we receive the finalized transaction from the Masternode, we must
// check it to make sure it's what we want, then sign it if we agree.
// If we refuse to sign, it's possible we'll be charged collateral
//
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : SignFinalTransaction ( const CTransaction & finalTransactionNew , CNode * pnode , CConnman & connman )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2018-07-28 15:50:41 +02:00
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode | | pnode = = nullptr ) return false ;
2018-12-17 15:45:36 +01:00
if ( ! mixingMasternode ) return false ;
2017-05-05 13:26:27 +02:00
2021-10-16 21:08:00 +02:00
LOCK ( cs_main ) ;
2021-12-13 06:35:45 +01:00
LOCK ( mixingWallet . cs_wallet ) ;
2021-09-30 20:58:33 +02:00
LOCK ( cs_coinjoin ) ;
2018-05-23 19:04:25 +02:00
finalMutableTransaction = CMutableTransaction { finalTransactionNew } ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- finalMutableTransaction=%s " , __func__ , finalMutableTransaction . ToString ( ) ) ; /* Continued */
2020-01-01 15:12:25 +01:00
// STEP 1: check final transaction general rules
2017-05-05 13:26:27 +02:00
2017-09-22 03:53:15 +02:00
// Make sure it's BIP69 compliant
sort ( finalMutableTransaction . vin . begin ( ) , finalMutableTransaction . vin . end ( ) , CompareInputBIP69 ( ) ) ;
sort ( finalMutableTransaction . vout . begin ( ) , finalMutableTransaction . vout . end ( ) , CompareOutputBIP69 ( ) ) ;
2018-11-05 10:29:07 +01:00
if ( finalMutableTransaction . GetHash ( ) ! = finalTransactionNew . GetHash ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- ERROR! Masternode %s is not BIP69 compliant! \n " , __func__ , mixingMasternode - > proTxHash . ToString ( ) ) ;
2020-01-01 15:12:25 +01:00
UnlockCoins ( ) ;
keyHolderStorage . ReturnAll ( ) ;
SetNull ( ) ;
return false ;
}
// Make sure all inputs/outputs are valid
PoolMessage nMessageID { MSG_NOERR } ;
if ( ! IsValidInOuts ( finalMutableTransaction . vin , finalMutableTransaction . vout , nMessageID , nullptr ) ) {
2022-04-06 22:37:20 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- ERROR! IsValidInOuts() failed: %s \n " , __func__ , CCoinJoin : : GetMessageByID ( nMessageID ) . translated ) ;
2017-09-22 03:53:15 +02:00
UnlockCoins ( ) ;
keyHolderStorage . ReturnAll ( ) ;
SetNull ( ) ;
return false ;
}
2020-01-01 15:12:25 +01:00
// STEP 2: make sure our own inputs/outputs are present, otherwise refuse to sign
2017-05-05 13:26:27 +02:00
std : : vector < CTxIn > sigs ;
2021-09-30 20:58:33 +02:00
for ( const auto & entry : vecEntries ) {
2020-01-01 15:12:25 +01:00
// Check that the final transaction has all our outputs
2021-09-30 20:58:33 +02:00
for ( const auto & txout : entry . vecTxOut ) {
2021-12-21 13:05:29 +01:00
bool fFound = ranges : : any_of ( finalMutableTransaction . vout , [ & txout ] ( const auto & txoutFinal ) {
return txoutFinal = = txout ;
} ) ;
2020-01-01 15:12:25 +01:00
if ( ! fFound ) {
// Something went wrong and we'll refuse to sign. It's possible we'll be charged collateral. But that's
// better than signing if the transaction doesn't look like what we wanted.
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- an output is missing, refusing to sign! txout=%s \n " , __func__ , txout . ToString ( ) ) ;
2020-01-01 15:12:25 +01:00
UnlockCoins ( ) ;
keyHolderStorage . ReturnAll ( ) ;
SetNull ( ) ;
return false ;
}
}
2018-02-06 12:09:33 +01:00
for ( const auto & txdsin : entry . vecTxDSIn ) {
2017-05-05 13:26:27 +02:00
/* Sign my transaction and all outputs */
int nMyInputIndex = - 1 ;
CScript prevPubKey = CScript ( ) ;
2018-11-05 10:29:07 +01:00
for ( unsigned int i = 0 ; i < finalMutableTransaction . vin . size ( ) ; i + + ) {
if ( finalMutableTransaction . vin [ i ] = = txdsin ) {
2017-05-05 13:26:27 +02:00
nMyInputIndex = i ;
prevPubKey = txdsin . prevPubKey ;
2020-01-01 15:12:25 +01:00
break ;
2017-05-05 13:26:27 +02:00
}
}
2020-01-01 15:12:25 +01:00
if ( nMyInputIndex = = - 1 ) {
// Can't find one of my own inputs, refuse to sign. It's possible we'll be charged collateral. But that's
// better than signing if the transaction doesn't look like what we wanted.
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- missing input! txdsin=%s \n " , __func__ , txdsin . ToString ( ) ) ;
2020-01-01 15:12:25 +01:00
UnlockCoins ( ) ;
keyHolderStorage . ReturnAll ( ) ;
SetNull ( ) ;
return false ;
}
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Signing my input %i \n " , __func__ , nMyInputIndex ) ;
2020-01-01 15:12:25 +01:00
// TODO we're using amount=0 here but we should use the correct amount. This works because Dash ignores the amount while signing/verifying (only used in Bitcoin/Segwit)
2020-08-28 11:52:00 +02:00
if ( ! SignSignature ( mixingWallet , prevPubKey , finalMutableTransaction , nMyInputIndex , 0 , int ( SIGHASH_ALL | SIGHASH_ANYONECANPAY ) ) ) { // changes scriptSig
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Unable to sign my own transaction! \n " , __func__ ) ;
2021-07-30 02:01:02 +02:00
// not sure what to do here, it will time out...?
2017-05-05 13:26:27 +02:00
}
2020-01-01 15:12:25 +01:00
sigs . push_back ( finalMutableTransaction . vin [ nMyInputIndex ] ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- nMyInputIndex: %d, sigs.size(): %d, scriptSig=%s \n " ,
2021-09-30 20:58:33 +02:00
__func__ , nMyInputIndex , ( int ) sigs . size ( ) , ScriptToAsmStr ( finalMutableTransaction . vin [ nMyInputIndex ] . scriptSig ) ) ;
2017-05-05 13:26:27 +02:00
}
}
2018-11-05 10:29:07 +01:00
if ( sigs . empty ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- can't sign anything! \n " , __func__ ) ;
2017-05-05 13:26:27 +02:00
UnlockCoins ( ) ;
2017-09-11 16:14:55 +02:00
keyHolderStorage . ReturnAll ( ) ;
2017-05-05 13:26:27 +02:00
SetNull ( ) ;
return false ;
}
// push all of our signatures to the Masternode
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- pushing sigs to the masternode, finalMutableTransaction=%s " , __func__ , finalMutableTransaction . ToString ( ) ) ; /* Continued */
2016-11-25 20:01:56 +01:00
CNetMsgMaker msgMaker ( pnode - > GetSendVersion ( ) ) ;
connman . PushMessage ( pnode , msgMaker . Make ( NetMsgType : : DSSIGNFINALTX , sigs ) ) ;
2017-05-05 13:26:27 +02:00
SetState ( POOL_STATE_SIGNING ) ;
2018-02-12 13:47:53 +01:00
nTimeLastSuccessfulStep = GetTime ( ) ;
2017-05-05 13:26:27 +02:00
return true ;
}
// mixing transaction was completed (failed or successful)
2021-03-17 23:36:11 +01:00
void CCoinJoinClientSession : : CompletedTransaction ( PoolMessage nMessageID )
2017-05-05 13:26:27 +02:00
{
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return ;
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
if ( nMessageID = = MSG_SUCCESS ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CompletedTransaction -- success \n " ) ;
coinJoinClientManagers . at ( mixingWallet . GetName ( ) ) - > UpdatedSuccessBlock ( ) ;
2017-09-11 16:14:55 +02:00
keyHolderStorage . KeepAll ( ) ;
2017-05-05 13:26:27 +02:00
} else {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CompletedTransaction -- error \n " ) ;
2017-09-11 16:14:55 +02:00
keyHolderStorage . ReturnAll ( ) ;
2017-05-05 13:26:27 +02:00
}
UnlockCoins ( ) ;
2022-04-25 11:28:37 +02:00
WITH_LOCK ( cs_coinjoin , SetNull ( ) ) ;
2021-03-17 23:36:11 +01:00
strLastMessage = CCoinJoin : : GetMessageByID ( nMessageID ) ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : UpdatedSuccessBlock ( )
2018-09-04 12:54:59 +02:00
{
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return ;
2018-09-04 12:54:59 +02:00
nCachedLastSuccessBlock = nCachedBlockHeight ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : WaitForAnotherBlock ( ) const
2017-09-03 15:30:58 +02:00
{
2019-06-18 13:33:05 +02:00
if ( ! masternodeSync . IsBlockchainSynced ( ) ) return true ;
2017-09-03 15:30:58 +02:00
2021-03-17 23:36:11 +01:00
if ( CCoinJoinClientOptions : : IsMultiSessionEnabled ( ) ) return false ;
2017-09-03 15:30:58 +02:00
return nCachedBlockHeight - nCachedLastSuccessBlock < nMinBlocksToWait ;
}
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : CheckAutomaticBackup ( )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) | | ! IsMixing ( ) ) return false ;
2018-07-28 15:50:41 +02:00
2018-11-05 10:29:07 +01:00
switch ( nWalletBackups ) {
case 0 :
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Automatic backups disabled " ) + Untranslated ( " , " ) + _ ( " no mixing available. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- %s \n " , strAutoDenomResult . original ) ;
2020-07-12 07:52:25 +02:00
StopMixing ( ) ;
2020-08-28 11:52:00 +02:00
mixingWallet . nKeysLeftSinceAutoBackup = 0 ; // no backup, no "keys since last backup"
2018-11-05 10:29:07 +01:00
return false ;
case - 1 :
// Automatic backup failed, nothing else we can do until user fixes the issue manually.
2021-07-30 02:01:02 +02:00
// There is no way to bring user attention in daemon mode, so we just update status and
2018-11-05 10:29:07 +01:00
// keep spamming if debug is on.
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " ERROR! Failed to create automatic backup " ) + Untranslated ( " , " ) + _ ( " see debug.log for details. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- %s \n " , strAutoDenomResult . original ) ;
2018-11-05 10:29:07 +01:00
return false ;
case - 2 :
// We were able to create automatic backup but keypool was not replenished because wallet is locked.
2021-07-30 02:01:02 +02:00
// There is no way to bring user attention in daemon mode, so we just update status and
2018-11-05 10:29:07 +01:00
// keep spamming if debug is on.
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " WARNING! Failed to replenish keypool, please unlock your wallet to do so. " ) + Untranslated ( " , " ) + _ ( " see debug.log for details. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- %s \n " , strAutoDenomResult . original ) ;
2018-11-05 10:29:07 +01:00
return false ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
if ( mixingWallet . nKeysLeftSinceAutoBackup < COINJOIN_KEYS_THRESHOLD_STOP ) {
2020-01-08 08:02:49 +01:00
// We should never get here via mixing itself but probably something else is still actively using keypool
2022-04-06 22:37:20 +02:00
strAutoDenomResult = strprintf ( _ ( " Very low number of keys left: %d " ) + Untranslated ( " , " ) + _ ( " no mixing available. " ) , mixingWallet . nKeysLeftSinceAutoBackup ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- %s \n " , strAutoDenomResult . original ) ;
2017-05-05 13:26:27 +02:00
// It's getting really dangerous, stop mixing
2020-07-12 07:52:25 +02:00
StopMixing ( ) ;
2017-05-05 13:26:27 +02:00
return false ;
2021-03-17 23:36:11 +01:00
} else if ( mixingWallet . nKeysLeftSinceAutoBackup < COINJOIN_KEYS_THRESHOLD_WARNING ) {
2021-07-30 02:01:02 +02:00
// Low number of keys left, but it's still more or less safe to continue
2022-04-06 22:37:20 +02:00
strAutoDenomResult = strprintf ( _ ( " Very low number of keys left: %d " ) , mixingWallet . nKeysLeftSinceAutoBackup ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- %s \n " , strAutoDenomResult . original ) ;
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
if ( fCreateAutoBackups ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- Trying to create new backup. \n " ) ;
2022-04-07 06:43:16 +02:00
bilingual_str errorString ;
std : : vector < bilingual_str > warnings ;
2017-05-05 13:26:27 +02:00
2022-03-24 21:07:40 +01:00
if ( ! mixingWallet . AutoBackupWallet ( " " , errorString , warnings ) ) {
if ( ! warnings . empty ( ) ) {
2017-05-05 13:26:27 +02:00
// There were some issues saving backup but yet more or less safe to continue
2020-05-13 20:30:31 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- WARNING! Something went wrong on automatic backup: %s \n " , Join ( warnings , Untranslated ( " \n " ) ) . translated ) ;
2017-05-05 13:26:27 +02:00
}
2022-04-07 06:43:16 +02:00
if ( ! errorString . original . empty ( ) ) {
2017-05-05 13:26:27 +02:00
// Things are really broken
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " ERROR! Failed to create automatic backup " ) + Untranslated ( " : " ) + errorString ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- %s \n " , strAutoDenomResult . original ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
}
} else {
2020-01-08 08:02:49 +01:00
// Wait for something else (e.g. GUI action) to create automatic backup for us
2017-05-05 13:26:27 +02:00
return false ;
}
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::CheckAutomaticBackup -- Keys left since latest backup: %d \n " , mixingWallet . nKeysLeftSinceAutoBackup ) ;
2017-05-05 13:26:27 +02:00
return true ;
}
//
2019-11-21 21:49:35 +01:00
// Passively run mixing in the background to mix funds based on the given configuration.
2017-05-05 13:26:27 +02:00
//
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : DoAutomaticDenominating ( CConnman & connman , bool fDryRun )
2017-05-05 13:26:27 +02:00
{
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return false ; // no client-side mixing on masternodes
if ( nState ! = POOL_STATE_IDLE ) return false ;
2017-05-05 13:26:27 +02:00
2019-01-03 10:17:43 +01:00
if ( ! masternodeSync . IsBlockchainSynced ( ) ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Can't mix while sync in progress. " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2018-07-28 15:51:45 +02:00
2018-09-04 12:54:59 +02:00
CAmount nBalanceNeedsAnonymized ;
{
2021-12-13 06:35:45 +01:00
LOCK2 ( cs_main , mixingWallet . cs_wallet ) ;
2017-05-05 13:26:27 +02:00
2020-08-28 11:52:00 +02:00
if ( ! fDryRun & & mixingWallet . IsLocked ( true ) ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Wallet is locked. " ) ;
2018-11-05 10:29:07 +01:00
return false ;
}
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
if ( GetEntriesCount ( ) > 0 ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Mixing in progress... " ) ;
2018-11-05 10:29:07 +01:00
return false ;
}
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
TRY_LOCK ( cs_coinjoin , lockDS ) ;
2018-11-05 10:29:07 +01:00
if ( ! lockDS ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Lock is already in place. " ) ;
2018-11-05 10:29:07 +01:00
return false ;
}
2017-05-05 13:26:27 +02:00
2020-05-15 11:34:12 +02:00
if ( deterministicMNManager - > GetListAtChainTip ( ) . GetValidMNsCount ( ) = = 0 & &
Params ( ) . NetworkIDString ( ) ! = CBaseChainParams : : REGTEST ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " No Masternodes detected. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- %s \n " , strAutoDenomResult . original ) ;
2018-11-05 10:29:07 +01:00
return false ;
}
2017-05-05 13:26:27 +02:00
2021-10-06 04:43:24 +02:00
const auto bal = mixingWallet . GetBalance ( ) ;
2019-04-30 15:19:23 +02:00
// check if there is anything left to do
2021-10-06 04:43:24 +02:00
CAmount nBalanceAnonymized = bal . m_anonymized ;
2021-03-17 23:36:11 +01:00
nBalanceNeedsAnonymized = CCoinJoinClientOptions : : GetAmount ( ) * COIN - nBalanceAnonymized ;
2019-04-30 15:19:23 +02:00
if ( nBalanceNeedsAnonymized < 0 ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- Nothing to do \n " ) ;
2019-04-30 15:19:23 +02:00
// nothing to do, just keep it in idle mode
return false ;
}
2021-03-17 23:36:11 +01:00
CAmount nValueMin = CCoinJoin : : GetSmallestDenomination ( ) ;
2019-04-30 15:19:23 +02:00
2018-11-05 10:29:07 +01:00
// if there are no confirmed DS collateral inputs yet
2020-08-28 11:52:00 +02:00
if ( ! mixingWallet . HasCollateralInputs ( ) ) {
2018-11-05 10:29:07 +01:00
// should have some additional amount for them
2021-03-17 23:36:11 +01:00
nValueMin + = CCoinJoin : : GetMaxCollateralAmount ( ) ;
2018-11-05 10:29:07 +01:00
}
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
// including denoms but applying some restrictions
2020-08-28 11:52:00 +02:00
CAmount nBalanceAnonymizable = mixingWallet . GetAnonymizableBalance ( ) ;
2017-05-05 13:26:27 +02:00
2019-11-21 21:49:35 +01:00
// mixable balance is way too small
2019-04-30 15:19:23 +02:00
if ( nBalanceAnonymizable < nValueMin ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Not enough funds to mix. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- %s \n " , strAutoDenomResult . original ) ;
2018-11-05 10:29:07 +01:00
return false ;
}
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
// excluding denoms
2020-08-28 11:52:00 +02:00
CAmount nBalanceAnonimizableNonDenom = mixingWallet . GetAnonymizableBalance ( true ) ;
2018-11-05 10:29:07 +01:00
// denoms
2021-10-06 04:43:24 +02:00
CAmount nBalanceDenominatedConf = bal . m_denominated_trusted ;
CAmount nBalanceDenominatedUnconf = bal . m_denominated_untrusted_pending ;
2018-11-05 10:29:07 +01:00
CAmount nBalanceDenominated = nBalanceDenominatedConf + nBalanceDenominatedUnconf ;
2021-03-17 23:36:11 +01:00
CAmount nBalanceToDenominate = CCoinJoinClientOptions : : GetAmount ( ) * COIN - nBalanceDenominated ;
2019-04-30 15:19:23 +02:00
// adjust nBalanceNeedsAnonymized to consume final denom
if ( nBalanceDenominated - nBalanceAnonymized > nBalanceNeedsAnonymized ) {
2021-03-17 23:36:11 +01:00
auto denoms = CCoinJoin : : GetStandardDenominations ( ) ;
2019-04-30 15:19:23 +02:00
CAmount nAdditionalDenom { 0 } ;
for ( const auto & denom : denoms ) {
if ( nBalanceNeedsAnonymized < denom ) {
nAdditionalDenom = denom ;
} else {
break ;
}
}
nBalanceNeedsAnonymized + = nAdditionalDenom ;
}
2018-11-05 10:29:07 +01:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- current stats: \n "
2019-04-30 14:48:21 +02:00
" nValueMin: %s \n "
2019-04-30 15:19:23 +02:00
" nBalanceAnonymizable: %s \n "
" nBalanceAnonymized: %s \n "
2019-04-30 14:48:21 +02:00
" nBalanceNeedsAnonymized: %s \n "
" nBalanceAnonimizableNonDenom: %s \n "
" nBalanceDenominatedConf: %s \n "
" nBalanceDenominatedUnconf: %s \n "
2019-05-02 01:20:36 +02:00
" nBalanceDenominated: %s \n "
2019-04-30 15:19:23 +02:00
" nBalanceToDenominate: %s \n " ,
2019-04-30 14:48:21 +02:00
FormatMoney ( nValueMin ) ,
2019-04-30 15:19:23 +02:00
FormatMoney ( nBalanceAnonymizable ) ,
FormatMoney ( nBalanceAnonymized ) ,
2019-04-30 14:48:21 +02:00
FormatMoney ( nBalanceNeedsAnonymized ) ,
FormatMoney ( nBalanceAnonimizableNonDenom ) ,
FormatMoney ( nBalanceDenominatedConf ) ,
FormatMoney ( nBalanceDenominatedUnconf ) ,
2019-04-30 15:19:23 +02:00
FormatMoney ( nBalanceDenominated ) ,
FormatMoney ( nBalanceToDenominate )
2019-04-30 14:48:21 +02:00
) ;
2018-11-05 10:29:07 +01:00
if ( fDryRun ) return true ;
// Check if we have should create more denominated inputs i.e.
// there are funds to denominate and denominated balance does not exceed
// max amount to mix yet.
2021-03-17 23:36:11 +01:00
if ( nBalanceAnonimizableNonDenom > = nValueMin + CCoinJoin : : GetCollateralAmount ( ) & & nBalanceToDenominate > 0 ) {
2020-09-01 17:27:33 +02:00
CreateDenominated ( nBalanceToDenominate ) ;
2019-04-30 15:19:23 +02:00
}
2018-11-05 10:29:07 +01:00
//check if we have the collateral sized inputs
2020-08-28 11:52:00 +02:00
if ( ! mixingWallet . HasCollateralInputs ( ) ) {
return ! mixingWallet . HasCollateralInputs ( false ) & & MakeCollateralAmounts ( ) ;
2019-06-18 13:33:05 +02:00
}
2018-11-05 10:29:07 +01:00
if ( nSessionID ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Mixing in progress... " ) ;
2018-11-05 10:29:07 +01:00
return false ;
}
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
// Initial phase, find a Masternode
// Clean if there is anything left from previous session
UnlockCoins ( ) ;
keyHolderStorage . ReturnAll ( ) ;
SetNull ( ) ;
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
// should be no unconfirmed denoms in non-multi-session mode
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsMultiSessionEnabled ( ) & & nBalanceDenominatedUnconf > 0 ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Found unconfirmed denominated outputs, will wait till they confirm to continue. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- %s \n " , strAutoDenomResult . original ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2018-11-05 10:29:07 +01:00
//check our collateral and create new if needed
std : : string strReason ;
2018-06-18 15:19:23 +02:00
if ( CTransaction ( txMyCollateral ) . IsNull ( ) ) {
2021-01-20 17:38:05 +01:00
if ( ! CreateCollateralTransaction ( txMyCollateral , strReason ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- create collateral error:%s \n " , strReason ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2018-11-05 10:29:07 +01:00
} else {
2021-10-11 17:06:49 +02:00
if ( ! CCoinJoin : : IsCollateralValid ( CTransaction ( txMyCollateral ) ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- invalid collateral, recreating... \n " ) ;
2021-01-20 17:38:05 +01:00
if ( ! CreateCollateralTransaction ( txMyCollateral , strReason ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::DoAutomaticDenominating -- create collateral error: %s \n " , strReason ) ;
2018-11-05 10:29:07 +01:00
return false ;
}
}
2017-05-05 13:26:27 +02:00
}
2019-11-13 21:03:52 +01:00
// lock the funds we're going to use for our collateral
for ( const auto & txin : txMyCollateral . vin ) {
2020-08-28 11:52:00 +02:00
mixingWallet . LockCoin ( txin . prevout ) ;
2019-11-13 21:03:52 +01:00
vecOutPointLocked . push_back ( txin . prevout ) ;
}
2020-08-28 11:52:00 +02:00
} // LOCK2(cs_main, mixingWallet.cs_wallet);
2018-09-04 12:54:59 +02:00
2019-09-17 14:07:56 +02:00
// Always attempt to join an existing queue
if ( JoinExistingQueue ( nBalanceNeedsAnonymized , connman ) ) {
2018-09-04 12:54:59 +02:00
return true ;
2019-06-18 13:33:05 +02:00
}
2018-09-04 12:54:59 +02:00
2019-09-17 14:07:56 +02:00
// If we were unable to find/join an existing queue then start a new one.
2019-06-18 13:33:05 +02:00
if ( StartNewQueue ( nBalanceNeedsAnonymized , connman ) ) return true ;
2018-09-04 12:54:59 +02:00
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " No compatible Masternode found. " ) ;
2018-09-04 12:54:59 +02:00
return false ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : DoAutomaticDenominating ( CConnman & connman , bool fDryRun )
2018-09-04 12:54:59 +02:00
{
if ( fMasternodeMode ) return false ; // no client-side mixing on masternodes
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) | | ! IsMixing ( ) ) return false ;
2018-09-04 12:54:59 +02:00
2019-01-03 10:17:43 +01:00
if ( ! masternodeSync . IsBlockchainSynced ( ) ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Can't mix while sync in progress. " ) ;
2018-09-04 12:54:59 +02:00
return false ;
}
2020-08-28 11:52:00 +02:00
if ( ! fDryRun & & mixingWallet . IsLocked ( true ) ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Wallet is locked. " ) ;
2018-09-04 12:54:59 +02:00
return false ;
}
2017-05-05 13:26:27 +02:00
2018-12-17 15:45:36 +01:00
int nMnCountEnabled = deterministicMNManager - > GetListAtChainTip ( ) . GetValidMNsCount ( ) ;
2017-05-05 13:26:27 +02:00
// If we've used 90% of the Masternode list then drop the oldest first ~30%
2018-11-27 08:03:41 +01:00
int nThreshold_high = nMnCountEnabled * 0.9 ;
2017-05-05 13:26:27 +02:00
int nThreshold_low = nThreshold_high * 0.7 ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " Checking vecMasternodesUsed: size: %d, threshold: %d \n " , ( int ) vecMasternodesUsed . size ( ) , nThreshold_high ) ;
2017-05-05 13:26:27 +02:00
2018-11-05 10:29:07 +01:00
if ( ( int ) vecMasternodesUsed . size ( ) > nThreshold_high ) {
2017-05-05 13:26:27 +02:00
vecMasternodesUsed . erase ( vecMasternodesUsed . begin ( ) , vecMasternodesUsed . begin ( ) + vecMasternodesUsed . size ( ) - nThreshold_low ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " vecMasternodesUsed: new size: %d, threshold: %d \n " , ( int ) vecMasternodesUsed . size ( ) , nThreshold_high ) ;
2017-05-05 13:26:27 +02:00
}
2018-09-04 12:54:59 +02:00
bool fResult = true ;
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2021-09-30 20:58:33 +02:00
LOCK ( cs_deqsessions ) ;
2021-03-17 23:36:11 +01:00
if ( ( int ) deqSessions . size ( ) < CCoinJoinClientOptions : : GetSessions ( ) ) {
2020-07-16 14:23:47 +02:00
deqSessions . emplace_back ( mixingWallet ) ;
2018-09-04 12:54:59 +02:00
}
2018-09-20 14:40:43 +02:00
for ( auto & session : deqSessions ) {
2019-06-18 13:33:05 +02:00
if ( ! CheckAutomaticBackup ( ) ) return false ;
2017-05-05 13:26:27 +02:00
2018-09-04 12:54:59 +02:00
if ( WaitForAnotherBlock ( ) ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Last successful action was too recent. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::DoAutomaticDenominating -- %s \n " , strAutoDenomResult . original ) ;
2018-09-04 12:54:59 +02:00
return false ;
}
2017-05-05 13:26:27 +02:00
2018-09-04 12:54:59 +02:00
fResult & = session . DoAutomaticDenominating ( connman , fDryRun ) ;
}
2017-05-05 13:26:27 +02:00
2018-09-04 12:54:59 +02:00
return fResult ;
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : AddUsedMasternode ( const COutPoint & outpointMn )
2018-09-04 12:54:59 +02:00
{
vecMasternodesUsed . push_back ( outpointMn ) ;
}
2021-03-17 23:36:11 +01:00
CDeterministicMNCPtr CCoinJoinClientManager : : GetRandomNotUsedMasternode ( )
2018-09-04 12:54:59 +02:00
{
2018-12-17 15:47:57 +01:00
auto mnList = deterministicMNManager - > GetListAtChainTip ( ) ;
2021-07-30 02:01:02 +02:00
size_t nCountEnabled = mnList . GetValidMNsCount ( ) ;
size_t nCountNotExcluded = nCountEnabled - vecMasternodesUsed . size ( ) ;
2018-12-17 15:47:57 +01:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::%s -- %d enabled masternodes, %d masternodes to choose from \n " , __func__ , nCountEnabled , nCountNotExcluded ) ;
2021-07-30 02:01:02 +02:00
if ( nCountNotExcluded < 1 ) {
2018-12-17 15:47:57 +01:00
return nullptr ;
}
// fill a vector
std : : vector < CDeterministicMNCPtr > vpMasternodesShuffled ;
2021-07-30 02:01:02 +02:00
vpMasternodesShuffled . reserve ( nCountEnabled ) ;
refactor: numerous changes to avoid passing around a const ref to shared_ptr of CDeterministicMNC when not needed. (#4653)
* refactor: numerous changes to avoid passing around a const ref to shared_ptr of CDeterministicMNC when not needed.
Introduces ForEachMNShared, a version of ForEachMN that uses a shared_ptr, and may extend the lifetime of the underlying shared_ptr. This is not preferred, should prefer ForEachMN. See docs.
Adjusts ForEachMN to pass a reference. This is preferred for use over ForEachMNShared. See docs. A reference should be used since in usage we assume it's non-null anyway. Additionally, it allows us to know that the lifespan of the dmn is not being being extended (if lifespan needs to be extended, should use ForEachMNShared.
IsMNValid, IsMNPoSeBanned, UpdateMN, UpdateMN, AddUniqueProperty, DeleteUniqueProperty, UpdateUniqueProperty now take a const reference to CDeterministicMN instead of a const reference to shared_ptr<CDeterministicMN>. All of these functions previously assumed (or would've crashed) a non-null ptr, and non extended lifetime, as such converting to ref is appropriate.
CompareByLastPaid ptr overload now takes raw ptr instead of a const ref to shared. Since we simply dereference them, a raw ptr makes the most sense. This also avoids a potential expensive and implicit raw ptr -> shared ptr conversion if the function was called with raw ptrs.
rpcevo BuildDMNListEntry now takes a const ref for reasons as stated above
Signed-off-by: Pasta <pasta@dashboost.org>
* make stuff const
Signed-off-by: Pasta <pasta@dashboost.org>
* refactor/llmq: use ranges count_if
Signed-off-by: Pasta <pasta@dashboost.org>
2022-01-04 17:13:38 +01:00
mnList . ForEachMNShared ( true , [ & vpMasternodesShuffled ] ( const CDeterministicMNCPtr & dmn ) {
2018-12-17 15:47:57 +01:00
vpMasternodesShuffled . emplace_back ( dmn ) ;
} ) ;
// shuffle pointers
2021-01-14 20:46:16 +01:00
Shuffle ( vpMasternodesShuffled . begin ( ) , vpMasternodesShuffled . end ( ) , FastRandomContext ( ) ) ;
2018-12-17 15:47:57 +01:00
std : : set < COutPoint > excludeSet ( vecMasternodesUsed . begin ( ) , vecMasternodesUsed . end ( ) ) ;
// loop through
for ( const auto & dmn : vpMasternodesShuffled ) {
if ( excludeSet . count ( dmn - > collateralOutpoint ) ) {
continue ;
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::%s -- found, masternode=%s \n " , __func__ , dmn - > collateralOutpoint . ToStringShort ( ) ) ;
2018-12-17 15:47:57 +01:00
return dmn ;
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::%s -- failed \n " , __func__ ) ;
2018-12-17 15:47:57 +01:00
return nullptr ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : JoinExistingQueue ( CAmount nBalanceNeedsAnonymized , CConnman & connman )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2018-07-28 15:50:41 +02:00
2018-12-17 15:45:36 +01:00
auto mnList = deterministicMNManager - > GetListAtChainTip ( ) ;
2021-09-07 02:23:28 +02:00
int winners_to_skip { 8 } ;
if ( Params ( ) . NetworkIDString ( ) = = CBaseChainParams : : DEVNET | | Params ( ) . NetworkIDString ( ) = = CBaseChainParams : : REGTEST ) {
winners_to_skip = 0 ;
}
2017-05-05 13:26:27 +02:00
// Look through the queues and see if anything matches
2021-03-17 23:36:11 +01:00
CCoinJoinQueue dsq ;
while ( coinJoinClientQueueManager . GetQueueItemAndTry ( dsq ) ) {
2018-12-17 15:45:36 +01:00
auto dmn = mnList . GetValidMNByCollateral ( dsq . masternodeOutpoint ) ;
2017-05-05 13:26:27 +02:00
2018-12-17 15:45:36 +01:00
if ( ! dmn ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::JoinExistingQueue -- dsq masternode is not in masternode list, masternode=%s \n " , dsq . masternodeOutpoint . ToStringShort ( ) ) ;
2017-05-05 13:26:27 +02:00
continue ;
}
2018-02-12 13:47:20 +01:00
// skip next mn payments winners
2022-02-11 17:15:26 +01:00
if ( dmn - > pdmnState - > nLastPaidHeight + int ( mnList . GetValidMNsCount ( ) ) < mnList . GetHeight ( ) + winners_to_skip ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::JoinExistingQueue -- skipping winner, masternode=%s \n " , dmn - > proTxHash . ToString ( ) ) ;
2018-02-12 13:47:20 +01:00
continue ;
}
2017-05-05 13:26:27 +02:00
// mixing rate limit i.e. nLastDsq check should already pass in DSQUEUE ProcessMessage
2021-03-17 23:36:11 +01:00
// in order for dsq to get into vecCoinJoinQueue, so we should be safe to mix already,
2017-05-05 13:26:27 +02:00
// no need for additional verification here
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::JoinExistingQueue -- trying queue: %s \n " , dsq . ToString ( ) ) ;
2017-05-05 13:26:27 +02:00
2021-01-20 17:38:05 +01:00
std : : vector < CTxDSIn > vecTxDSInTmp ;
2018-05-26 20:03:23 +02:00
// Try to match their denominations if possible, select exact number of denominations
2021-01-20 17:38:05 +01:00
if ( ! mixingWallet . SelectTxDSInsByDenomination ( dsq . nDenom , nBalanceNeedsAnonymized , vecTxDSInTmp ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::JoinExistingQueue -- Couldn't match denomination %d (%s) \n " , dsq . nDenom , CCoinJoin : : DenominationToString ( dsq . nDenom ) ) ;
2017-05-05 13:26:27 +02:00
continue ;
}
2021-03-17 23:36:11 +01:00
coinJoinClientManagers . at ( mixingWallet . GetName ( ) ) - > AddUsedMasternode ( dsq . masternodeOutpoint ) ;
2017-05-05 13:26:27 +02:00
2018-12-17 15:45:36 +01:00
if ( connman . IsMasternodeOrDisconnectRequested ( dmn - > pdmnState - > addr ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::JoinExistingQueue -- skipping masternode connection, addr=%s \n " , dmn - > pdmnState - > addr . ToString ( ) ) ;
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
continue ;
2018-01-09 10:17:54 +01:00
}
2017-05-05 13:26:27 +02:00
2018-02-01 02:10:52 +01:00
nSessionDenom = dsq . nDenom ;
2018-12-17 15:45:36 +01:00
mixingMasternode = dmn ;
2021-03-17 23:36:11 +01:00
pendingDsaRequest = CPendingDsaRequest ( dmn - > pdmnState - > addr , CCoinJoinAccept ( nSessionDenom , txMyCollateral ) ) ;
2020-03-19 14:21:02 +01:00
connman . AddPendingMasternode ( dmn - > proTxHash ) ;
2018-02-01 02:10:52 +01:00
SetState ( POOL_STATE_QUEUE ) ;
2018-02-12 13:47:53 +01:00
nTimeLastSuccessfulStep = GetTime ( ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::JoinExistingQueue -- pending connection (from queue): nSessionDenom: %d (%s), addr=%s \n " ,
nSessionDenom , CCoinJoin : : DenominationToString ( nSessionDenom ) , dmn - > pdmnState - > addr . ToString ( ) ) ;
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Trying to connect... " ) ;
2018-01-17 16:09:08 +01:00
return true ;
2017-05-05 13:26:27 +02:00
}
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Failed to find mixing queue to join " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : StartNewQueue ( CAmount nBalanceNeedsAnonymized , CConnman & connman )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2019-04-30 15:19:23 +02:00
if ( nBalanceNeedsAnonymized < = 0 ) return false ;
2018-07-28 15:50:41 +02:00
2017-05-05 13:26:27 +02:00
int nTries = 0 ;
2020-05-02 17:50:03 +02:00
auto mnList = deterministicMNManager - > GetListAtChainTip ( ) ;
int nMnCount = mnList . GetValidMNsCount ( ) ;
2017-05-05 13:26:27 +02:00
2020-03-12 11:31:55 +01:00
// find available denominated amounts
std : : set < CAmount > setAmounts ;
2020-08-28 11:52:00 +02:00
if ( ! mixingWallet . SelectDenominatedAmounts ( nBalanceNeedsAnonymized , setAmounts ) ) {
2017-05-05 13:26:27 +02:00
// this should never happen
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Can't mix: no compatible inputs found! " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::StartNewQueue -- %s \n " , strAutoDenomResult . original ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
// otherwise, try one randomly
2018-11-05 10:29:07 +01:00
while ( nTries < 10 ) {
2021-03-17 23:36:11 +01:00
auto dmn = coinJoinClientManagers . at ( mixingWallet . GetName ( ) ) - > GetRandomNotUsedMasternode ( ) ;
2018-02-12 13:47:20 +01:00
2018-12-17 15:45:36 +01:00
if ( ! dmn ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Can't find random Masternode. " ) ;
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::StartNewQueue -- %s \n " , strAutoDenomResult . original ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2018-02-12 13:47:20 +01:00
2021-03-17 23:36:11 +01:00
coinJoinClientManagers . at ( mixingWallet . GetName ( ) ) - > AddUsedMasternode ( dmn - > collateralOutpoint ) ;
2018-10-11 16:32:51 +02:00
2018-02-12 13:47:20 +01:00
// skip next mn payments winners
2020-05-02 17:50:03 +02:00
if ( dmn - > pdmnState - > nLastPaidHeight + nMnCount < mnList . GetHeight ( ) + 8 ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::StartNewQueue -- skipping winner, masternode=%s \n " , dmn - > proTxHash . ToString ( ) ) ;
2018-02-12 13:47:20 +01:00
nTries + + ;
continue ;
}
2019-01-03 21:08:34 +01:00
int64_t nLastDsq = mmetaman . GetMetaInfo ( dmn - > proTxHash ) - > GetLastDsq ( ) ;
2020-05-02 17:50:03 +02:00
int64_t nDsqThreshold = mmetaman . GetDsqThreshold ( dmn - > proTxHash , nMnCount ) ;
if ( nLastDsq ! = 0 & & nDsqThreshold > mmetaman . GetDsqCount ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::StartNewQueue -- Too early to mix on this masternode! " /* Continued */
2020-05-02 17:50:03 +02:00
" masternode=%s addr=%s nLastDsq=%d nDsqThreshold=%d nDsqCount=%d \n " ,
2018-12-17 15:45:36 +01:00
dmn - > proTxHash . ToString ( ) , dmn - > pdmnState - > addr . ToString ( ) , nLastDsq ,
2020-05-02 17:50:03 +02:00
nDsqThreshold , mmetaman . GetDsqCount ( ) ) ;
2017-05-05 13:26:27 +02:00
nTries + + ;
continue ;
}
2018-12-17 15:45:36 +01:00
if ( connman . IsMasternodeOrDisconnectRequested ( dmn - > pdmnState - > addr ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::StartNewQueue -- skipping masternode connection, addr=%s \n " , dmn - > pdmnState - > addr . ToString ( ) ) ;
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
nTries + + ;
continue ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::StartNewQueue -- attempt %d connection to Masternode %s \n " , nTries , dmn - > pdmnState - > addr . ToString ( ) ) ;
2018-02-01 02:10:52 +01:00
2020-03-12 11:31:55 +01:00
// try to get a single random denom out of setAmounts
2018-11-05 10:29:07 +01:00
while ( nSessionDenom = = 0 ) {
2020-05-02 17:50:03 +02:00
for ( auto it = setAmounts . rbegin ( ) ; it ! = setAmounts . rend ( ) ; + + it ) {
2020-03-12 11:31:55 +01:00
if ( setAmounts . size ( ) > 1 & & GetRandInt ( 2 ) ) continue ;
2021-03-17 23:36:11 +01:00
nSessionDenom = CCoinJoin : : AmountToDenomination ( * it ) ;
2020-03-12 11:31:55 +01:00
break ;
}
2017-05-05 13:26:27 +02:00
}
2018-02-01 02:10:52 +01:00
2018-12-17 15:45:36 +01:00
mixingMasternode = dmn ;
2020-03-19 14:21:02 +01:00
connman . AddPendingMasternode ( dmn - > proTxHash ) ;
2021-03-17 23:36:11 +01:00
pendingDsaRequest = CPendingDsaRequest ( dmn - > pdmnState - > addr , CCoinJoinAccept ( nSessionDenom , txMyCollateral ) ) ;
2018-02-01 02:10:52 +01:00
SetState ( POOL_STATE_QUEUE ) ;
2018-02-12 13:47:53 +01:00
nTimeLastSuccessfulStep = GetTime ( ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::StartNewQueue -- pending connection, nSessionDenom: %d (%s), addr=%s \n " ,
nSessionDenom , CCoinJoin : : DenominationToString ( nSessionDenom ) , dmn - > pdmnState - > addr . ToString ( ) ) ;
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Trying to connect... " ) ;
2018-01-17 16:09:08 +01:00
return true ;
2017-05-05 13:26:27 +02:00
}
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Failed to start a new mixing queue " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : ProcessPendingDsaRequest ( CConnman & connman )
2018-02-01 02:10:52 +01:00
{
2018-09-04 12:54:59 +02:00
if ( ! pendingDsaRequest ) return false ;
2018-02-01 02:10:52 +01:00
2021-07-30 02:01:02 +02:00
bool fDone = connman . ForNode ( pendingDsaRequest . GetAddr ( ) , [ this , & connman ] ( CNode * pnode ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " -- processing dsa queue for addr=%s \n " , pnode - > addr . ToString ( ) ) ;
2018-02-12 13:47:53 +01:00
nTimeLastSuccessfulStep = GetTime ( ) ;
2018-02-01 02:10:52 +01:00
CNetMsgMaker msgMaker ( pnode - > GetSendVersion ( ) ) ;
connman . PushMessage ( pnode , msgMaker . Make ( NetMsgType : : DSACCEPT , pendingDsaRequest . GetDSA ( ) ) ) ;
return true ;
} ) ;
if ( fDone ) {
pendingDsaRequest = CPendingDsaRequest ( ) ;
} else if ( pendingDsaRequest . IsExpired ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- failed to connect to %s \n " , __func__ , pendingDsaRequest . GetAddr ( ) . ToString ( ) ) ;
2022-04-25 11:28:37 +02:00
WITH_LOCK ( cs_coinjoin , SetNull ( ) ) ;
2018-02-01 02:10:52 +01:00
}
2018-09-04 12:54:59 +02:00
return fDone ;
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : ProcessPendingDsaRequest ( CConnman & connman )
2018-09-04 12:54:59 +02:00
{
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2018-09-20 14:40:43 +02:00
LOCK ( cs_deqsessions ) ;
for ( auto & session : deqSessions ) {
2018-09-04 12:54:59 +02:00
if ( session . ProcessPendingDsaRequest ( connman ) ) {
2022-04-06 22:37:20 +02:00
strAutoDenomResult = _ ( " Mixing in progress... " ) ;
2018-09-04 12:54:59 +02:00
}
}
2018-02-01 02:10:52 +01:00
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : TrySubmitDenominate ( const CService & mnAddr , CConnman & connman )
2020-07-16 14:23:37 +02:00
{
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2020-07-16 14:23:37 +02:00
LOCK ( cs_deqsessions ) ;
for ( auto & session : deqSessions ) {
CDeterministicMNCPtr mnMixing ;
if ( session . GetMixingMasternodeInfo ( mnMixing ) & & mnMixing - > pdmnState - > addr = = mnAddr & & session . GetState ( ) = = POOL_STATE_QUEUE ) {
session . SubmitDenominate ( connman ) ;
return true ;
}
}
return false ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientManager : : MarkAlreadyJoinedQueueAsTried ( CCoinJoinQueue & dsq ) const
2020-07-16 14:23:37 +02:00
{
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2020-07-16 14:23:37 +02:00
LOCK ( cs_deqsessions ) ;
for ( const auto & session : deqSessions ) {
CDeterministicMNCPtr mnMixing ;
if ( session . GetMixingMasternodeInfo ( mnMixing ) & & mnMixing - > collateralOutpoint = = dsq . masternodeOutpoint ) {
dsq . fTried = true ;
return true ;
}
}
return false ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : SubmitDenominate ( CConnman & connman )
2017-05-05 13:26:27 +02:00
{
2021-12-13 06:35:45 +01:00
LOCK2 ( cs_main , mixingWallet . cs_wallet ) ;
2018-09-04 12:54:59 +02:00
2017-05-05 13:26:27 +02:00
std : : string strError ;
2021-01-20 17:38:05 +01:00
std : : vector < CTxDSIn > vecTxDSIn ;
std : : vector < std : : pair < CTxDSIn , CTxOut > > vecPSInOutPairsTmp ;
2018-09-15 12:18:32 +02:00
2021-01-20 17:38:05 +01:00
if ( ! SelectDenominate ( strError , vecTxDSIn ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SubmitDenominate -- SelectDenominate failed, error: %s \n " , strError ) ;
2018-09-15 12:18:32 +02:00
return false ;
}
2018-11-05 10:29:07 +01:00
std : : vector < std : : pair < int , size_t > > vecInputsByRounds ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
for ( int i = 0 ; i < CCoinJoinClientOptions : : GetRounds ( ) + CCoinJoinClientOptions : : GetRandomRounds ( ) ; i + + ) {
2021-01-20 17:38:05 +01:00
if ( PrepareDenominate ( i , i , strError , vecTxDSIn , vecPSInOutPairsTmp , true ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for %d rounds, success \n " , i ) ;
2018-10-15 13:58:47 +02:00
vecInputsByRounds . emplace_back ( i , vecPSInOutPairsTmp . size ( ) ) ;
} else {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for %d rounds, error: %s \n " , i , strError ) ;
2017-05-05 13:26:27 +02:00
}
2018-10-15 13:58:47 +02:00
}
2021-07-30 02:01:02 +02:00
// more inputs first, for equal input count prefer the one with fewer rounds
2018-10-15 13:58:47 +02:00
std : : sort ( vecInputsByRounds . begin ( ) , vecInputsByRounds . end ( ) , [ ] ( const auto & a , const auto & b ) {
return a . second > b . second | | ( a . second = = b . second & & a . first < b . first ) ;
} ) ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " vecInputsByRounds for denom %d \n " , nSessionDenom ) ;
2018-10-15 13:58:47 +02:00
for ( const auto & pair : vecInputsByRounds ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " vecInputsByRounds: rounds: %d, inputs: %d \n " , pair . first , pair . second ) ;
2018-10-15 13:58:47 +02:00
}
int nRounds = vecInputsByRounds . begin ( ) - > first ;
2021-01-20 17:38:05 +01:00
if ( PrepareDenominate ( nRounds , nRounds , strError , vecTxDSIn , vecPSInOutPairsTmp ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for %d rounds, success \n " , nRounds ) ;
2018-10-15 13:58:47 +02:00
return SendDenominate ( vecPSInOutPairsTmp , connman ) ;
2017-05-05 13:26:27 +02:00
}
// We failed? That's strange but let's just make final attempt and try to mix everything
2021-03-17 23:36:11 +01:00
if ( PrepareDenominate ( 0 , CCoinJoinClientOptions : : GetRounds ( ) - 1 , strError , vecTxDSIn , vecPSInOutPairsTmp ) ) {
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for all rounds, success \n " ) ;
2018-09-15 12:18:32 +02:00
return SendDenominate ( vecPSInOutPairsTmp , connman ) ;
2017-05-05 13:26:27 +02:00
}
// Should never actually get here but just in case
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for all rounds, error: %s \n " , strError ) ;
2022-04-06 22:37:20 +02:00
strAutoDenomResult = Untranslated ( strError ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : SelectDenominate ( std : : string & strErrorRet , std : : vector < CTxDSIn > & vecTxDSInRet )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2017-05-05 13:26:27 +02:00
2020-08-28 11:52:00 +02:00
if ( mixingWallet . IsLocked ( true ) ) {
2017-05-05 13:26:27 +02:00
strErrorRet = " Wallet locked, unable to create transaction! " ;
return false ;
}
if ( GetEntriesCount ( ) > 0 ) {
2021-03-17 23:36:11 +01:00
strErrorRet = " Already have pending entries in the CoinJoin pool " ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-01-20 17:38:05 +01:00
vecTxDSInRet . clear ( ) ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
bool fSelected = mixingWallet . SelectTxDSInsByDenomination ( nSessionDenom , CCoinJoin : : GetMaxPoolAmount ( ) , vecTxDSInRet ) ;
2018-09-15 12:18:32 +02:00
if ( ! fSelected ) {
2017-05-05 13:26:27 +02:00
strErrorRet = " Can't select current denominated inputs " ;
return false ;
}
2018-09-15 12:18:32 +02:00
return true ;
}
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : PrepareDenominate ( int nMinRounds , int nMaxRounds , std : : string & strErrorRet , const std : : vector < CTxDSIn > & vecTxDSIn , std : : vector < std : : pair < CTxDSIn , CTxOut > > & vecPSInOutPairsRet , bool fDryRun )
2018-09-15 12:18:32 +02:00
{
2019-11-13 21:03:52 +01:00
AssertLockHeld ( cs_main ) ;
2020-08-28 11:52:00 +02:00
AssertLockHeld ( mixingWallet . cs_wallet ) ;
2019-11-13 21:03:52 +01:00
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoin : : IsValidDenomination ( nSessionDenom ) ) {
2018-09-15 12:18:32 +02:00
strErrorRet = " Incorrect session denom " ;
return false ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
CAmount nDenomAmount = CCoinJoin : : DenominationToAmount ( nSessionDenom ) ;
2017-05-05 13:26:27 +02:00
// NOTE: No need to randomize order of inputs because they were
2021-01-20 17:38:05 +01:00
// initially shuffled in CWallet::SelectTxDSInsByDenomination already.
2022-02-11 17:15:26 +01:00
size_t nSteps { 0 } ;
2018-09-15 12:18:32 +02:00
vecPSInOutPairsRet . clear ( ) ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
// Try to add up to COINJOIN_ENTRY_MAX_SIZE of every needed denomination
2021-01-20 17:38:05 +01:00
for ( const auto & entry : vecTxDSIn ) {
2021-03-17 23:36:11 +01:00
if ( nSteps > = COINJOIN_ENTRY_MAX_SIZE ) break ;
2021-01-20 17:38:05 +01:00
if ( entry . nRounds < nMinRounds | | entry . nRounds > nMaxRounds ) continue ;
2020-03-12 11:31:55 +01:00
CScript scriptDenom ;
if ( fDryRun ) {
scriptDenom = CScript ( ) ;
} else {
// randomly skip some inputs when we have at least one of the same denom already
// TODO: make it adjustable via options/cmd-line params
if ( nSteps > = 1 & & GetRandInt ( 5 ) = = 0 ) {
// still count it as a step to randomize number of inputs
2021-03-17 23:36:11 +01:00
// if we have more than (or exactly) COINJOIN_ENTRY_MAX_SIZE of them
2020-03-12 11:31:55 +01:00
+ + nSteps ;
continue ;
2017-05-05 13:26:27 +02:00
}
2021-04-01 19:28:43 +02:00
const auto pwallet = GetWallet ( mixingWallet . GetName ( ) ) ;
2020-08-28 11:52:00 +02:00
if ( ! pwallet ) {
strErrorRet = " Couldn't get wallet pointer " ;
return false ;
}
2021-04-01 19:28:43 +02:00
scriptDenom = keyHolderStorage . AddKey ( pwallet . get ( ) ) ;
2017-05-05 13:26:27 +02:00
}
2021-01-20 17:38:05 +01:00
vecPSInOutPairsRet . emplace_back ( entry , CTxOut ( nDenomAmount , scriptDenom ) ) ;
2020-03-12 11:31:55 +01:00
// step is complete
+ + nSteps ;
2017-05-05 13:26:27 +02:00
}
2020-03-12 11:31:55 +01:00
if ( vecPSInOutPairsRet . empty ( ) ) {
2017-09-11 16:14:55 +02:00
keyHolderStorage . ReturnAll ( ) ;
2018-09-04 12:54:59 +02:00
strErrorRet = " Can't prepare current denominated outputs " ;
2017-05-05 13:26:27 +02:00
return false ;
}
2019-11-13 21:03:52 +01:00
if ( fDryRun ) {
return true ;
}
for ( const auto & pair : vecPSInOutPairsRet ) {
2020-08-28 11:52:00 +02:00
mixingWallet . LockCoin ( pair . first . prevout ) ;
2019-11-13 21:03:52 +01:00
vecOutPointLocked . push_back ( pair . first . prevout ) ;
}
2017-05-05 13:26:27 +02:00
return true ;
}
// Create collaterals by looping through inputs grouped by addresses
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : MakeCollateralAmounts ( )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2018-07-28 15:50:41 +02:00
2021-12-13 06:35:45 +01:00
LOCK2 ( cs_main , mixingWallet . cs_wallet ) ;
2019-11-13 21:04:12 +01:00
2021-07-30 02:01:02 +02:00
// NOTE: We do not allow txes larger than 100 kB, so we have to limit number of inputs here.
2019-10-17 11:36:18 +02:00
// We still want to consume a lot of inputs to avoid creating only smaller denoms though.
2021-07-30 02:01:02 +02:00
// Knowing that each CTxIn is at least 148 B big, 400 inputs should take 400 x ~148 B = ~60 kB.
2019-10-17 11:36:18 +02:00
// This still leaves more than enough room for another data of typical MakeCollateralAmounts tx.
2022-02-11 17:15:26 +01:00
std : : vector < CompactTallyItem > vecTally = mixingWallet . SelectCoinsGroupedByAddresses ( false , false , true , 400 ) ;
if ( vecTally . empty ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::MakeCollateralAmounts -- SelectCoinsGroupedByAddresses can't find any inputs! \n " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-07-30 02:01:02 +02:00
// Start from the smallest balances first to consume tiny amounts and cleanup UTXO a bit
2018-11-26 14:22:39 +01:00
std : : sort ( vecTally . begin ( ) , vecTally . end ( ) , [ ] ( const CompactTallyItem & a , const CompactTallyItem & b ) {
return a . nAmount < b . nAmount ;
} ) ;
2017-07-10 16:42:32 +02:00
// First try to use only non-denominated funds
2018-02-06 12:09:33 +01:00
for ( const auto & item : vecTally ) {
2020-09-01 17:27:33 +02:00
if ( ! MakeCollateralAmounts ( item , false ) ) continue ;
2017-05-05 13:26:27 +02:00
return true ;
}
2017-07-10 16:42:32 +02:00
// There should be at least some denominated funds we should be able to break in pieces to continue mixing
2018-02-06 12:09:33 +01:00
for ( const auto & item : vecTally ) {
2020-09-01 17:27:33 +02:00
if ( ! MakeCollateralAmounts ( item , true ) ) continue ;
2017-07-10 16:42:32 +02:00
return true ;
}
2020-01-08 08:02:49 +01:00
// If we got here then something is terribly broken actually
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::MakeCollateralAmounts -- ERROR: Can't make collaterals! \n " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
// Split up large inputs or create fee sized inputs
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : MakeCollateralAmounts ( const CompactTallyItem & tallyItem , bool fTryDenominated )
2017-05-05 13:26:27 +02:00
{
2019-11-13 21:04:12 +01:00
AssertLockHeld ( cs_main ) ;
2020-08-28 11:52:00 +02:00
AssertLockHeld ( mixingWallet . cs_wallet ) ;
2018-07-28 15:50:41 +02:00
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2017-07-10 16:42:32 +02:00
2020-09-01 17:27:33 +02:00
// Denominated input is always a single one, so we can check its amount directly and return early
2021-03-17 23:36:11 +01:00
if ( ! fTryDenominated & & tallyItem . vecInputCoins . size ( ) = = 1 & & CCoinJoin : : IsDenominatedAmount ( tallyItem . nAmount ) ) {
2020-03-12 11:32:12 +01:00
return false ;
}
// Skip single inputs that can be used as collaterals already
2021-03-17 23:36:11 +01:00
if ( tallyItem . vecInputCoins . size ( ) = = 1 & & CCoinJoin : : IsCollateralAmount ( tallyItem . nAmount ) ) {
2020-03-12 11:32:12 +01:00
return false ;
}
2021-04-01 19:28:43 +02:00
const auto pwallet = GetWallet ( mixingWallet . GetName ( ) ) ;
2020-08-28 11:52:00 +02:00
if ( ! pwallet ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Couldn't get wallet pointer \n " , __func__ ) ;
2020-08-28 11:52:00 +02:00
return false ;
}
CTransactionBuilder txBuilder ( pwallet , tallyItem ) ;
2020-09-01 17:27:33 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Start %s \n " , __func__ , txBuilder . ToString ( ) ) ;
2020-09-01 17:27:33 +02:00
// Skip way too tiny amounts. Smallest we want is minimum collateral amount in a one output tx
2021-03-17 23:36:11 +01:00
if ( ! txBuilder . CouldAddOutput ( CCoinJoin : : GetCollateralAmount ( ) ) ) {
2017-09-27 19:42:53 +02:00
return false ;
2019-06-18 13:33:05 +02:00
}
2017-09-27 19:42:53 +02:00
2020-09-01 17:27:33 +02:00
int nCase { 0 } ; // Just for debug logs
2021-03-17 23:36:11 +01:00
if ( txBuilder . CouldAddOutputs ( { CCoinJoin : : GetMaxCollateralAmount ( ) , CCoinJoin : : GetCollateralAmount ( ) } ) ) {
2020-09-01 17:27:33 +02:00
nCase = 1 ;
// <case1>, see TransactionRecord::decomposeTransaction
2021-03-17 23:36:11 +01:00
// Out1 == CCoinJoin::GetMaxCollateralAmount()
// Out2 >= CCoinJoin::GetCollateralAmount()
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
txBuilder . AddOutput ( CCoinJoin : : GetMaxCollateralAmount ( ) ) ;
2020-09-01 17:27:33 +02:00
// Note, here we first add a zero amount output to get the remainder after all fees and then assign it
CTransactionBuilderOutput * out = txBuilder . AddOutput ( ) ;
CAmount nAmountLeft = txBuilder . GetAmountLeft ( ) ;
// If remainder is denominated add one duff to the fee
2021-03-17 23:36:11 +01:00
out - > UpdateAmount ( CCoinJoin : : IsDenominatedAmount ( nAmountLeft ) ? nAmountLeft - 1 : nAmountLeft ) ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
} else if ( txBuilder . CouldAddOutputs ( { CCoinJoin : : GetCollateralAmount ( ) , CCoinJoin : : GetCollateralAmount ( ) } ) ) {
2020-09-01 17:27:33 +02:00
nCase = 2 ;
// <case2>, see TransactionRecord::decomposeTransaction
2021-03-17 23:36:11 +01:00
// Out1 CCoinJoin::IsCollateralAmount()
// Out2 CCoinJoin::IsCollateralAmount()
2017-05-05 13:26:27 +02:00
2020-09-01 17:27:33 +02:00
// First add two outputs to get the available value after all fees
CTransactionBuilderOutput * out1 = txBuilder . AddOutput ( ) ;
CTransactionBuilderOutput * out2 = txBuilder . AddOutput ( ) ;
// Create two equal outputs from the available value. This adds one duff to the fee if txBuilder.GetAmountLeft() is odd.
CAmount nAmountOutputs = txBuilder . GetAmountLeft ( ) / 2 ;
2021-03-17 23:36:11 +01:00
assert ( CCoinJoin : : IsCollateralAmount ( nAmountOutputs ) ) ;
2020-09-01 17:27:33 +02:00
out1 - > UpdateAmount ( nAmountOutputs ) ;
out2 - > UpdateAmount ( nAmountOutputs ) ;
2021-03-17 23:36:11 +01:00
} else { // still at least possible to add one CCoinJoin::GetCollateralAmount() output
2020-09-01 17:27:33 +02:00
nCase = 3 ;
// <case3>, see TransactionRecord::decomposeTransaction
2021-03-17 23:36:11 +01:00
// Out1 CCoinJoin::IsCollateralAmount()
2020-09-01 17:27:33 +02:00
// Out2 Skipped
CTransactionBuilderOutput * out = txBuilder . AddOutput ( ) ;
out - > UpdateAmount ( txBuilder . GetAmountLeft ( ) ) ;
2021-03-17 23:36:11 +01:00
assert ( CCoinJoin : : IsCollateralAmount ( out - > GetAmount ( ) ) ) ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Done with case %d: %s \n " , __func__ , nCase , txBuilder . ToString ( ) ) ;
2017-05-05 13:26:27 +02:00
2020-09-01 17:27:33 +02:00
assert ( txBuilder . IsDust ( txBuilder . GetAmountLeft ( ) ) ) ;
2017-05-05 13:26:27 +02:00
2022-04-07 06:43:16 +02:00
bilingual_str strResult ;
2020-09-01 17:27:33 +02:00
if ( ! txBuilder . Commit ( strResult ) ) {
2022-04-07 06:43:16 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Commit failed: %s \n " , __func__ , strResult . original ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-03-17 23:36:11 +01:00
coinJoinClientManagers . at ( mixingWallet . GetName ( ) ) - > UpdatedSuccessBlock ( ) ;
2017-05-05 13:26:27 +02:00
2022-04-07 06:43:16 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- txid: %s \n " , __func__ , strResult . original ) ;
2020-09-01 17:27:33 +02:00
2017-05-05 13:26:27 +02:00
return true ;
}
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : CreateCollateralTransaction ( CMutableTransaction & txCollateral , std : : string & strReason )
2021-01-20 17:38:05 +01:00
{
2018-11-09 15:36:34 +01:00
auto locked_chain = mixingWallet . chain ( ) . lock ( ) ;
LOCK ( mixingWallet . cs_wallet ) ;
2021-01-20 17:38:05 +01:00
std : : vector < COutput > vCoins ;
CCoinControl coin_control ;
2021-03-17 23:36:11 +01:00
coin_control . nCoinType = CoinType : : ONLY_COINJOIN_COLLATERAL ;
2021-01-20 17:38:05 +01:00
2018-11-09 15:36:34 +01:00
mixingWallet . AvailableCoins ( * locked_chain , vCoins , true , & coin_control ) ;
2021-01-20 17:38:05 +01:00
if ( vCoins . empty ( ) ) {
2021-07-11 11:28:50 +02:00
strReason = strprintf ( " %s requires a collateral transaction and could not locate an acceptable input! " , gCoinJoinName ) ;
2021-01-20 17:38:05 +01:00
return false ;
}
const auto & output = vCoins . at ( GetRandInt ( vCoins . size ( ) ) ) ;
const CTxOut txout = output . tx - > tx - > vout [ output . i ] ;
txCollateral . vin . clear ( ) ;
txCollateral . vin . emplace_back ( output . tx - > GetHash ( ) , output . i ) ;
txCollateral . vout . clear ( ) ;
// pay collateral charge in fees
// NOTE: no need for protobump patch here,
2021-03-17 23:36:11 +01:00
// CCoinJoin::IsCollateralAmount in GetCollateralTxDSIn should already take care of this
if ( txout . nValue > = CCoinJoin : : GetCollateralAmount ( ) * 2 ) {
2021-01-20 17:38:05 +01:00
// make our change address
CScript scriptChange ;
CPubKey vchPubKey ;
CReserveKey reservekey ( & mixingWallet ) ;
bool success = reservekey . GetReservedKey ( vchPubKey , true ) ;
assert ( success ) ; // should never fail, as we just unlocked
scriptChange = GetScriptForDestination ( vchPubKey . GetID ( ) ) ;
reservekey . KeepKey ( ) ;
// return change
2021-07-30 02:01:02 +02:00
txCollateral . vout . emplace_back ( txout . nValue - CCoinJoin : : GetCollateralAmount ( ) , scriptChange ) ;
2021-03-17 23:36:11 +01:00
} else { // txout.nValue < CCoinJoin::GetCollateralAmount() * 2
2021-01-20 17:38:05 +01:00
// create dummy data output only and pay everything as a fee
2021-07-30 02:01:02 +02:00
txCollateral . vout . emplace_back ( 0 , CScript ( ) < < OP_RETURN ) ;
2021-01-20 17:38:05 +01:00
}
if ( ! SignSignature ( mixingWallet , txout . scriptPubKey , txCollateral , 0 , txout . nValue , SIGHASH_ALL ) ) {
strReason = " Unable to sign collateral transaction! " ;
return false ;
}
return true ;
}
2017-05-05 13:26:27 +02:00
// Create denominations by looping through inputs grouped by addresses
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : CreateDenominated ( CAmount nBalanceToDenominate )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2018-07-28 15:50:41 +02:00
2021-12-13 06:35:45 +01:00
LOCK2 ( cs_main , mixingWallet . cs_wallet ) ;
2017-05-28 15:50:07 +02:00
2021-07-30 02:01:02 +02:00
// NOTE: We do not allow txes larger than 100 kB, so we have to limit number of inputs here.
2018-11-25 14:27:31 +01:00
// We still want to consume a lot of inputs to avoid creating only smaller denoms though.
2021-07-30 02:01:02 +02:00
// Knowing that each CTxIn is at least 148 B big, 400 inputs should take 400 x ~148 B = ~60 kB.
2018-11-25 14:27:31 +01:00
// This still leaves more than enough room for another data of typical CreateDenominated tx.
2022-02-11 17:15:26 +01:00
std : : vector < CompactTallyItem > vecTally = mixingWallet . SelectCoinsGroupedByAddresses ( true , true , true , 400 ) ;
if ( vecTally . empty ( ) ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::CreateDenominated -- SelectCoinsGroupedByAddresses can't find any inputs! \n " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
2021-07-30 02:01:02 +02:00
// Start from the largest balances first to speed things up by creating txes with larger/largest denoms included
2018-11-26 14:22:39 +01:00
std : : sort ( vecTally . begin ( ) , vecTally . end ( ) , [ ] ( const CompactTallyItem & a , const CompactTallyItem & b ) {
return a . nAmount > b . nAmount ;
} ) ;
2020-08-28 11:52:00 +02:00
bool fCreateMixingCollaterals = ! mixingWallet . HasCollateralInputs ( ) ;
2017-05-05 13:26:27 +02:00
2018-02-06 12:09:33 +01:00
for ( const auto & item : vecTally ) {
2020-09-01 17:27:33 +02:00
if ( ! CreateDenominated ( nBalanceToDenominate , item , fCreateMixingCollaterals ) ) continue ;
2017-05-05 13:26:27 +02:00
return true ;
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::CreateDenominated -- failed! \n " ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
// Create denominations
2021-03-17 23:36:11 +01:00
bool CCoinJoinClientSession : : CreateDenominated ( CAmount nBalanceToDenominate , const CompactTallyItem & tallyItem , bool fCreateMixingCollaterals )
2017-05-05 13:26:27 +02:00
{
2020-05-02 17:49:49 +02:00
AssertLockHeld ( cs_main ) ;
2020-08-28 11:52:00 +02:00
AssertLockHeld ( mixingWallet . cs_wallet ) ;
2020-05-02 17:49:49 +02:00
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return false ;
2018-07-28 15:50:41 +02:00
2020-09-01 17:27:33 +02:00
// denominated input is always a single one, so we can check its amount directly and return early
2021-03-17 23:36:11 +01:00
if ( tallyItem . vecInputCoins . size ( ) = = 1 & & CCoinJoin : : IsDenominatedAmount ( tallyItem . nAmount ) ) {
2020-09-01 17:27:33 +02:00
return false ;
}
2020-07-07 19:31:33 +02:00
2021-04-01 19:28:43 +02:00
const auto pwallet = GetWallet ( mixingWallet . GetName ( ) ) ;
2020-08-28 11:52:00 +02:00
if ( ! pwallet ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Couldn't get wallet pointer \n " , __func__ ) ;
2020-08-28 11:52:00 +02:00
return false ;
}
CTransactionBuilder txBuilder ( pwallet , tallyItem ) ;
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Start %s \n " , __func__ , txBuilder . ToString ( ) ) ;
2017-05-05 13:26:27 +02:00
2017-09-11 16:14:55 +02:00
// ****** Add an output for mixing collaterals ************ /
2017-05-05 13:26:27 +02:00
2021-03-17 23:36:11 +01:00
if ( fCreateMixingCollaterals & & ! txBuilder . AddOutput ( CCoinJoin : : GetMaxCollateralAmount ( ) ) ) {
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Failed to add collateral output \n " , __func__ ) ;
2020-09-01 17:27:33 +02:00
return false ;
2017-05-05 13:26:27 +02:00
}
2017-09-11 16:14:55 +02:00
// ****** Add outputs for denoms ************ /
2017-05-05 13:26:27 +02:00
2019-04-30 15:19:23 +02:00
bool fAddFinal = true ;
2022-01-11 01:57:37 +01:00
auto denoms = CCoinJoin : : GetStandardDenominations ( ) ;
2017-05-05 13:26:27 +02:00
2020-05-15 11:34:12 +02:00
std : : map < CAmount , int > mapDenomCount ;
2022-01-11 01:57:37 +01:00
for ( auto nDenomValue : denoms ) {
2020-08-28 11:52:00 +02:00
mapDenomCount . insert ( std : : pair < CAmount , int > ( nDenomValue , mixingWallet . CountInputsWithAmount ( nDenomValue ) ) ) ;
2020-05-15 11:34:12 +02:00
}
2021-03-17 23:36:11 +01:00
// Will generate outputs for the createdenoms up to coinjoinmaxdenoms per denom
2020-05-15 11:34:12 +02:00
// This works in the way creating PS denoms has traditionally worked, assuming enough funds,
// it will start with the smallest denom then create 11 of those, then go up to the next biggest denom create 11
// and repeat. Previously, once the largest denom was reached, as many would be created were created as possible and
// then any remaining was put into a change address and denominations were created in the same manner a block later.
2021-03-17 23:36:11 +01:00
// Now, in this system, so long as we don't reach COINJOIN_DENOM_OUTPUTS_THRESHOLD outputs the process repeats in
// the same transaction, creating up to nCoinJoinDenomsHardCap per denomination in a single transaction.
2020-05-20 11:23:48 +02:00
2021-03-17 23:36:11 +01:00
while ( txBuilder . CouldAddOutput ( CCoinJoin : : GetSmallestDenomination ( ) ) & & txBuilder . CountOutputs ( ) < COINJOIN_DENOM_OUTPUTS_THRESHOLD ) {
2022-01-11 01:57:37 +01:00
for ( auto it = denoms . rbegin ( ) ; it ! = denoms . rend ( ) ; + + it ) {
2020-05-15 11:34:12 +02:00
CAmount nDenomValue = * it ;
auto currentDenomIt = mapDenomCount . find ( nDenomValue ) ;
int nOutputs = 0 ;
2020-09-01 17:27:33 +02:00
const auto & strFunc = __func__ ;
2020-05-15 11:34:12 +02:00
auto needMoreOutputs = [ & ] ( ) {
2020-09-01 17:27:33 +02:00
if ( txBuilder . CouldAddOutput ( nDenomValue ) ) {
if ( fAddFinal & & nBalanceToDenominate > 0 & & nBalanceToDenominate < nDenomValue ) {
fAddFinal = false ; // add final denom only once, only the smalest possible one
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 1 - FINAL - nDenomValue: %f, nBalanceToDenominate: %f, nOutputs: %d, %s \n " ,
2020-09-01 17:27:33 +02:00
strFunc , ( float ) nDenomValue / COIN , ( float ) nBalanceToDenominate / COIN , nOutputs , txBuilder . ToString ( ) ) ;
return true ;
} else if ( nBalanceToDenominate > = nDenomValue ) {
return true ;
}
2020-05-20 11:23:48 +02:00
}
2020-09-01 17:27:33 +02:00
return false ;
2020-05-15 11:34:12 +02:00
} ;
2021-03-17 23:36:11 +01:00
// add each output up to 11 times or until it can't be added again or until we reach nCoinJoinDenomsGoal
while ( needMoreOutputs ( ) & & nOutputs < = 10 & & currentDenomIt - > second < CCoinJoinClientOptions : : GetDenomsGoal ( ) ) {
2020-09-01 17:27:33 +02:00
// Add output and subtract denomination amount
if ( txBuilder . AddOutput ( nDenomValue ) ) {
+ + nOutputs ;
+ + currentDenomIt - > second ;
nBalanceToDenominate - = nDenomValue ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 1 - nDenomValue: %f, nBalanceToDenominate: %f, nOutputs: %d, %s \n " ,
2020-09-01 17:27:33 +02:00
__func__ , ( float ) nDenomValue / COIN , ( float ) nBalanceToDenominate / COIN , nOutputs , txBuilder . ToString ( ) ) ;
} else {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 1 - Error: AddOutput failed for nDenomValue: %f, nBalanceToDenominate: %f, nOutputs: %d, %s \n " ,
2020-09-01 17:27:33 +02:00
__func__ , ( float ) nDenomValue / COIN , ( float ) nBalanceToDenominate / COIN , nOutputs , txBuilder . ToString ( ) ) ;
return false ;
}
2020-05-15 11:34:12 +02:00
}
2017-05-05 13:26:27 +02:00
2020-09-01 17:27:33 +02:00
if ( txBuilder . GetAmountLeft ( ) = = 0 | | nBalanceToDenominate < = 0 ) break ;
2019-04-30 15:19:23 +02:00
}
2017-05-05 13:26:27 +02:00
2020-05-15 11:34:12 +02:00
bool finished = true ;
2020-05-20 11:23:48 +02:00
for ( const auto it : mapDenomCount ) {
2021-03-17 23:36:11 +01:00
// Check if this specific denom could use another loop, check that there aren't nCoinJoinDenomsGoal of this
2020-05-15 11:34:12 +02:00
// denom and that our nValueLeft/nBalanceToDenominate is enough to create one of these denoms, if so, loop again.
2021-03-17 23:36:11 +01:00
if ( it . second < CCoinJoinClientOptions : : GetDenomsGoal ( ) & & txBuilder . CouldAddOutput ( it . first ) & & nBalanceToDenominate > 0 ) {
2020-05-15 11:34:12 +02:00
finished = false ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 1 - NOT finished - nDenomValue: %f, count: %d, nBalanceToDenominate: %f, %s \n " ,
2020-09-01 17:27:33 +02:00
__func__ , ( float ) it . first / COIN , it . second , ( float ) nBalanceToDenominate / COIN , txBuilder . ToString ( ) ) ;
2020-05-15 11:34:12 +02:00
break ;
}
2021-07-19 12:39:22 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 1 - FINISHED - nDenomValue: %f, count: %d, nBalanceToDenominate: %f, %s \n " ,
2020-09-01 17:27:33 +02:00
__func__ , ( float ) it . first / COIN , it . second , ( float ) nBalanceToDenominate / COIN , txBuilder . ToString ( ) ) ;
2019-04-30 15:19:23 +02:00
}
2017-05-05 13:26:27 +02:00
2020-05-15 11:34:12 +02:00
if ( finished ) break ;
}
2021-03-17 23:36:11 +01:00
// Now that nCoinJoinDenomsGoal worth of each denom have been created or the max number of denoms given the value of the input, do something with the remainder.
if ( txBuilder . CouldAddOutput ( CCoinJoin : : GetSmallestDenomination ( ) ) & & nBalanceToDenominate > = CCoinJoin : : GetSmallestDenomination ( ) & & txBuilder . CountOutputs ( ) < COINJOIN_DENOM_OUTPUTS_THRESHOLD ) {
2022-01-11 01:57:37 +01:00
CAmount nLargestDenomValue = denoms . front ( ) ;
2020-05-15 11:34:12 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 2 - Process remainder: %s \n " , __func__ , txBuilder . ToString ( ) ) ;
2020-09-01 17:27:33 +02:00
auto countPossibleOutputs = [ & ] ( CAmount nAmount ) - > int {
std : : vector < CAmount > vecOutputs ;
while ( true ) {
2021-07-30 02:01:02 +02:00
// Create a potential output
2020-09-01 17:27:33 +02:00
vecOutputs . push_back ( nAmount ) ;
2021-03-17 23:36:11 +01:00
if ( ! txBuilder . CouldAddOutputs ( vecOutputs ) | | txBuilder . CountOutputs ( ) + vecOutputs . size ( ) > COINJOIN_DENOM_OUTPUTS_THRESHOLD ) {
2021-07-30 02:01:02 +02:00
// If it's not possible to add it due to insufficient amount left or total number of outputs exceeds
2021-03-17 23:36:11 +01:00
// COINJOIN_DENOM_OUTPUTS_THRESHOLD drop the output again and stop trying.
2020-09-01 17:27:33 +02:00
vecOutputs . pop_back ( ) ;
break ;
}
}
return static_cast < int > ( vecOutputs . size ( ) ) ;
} ;
2020-05-15 11:34:12 +02:00
// Go big to small
2022-01-11 01:57:37 +01:00
for ( auto nDenomValue : denoms ) {
2020-05-15 11:34:12 +02:00
int nOutputs = 0 ;
2019-04-30 15:19:23 +02:00
2020-05-15 11:34:12 +02:00
// Number of denoms we can create given our denom and the amount of funds we have left
2020-09-01 17:27:33 +02:00
int denomsToCreateValue = countPossibleOutputs ( nDenomValue ) ;
2021-07-17 21:15:21 +02:00
// Prefer overshooting the target balance by larger denoms (hence `+1`) instead of a more
2020-07-08 14:55:08 +02:00
// accurate approximation by many smaller denoms. This is ok because when we get here we
2021-03-17 23:36:11 +01:00
// should have nCoinJoinDenomsGoal of each smaller denom already. Also, without `+1`
// we can end up in a situation when there is already nCoinJoinDenomsHardCap of smaller
2021-07-30 02:01:02 +02:00
// denoms, yet we can't mix the remaining nBalanceToDenominate because it's smaller than
2020-07-08 14:55:08 +02:00
// nDenomValue (and thus denomsToCreateBal == 0), so the target would never get reached
// even when there is enough funds for that.
int denomsToCreateBal = ( nBalanceToDenominate / nDenomValue ) + 1 ;
2020-05-15 11:34:12 +02:00
// Use the smaller value
int denomsToCreate = denomsToCreateValue > denomsToCreateBal ? denomsToCreateBal : denomsToCreateValue ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 2 - nBalanceToDenominate: %f, nDenomValue: %f, denomsToCreateValue: %d, denomsToCreateBal: %d \n " ,
2020-09-01 17:27:33 +02:00
__func__ , ( float ) nBalanceToDenominate / COIN , ( float ) nDenomValue / COIN , denomsToCreateValue , denomsToCreateBal ) ;
2020-05-20 11:23:48 +02:00
auto it = mapDenomCount . find ( nDenomValue ) ;
2020-05-15 11:34:12 +02:00
for ( int i = 0 ; i < denomsToCreate ; i + + ) {
2020-05-20 11:23:48 +02:00
// Never go above the cap unless it's the largest denom
2021-03-17 23:36:11 +01:00
if ( nDenomValue ! = nLargestDenomValue & & it - > second > = CCoinJoinClientOptions : : GetDenomsHardCap ( ) ) break ;
2020-05-15 11:34:12 +02:00
2020-09-01 17:27:33 +02:00
// Increment helpers, add output and subtract denomination amount
if ( txBuilder . AddOutput ( nDenomValue ) ) {
nOutputs + + ;
it - > second + + ;
nBalanceToDenominate - = nDenomValue ;
} else {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 2 - Error: AddOutput failed at %d/%d, %s \n " , __func__ , i + 1 , denomsToCreate , txBuilder . ToString ( ) ) ;
2020-09-01 17:27:33 +02:00
break ;
}
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 2 - nDenomValue: %f, nBalanceToDenominate: %f, nOutputs: %d, %s \n " ,
2020-09-01 17:27:33 +02:00
__func__ , ( float ) nDenomValue / COIN , ( float ) nBalanceToDenominate / COIN , nOutputs , txBuilder . ToString ( ) ) ;
2021-03-17 23:36:11 +01:00
if ( txBuilder . CountOutputs ( ) > = COINJOIN_DENOM_OUTPUTS_THRESHOLD ) break ;
2020-05-15 11:34:12 +02:00
}
2021-03-17 23:36:11 +01:00
if ( txBuilder . CountOutputs ( ) > = COINJOIN_DENOM_OUTPUTS_THRESHOLD ) break ;
2020-05-15 11:34:12 +02:00
}
2019-04-30 15:19:23 +02:00
}
2020-05-15 11:34:12 +02:00
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 3 - nBalanceToDenominate: %f, %s \n " , __func__ , ( float ) nBalanceToDenominate / COIN , txBuilder . ToString ( ) ) ;
2020-05-20 11:23:48 +02:00
for ( const auto it : mapDenomCount ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- 3 - DONE - nDenomValue: %f, count: %d \n " , __func__ , ( float ) it . first / COIN , it . second ) ;
2020-05-20 11:23:48 +02:00
}
2017-05-05 13:26:27 +02:00
2018-09-15 12:19:00 +02:00
// No reasons to create mixing collaterals if we can't create denoms to mix
2020-09-01 17:27:33 +02:00
if ( ( fCreateMixingCollaterals & & txBuilder . CountOutputs ( ) = = 1 ) | | txBuilder . CountOutputs ( ) = = 0 ) {
2017-05-05 13:26:27 +02:00
return false ;
}
2022-04-07 06:43:16 +02:00
bilingual_str strResult ;
2020-09-01 17:27:33 +02:00
if ( ! txBuilder . Commit ( strResult ) ) {
2022-04-07 06:43:16 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- Commit failed: %s \n " , __func__ , strResult . original ) ;
2017-05-05 13:26:27 +02:00
return false ;
}
// use the same nCachedLastSuccessBlock as for DS mixing to prevent race
2021-03-17 23:36:11 +01:00
coinJoinClientManagers . at ( mixingWallet . GetName ( ) ) - > UpdatedSuccessBlock ( ) ;
2020-09-01 17:27:33 +02:00
2022-04-07 06:43:16 +02:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::%s -- txid: %s \n " , __func__ , strResult . original ) ;
2017-05-05 13:26:27 +02:00
return true ;
}
2021-07-30 02:01:02 +02:00
void CCoinJoinClientSession : : RelayIn ( const CCoinJoinEntry & entry , CConnman & connman ) const
2017-05-05 13:26:27 +02:00
{
2018-12-17 15:45:36 +01:00
if ( ! mixingMasternode ) return ;
2017-05-05 13:26:27 +02:00
2018-12-17 15:45:36 +01:00
connman . ForNode ( mixingMasternode - > pdmnState - > addr , [ & entry , & connman ] ( CNode * pnode ) {
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::RelayIn -- found master, relaying message to %s \n " , pnode - > addr . ToString ( ) ) ;
2016-11-25 20:01:56 +01:00
CNetMsgMaker msgMaker ( pnode - > GetSendVersion ( ) ) ;
connman . PushMessage ( pnode , msgMaker . Make ( NetMsgType : : DSVIN , entry ) ) ;
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
return true ;
} ) ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientSession : : SetState ( PoolState nStateNew )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientSession::SetState -- nState: %d, nStateNew: %d \n " , nState , nStateNew ) ;
2017-05-05 13:26:27 +02:00
nState = nStateNew ;
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : UpdatedBlockTip ( const CBlockIndex * pindex )
2017-05-05 13:26:27 +02:00
{
2017-08-25 14:57:05 +02:00
nCachedBlockHeight = pindex - > nHeight ;
2021-03-17 23:36:11 +01:00
LogPrint ( BCLog : : COINJOIN , " CCoinJoinClientManager::UpdatedBlockTip -- nCachedBlockHeight: %d \n " , nCachedBlockHeight ) ;
2017-05-05 13:26:27 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientQueueManager : : DoMaintenance ( )
2020-07-16 14:23:37 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return ;
2020-07-16 14:23:37 +02:00
if ( fMasternodeMode ) return ; // no client-side mixing on masternodes
if ( ! masternodeSync . IsBlockchainSynced ( ) | | ShutdownRequested ( ) ) return ;
CheckQueue ( ) ;
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : DoMaintenance ( CConnman & connman )
2017-05-05 13:26:27 +02:00
{
2021-03-17 23:36:11 +01:00
if ( ! CCoinJoinClientOptions : : IsEnabled ( ) ) return ;
2018-11-05 10:29:07 +01:00
if ( fMasternodeMode ) return ; // no client-side mixing on masternodes
2017-05-05 13:26:27 +02:00
2019-06-18 13:33:05 +02:00
if ( ! masternodeSync . IsBlockchainSynced ( ) | | ShutdownRequested ( ) ) return ;
2017-05-05 13:26:27 +02:00
2020-08-24 17:15:29 +02:00
static int nTick = 0 ;
2021-03-17 23:36:11 +01:00
static int nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN ;
2017-05-05 13:26:27 +02:00
2018-07-16 14:47:37 +02:00
nTick + + ;
2018-09-04 12:54:59 +02:00
CheckTimeout ( ) ;
ProcessPendingDsaRequest ( connman ) ;
2018-11-05 10:29:07 +01:00
if ( nDoAutoNextRun = = nTick ) {
2018-09-04 12:54:59 +02:00
DoAutomaticDenominating ( connman ) ;
2021-03-17 23:36:11 +01:00
nDoAutoNextRun = nTick + COINJOIN_AUTO_TIMEOUT_MIN + GetRandInt ( COINJOIN_AUTO_TIMEOUT_MAX - COINJOIN_AUTO_TIMEOUT_MIN ) ;
2017-05-05 13:26:27 +02:00
}
}
2019-10-09 18:48:53 +02:00
2021-03-17 23:36:11 +01:00
void CCoinJoinClientSession : : GetJsonInfo ( UniValue & obj ) const
2019-10-09 18:48:53 +02:00
{
2020-07-17 01:27:35 +02:00
assert ( obj . isObject ( ) ) ;
2019-10-09 18:48:53 +02:00
if ( mixingMasternode ! = nullptr ) {
assert ( mixingMasternode - > pdmnState ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " protxhash " , mixingMasternode - > proTxHash . ToString ( ) ) ;
obj . pushKV ( " outpoint " , mixingMasternode - > collateralOutpoint . ToStringShort ( ) ) ;
obj . pushKV ( " service " , mixingMasternode - > pdmnState - > addr . ToString ( ) ) ;
2019-10-09 18:48:53 +02:00
}
2021-03-17 23:36:11 +01:00
obj . pushKV ( " denomination " , ValueFromAmount ( CCoinJoin : : DenominationToAmount ( nSessionDenom ) ) ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " state " , GetStateString ( ) ) ;
obj . pushKV ( " entries_count " , GetEntriesCount ( ) ) ;
2019-10-09 18:48:53 +02:00
}
2021-03-17 23:36:11 +01:00
void CCoinJoinClientManager : : GetJsonInfo ( UniValue & obj ) const
2019-10-09 18:48:53 +02:00
{
2020-07-17 01:27:35 +02:00
assert ( obj . isObject ( ) ) ;
2020-07-12 07:52:25 +02:00
obj . pushKV ( " running " , IsMixing ( ) ) ;
2019-10-09 18:48:53 +02:00
UniValue arrSessions ( UniValue : : VARR ) ;
2022-04-25 11:28:37 +02:00
AssertLockNotHeld ( cs_deqsessions ) ;
2021-09-30 20:58:33 +02:00
LOCK ( cs_deqsessions ) ;
2019-10-09 18:48:53 +02:00
for ( const auto & session : deqSessions ) {
if ( session . GetState ( ) ! = POOL_STATE_IDLE ) {
UniValue objSession ( UniValue : : VOBJ ) ;
session . GetJsonInfo ( objSession ) ;
arrSessions . push_back ( objSession ) ;
}
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " sessions " , arrSessions ) ;
2019-10-09 18:48:53 +02:00
}
2020-07-16 14:23:37 +02:00
2021-03-17 23:36:11 +01:00
void DoCoinJoinMaintenance ( CConnman & connman )
2020-07-16 14:23:37 +02:00
{
2021-03-17 23:36:11 +01:00
coinJoinClientQueueManager . DoMaintenance ( ) ;
2021-07-30 02:01:02 +02:00
for ( const auto & pair : coinJoinClientManagers ) {
2020-07-16 14:23:58 +02:00
pair . second - > DoMaintenance ( connman ) ;
}
2020-07-16 14:23:37 +02:00
}
2020-07-17 02:25:11 +02:00