diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -108,11 +108,6 @@ public: virtual ~Lock() {} - //! Get current chain height, not including genesis block (returns 0 if - //! chain only contains genesis block, nullopt if chain does not contain - //! any blocks). - virtual Optional getHeight() = 0; - //! Get block height above genesis block. Returns 0 for genesis block, //! 1 for following block, and so on. Returns nullopt for a block not //! included in the current chain. @@ -161,6 +156,11 @@ //! unlocked when the returned interface is freed. virtual std::unique_ptr lock(bool try_lock = false) = 0; + //! Get current chain height, not including genesis block (returns 0 if + //! chain only contains genesis block, nullopt if chain does not contain + //! any blocks) + virtual Optional getHeight() = 0; + //! Return whether node has the block and optionally return block metadata //! or contents. virtual bool findBlock(const BlockHash &hash, diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -66,14 +66,6 @@ } class LockImpl : public Chain::Lock, public UniqueLock { - Optional getHeight() override { - LockAssertion lock(::cs_main); - int height = ::ChainActive().Height(); - if (height >= 0) { - return height; - } - return nullopt; - } Optional getBlockHeight(const BlockHash &hash) override { LockAssertion lock(::cs_main); CBlockIndex *block = LookupBlockIndex(hash); @@ -257,6 +249,14 @@ std::unique_ptr result = std::move(lock); return result; } + Optional getHeight() override { + LOCK(::cs_main); + int height = ::ChainActive().Height(); + if (height >= 0) { + return height; + } + return nullopt; + } bool findBlock(const BlockHash &hash, const FoundBlock &block) override { WAIT_LOCK(cs_main, lock); diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -239,9 +239,8 @@ auto locked_chain = m_wallet->chain().lock(); LOCK(m_wallet->cs_wallet); CTransactionRef tx; - if (!m_wallet->CreateTransaction(*locked_chain, recipients, tx, fee, - change_pos, fail_reason, - coin_control, sign)) { + if (!m_wallet->CreateTransaction(recipients, tx, fee, change_pos, + fail_reason, coin_control, sign)) { return {}; } return tx; @@ -319,7 +318,7 @@ LOCK(m_wallet->cs_wallet); auto mi = m_wallet->mapWallet.find(txid); if (mi != m_wallet->mapWallet.end()) { - num_blocks = locked_chain->getHeight().value_or(-1); + num_blocks = m_wallet->GetLastBlockHeight(); in_mempool = mi->second.InMempool(); order_form = mi->second.vOrderForm; tx_status = MakeWalletTxStatus(*locked_chain, mi->second); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -360,8 +360,7 @@ return NullUniValue; } -static CTransactionRef SendMoney(interfaces::Chain::Lock &locked_chain, - CWallet *const pwallet, +static CTransactionRef SendMoney(CWallet *const pwallet, const CTxDestination &address, Amount nValue, bool fSubtractFeeFromAmount, const CCoinControl &coin_control, @@ -391,8 +390,8 @@ vecSend.push_back(recipient); CTransactionRef tx; - if (!pwallet->CreateTransaction(locked_chain, vecSend, tx, nFeeRequired, - nChangePosRet, error, coin_control)) { + if (!pwallet->CreateTransaction(vecSend, tx, nFeeRequired, nChangePosRet, + error, coin_control)) { if (!fSubtractFeeFromAmount && nValue + nFeeRequired > curBalance) { error = strprintf(Untranslated("Error: This transaction requires a " "transaction fee of at least %s"), @@ -502,8 +501,8 @@ EnsureWalletIsUnlocked(pwallet); CTransactionRef tx = - SendMoney(*locked_chain, pwallet, dest, nAmount, fSubtractFeeFromAmount, - coin_control, std::move(mapValue)); + SendMoney(pwallet, dest, nAmount, fSubtractFeeFromAmount, coin_control, + std::move(mapValue)); return tx->GetId().GetHex(); } @@ -1059,9 +1058,8 @@ bilingual_str error; CTransactionRef tx; CCoinControl coinControl; - bool fCreated = - pwallet->CreateTransaction(*locked_chain, vecSend, tx, nFeeRequired, - nChangePosRet, error, coinControl); + bool fCreated = pwallet->CreateTransaction( + vecSend, tx, nFeeRequired, nChangePosRet, error, coinControl); if (!fCreated) { throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, error.original); } diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -539,8 +539,8 @@ CCoinControl dummy; { auto locked_chain = m_chain->lock(); - BOOST_CHECK(wallet->CreateTransaction( - *locked_chain, {recipient}, tx, fee, changePos, error, dummy)); + BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, fee, + changePos, error, dummy)); } wallet->CommitTransaction(tx, {}, {}); CMutableTransaction blocktx; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -794,8 +794,7 @@ */ int m_last_block_processed_height GUARDED_BY(cs_wallet) = -1; - bool CreateTransactionInternal(interfaces::Chain::Lock &locked_chain, - const std::vector &vecSend, + bool CreateTransactionInternal(const std::vector &vecSend, CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, const CCoinControl &coin_control, bool sign); @@ -1111,11 +1110,11 @@ * @note passing nChangePosInOut as -1 will result in setting a random * position */ - bool CreateTransaction(interfaces::Chain::Lock &locked_chain, - const std::vector &vecSend, + bool CreateTransaction(const std::vector &vecSend, CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, const CCoinControl &coin_control, bool sign = true); + /** * Submit the transaction to the node's mempool and then relay to peers. * Should be called after CreateTransaction unless you want to abort diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2774,8 +2774,8 @@ LOCK(cs_wallet); CTransactionRef tx_new; - if (!CreateTransaction(*locked_chain, vecSend, tx_new, nFeeRet, - nChangePosInOut, error, coinControl, false)) { + if (!CreateTransaction(vecSend, tx_new, nFeeRet, nChangePosInOut, error, + coinControl, false)) { return false; } @@ -2886,8 +2886,7 @@ return m_default_address_type; } -bool CWallet::CreateTransactionInternal(interfaces::Chain::Lock &locked_chainIn, - const std::vector &vecSend, +bool CWallet::CreateTransactionInternal(const std::vector &vecSend, CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, @@ -3260,16 +3259,14 @@ return true; } -bool CWallet::CreateTransaction(interfaces::Chain::Lock &locked_chain, - const std::vector &vecSend, +bool CWallet::CreateTransaction(const std::vector &vecSend, CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, const CCoinControl &coin_control, bool sign) { int nChangePosIn = nChangePosInOut; CTransactionRef tx2 = tx; - bool res = - CreateTransactionInternal(locked_chain, vecSend, tx, nFeeRet, - nChangePosInOut, error, coin_control, sign); + bool res = CreateTransactionInternal(vecSend, tx, nFeeRet, nChangePosInOut, + error, coin_control, sign); // try with avoidpartialspends unless it's enabled already if (res && nFeeRet > @@ -3282,8 +3279,8 @@ int nChangePosInOut2 = nChangePosIn; // fired and forgotten; if an error occurs, we discard the results bilingual_str error2; - if (CreateTransactionInternal(locked_chain, vecSend, tx2, nFeeRet2, - nChangePosInOut2, error2, tmp_cc, sign)) { + if (CreateTransactionInternal(vecSend, tx2, nFeeRet2, nChangePosInOut2, + error2, tmp_cc, sign)) { // if fee of this alternative one is within the range of the max // fee, we use this one const bool use_aps = nFeeRet2 <= nFeeRet + m_max_aps_fee; @@ -4313,7 +4310,7 @@ } } - const Optional tip_height = locked_chain->getHeight(); + const Optional tip_height = chain.getHeight(); if (tip_height) { walletInstance->m_last_block_processed = locked_chain->getBlockHash(*tip_height);