Skip to content

Commit 20c10c9

Browse files
author
evan82
committed
Merge pull request #710 from UdjinM6/locks
"Fix locks" Pack
2 parents 3ad675a + e8df74a commit 20c10c9

23 files changed

+402
-241
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ BITCOIN_CORE_H = \
106106
consensus/validation.h \
107107
core_io.h \
108108
darksend.h \
109+
dsnotificationinterface.h \
109110
darksend-relay.h \
110111
core_memusage.h \
111112
hash.h \
@@ -249,6 +250,7 @@ libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
249250
libbitcoin_wallet_a_SOURCES = \
250251
activemasternode.cpp \
251252
darksend.cpp \
253+
dsnotificationinterface.cpp \
252254
darksend-relay.cpp \
253255
instantx.cpp \
254256
masternode.cpp \

src/darksend.cpp

+60-49
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ std::string CDarksendPool::GetStatus()
450450
showingDarkSendMessage += 10;
451451
std::string suffix = "";
452452

453-
if(chainActive.Tip()->nHeight - cachedLastSuccess < minBlockSpacing || !masternodeSync.IsBlockchainSynced()) {
453+
if((pCurrentBlockIndex && pCurrentBlockIndex->nHeight - cachedLastSuccess < minBlockSpacing) || !masternodeSync.IsBlockchainSynced()) {
454454
return strAutoDenomResult;
455455
}
456456
switch(state) {
@@ -570,10 +570,10 @@ void CDarksendPool::CheckFinalTransaction()
570570

571571
CWalletTx txNew = CWalletTx(pwalletMain, finalTransaction);
572572

573-
LOCK2(cs_main, pwalletMain->cs_wallet);
574-
{
575-
LogPrint("darksend", "Transaction 2: %s\n", txNew.ToString());
573+
LogPrint("darksend", "Transaction 2: %s\n", txNew.ToString());
576574

575+
{
576+
LOCK(cs_main);
577577
// See if the transaction is valid
578578
if (!txNew.AcceptToMemoryPool(false, true))
579579
{
@@ -585,58 +585,57 @@ void CDarksendPool::CheckFinalTransaction()
585585
RelayCompletedTransaction(sessionID, true, ERR_INVALID_TX);
586586
return;
587587
}
588+
}
589+
LogPrintf("CDarksendPool::Check() -- IS MASTER -- TRANSMITTING DARKSEND\n");
588590

589-
LogPrintf("CDarksendPool::Check() -- IS MASTER -- TRANSMITTING DARKSEND\n");
590-
591-
// sign a message
591+
// sign a message
592592

593-
int64_t sigTime = GetAdjustedTime();
594-
std::string strMessage = txNew.GetHash().ToString() + boost::lexical_cast<std::string>(sigTime);
595-
std::string strError = "";
596-
std::vector<unsigned char> vchSig;
597-
CKey key2;
598-
CPubKey pubkey2;
593+
int64_t sigTime = GetAdjustedTime();
594+
std::string strMessage = txNew.GetHash().ToString() + boost::lexical_cast<std::string>(sigTime);
595+
std::string strError = "";
596+
std::vector<unsigned char> vchSig;
597+
CKey key2;
598+
CPubKey pubkey2;
599599

600-
if(!darkSendSigner.SetKey(strMasterNodePrivKey, strError, key2, pubkey2))
601-
{
602-
LogPrintf("CDarksendPool::Check() - ERROR: Invalid Masternodeprivkey: '%s'\n", strError);
603-
return;
604-
}
600+
if(!darkSendSigner.SetKey(strMasterNodePrivKey, strError, key2, pubkey2))
601+
{
602+
LogPrintf("CDarksendPool::Check() - ERROR: Invalid Masternodeprivkey: '%s'\n", strError);
603+
return;
604+
}
605605

606-
if(!darkSendSigner.SignMessage(strMessage, strError, vchSig, key2)) {
607-
LogPrintf("CDarksendPool::Check() - Sign message failed\n");
608-
return;
609-
}
606+
if(!darkSendSigner.SignMessage(strMessage, strError, vchSig, key2)) {
607+
LogPrintf("CDarksendPool::Check() - Sign message failed\n");
608+
return;
609+
}
610610

611-
if(!darkSendSigner.VerifyMessage(pubkey2, vchSig, strMessage, strError)) {
612-
LogPrintf("CDarksendPool::Check() - Verify message failed\n");
613-
return;
614-
}
611+
if(!darkSendSigner.VerifyMessage(pubkey2, vchSig, strMessage, strError)) {
612+
LogPrintf("CDarksendPool::Check() - Verify message failed\n");
613+
return;
614+
}
615615

616-
if(!mapDarksendBroadcastTxes.count(txNew.GetHash())){
617-
CDarksendBroadcastTx dstx;
618-
dstx.tx = txNew;
619-
dstx.vin = activeMasternode.vin;
620-
dstx.vchSig = vchSig;
621-
dstx.sigTime = sigTime;
616+
if(!mapDarksendBroadcastTxes.count(txNew.GetHash())){
617+
CDarksendBroadcastTx dstx;
618+
dstx.tx = txNew;
619+
dstx.vin = activeMasternode.vin;
620+
dstx.vchSig = vchSig;
621+
dstx.sigTime = sigTime;
622622

623-
mapDarksendBroadcastTxes.insert(make_pair(txNew.GetHash(), dstx));
624-
}
623+
mapDarksendBroadcastTxes.insert(make_pair(txNew.GetHash(), dstx));
624+
}
625625

626-
CInv inv(MSG_DSTX, txNew.GetHash());
627-
RelayInv(inv);
626+
CInv inv(MSG_DSTX, txNew.GetHash());
627+
RelayInv(inv);
628628

629-
// Tell the clients it was successful
630-
RelayCompletedTransaction(sessionID, false, MSG_SUCCESS);
629+
// Tell the clients it was successful
630+
RelayCompletedTransaction(sessionID, false, MSG_SUCCESS);
631631

632-
// Randomly charge clients
633-
ChargeRandomFees();
632+
// Randomly charge clients
633+
ChargeRandomFees();
634634

635-
// Reset
636-
LogPrint("darksend", "CDarksendPool::Check() -- COMPLETED -- RESETTING\n");
637-
SetNull();
638-
RelayStatus(sessionID, GetState(), GetEntriesCount(), MASTERNODE_RESET);
639-
}
635+
// Reset
636+
LogPrint("darksend", "CDarksendPool::Check() -- COMPLETED -- RESETTING\n");
637+
SetNull();
638+
RelayStatus(sessionID, GetState(), GetEntriesCount(), MASTERNODE_RESET);
640639
}
641640

642641
//
@@ -1342,7 +1341,7 @@ void CDarksendPool::CompletedTransaction(bool error, int errorID)
13421341
SetNull();
13431342

13441343
// To avoid race conditions, we'll only let DS run once per block
1345-
cachedLastSuccess = chainActive.Tip()->nHeight;
1344+
cachedLastSuccess = pCurrentBlockIndex->nHeight;
13461345
}
13471346
lastMessage = GetMessageByID(errorID);
13481347
}
@@ -1361,6 +1360,9 @@ bool CDarksendPool::DoAutomaticDenominating(bool fDryRun)
13611360
{
13621361
if(!fEnableDarksend) return false;
13631362
if(fMasterNode) return false;
1363+
1364+
if(!pCurrentBlockIndex) return false;
1365+
13641366
if(state == POOL_STATUS_ERROR || state == POOL_STATUS_SUCCESS) return false;
13651367
if(GetEntriesCount() > 0) {
13661368
strAutoDenomResult = _("Mixing in progress...");
@@ -1383,7 +1385,7 @@ bool CDarksendPool::DoAutomaticDenominating(bool fDryRun)
13831385
return false;
13841386
}
13851387

1386-
if(!fDarksendMultiSession && chainActive.Tip()->nHeight - cachedLastSuccess < minBlockSpacing) {
1388+
if(!fDarksendMultiSession && pCurrentBlockIndex->nHeight - cachedLastSuccess < minBlockSpacing) {
13871389
LogPrintf("CDarksendPool::DoAutomaticDenominating - Last successful Darksend action was too recent\n");
13881390
strAutoDenomResult = _("Last successful Darksend action was too recent.");
13891391
return false;
@@ -1704,7 +1706,7 @@ bool CDarksendPool::MakeCollateralAmounts()
17041706
return false;
17051707
}
17061708

1707-
cachedLastSuccess = chainActive.Tip()->nHeight;
1709+
cachedLastSuccess = pCurrentBlockIndex->nHeight;
17081710

17091711
return true;
17101712
}
@@ -1795,7 +1797,7 @@ bool CDarksendPool::CreateDenominated(CAmount nTotalValue)
17951797

17961798
// use the same cachedLastSuccess as for DS mixinx to prevent race
17971799
if(pwalletMain->CommitTransaction(wtx, reservekeyChange))
1798-
cachedLastSuccess = chainActive.Tip()->nHeight;
1800+
cachedLastSuccess = pCurrentBlockIndex->nHeight;
17991801
else
18001802
LogPrintf("CreateDenominated: CommitTransaction failed!\n");
18011803

@@ -2198,6 +2200,15 @@ void CDarksendPool::RelayCompletedTransaction(const int sessionID, const bool er
21982200
pnode->PushMessage(NetMsgType::DSSTATUSUPDATE, sessionID, error, errorID);
21992201
}
22002202

2203+
void CDarksendPool::UpdatedBlockTip(const CBlockIndex *pindex)
2204+
{
2205+
pCurrentBlockIndex = pindex;
2206+
LogPrint("darksend", "pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
2207+
2208+
if(!fLiteMode && masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST)
2209+
NewBlock();
2210+
}
2211+
22012212
//TODO: Rename/move to core
22022213
void ThreadCheckDarkSendPool()
22032214
{

src/darksend.h

+5
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ class CDarksendPool
289289

290290
int64_t lastNewBlock;
291291

292+
// Keep track of current block index
293+
const CBlockIndex *pCurrentBlockIndex;
294+
292295
std::vector<CAmount> darkSendDenominationsSkipped;
293296

294297
//debugging data
@@ -535,6 +538,8 @@ class CDarksendPool
535538
void RelayIn(const std::vector<CTxDSIn>& vin, const CAmount& nAmount, const CTransaction& txCollateral, const std::vector<CTxDSOut>& vout);
536539
void RelayStatus(const int sessionID, const int newState, const int newEntriesCount, const int newAccepted, const int errorID=MSG_NOERR);
537540
void RelayCompletedTransaction(const int sessionID, const bool error, const int errorID);
541+
542+
void UpdatedBlockTip(const CBlockIndex *pindex);
538543
};
539544

540545
void ThreadCheckDarkSendPool();

src/dsnotificationinterface.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2015 The Dash Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "dsnotificationinterface.h"
6+
#include "darksend.h"
7+
#include "masternode-budget.h"
8+
#include "masternode-payments.h"
9+
#include "masternode-sync.h"
10+
11+
CDSNotificationInterface::CDSNotificationInterface()
12+
{
13+
}
14+
15+
CDSNotificationInterface::~CDSNotificationInterface()
16+
{
17+
}
18+
19+
void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindex)
20+
{
21+
darkSendPool.UpdatedBlockTip(pindex);
22+
mnpayments.UpdatedBlockTip(pindex);
23+
budget.UpdatedBlockTip(pindex);
24+
masternodeSync.UpdatedBlockTip(pindex);
25+
}

src/dsnotificationinterface.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2015 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_DSNOTIFICATIONINTERFACE_H
6+
#define BITCOIN_DSNOTIFICATIONINTERFACE_H
7+
8+
#include "validationinterface.h"
9+
10+
class CDSNotificationInterface : public CValidationInterface
11+
{
12+
public:
13+
// virtual CDSNotificationInterface();
14+
CDSNotificationInterface();
15+
virtual ~CDSNotificationInterface();
16+
17+
protected:
18+
// CValidationInterface
19+
void UpdatedBlockTip(const CBlockIndex *pindex);
20+
21+
private:
22+
};
23+
24+
#endif // BITCOIN_DSNOTIFICATIONINTERFACE_H

src/init.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
#include "zmq/zmqnotificationinterface.h"
7171
#endif
7272

73+
#include "dsnotificationinterface.h"
74+
7375
using namespace std;
7476

7577
#ifdef ENABLE_WALLET
@@ -87,6 +89,8 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
8789
static CZMQNotificationInterface* pzmqNotificationInterface = NULL;
8890
#endif
8991

92+
static CDSNotificationInterface* pdsNotificationInterface = NULL;
93+
9094
#ifdef WIN32
9195
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
9296
// accessing block files don't count towards the fd_set size limit
@@ -254,6 +258,12 @@ void PrepareShutdown()
254258
}
255259
#endif
256260

261+
if (pdsNotificationInterface) {
262+
UnregisterValidationInterface(pdsNotificationInterface);
263+
delete pdsNotificationInterface;
264+
pdsNotificationInterface = NULL;
265+
}
266+
257267
#ifndef WIN32
258268
try {
259269
boost::filesystem::remove(GetPidFile());
@@ -1428,6 +1438,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14281438
RegisterValidationInterface(pzmqNotificationInterface);
14291439
}
14301440
#endif
1441+
1442+
pdsNotificationInterface = new CDSNotificationInterface();
1443+
RegisterValidationInterface(pdsNotificationInterface);
1444+
14311445
if (mapArgs.count("-maxuploadtarget")) {
14321446
CNode::SetMaxOutboundTarget(GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET)*1024*1024);
14331447
}
@@ -1938,6 +1952,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
19381952
darkSendPool.InitDenominations();
19391953
darkSendPool.InitCollateralAddress();
19401954

1955+
// force UpdatedBlockTip to initialize pCurrentBlockIndex for DS, MN payments and budgets
1956+
GetMainSignals().UpdatedBlockTip(chainActive.Tip());
1957+
1958+
// start dash-darksend thread
19411959
threadGroup.create_thread(boost::bind(&ThreadCheckDarkSendPool));
19421960

19431961
// ********************************************************* Step 11: start node

src/instantx.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,13 @@ int64_t CreateNewLock(CTransaction tx)
235235
This prevents attackers from using transaction mallibility to predict which masternodes
236236
they'll use.
237237
*/
238-
int nBlockHeight = (chainActive.Tip()->nHeight - nTxAge)+4;
238+
int nBlockHeight = 0;
239+
{
240+
LOCK(cs_main);
241+
CBlockIndex* tip = chainActive.Tip();
242+
if(tip) nBlockHeight = tip->nHeight - nTxAge + 4;
243+
else return 0;
244+
}
239245

240246
if (!mapTxLocks.count(tx.GetHash())){
241247
LogPrintf("CreateNewLock - New Transaction Lock %s !\n", tx.GetHash().ToString().c_str());
@@ -434,8 +440,6 @@ int64_t GetAverageVoteTime()
434440

435441
void CleanTransactionLocksList()
436442
{
437-
if(chainActive.Tip() == NULL) return;
438-
439443
std::map<uint256, CTransactionLock>::iterator it = mapTxLocks.begin();
440444

441445
while(it != mapTxLocks.end()) {
@@ -460,7 +464,6 @@ void CleanTransactionLocksList()
460464
it++;
461465
}
462466
}
463-
464467
}
465468

466469
uint256 CConsensusVote::GetHash() const

src/main.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -3527,14 +3527,6 @@ bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, c
35273527
if (!ActivateBestChain(state, chainparams, pblock))
35283528
return error("%s: ActivateBestChain failed", __func__);
35293529

3530-
if(!fLiteMode){
3531-
if (masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST) {
3532-
darkSendPool.NewBlock();
3533-
mnpayments.ProcessBlock(GetHeight()+10);
3534-
budget.NewBlock();
3535-
}
3536-
}
3537-
35383530
LogPrintf("%s : ACCEPTED\n", __func__);
35393531
return true;
35403532
}

0 commit comments

Comments
 (0)