Skip to content

Commit

Permalink
net: Pass CConnection to wallet rather than using the global
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed Aug 25, 2020
1 parent 00591b8 commit 7762b97
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4228,7 +4228,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const CBlock* pblock

// If turned on Auto Combine will scan wallet for dust to combine
if (pwalletMain->fCombineDust)
pwalletMain->AutoCombineDust();
pwalletMain->AutoCombineDust(connman);
}

LogPrintf("%s : ACCEPTED Block %ld in %ld milliseconds with size=%d\n", __func__, GetHeight(), GetTimeMillis() - nStartTime,
Expand Down Expand Up @@ -6258,7 +6258,7 @@ bool SendMessages(CNode* pto, CConnman& connman)
// Except during reindex, importing and IBD, when old wallet
// transactions become unconfirmed and spams other nodes.
if (!fReindex && !fImporting && !IsInitialBlockDownload()) {
GetMainSignals().Broadcast();
GetMainSignals().Broadcast(&connman);
}

//
Expand Down
2 changes: 1 addition & 1 deletion src/masternode-budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void CBudgetManager::SubmitFinalBudget()
// Get our change address
CReserveKey reservekey(pwalletMain);
// Send the tx to the network. Do NOT use SwiftTx, locking might need too much time to propagate, especially for testnet
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, reservekey, "NO-ix");
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, reservekey, g_connman.get(), "NO-ix");
if (res.status != CWallet::CommitStatus::OK)
return;
tx = (CTransaction)wtx;
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction& tran
}

CReserveKey* keyChange = transaction.getPossibleKeyChange();
const CWallet::CommitResult& res = wallet->CommitTransaction(*newTx, *keyChange, (recipients[0].useSwiftTX) ? NetMsgType::IX : NetMsgType::TX);
const CWallet::CommitResult& res = wallet->CommitTransaction(*newTx, *keyChange, g_connman.get(), (recipients[0].useSwiftTX) ? NetMsgType::IX : NetMsgType::TX);
if (res.status != CWallet::CommitStatus::OK) {
return SendCoinsReturn(res);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ UniValue preparebudget(const JSONRPCRequest& request)
// make our change address
CReserveKey reservekey(pwalletMain);
//send the tx to the network
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, reservekey, useIX ? NetMsgType::IX : NetMsgType::TX);
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, reservekey, g_connman.get(), useIX ? NetMsgType::IX : NetMsgType::TX);
if (res.status != CWallet::CommitStatus::OK)
throw JSONRPCError(RPC_WALLET_ERROR, res.ToString());

Expand Down
4 changes: 2 additions & 2 deletions src/validationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
g_signals.UpdatedTransaction.connect(boost::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, _1));
g_signals.SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
g_signals.Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
g_signals.Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn));
g_signals.Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1));
g_signals.BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
// XX42 g_signals.ScriptForMining.connect(boost::bind(&CValidationInterface::GetScriptForMining, pwalletIn, _1));
g_signals.BlockFound.connect(boost::bind(&CValidationInterface::ResetRequestCount, pwalletIn, _1));
Expand All @@ -31,7 +31,7 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
g_signals.BlockFound.disconnect(boost::bind(&CValidationInterface::ResetRequestCount, pwalletIn, _1));
// XX42 g_signals.ScriptForMining.disconnect(boost::bind(&CValidationInterface::GetScriptForMining, pwalletIn, _1));
g_signals.BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
g_signals.Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn));
g_signals.Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1));
g_signals.Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
g_signals.SetBestChain.disconnect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
g_signals.UpdatedTransaction.disconnect(boost::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, _1));
Expand Down
7 changes: 3 additions & 4 deletions src/validationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class CBlock;
struct CBlockLocator;
class CBlockIndex;
class CConnman;
class CReserveScript;
class CTransaction;
class CValidationInterface;
Expand All @@ -37,8 +38,7 @@ class CValidationInterface {
virtual void SetBestChain(const CBlockLocator &locator) {}
virtual bool UpdatedTransaction(const uint256 &hash) { return false;}
virtual void Inventory(const uint256 &hash) {}
// XX42 virtual void ResendWalletTransactions(int64_t nBestBlockTime) {}
virtual void ResendWalletTransactions() {}
virtual void ResendWalletTransactions(CConnman* connman) {}
virtual void BlockChecked(const CBlock&, const CValidationState&) {}
// XX42 virtual void GetScriptForMining(boost::shared_ptr<CReserveScript>&) {};
virtual void ResetRequestCount(const uint256 &hash) {};
Expand All @@ -64,8 +64,7 @@ struct CMainSignals {
/** Notifies listeners about an inventory item being seen on the network. */
boost::signals2::signal<void (const uint256 &)> Inventory;
/** Tells listeners to broadcast their data. */
// XX42 boost::signals2::signal<void (int64_t nBestBlockTime)> Broadcast;
boost::signals2::signal<void ()> Broadcast;
boost::signals2::signal<void (CConnman* connman)> Broadcast;
/** Notifies listeners of a block validation result */
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
/** Notifies listeners that a key for mining is required (coinbase) */
Expand Down
12 changes: 9 additions & 3 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,9 @@ void SendMoney(const CTxDestination& address, CAmount nValue, CWalletTx& wtxNew,
if (nValue > pwalletMain->GetBalance())
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds");

if (!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

std::string strError;
if (pwalletMain->IsLocked()) {
strError = "Error: Wallet locked, unable to create transaction!";
Expand All @@ -959,7 +962,7 @@ void SendMoney(const CTxDestination& address, CAmount nValue, CWalletTx& wtxNew,
LogPrintf("SendMoney() : %s\n", strError);
throw JSONRPCError(RPC_WALLET_ERROR, strError);
}
const CWallet::CommitResult&& res = pwalletMain->CommitTransaction(wtxNew, reservekey, (!fUseIX ? NetMsgType::TX : NetMsgType::IX));
const CWallet::CommitResult&& res = pwalletMain->CommitTransaction(wtxNew, reservekey, g_connman.get(), (!fUseIX ? NetMsgType::TX : NetMsgType::IX));
if (res.status != CWallet::CommitStatus::OK)
throw JSONRPCError(RPC_WALLET_ERROR, res.ToString());
}
Expand Down Expand Up @@ -1143,7 +1146,7 @@ UniValue delegatestake(const JSONRPCRequest& request)
CReserveKey reservekey(pwalletMain);
UniValue ret = CreateColdStakeDelegation(request.params, wtx, reservekey);

const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, reservekey, NetMsgType::TX);
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, reservekey, g_connman.get(), NetMsgType::TX);
if (res.status != CWallet::CommitStatus::OK)
throw JSONRPCError(RPC_WALLET_ERROR, res.ToString());

Expand Down Expand Up @@ -1852,6 +1855,9 @@ UniValue sendmany(const JSONRPCRequest& request)

LOCK2(cs_main, pwalletMain->cs_wallet);

if (!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

if (!IsDeprecatedRPCEnabled("accounts") && !request.params[0].get_str().empty()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Dummy value must be set to \"\"");
}
Expand Down Expand Up @@ -1909,7 +1915,7 @@ UniValue sendmany(const JSONRPCRequest& request)
bool fCreated = pwalletMain->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, nChangePosInOut, strFailReason);
if (!fCreated)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, keyChange);
const CWallet::CommitResult& res = pwalletMain->CommitTransaction(wtx, keyChange, g_connman.get());
if (res.status != CWallet::CommitStatus::OK)
throw JSONRPCError(RPC_WALLET_ERROR, res.ToString());

Expand Down
14 changes: 7 additions & 7 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ bool CWalletTx::InMempool() const
return false;
}

void CWalletTx::RelayWalletTransaction(std::string strCommand)
void CWalletTx::RelayWalletTransaction(CConnman* connman, std::string strCommand)
{
LOCK(cs_main);
if (!IsCoinBase() && !IsCoinStake()) {
Expand Down Expand Up @@ -1733,7 +1733,7 @@ bool CWallet::Verify()



void CWallet::ResendWalletTransactions()
void CWallet::ResendWalletTransactions(CConnman* connman)
{
// Do this infrequently and randomly to avoid giving away
// that these are our transactions.
Expand Down Expand Up @@ -1764,7 +1764,7 @@ void CWallet::ResendWalletTransactions()
}
for (PAIRTYPE(const unsigned int, CWalletTx*) & item : mapSorted) {
CWalletTx& wtx = *item.second;
wtx.RelayWalletTransaction();
wtx.RelayWalletTransaction(connman);
}
}
}
Expand Down Expand Up @@ -2957,7 +2957,7 @@ std::string CWallet::CommitResult::ToString() const
/**
* Call after CreateTransaction unless you want to abort
*/
CWallet::CommitResult CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, std::string strCommand)
CWallet::CommitResult CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, CConnman* connman, std::string strCommand)
{
CommitResult res;
{
Expand Down Expand Up @@ -3009,7 +3009,7 @@ CWallet::CommitResult CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey&
mapRequestCount[res.hashTx] = 0;

// Broadcast
wtxNew.RelayWalletTransaction(strCommand);
wtxNew.RelayWalletTransaction(connman, strCommand);
}
return res;
}
Expand Down Expand Up @@ -3605,7 +3605,7 @@ bool CWallet::LoadDestData(const CTxDestination& dest, const std::string& key, c
return true;
}

void CWallet::AutoCombineDust()
void CWallet::AutoCombineDust(CConnman* connman)
{
LOCK2(cs_main, cs_wallet);
const CBlockIndex* tip = chainActive.Tip();
Expand Down Expand Up @@ -3695,7 +3695,7 @@ void CWallet::AutoCombineDust()
if (!maxSize && nTotalRewardsValue < nAutoCombineThreshold * COIN && nFeeRet > 0)
continue;

const CWallet::CommitResult& res = CommitTransaction(wtx, keyChange);
const CWallet::CommitResult& res = CommitTransaction(wtx, keyChange, connman);
if (res.status != CWallet::CommitStatus::OK) {
LogPrintf("AutoCombineDust transaction commit failed\n");
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false, bool fromStartup = false);
void ReacceptWalletTransactions(bool fFirstLoad = false);
void ResendWalletTransactions();
void ResendWalletTransactions(CConnman* connman);

CAmount loopTxsBalance(std::function<void(const uint256&, const CWalletTx&, CAmount&)>method) const;
CAmount GetBalance(bool fIncludeDelegated = true) const;
Expand Down Expand Up @@ -567,11 +567,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
// converts CommitResult in human-readable format
std::string ToString() const;
};
CWallet::CommitResult CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, std::string strCommand = NetMsgType::TX);
CWallet::CommitResult CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, CConnman* connman, std::string strCommand = NetMsgType::TX);
bool AddAccountingEntry(const CAccountingEntry&, CWalletDB & pwalletdb);
bool CreateCoinStake(const CKeyStore& keystore, const CBlockIndex* pindexPrev, unsigned int nBits, CMutableTransaction& txNew, int64_t& nTxNewTime);
bool MultiSend();
void AutoCombineDust();
void AutoCombineDust(CConnman* connman);

static CFeeRate minTxFee;
/**
Expand Down Expand Up @@ -992,7 +992,7 @@ class CWalletTx : public CMerkleTx
int64_t GetTxTime() const;
void UpdateTimeSmart();
int GetRequestCount() const;
void RelayWalletTransaction(std::string strCommand = NetMsgType::TX);
void RelayWalletTransaction(CConnman* connman, std::string strCommand = NetMsgType::TX);
std::set<uint256> GetConflicts() const;
};

Expand Down
4 changes: 2 additions & 2 deletions src/wallet/wallet_zerocoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ std::string CWallet::MintZerocoin(CAmount nValue, CWalletTx& wtxNew, std::vector
}

//commit the transaction to the network
const CWallet::CommitResult& res = CommitTransaction(wtxNew, reservekey);
const CWallet::CommitResult& res = CommitTransaction(wtxNew, reservekey, g_connman.get());
if (res.status != CWallet::CommitStatus::OK) {
return res.ToString();
} else {
Expand Down Expand Up @@ -398,7 +398,7 @@ bool CWallet::SpendZerocoin(CAmount nAmount, CWalletTx& wtxNew, CZerocoinSpendRe


CWalletDB walletdb(strWalletFile);
const CWallet::CommitResult& res = CommitTransaction(wtxNew, reserveKey);
const CWallet::CommitResult& res = CommitTransaction(wtxNew, reserveKey, g_connman.get());
if (res.status != CWallet::CommitStatus::OK) {
LogPrintf("%s: failed to commit\n", __func__);
nStatus = ZPIV_COMMIT_FAILED;
Expand Down

0 comments on commit 7762b97

Please sign in to comment.