Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Fix locks" Pack #710

Merged
merged 4 commits into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ BITCOIN_CORE_H = \
consensus/validation.h \
core_io.h \
darksend.h \
dsnotificationinterface.h \
darksend-relay.h \
core_memusage.h \
hash.h \
Expand Down Expand Up @@ -249,6 +250,7 @@ libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_wallet_a_SOURCES = \
activemasternode.cpp \
darksend.cpp \
dsnotificationinterface.cpp \
darksend-relay.cpp \
instantx.cpp \
masternode.cpp \
Expand Down
109 changes: 60 additions & 49 deletions src/darksend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ std::string CDarksendPool::GetStatus()
showingDarkSendMessage += 10;
std::string suffix = "";

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

CWalletTx txNew = CWalletTx(pwalletMain, finalTransaction);

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

{
LOCK(cs_main);
// See if the transaction is valid
if (!txNew.AcceptToMemoryPool(false, true))
{
Expand All @@ -585,58 +585,57 @@ void CDarksendPool::CheckFinalTransaction()
RelayCompletedTransaction(sessionID, true, ERR_INVALID_TX);
return;
}
}
LogPrintf("CDarksendPool::Check() -- IS MASTER -- TRANSMITTING DARKSEND\n");

LogPrintf("CDarksendPool::Check() -- IS MASTER -- TRANSMITTING DARKSEND\n");

// sign a message
// sign a message

int64_t sigTime = GetAdjustedTime();
std::string strMessage = txNew.GetHash().ToString() + boost::lexical_cast<std::string>(sigTime);
std::string strError = "";
std::vector<unsigned char> vchSig;
CKey key2;
CPubKey pubkey2;
int64_t sigTime = GetAdjustedTime();
std::string strMessage = txNew.GetHash().ToString() + boost::lexical_cast<std::string>(sigTime);
std::string strError = "";
std::vector<unsigned char> vchSig;
CKey key2;
CPubKey pubkey2;

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

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

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

if(!mapDarksendBroadcastTxes.count(txNew.GetHash())){
CDarksendBroadcastTx dstx;
dstx.tx = txNew;
dstx.vin = activeMasternode.vin;
dstx.vchSig = vchSig;
dstx.sigTime = sigTime;
if(!mapDarksendBroadcastTxes.count(txNew.GetHash())){
CDarksendBroadcastTx dstx;
dstx.tx = txNew;
dstx.vin = activeMasternode.vin;
dstx.vchSig = vchSig;
dstx.sigTime = sigTime;

mapDarksendBroadcastTxes.insert(make_pair(txNew.GetHash(), dstx));
}
mapDarksendBroadcastTxes.insert(make_pair(txNew.GetHash(), dstx));
}

CInv inv(MSG_DSTX, txNew.GetHash());
RelayInv(inv);
CInv inv(MSG_DSTX, txNew.GetHash());
RelayInv(inv);

// Tell the clients it was successful
RelayCompletedTransaction(sessionID, false, MSG_SUCCESS);
// Tell the clients it was successful
RelayCompletedTransaction(sessionID, false, MSG_SUCCESS);

// Randomly charge clients
ChargeRandomFees();
// Randomly charge clients
ChargeRandomFees();

// Reset
LogPrint("darksend", "CDarksendPool::Check() -- COMPLETED -- RESETTING\n");
SetNull();
RelayStatus(sessionID, GetState(), GetEntriesCount(), MASTERNODE_RESET);
}
// Reset
LogPrint("darksend", "CDarksendPool::Check() -- COMPLETED -- RESETTING\n");
SetNull();
RelayStatus(sessionID, GetState(), GetEntriesCount(), MASTERNODE_RESET);
}

//
Expand Down Expand Up @@ -1342,7 +1341,7 @@ void CDarksendPool::CompletedTransaction(bool error, int errorID)
SetNull();

// To avoid race conditions, we'll only let DS run once per block
cachedLastSuccess = chainActive.Tip()->nHeight;
cachedLastSuccess = pCurrentBlockIndex->nHeight;
}
lastMessage = GetMessageByID(errorID);
}
Expand All @@ -1361,6 +1360,9 @@ bool CDarksendPool::DoAutomaticDenominating(bool fDryRun)
{
if(!fEnableDarksend) return false;
if(fMasterNode) return false;

if(!pCurrentBlockIndex) return false;

if(state == POOL_STATUS_ERROR || state == POOL_STATUS_SUCCESS) return false;
if(GetEntriesCount() > 0) {
strAutoDenomResult = _("Mixing in progress...");
Expand All @@ -1383,7 +1385,7 @@ bool CDarksendPool::DoAutomaticDenominating(bool fDryRun)
return false;
}

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

cachedLastSuccess = chainActive.Tip()->nHeight;
cachedLastSuccess = pCurrentBlockIndex->nHeight;

return true;
}
Expand Down Expand Up @@ -1795,7 +1797,7 @@ bool CDarksendPool::CreateDenominated(CAmount nTotalValue)

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

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

void CDarksendPool::UpdatedBlockTip(const CBlockIndex *pindex)
{
pCurrentBlockIndex = pindex;
LogPrint("darksend", "pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);

if(!fLiteMode && masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST)
NewBlock();
}

//TODO: Rename/move to core
void ThreadCheckDarkSendPool()
{
Expand Down
5 changes: 5 additions & 0 deletions src/darksend.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ class CDarksendPool

int64_t lastNewBlock;

// Keep track of current block index
const CBlockIndex *pCurrentBlockIndex;

std::vector<CAmount> darkSendDenominationsSkipped;

//debugging data
Expand Down Expand Up @@ -535,6 +538,8 @@ class CDarksendPool
void RelayIn(const std::vector<CTxDSIn>& vin, const CAmount& nAmount, const CTransaction& txCollateral, const std::vector<CTxDSOut>& vout);
void RelayStatus(const int sessionID, const int newState, const int newEntriesCount, const int newAccepted, const int errorID=MSG_NOERR);
void RelayCompletedTransaction(const int sessionID, const bool error, const int errorID);

void UpdatedBlockTip(const CBlockIndex *pindex);
};

void ThreadCheckDarkSendPool();
Expand Down
25 changes: 25 additions & 0 deletions src/dsnotificationinterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2015 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "dsnotificationinterface.h"
#include "darksend.h"
#include "masternode-budget.h"
#include "masternode-payments.h"
#include "masternode-sync.h"

CDSNotificationInterface::CDSNotificationInterface()
{
}

CDSNotificationInterface::~CDSNotificationInterface()
{
}

void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindex)
{
darkSendPool.UpdatedBlockTip(pindex);
mnpayments.UpdatedBlockTip(pindex);
budget.UpdatedBlockTip(pindex);
masternodeSync.UpdatedBlockTip(pindex);
}
24 changes: 24 additions & 0 deletions src/dsnotificationinterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_DSNOTIFICATIONINTERFACE_H
#define BITCOIN_DSNOTIFICATIONINTERFACE_H

#include "validationinterface.h"

class CDSNotificationInterface : public CValidationInterface
{
public:
// virtual CDSNotificationInterface();
CDSNotificationInterface();
virtual ~CDSNotificationInterface();

protected:
// CValidationInterface
void UpdatedBlockTip(const CBlockIndex *pindex);

private:
};

#endif // BITCOIN_DSNOTIFICATIONINTERFACE_H
18 changes: 18 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
#include "zmq/zmqnotificationinterface.h"
#endif

#include "dsnotificationinterface.h"

using namespace std;

#ifdef ENABLE_WALLET
Expand All @@ -87,6 +89,8 @@ static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
static CZMQNotificationInterface* pzmqNotificationInterface = NULL;
#endif

static CDSNotificationInterface* pdsNotificationInterface = NULL;

#ifdef WIN32
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
// accessing block files don't count towards the fd_set size limit
Expand Down Expand Up @@ -254,6 +258,12 @@ void PrepareShutdown()
}
#endif

if (pdsNotificationInterface) {
UnregisterValidationInterface(pdsNotificationInterface);
delete pdsNotificationInterface;
pdsNotificationInterface = NULL;
}

#ifndef WIN32
try {
boost::filesystem::remove(GetPidFile());
Expand Down Expand Up @@ -1428,6 +1438,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
RegisterValidationInterface(pzmqNotificationInterface);
}
#endif

pdsNotificationInterface = new CDSNotificationInterface();
RegisterValidationInterface(pdsNotificationInterface);

if (mapArgs.count("-maxuploadtarget")) {
CNode::SetMaxOutboundTarget(GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET)*1024*1024);
}
Expand Down Expand Up @@ -1938,6 +1952,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
darkSendPool.InitDenominations();
darkSendPool.InitCollateralAddress();

// force UpdatedBlockTip to initialize pCurrentBlockIndex for DS, MN payments and budgets
GetMainSignals().UpdatedBlockTip(chainActive.Tip());

// start dash-darksend thread
threadGroup.create_thread(boost::bind(&ThreadCheckDarkSendPool));

// ********************************************************* Step 11: start node
Expand Down
11 changes: 7 additions & 4 deletions src/instantx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,13 @@ int64_t CreateNewLock(CTransaction tx)
This prevents attackers from using transaction mallibility to predict which masternodes
they'll use.
*/
int nBlockHeight = (chainActive.Tip()->nHeight - nTxAge)+4;
int nBlockHeight = 0;
{
LOCK(cs_main);
CBlockIndex* tip = chainActive.Tip();
if(tip) nBlockHeight = tip->nHeight - nTxAge + 4;
else return 0;
}

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

void CleanTransactionLocksList()
{
if(chainActive.Tip() == NULL) return;

std::map<uint256, CTransactionLock>::iterator it = mapTxLocks.begin();

while(it != mapTxLocks.end()) {
Expand All @@ -460,7 +464,6 @@ void CleanTransactionLocksList()
it++;
}
}

}

uint256 CConsensusVote::GetHash() const
Expand Down
8 changes: 0 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3527,14 +3527,6 @@ bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, c
if (!ActivateBestChain(state, chainparams, pblock))
return error("%s: ActivateBestChain failed", __func__);

if(!fLiteMode){
if (masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST) {
darkSendPool.NewBlock();
mnpayments.ProcessBlock(GetHeight()+10);
budget.NewBlock();
}
}

LogPrintf("%s : ACCEPTED\n", __func__);
return true;
}
Expand Down
Loading