Skip to content

Commit 5e06330

Browse files
committed
Removed IsFinalTx() cs_main lock requirement.
1 parent 1bd97ca commit 5e06330

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

src/consensus/tx_verify.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212

1313
bool IsFinalTx(const CTransactionRef& tx, int nBlockHeight, int64_t nBlockTime)
1414
{
15-
AssertLockHeld(cs_main);
1615
// Time based nLockTime implemented in 0.1.6
1716
if (tx->nLockTime == 0)
1817
return true;
19-
if (nBlockHeight == 0)
20-
nBlockHeight = chainActive.Height();
2118
if (nBlockTime == 0)
2219
nBlockTime = GetAdjustedTime();
2320
if ((int64_t)tx->nLockTime < ((int64_t)tx->nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))

src/consensus/tx_verify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& ma
3939
* Check if transaction is final and can be included in a block with the
4040
* specified height and time. Consensus critical.
4141
*/
42-
bool IsFinalTx(const CTransactionRef& tx, int nBlockHeight = 0, int64_t nBlockTime = 0);
42+
bool IsFinalTx(const CTransactionRef& tx, int nBlockHeight, int64_t nBlockTime = 0);
4343

4444
#endif // BITCOIN_CONSENSUS_TX_VERIFY_H

src/wallet/rpcwallet.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,7 @@ UniValue getreceivedbyaddress(const JSONRPCRequest& request)
18951895
pwalletMain->BlockUntilSyncedToCurrentChain();
18961896

18971897
LOCK2(cs_main, pwalletMain->cs_wallet);
1898+
int nBlockHeight = chainActive.Height();
18981899

18991900
// pivx address
19001901
CTxDestination address = DecodeDestination(request.params[0].get_str());
@@ -1913,7 +1914,7 @@ UniValue getreceivedbyaddress(const JSONRPCRequest& request)
19131914
CAmount nAmount = 0;
19141915
for (std::map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) {
19151916
const CWalletTx& wtx = (*it).second;
1916-
if (wtx.IsCoinBase() || !IsFinalTx(wtx.tx))
1917+
if (wtx.IsCoinBase() || !IsFinalTx(wtx.tx, nBlockHeight))
19171918
continue;
19181919

19191920
for (const CTxOut& txout : wtx.tx->vout)
@@ -1955,6 +1956,7 @@ UniValue getreceivedbylabel(const JSONRPCRequest& request)
19551956
pwalletMain->BlockUntilSyncedToCurrentChain();
19561957

19571958
LOCK2(cs_main, pwalletMain->cs_wallet);
1959+
int nBlockHeight = chainActive.Height();
19581960

19591961
// Minimum confirmations
19601962
int nMinDepth = 1;
@@ -1969,7 +1971,7 @@ UniValue getreceivedbylabel(const JSONRPCRequest& request)
19691971
CAmount nAmount = 0;
19701972
for (std::map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) {
19711973
const CWalletTx& wtx = (*it).second;
1972-
if (wtx.IsCoinBase() || !IsFinalTx(wtx.tx))
1974+
if (wtx.IsCoinBase() || !IsFinalTx(wtx.tx, nBlockHeight))
19731975
continue;
19741976

19751977
for (const CTxOut& txout : wtx.tx->vout) {
@@ -2302,7 +2304,7 @@ struct tallyitem {
23022304
}
23032305
};
23042306

2305-
UniValue ListReceived(const UniValue& params, bool by_label)
2307+
UniValue ListReceived(const UniValue& params, bool by_label, int nBlockHeight)
23062308
{
23072309
// Minimum confirmations
23082310
int nMinDepth = 1;
@@ -2335,7 +2337,7 @@ UniValue ListReceived(const UniValue& params, bool by_label)
23352337
for (std::map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) {
23362338
const CWalletTx& wtx = (*it).second;
23372339

2338-
if (wtx.IsCoinBase() || !IsFinalTx(wtx.tx))
2340+
if (wtx.IsCoinBase() || !IsFinalTx(wtx.tx, nBlockHeight))
23392341
continue;
23402342

23412343
int nDepth = wtx.GetDepthInMainChain();
@@ -2477,8 +2479,8 @@ UniValue listreceivedbyaddress(const JSONRPCRequest& request)
24772479
pwalletMain->BlockUntilSyncedToCurrentChain();
24782480

24792481
LOCK2(cs_main, pwalletMain->cs_wallet);
2480-
2481-
return ListReceived(request.params, false);
2482+
int nBlockHeight = chainActive.Height();
2483+
return ListReceived(request.params, false, nBlockHeight);
24822484
}
24832485

24842486
UniValue listreceivedbyshieldaddress(const JSONRPCRequest& request)
@@ -2613,8 +2615,8 @@ UniValue listreceivedbylabel(const JSONRPCRequest& request)
26132615
pwalletMain->BlockUntilSyncedToCurrentChain();
26142616

26152617
LOCK2(cs_main, pwalletMain->cs_wallet);
2616-
2617-
return ListReceived(request.params, true);
2618+
int nBlockHeight = chainActive.Height();
2619+
return ListReceived(request.params, true, nBlockHeight);
26182620
}
26192621

26202622
UniValue listcoldutxos(const JSONRPCRequest& request)

src/wallet/wallet.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ CAmount CWallet::GetLegacyBalance(const isminefilter& filter, int minDepth) cons
21912191
const CWalletTx& wtx = entry.second;
21922192
bool fConflicted;
21932193
const int depth = wtx.GetDepthAndMempool(fConflicted);
2194-
if (!IsFinalTx(wtx.tx) || wtx.GetBlocksToMaturity() > 0 || depth < 0 || fConflicted) {
2194+
if (!IsFinalTx(wtx.tx, m_last_block_processed_height) || wtx.GetBlocksToMaturity() > 0 || depth < 0 || fConflicted) {
21952195
continue;
21962196
}
21972197

@@ -3480,7 +3480,7 @@ std::map<CTxDestination, CAmount> CWallet::GetAddressBalances()
34803480
for (std::pair<uint256, CWalletTx> walletEntry : mapWallet) {
34813481
CWalletTx* pcoin = &walletEntry.second;
34823482

3483-
if (!IsFinalTx(pcoin->tx) || !pcoin->IsTrusted())
3483+
if (!IsFinalTx(pcoin->tx, m_last_block_processed_height) || !pcoin->IsTrusted())
34843484
continue;
34853485

34863486
if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
@@ -4626,9 +4626,12 @@ bool CWalletTx::IsTrusted() const
46264626

46274627
bool CWalletTx::IsTrusted(int& nDepth, bool& fConflicted) const
46284628
{
4629-
// Quick answer in most cases
4630-
if (!IsFinalTx(tx))
4631-
return false;
4629+
{
4630+
LOCK(pwallet->cs_wallet); // future: receive block height instead of locking here.
4631+
// Quick answer in most cases
4632+
if (!IsFinalTx(tx, pwallet->GetLastBlockHeight()))
4633+
return false;
4634+
}
46324635

46334636
nDepth = GetDepthAndMempool(fConflicted);
46344637

0 commit comments

Comments
 (0)