Skip to content

Commit b855592

Browse files
author
Antoine Riard
committed
[wallet] Move getHeight from Chain::Lock interface to simple Chain
Instead of calling getHeight, we rely on CWallet::m_last_block processed_height where it's possible.
1 parent 0f204dd commit b855592

File tree

8 files changed

+27
-30
lines changed

8 files changed

+27
-30
lines changed

src/interfaces/chain.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<Rec
5555

5656
class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex>
5757
{
58-
Optional<int> getHeight() override
59-
{
60-
LockAssertion lock(::cs_main);
61-
int height = ::ChainActive().Height();
62-
if (height >= 0) {
63-
return height;
64-
}
65-
return nullopt;
66-
}
6758
Optional<int> getBlockHeight(const uint256& hash) override
6859
{
6960
LockAssertion lock(::cs_main);
@@ -234,6 +225,15 @@ class ChainImpl : public Chain
234225
std::unique_ptr<Chain::Lock> result = std::move(lock); // Temporary to avoid CWG 1579
235226
return result;
236227
}
228+
Optional<int> getHeight() override
229+
{
230+
LOCK(::cs_main);
231+
int height = ::ChainActive().Height();
232+
if (height >= 0) {
233+
return height;
234+
}
235+
return nullopt;
236+
}
237237
bool findBlock(const uint256& hash, const FoundBlock& block) override
238238
{
239239
WAIT_LOCK(cs_main, lock);

src/interfaces/chain.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ class Chain
8787
public:
8888
virtual ~Lock() {}
8989

90-
//! Get current chain height, not including genesis block (returns 0 if
91-
//! chain only contains genesis block, nullopt if chain does not contain
92-
//! any blocks).
93-
virtual Optional<int> getHeight() = 0;
94-
9590
//! Get block height above genesis block. Returns 0 for genesis block,
9691
//! 1 for following block, and so on. Returns nullopt for a block not
9792
//! included in the current chain.
@@ -135,6 +130,11 @@ class Chain
135130
//! unlocked when the returned interface is freed.
136131
virtual std::unique_ptr<Lock> lock(bool try_lock = false) = 0;
137132

133+
//! Get current chain height, not including genesis block (returns 0 if
134+
//! chain only contains genesis block, nullopt if chain does not contain
135+
//! any blocks)
136+
virtual Optional<int> getHeight() = 0;
137+
138138
//! Return whether node has the block and optionally return block metadata
139139
//! or contents.
140140
virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;

src/interfaces/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class WalletImpl : public Wallet
228228
auto locked_chain = m_wallet->chain().lock();
229229
LOCK(m_wallet->cs_wallet);
230230
CTransactionRef tx;
231-
if (!m_wallet->CreateTransaction(*locked_chain, recipients, tx, fee, change_pos,
231+
if (!m_wallet->CreateTransaction(recipients, tx, fee, change_pos,
232232
fail_reason, coin_control, sign)) {
233233
return {};
234234
}
@@ -335,7 +335,7 @@ class WalletImpl : public Wallet
335335
LOCK(m_wallet->cs_wallet);
336336
auto mi = m_wallet->mapWallet.find(txid);
337337
if (mi != m_wallet->mapWallet.end()) {
338-
num_blocks = locked_chain->getHeight().get_value_or(-1);
338+
num_blocks = m_wallet->GetLastBlockHeight();
339339
in_mempool = mi->second.InMempool();
340340
order_form = mi->second.vOrderForm;
341341
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);

src/wallet/feebumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCo
219219
CAmount fee_ret;
220220
int change_pos_in_out = -1; // No requested location for change
221221
std::string fail_reason;
222-
if (!wallet.CreateTransaction(*locked_chain, recipients, tx_new, fee_ret, change_pos_in_out, fail_reason, new_coin_control, false)) {
222+
if (!wallet.CreateTransaction(recipients, tx_new, fee_ret, change_pos_in_out, fail_reason, new_coin_control, false)) {
223223
errors.push_back("Unable to create transaction: " + fail_reason);
224224
return Result::WALLET_ERROR;
225225
}

src/wallet/rpcwallet.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ static UniValue setlabel(const JSONRPCRequest& request)
322322
}
323323

324324

325-
static CTransactionRef SendMoney(interfaces::Chain::Lock& locked_chain, CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, const CCoinControl& coin_control, mapValue_t mapValue)
325+
static CTransactionRef SendMoney(CWallet* const pwallet, const CTxDestination& address, CAmount nValue, bool fSubtractFeeFromAmount, const CCoinControl& coin_control, mapValue_t mapValue)
326326
{
327327
CAmount curBalance = pwallet->GetBalance(0, coin_control.m_avoid_address_reuse).m_mine_trusted;
328328

@@ -344,7 +344,7 @@ static CTransactionRef SendMoney(interfaces::Chain::Lock& locked_chain, CWallet
344344
CRecipient recipient = {scriptPubKey, nValue, fSubtractFeeFromAmount};
345345
vecSend.push_back(recipient);
346346
CTransactionRef tx;
347-
if (!pwallet->CreateTransaction(locked_chain, vecSend, tx, nFeeRequired, nChangePosRet, strError, coin_control)) {
347+
if (!pwallet->CreateTransaction(vecSend, tx, nFeeRequired, nChangePosRet, strError, coin_control)) {
348348
if (!fSubtractFeeFromAmount && nValue + nFeeRequired > curBalance)
349349
strError = strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired));
350350
throw JSONRPCError(RPC_WALLET_ERROR, strError);
@@ -445,7 +445,7 @@ static UniValue sendtoaddress(const JSONRPCRequest& request)
445445

446446
EnsureWalletIsUnlocked(pwallet);
447447

448-
CTransactionRef tx = SendMoney(*locked_chain, pwallet, dest, nAmount, fSubtractFeeFromAmount, coin_control, std::move(mapValue));
448+
CTransactionRef tx = SendMoney(pwallet, dest, nAmount, fSubtractFeeFromAmount, coin_control, std::move(mapValue));
449449
return tx->GetHash().GetHex();
450450
}
451451

@@ -913,7 +913,7 @@ static UniValue sendmany(const JSONRPCRequest& request)
913913
int nChangePosRet = -1;
914914
std::string strFailReason;
915915
CTransactionRef tx;
916-
bool fCreated = pwallet->CreateTransaction(*locked_chain, vecSend, tx, nFeeRequired, nChangePosRet, strFailReason, coin_control);
916+
bool fCreated = pwallet->CreateTransaction(vecSend, tx, nFeeRequired, nChangePosRet, strFailReason, coin_control);
917917
if (!fCreated)
918918
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
919919
pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */);
@@ -3563,8 +3563,7 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
35633563
stop_height = request.params[1].get_int();
35643564
if (*stop_height < 0 || *stop_height > tip_height) {
35653565
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid stop_height");
3566-
}
3567-
else if (*stop_height < start_height) {
3566+
} else if (*stop_height < start_height) {
35683567
throw JSONRPCError(RPC_INVALID_PARAMETER, "stop_height must be greater than start_height");
35693568
}
35703569
}

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
521521
CCoinControl dummy;
522522
{
523523
auto locked_chain = m_chain->lock();
524-
BOOST_CHECK(wallet->CreateTransaction(*locked_chain, {recipient}, tx, fee, changePos, error, dummy));
524+
BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, fee, changePos, error, dummy));
525525
}
526526
wallet->CommitTransaction(tx, {}, {});
527527
CMutableTransaction blocktx;

src/wallet/wallet.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,7 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nC
25552555
LOCK(cs_wallet);
25562556

25572557
CTransactionRef tx_new;
2558-
if (!CreateTransaction(*locked_chain, vecSend, tx_new, nFeeRet, nChangePosInOut, strFailReason, coinControl, false)) {
2558+
if (!CreateTransaction(vecSend, tx_new, nFeeRet, nChangePosInOut, strFailReason, coinControl, false)) {
25592559
return false;
25602560
}
25612561

@@ -2671,8 +2671,7 @@ OutputType CWallet::TransactionChangeType(OutputType change_type, const std::vec
26712671
return m_default_address_type;
26722672
}
26732673

2674-
bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet,
2675-
int& nChangePosInOut, std::string& strFailReason, const CCoinControl& coin_control, bool sign)
2674+
bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, const CCoinControl& coin_control, bool sign)
26762675
{
26772676
CAmount nValue = 0;
26782677
const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend);
@@ -3943,7 +3942,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
39433942
}
39443943
}
39453944

3946-
const Optional<int> tip_height = locked_chain->getHeight();
3945+
const Optional<int> tip_height = chain.getHeight();
39473946
if (tip_height) {
39483947
walletInstance->m_last_block_processed = locked_chain->getBlockHash(*tip_height);
39493948
walletInstance->m_last_block_processed_height = *tip_height;

src/wallet/wallet.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
961961
* selected by SelectCoins(); Also create the change output, when needed
962962
* @note passing nChangePosInOut as -1 will result in setting a random position
963963
*/
964-
bool CreateTransaction(interfaces::Chain::Lock& locked_chain, const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut,
965-
std::string& strFailReason, const CCoinControl& coin_control, bool sign = true);
964+
bool CreateTransaction(const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, const CCoinControl& coin_control, bool sign = true);
966965
/**
967966
* Submit the transaction to the node's mempool and then relay to peers.
968967
* Should be called after CreateTransaction unless you want to abort

0 commit comments

Comments
 (0)