Skip to content
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
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ BITCOIN_CORE_H = \
util/memory.h \
util/moneystr.h \
util/ranges.h \
util/ref.h \
util/serfloat.h \
util/settings.h \
util/string.h \
Expand All @@ -317,6 +318,7 @@ BITCOIN_CORE_H = \
walletinitinterface.h \
wallet/bdb.h \
wallet/coincontrol.h \
wallet/context.h \
wallet/crypter.h \
wallet/db.h \
wallet/fees.h \
Expand Down Expand Up @@ -480,6 +482,7 @@ libdash_wallet_a_SOURCES = \
interfaces/wallet.cpp \
wallet/bdb.cpp \
wallet/coincontrol.cpp \
wallet/context.cpp \
wallet/crypter.cpp \
wallet/db.cpp \
wallet/fees.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ BITCOIN_TESTS =\
test/raii_event_tests.cpp \
test/random_tests.cpp \
test/ratecheck_tests.cpp \
test/ref_tests.cpp \
test/reverselock_tests.cpp \
test/rpc_tests.cpp \
test/sanity_tests.cpp \
Expand Down Expand Up @@ -228,6 +229,7 @@ BITCOIN_TESTS =\
test/uint256_tests.cpp \
test/util_tests.cpp \
test/validation_block_tests.cpp \
test/validation_chainstatemanager_tests.cpp \
test/validation_flush_tests.cpp \
test/versionbits_tests.cpp

Expand Down
19 changes: 7 additions & 12 deletions src/coinjoin/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ bool CCoinJoinClientSession::SignFinalTransaction(const CTransaction& finalTrans
if (fMasternodeMode || pnode == nullptr) return false;
if (!mixingMasternode) return false;

LOCK(cs_main);
LOCK(mixingWallet.cs_wallet);
LOCK(cs_coinjoin);

Expand Down Expand Up @@ -749,7 +748,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
CAmount nBalanceNeedsAnonymized;

{
LOCK2(cs_main, mixingWallet.cs_wallet);
LOCK(mixingWallet.cs_wallet);

if (!fDryRun && mixingWallet.IsLocked(true)) {
strAutoDenomResult = _("Wallet is locked.");
Expand Down Expand Up @@ -900,7 +899,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
mixingWallet.LockCoin(txin.prevout);
vecOutPointLocked.push_back(txin.prevout);
}
} // LOCK2(cs_main, mixingWallet.cs_wallet);
} // LOCK(mixingWallet.cs_wallet);

// Always attempt to join an existing queue
if (JoinExistingQueue(nBalanceNeedsAnonymized, connman)) {
Expand Down Expand Up @@ -1211,7 +1210,7 @@ bool CCoinJoinClientManager::MarkAlreadyJoinedQueueAsTried(CCoinJoinQueue& dsq)

bool CCoinJoinClientSession::SubmitDenominate(CConnman& connman)
{
LOCK2(cs_main, mixingWallet.cs_wallet);
LOCK(mixingWallet.cs_wallet);

std::string strError;
std::vector<CTxDSIn> vecTxDSIn;
Expand Down Expand Up @@ -1288,7 +1287,6 @@ bool CCoinJoinClientSession::SelectDenominate(std::string& strErrorRet, std::vec

bool CCoinJoinClientSession::PrepareDenominate(int nMinRounds, int nMaxRounds, std::string& strErrorRet, const std::vector<CTxDSIn>& vecTxDSIn, std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsRet, bool fDryRun)
{
AssertLockHeld(cs_main);
AssertLockHeld(mixingWallet.cs_wallet);

if (!CCoinJoin::IsValidDenomination(nSessionDenom)) {
Expand Down Expand Up @@ -1354,7 +1352,7 @@ bool CCoinJoinClientSession::MakeCollateralAmounts()
{
if (!CCoinJoinClientOptions::IsEnabled()) return false;

LOCK2(cs_main, mixingWallet.cs_wallet);
LOCK(mixingWallet.cs_wallet);

// NOTE: We do not allow txes larger than 100 kB, so we have to limit number of inputs here.
// We still want to consume a lot of inputs to avoid creating only smaller denoms though.
Expand Down Expand Up @@ -1391,7 +1389,6 @@ bool CCoinJoinClientSession::MakeCollateralAmounts()
// Split up large inputs or create fee sized inputs
bool CCoinJoinClientSession::MakeCollateralAmounts(const CompactTallyItem& tallyItem, bool fTryDenominated)
{
AssertLockHeld(cs_main);
AssertLockHeld(mixingWallet.cs_wallet);

if (!CCoinJoinClientOptions::IsEnabled()) return false;
Expand Down Expand Up @@ -1484,14 +1481,13 @@ bool CCoinJoinClientSession::MakeCollateralAmounts(const CompactTallyItem& tally

bool CCoinJoinClientSession::CreateCollateralTransaction(CMutableTransaction& txCollateral, std::string& strReason)
{
auto locked_chain = mixingWallet.chain().lock();
LOCK(mixingWallet.cs_wallet);
AssertLockHeld(mixingWallet.cs_wallet);

std::vector<COutput> vCoins;
CCoinControl coin_control;
coin_control.nCoinType = CoinType::ONLY_COINJOIN_COLLATERAL;

mixingWallet.AvailableCoins(*locked_chain, vCoins, true, &coin_control);
mixingWallet.AvailableCoins(vCoins, true, &coin_control);

if (vCoins.empty()) {
strReason = strprintf("%s requires a collateral transaction and could not locate an acceptable input!", gCoinJoinName);
Expand Down Expand Up @@ -1537,7 +1533,7 @@ bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate)
{
if (!CCoinJoinClientOptions::IsEnabled()) return false;

LOCK2(cs_main, mixingWallet.cs_wallet);
LOCK(mixingWallet.cs_wallet);

// NOTE: We do not allow txes larger than 100 kB, so we have to limit number of inputs here.
// We still want to consume a lot of inputs to avoid creating only smaller denoms though.
Expand Down Expand Up @@ -1568,7 +1564,6 @@ bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate)
// Create denominations
bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate, const CompactTallyItem& tallyItem, bool fCreateMixingCollaterals)
{
AssertLockHeld(cs_main);
AssertLockHeld(mixingWallet.cs_wallet);

if (!CCoinJoinClientOptions::IsEnabled()) return false;
Expand Down
4 changes: 1 addition & 3 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ std::string CCoinJoinBaseSession::GetStateString() const

bool CCoinJoinBaseSession::IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const
{
AssertLockHeld(cs_main);

std::set<CScript> setScripPubKeys;
nMessageIDRet = MSG_NOERR;
if (fConsumeCollateralRet) *fConsumeCollateralRet = false;
Expand Down Expand Up @@ -262,7 +260,7 @@ bool CCoinJoinBaseSession::IsValidInOuts(const std::vector<CTxIn>& vin, const st
nFees -= txout.nValue;
}

CCoinsViewMemPool viewMemPool(&::ChainstateActive().CoinsTip(), mempool);
CCoinsViewMemPool viewMemPool(WITH_LOCK(cs_main, return &::ChainstateActive().CoinsTip()), mempool);

for (const auto& txin : vin) {
LogPrint(BCLog::COINJOIN, "CCoinJoinBaseSession::%s -- txin=%s\n", __func__, txin.ToString());
Expand Down
4 changes: 1 addition & 3 deletions src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class CConnman;
class CBLSPublicKey;
class CBlockIndex;

extern CCriticalSection cs_main;

// timeouts
static constexpr int COINJOIN_AUTO_TIMEOUT_MIN = 5;
static constexpr int COINJOIN_AUTO_TIMEOUT_MAX = 15;
Expand Down Expand Up @@ -321,7 +319,7 @@ class CCoinJoinBaseSession

void SetNull() EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin);

bool IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const;

public:
int nSessionDenom{0}; // Users must submit a denom matching this
Expand Down
2 changes: 0 additions & 2 deletions src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,6 @@ bool CCoinJoinServer::AddEntry(CConnman& connman, const CCoinJoinEntry& entry, P
vin.emplace_back(txin);
}

LOCK(cs_main);

bool fConsumeCollateral{false};
if (!IsValidInOuts(vin, entry.vecTxOut, nMessageIDRet, &fConsumeCollateral)) {
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR! IsValidInOuts() failed: %s\n", __func__, CCoinJoin::GetMessageByID(nMessageIDRet).translated);
Expand Down
12 changes: 9 additions & 3 deletions src/coinjoin/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,11 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
}

CTransactionRef tx;
if (!pwallet->CreateTransaction(*pwallet->chain().lock(), vecSend, tx, nFeeRet, nChangePosRet, strResult, coinControl)) {
return false;
{
LOCK2(pwallet->cs_wallet, cs_main);
if (!pwallet->CreateTransaction(vecSend, tx, nFeeRet, nChangePosRet, strResult, coinControl)) {
return false;
}
}

CAmount nAmountLeft = GetAmountLeft();
Expand Down Expand Up @@ -306,7 +309,10 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
return false;
}

pwallet->CommitTransaction(tx, {}, {});
{
LOCK2(pwallet->cs_wallet, cs_main);
pwallet->CommitTransaction(tx, {}, {});
}

fKeepKeys = true;

Expand Down
4 changes: 3 additions & 1 deletion src/dashd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <noui.h>
#include <shutdown.h>
#include <ui_interface.h>
#include <util/ref.h>
#include <util/system.h>
#include <util/strencodings.h>
#include <util/threadnames.h>
Expand Down Expand Up @@ -83,6 +84,7 @@ static bool AppInit(int argc, char* argv[])
return true;
}

util::Ref context{node};
try
{
bool datadirFromCmdLine = gArgs.IsArgSet("-datadir");
Expand Down Expand Up @@ -158,7 +160,7 @@ static bool AppInit(int argc, char* argv[])
// If locking the data directory failed, exit immediately
return false;
}
fRet = AppInitMain(node);
fRet = AppInitMain(context, node);
} catch (...) {
PrintExceptionContinue(std::current_exception(), "AppInit()");
}
Expand Down
1 change: 0 additions & 1 deletion src/dsnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

void CDSNotificationInterface::InitializeCurrentBlockTip()
{
LOCK(cs_main);
SynchronousUpdatedBlockTip(::ChainActive().Tip(), nullptr, ::ChainstateActive().IsInitialBlockDownload());
UpdatedBlockTip(::ChainActive().Tip(), nullptr, ::ChainstateActive().IsInitialBlockDownload());
}
Expand Down
12 changes: 8 additions & 4 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void CGovernanceManager::ProcessMessage(CNode* pfrom, const std::string& msg_typ
return;
}

LOCK2(cs_main, cs);
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
LOCK(cs);

if (mapObjects.count(nHash) || mapPostponedObjects.count(nHash) || mapErasedGovernanceObjects.count(nHash)) {
// TODO - print error code? what if it's GOVOBJ_ERROR_IMMATURE?
Expand Down Expand Up @@ -262,7 +263,8 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman

govobj.UpdateSentinelVariables(); //this sets local vars in object

LOCK2(cs_main, cs);
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
LOCK(cs);
std::string strError;

// MAKE SURE THIS OBJECT IS OK
Expand Down Expand Up @@ -321,7 +323,8 @@ void CGovernanceManager::UpdateCachesAndClean()

std::vector<uint256> vecDirtyHashes = mmetaman.GetAndClearDirtyGovernanceObjectHashes();

LOCK2(cs_main, cs);
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
LOCK(cs);

for (const uint256& nHash : vecDirtyHashes) {
auto it = mapObjects.find(nHash);
Expand Down Expand Up @@ -836,7 +839,8 @@ void CGovernanceManager::CheckPostponedObjects(CConnman& connman)
{
if (!masternodeSync.IsSynced()) return;

LOCK2(cs_main, cs);
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
LOCK(cs);

// Check postponed proposals
for (auto it = mapPostponedObjects.begin(); it != mapPostponedObjects.end();) {
Expand Down
5 changes: 4 additions & 1 deletion src/governance/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ UniValue CGovernanceObject::ToJson() const

void CGovernanceObject::UpdateLocalValidity()
{
LOCK(cs_main);
AssertLockHeld(cs_main);
// THIS DOES NOT CHECK COLLATERAL, THIS IS CHECKED UPON ORIGINAL ARRIVAL
fCachedLocalValidity = IsValidLocally(strLocalValidityError, false);
}
Expand Down Expand Up @@ -526,6 +526,9 @@ CAmount CGovernanceObject::GetMinCollateralFee(bool fork_active) const

bool CGovernanceObject::IsCollateralValid(std::string& strError, bool& fMissingConfirmations) const
{
AssertLockHeld(cs_main);
AssertLockHeld(::mempool.cs); // because of GetTransaction

strError = "";
fMissingConfirmations = false;
uint256 nExpectedHash = GetHash();
Expand Down
11 changes: 6 additions & 5 deletions src/httprpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
return multiUserAuthorized(strUserPass);
}

static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
static bool HTTPReq_JSONRPC(const util::Ref& context, HTTPRequest* req)
{
// JSONRPC handles only POST
if (req->GetRequestMethod() != HTTPRequest::POST) {
Expand All @@ -160,7 +160,7 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
return false;
}

JSONRPCRequest jreq;
JSONRPCRequest jreq(context);
jreq.peerAddr = req->GetPeer().ToString();
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
Expand Down Expand Up @@ -234,15 +234,16 @@ static bool InitRPCAuthentication()
return true;
}

bool StartHTTPRPC()
bool StartHTTPRPC(const util::Ref& context)
{
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
if (!InitRPCAuthentication())
return false;

RegisterHTTPHandler("/", true, HTTPReq_JSONRPC);
auto handle_rpc = [&context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
RegisterHTTPHandler("/", true, handle_rpc);
if (g_wallet_init_interface.HasWalletSupport()) {
RegisterHTTPHandler("/wallet/", false, HTTPReq_JSONRPC);
RegisterHTTPHandler("/wallet/", false, handle_rpc);
}
struct event_base* eventBase = EventBase();
assert(eventBase);
Expand Down
7 changes: 5 additions & 2 deletions src/httprpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
#ifndef BITCOIN_HTTPRPC_H
#define BITCOIN_HTTPRPC_H

namespace util {
class Ref;
} // namespace util

/** Start HTTP RPC subsystem.
* Precondition; HTTP and RPC has been started.
*/
bool StartHTTPRPC();
bool StartHTTPRPC(const util::Ref& context);
/** Interrupt HTTP RPC subsystem.
*/
void InterruptHTTPRPC();
Expand All @@ -21,7 +24,7 @@ void StopHTTPRPC();
/** Start HTTP REST subsystem.
* Precondition; HTTP and RPC has been started.
*/
void StartREST();
void StartREST(const util::Ref& context);
/** Interrupt RPC REST subsystem.
*/
void InterruptREST();
Expand Down
Loading