Merge #10973: Refactor: separate wallet from node
d358466de
Remove remaining wallet accesses to node globals (Russell Yanofsky)b1b2b2389
Remove use of CCoinsViewMemPool::GetCoin in wallet code (Russell Yanofsky)4e4d9e9f8
Remove use of CRPCTable::appendCommand in wallet code (Russell Yanofsky)91868e628
Remove use CValidationInterface in wallet code (Russell Yanofsky) Pull request description: This PR is the last in a chain of PRs (#14437, #14711, and #15288) that make the wallet code access node state through an abstract [`Chain`](https://github.com/ryanofsky/bitcoin/blob/pr/wipc-sep/src/interfaces/chain.h) class in [`src/interfaces/`](https://github.com/ryanofsky/bitcoin/tree/pr/wipc-sep/src/interfaces) instead of using global variables like `cs_main`, `chainActive`, and `g_connman`. After this PR, wallet code no longer accesses global variables declared outside the wallet directory, and no longer calls functions accessing those globals (as verified by the `hide-globals` script in #10244). This PR and the previous PRs have been refactoring changes that do not affect behavior. Previous PRs have consisted of lots of mechanical changes like: ```diff - wtx.nTimeReceived = GetAdjustedTime(); + wtx.nTimeReceived = m_chain->getAdjustedTime(); ``` This PR is smaller, but less mechanical. It replaces last few bits of wallet code that access node state directly (through `CValidationInterface`, `CRPCTable`, and `CCoinsViewMemPool` interfaces) with code that uses the `Chain` interface. These changes allow followup PR #10102 (multiprocess gui & wallet PR) to work without any significant updates to wallet code. Additionally they: * Provide a single place to describe the interface between wallet and node code. * Can make better wallet testing possible, because the `Chain` object consists of virtual methods that can be overloaded for mocking. (This could be used to test edge cases in the rescan code, for example). Tree-SHA512: e6291d8a3c50bdff18a9c8ad11e729beb30b5b7040d7aaf31ba678800b4a97b2dd2be76340b1e5c01fe2827d67d37ed1bb4c8380cf8ed653aadfea003e9b22e7
This commit is contained in:
commit
2607d960a0
22 changed files with 373 additions and 137 deletions
|
@ -154,6 +154,7 @@ BITCOIN_CORE_H = \
|
||||||
netaddress.h \
|
netaddress.h \
|
||||||
netbase.h \
|
netbase.h \
|
||||||
netmessagemaker.h \
|
netmessagemaker.h \
|
||||||
|
node/coin.h \
|
||||||
node/transaction.h \
|
node/transaction.h \
|
||||||
noui.h \
|
noui.h \
|
||||||
optional.h \
|
optional.h \
|
||||||
|
@ -262,6 +263,7 @@ libbitcoin_server_a_SOURCES = \
|
||||||
miner.cpp \
|
miner.cpp \
|
||||||
net.cpp \
|
net.cpp \
|
||||||
net_processing.cpp \
|
net_processing.cpp \
|
||||||
|
node/coin.cpp \
|
||||||
node/transaction.cpp \
|
node/transaction.cpp \
|
||||||
noui.cpp \
|
noui.cpp \
|
||||||
outputtype.cpp \
|
outputtype.cpp \
|
||||||
|
|
|
@ -6,22 +6,29 @@
|
||||||
|
|
||||||
#include <chain.h>
|
#include <chain.h>
|
||||||
#include <chainparams.h>
|
#include <chainparams.h>
|
||||||
|
#include <interfaces/handler.h>
|
||||||
#include <interfaces/wallet.h>
|
#include <interfaces/wallet.h>
|
||||||
#include <net.h>
|
#include <net.h>
|
||||||
|
#include <node/coin.h>
|
||||||
#include <policy/fees.h>
|
#include <policy/fees.h>
|
||||||
#include <policy/policy.h>
|
#include <policy/policy.h>
|
||||||
#include <policy/rbf.h>
|
#include <policy/rbf.h>
|
||||||
#include <primitives/block.h>
|
#include <primitives/block.h>
|
||||||
#include <primitives/transaction.h>
|
#include <primitives/transaction.h>
|
||||||
#include <protocol.h>
|
#include <protocol.h>
|
||||||
|
#include <rpc/protocol.h>
|
||||||
|
#include <rpc/server.h>
|
||||||
|
#include <shutdown.h>
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <threadsafety.h>
|
#include <threadsafety.h>
|
||||||
#include <timedata.h>
|
#include <timedata.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <ui_interface.h>
|
#include <ui_interface.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
|
#include <univalue.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
#include <validationinterface.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
@ -161,6 +168,94 @@ class LockingStateImpl : public LockImpl, public UniqueLock<CCriticalSection>
|
||||||
using UniqueLock::UniqueLock;
|
using UniqueLock::UniqueLock;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class NotificationsHandlerImpl : public Handler, CValidationInterface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit NotificationsHandlerImpl(Chain& chain, Chain::Notifications& notifications)
|
||||||
|
: m_chain(chain), m_notifications(¬ifications)
|
||||||
|
{
|
||||||
|
RegisterValidationInterface(this);
|
||||||
|
}
|
||||||
|
~NotificationsHandlerImpl() override { disconnect(); }
|
||||||
|
void disconnect() override
|
||||||
|
{
|
||||||
|
if (m_notifications) {
|
||||||
|
m_notifications = nullptr;
|
||||||
|
UnregisterValidationInterface(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void TransactionAddedToMempool(const CTransactionRef& tx) override
|
||||||
|
{
|
||||||
|
m_notifications->TransactionAddedToMempool(tx);
|
||||||
|
}
|
||||||
|
void TransactionRemovedFromMempool(const CTransactionRef& tx) override
|
||||||
|
{
|
||||||
|
m_notifications->TransactionRemovedFromMempool(tx);
|
||||||
|
}
|
||||||
|
void BlockConnected(const std::shared_ptr<const CBlock>& block,
|
||||||
|
const CBlockIndex* index,
|
||||||
|
const std::vector<CTransactionRef>& tx_conflicted) override
|
||||||
|
{
|
||||||
|
m_notifications->BlockConnected(*block, tx_conflicted);
|
||||||
|
}
|
||||||
|
void BlockDisconnected(const std::shared_ptr<const CBlock>& block) override
|
||||||
|
{
|
||||||
|
m_notifications->BlockDisconnected(*block);
|
||||||
|
}
|
||||||
|
void ChainStateFlushed(const CBlockLocator& locator) override { m_notifications->ChainStateFlushed(locator); }
|
||||||
|
void ResendWalletTransactions(int64_t best_block_time, CConnman*) override
|
||||||
|
{
|
||||||
|
// `cs_main` is always held when this method is called, so it is safe to
|
||||||
|
// call `assumeLocked`. This is awkward, and the `assumeLocked` method
|
||||||
|
// should be able to be removed entirely if `ResendWalletTransactions`
|
||||||
|
// is replaced by a wallet timer as suggested in
|
||||||
|
// https://github.com/bitcoin/bitcoin/issues/15619
|
||||||
|
auto locked_chain = m_chain.assumeLocked();
|
||||||
|
m_notifications->ResendWalletTransactions(*locked_chain, best_block_time);
|
||||||
|
}
|
||||||
|
Chain& m_chain;
|
||||||
|
Chain::Notifications* m_notifications;
|
||||||
|
};
|
||||||
|
|
||||||
|
class RpcHandlerImpl : public Handler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RpcHandlerImpl(const CRPCCommand& command) : m_command(command), m_wrapped_command(&command)
|
||||||
|
{
|
||||||
|
m_command.actor = [this](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
|
||||||
|
if (!m_wrapped_command) return false;
|
||||||
|
try {
|
||||||
|
return m_wrapped_command->actor(request, result, last_handler);
|
||||||
|
} catch (const UniValue& e) {
|
||||||
|
// If this is not the last handler and a wallet not found
|
||||||
|
// exception was thrown, return false so the next handler can
|
||||||
|
// try to handle the request. Otherwise, reraise the exception.
|
||||||
|
if (!last_handler) {
|
||||||
|
const UniValue& code = e["code"];
|
||||||
|
if (code.isNum() && code.get_int() == RPC_WALLET_NOT_FOUND) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
::tableRPC.appendCommand(m_command.name, &m_command);
|
||||||
|
}
|
||||||
|
|
||||||
|
void disconnect() override final
|
||||||
|
{
|
||||||
|
if (m_wrapped_command) {
|
||||||
|
m_wrapped_command = nullptr;
|
||||||
|
::tableRPC.removeCommand(m_command.name, &m_command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~RpcHandlerImpl() override { disconnect(); }
|
||||||
|
|
||||||
|
CRPCCommand m_command;
|
||||||
|
const CRPCCommand* m_wrapped_command;
|
||||||
|
};
|
||||||
|
|
||||||
class ChainImpl : public Chain
|
class ChainImpl : public Chain
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -194,6 +289,7 @@ public:
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(coins); }
|
||||||
double guessVerificationProgress(const uint256& block_hash) override
|
double guessVerificationProgress(const uint256& block_hash) override
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
|
@ -245,17 +341,33 @@ public:
|
||||||
{
|
{
|
||||||
return ::mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
|
return ::mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
|
||||||
}
|
}
|
||||||
|
CFeeRate relayMinFee() override { return ::minRelayTxFee; }
|
||||||
|
CFeeRate relayIncrementalFee() override { return ::incrementalRelayFee; }
|
||||||
|
CFeeRate relayDustFee() override { return ::dustRelayFee; }
|
||||||
CAmount maxTxFee() override { return ::maxTxFee; }
|
CAmount maxTxFee() override { return ::maxTxFee; }
|
||||||
bool getPruneMode() override { return ::fPruneMode; }
|
bool getPruneMode() override { return ::fPruneMode; }
|
||||||
bool p2pEnabled() override { return g_connman != nullptr; }
|
bool p2pEnabled() override { return g_connman != nullptr; }
|
||||||
bool isInitialBlockDownload() override { return IsInitialBlockDownload(); }
|
bool isInitialBlockDownload() override { return IsInitialBlockDownload(); }
|
||||||
|
bool shutdownRequested() override { return ShutdownRequested(); }
|
||||||
int64_t getAdjustedTime() override { return GetAdjustedTime(); }
|
int64_t getAdjustedTime() override { return GetAdjustedTime(); }
|
||||||
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
|
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
|
||||||
void initWarning(const std::string& message) override { InitWarning(message); }
|
void initWarning(const std::string& message) override { InitWarning(message); }
|
||||||
void initError(const std::string& message) override { InitError(message); }
|
void initError(const std::string& message) override { InitError(message); }
|
||||||
void loadWallet(std::unique_ptr<Wallet> wallet) override { ::uiInterface.LoadWallet(wallet); }
|
void loadWallet(std::unique_ptr<Wallet> wallet) override { ::uiInterface.LoadWallet(wallet); }
|
||||||
|
void showProgress(const std::string& title, int progress, bool resume_possible) override
|
||||||
|
{
|
||||||
|
::uiInterface.ShowProgress(title, progress, resume_possible);
|
||||||
|
}
|
||||||
|
std::unique_ptr<Handler> handleNotifications(Notifications& notifications) override
|
||||||
|
{
|
||||||
|
return MakeUnique<NotificationsHandlerImpl>(*this, notifications);
|
||||||
|
}
|
||||||
|
void waitForNotifications() override { SyncWithValidationInterfaceQueue(); }
|
||||||
|
std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
|
||||||
|
{
|
||||||
|
return MakeUnique<RpcHandlerImpl>(command);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::unique_ptr<Chain> MakeChain() { return MakeUnique<ChainImpl>(); }
|
std::unique_ptr<Chain> MakeChain() { return MakeUnique<ChainImpl>(); }
|
||||||
|
|
|
@ -16,8 +16,10 @@
|
||||||
|
|
||||||
class CBlock;
|
class CBlock;
|
||||||
class CFeeRate;
|
class CFeeRate;
|
||||||
|
class CRPCCommand;
|
||||||
class CScheduler;
|
class CScheduler;
|
||||||
class CValidationState;
|
class CValidationState;
|
||||||
|
class Coin;
|
||||||
class uint256;
|
class uint256;
|
||||||
enum class RBFTransactionState;
|
enum class RBFTransactionState;
|
||||||
struct CBlockLocator;
|
struct CBlockLocator;
|
||||||
|
@ -25,6 +27,7 @@ struct FeeCalculation;
|
||||||
|
|
||||||
namespace interfaces {
|
namespace interfaces {
|
||||||
|
|
||||||
|
class Handler;
|
||||||
class Wallet;
|
class Wallet;
|
||||||
|
|
||||||
//! Interface giving clients (wallet processes, maybe other analysis tools in
|
//! Interface giving clients (wallet processes, maybe other analysis tools in
|
||||||
|
@ -40,6 +43,12 @@ class Wallet;
|
||||||
//! asynchronously
|
//! asynchronously
|
||||||
//! (https://github.com/bitcoin/bitcoin/pull/10973#issuecomment-380101269).
|
//! (https://github.com/bitcoin/bitcoin/pull/10973#issuecomment-380101269).
|
||||||
//!
|
//!
|
||||||
|
//! * The isPotentialTip() and waitForNotifications() methods are too low-level
|
||||||
|
//! and should be replaced with a higher level
|
||||||
|
//! waitForNotificationsUpTo(block_hash) method that the wallet can call
|
||||||
|
//! instead
|
||||||
|
//! (https://github.com/bitcoin/bitcoin/pull/10973#discussion_r266995234).
|
||||||
|
//!
|
||||||
//! * The relayTransactions() and submitToMemoryPool() methods could be replaced
|
//! * The relayTransactions() and submitToMemoryPool() methods could be replaced
|
||||||
//! with a higher-level broadcastTransaction method
|
//! with a higher-level broadcastTransaction method
|
||||||
//! (https://github.com/bitcoin/bitcoin/pull/14978#issuecomment-459373984).
|
//! (https://github.com/bitcoin/bitcoin/pull/14978#issuecomment-459373984).
|
||||||
|
@ -160,6 +169,11 @@ public:
|
||||||
int64_t* time = nullptr,
|
int64_t* time = nullptr,
|
||||||
int64_t* max_time = nullptr) = 0;
|
int64_t* max_time = nullptr) = 0;
|
||||||
|
|
||||||
|
//! Look up unspent output information. Returns coins in the mempool and in
|
||||||
|
//! the current chain UTXO set. Iterates through all the keys in the map and
|
||||||
|
//! populates the values.
|
||||||
|
virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;
|
||||||
|
|
||||||
//! Estimate fraction of total transactions verified if blocks up to
|
//! Estimate fraction of total transactions verified if blocks up to
|
||||||
//! the specified block hash are verified.
|
//! the specified block hash are verified.
|
||||||
virtual double guessVerificationProgress(const uint256& block_hash) = 0;
|
virtual double guessVerificationProgress(const uint256& block_hash) = 0;
|
||||||
|
@ -188,6 +202,15 @@ public:
|
||||||
//! Mempool minimum fee.
|
//! Mempool minimum fee.
|
||||||
virtual CFeeRate mempoolMinFee() = 0;
|
virtual CFeeRate mempoolMinFee() = 0;
|
||||||
|
|
||||||
|
//! Relay current minimum fee (from -minrelaytxfee and -incrementalrelayfee settings).
|
||||||
|
virtual CFeeRate relayMinFee() = 0;
|
||||||
|
|
||||||
|
//! Relay incremental fee setting (-incrementalrelayfee), reflecting cost of relay.
|
||||||
|
virtual CFeeRate relayIncrementalFee() = 0;
|
||||||
|
|
||||||
|
//! Relay dust fee setting (-dustrelayfee), reflecting lowest rate it's economical to spend.
|
||||||
|
virtual CFeeRate relayDustFee() = 0;
|
||||||
|
|
||||||
//! Node max tx fee setting (-maxtxfee).
|
//! Node max tx fee setting (-maxtxfee).
|
||||||
//! This could be replaced by a per-wallet max fee, as proposed at
|
//! This could be replaced by a per-wallet max fee, as proposed at
|
||||||
//! https://github.com/bitcoin/bitcoin/issues/15355
|
//! https://github.com/bitcoin/bitcoin/issues/15355
|
||||||
|
@ -200,9 +223,12 @@ public:
|
||||||
//! Check if p2p enabled.
|
//! Check if p2p enabled.
|
||||||
virtual bool p2pEnabled() = 0;
|
virtual bool p2pEnabled() = 0;
|
||||||
|
|
||||||
// Check if in IBD.
|
//! Check if in IBD.
|
||||||
virtual bool isInitialBlockDownload() = 0;
|
virtual bool isInitialBlockDownload() = 0;
|
||||||
|
|
||||||
|
//! Check if shutdown requested.
|
||||||
|
virtual bool shutdownRequested() = 0;
|
||||||
|
|
||||||
//! Get adjusted time.
|
//! Get adjusted time.
|
||||||
virtual int64_t getAdjustedTime() = 0;
|
virtual int64_t getAdjustedTime() = 0;
|
||||||
|
|
||||||
|
@ -217,6 +243,32 @@ public:
|
||||||
|
|
||||||
//! Send wallet load notification to the GUI.
|
//! Send wallet load notification to the GUI.
|
||||||
virtual void loadWallet(std::unique_ptr<Wallet> wallet) = 0;
|
virtual void loadWallet(std::unique_ptr<Wallet> wallet) = 0;
|
||||||
|
|
||||||
|
//! Send progress indicator.
|
||||||
|
virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
|
||||||
|
|
||||||
|
//! Chain notifications.
|
||||||
|
class Notifications
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Notifications() {}
|
||||||
|
virtual void TransactionAddedToMempool(const CTransactionRef& tx) {}
|
||||||
|
virtual void TransactionRemovedFromMempool(const CTransactionRef& ptx) {}
|
||||||
|
virtual void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& tx_conflicted) {}
|
||||||
|
virtual void BlockDisconnected(const CBlock& block) {}
|
||||||
|
virtual void ChainStateFlushed(const CBlockLocator& locator) {}
|
||||||
|
virtual void ResendWalletTransactions(Lock& locked_chain, int64_t best_block_time) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Register handler for notifications.
|
||||||
|
virtual std::unique_ptr<Handler> handleNotifications(Notifications& notifications) = 0;
|
||||||
|
|
||||||
|
//! Wait for pending notifications to be handled.
|
||||||
|
virtual void waitForNotifications() = 0;
|
||||||
|
|
||||||
|
//! Register handler for RPC. Command is not copied, so reference
|
||||||
|
//! needs to remain valid until Handler is disconnected.
|
||||||
|
virtual std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Interface to let node manage chain clients (wallets, or maybe tools for
|
//! Interface to let node manage chain clients (wallets, or maybe tools for
|
||||||
|
|
|
@ -47,8 +47,6 @@ public:
|
||||||
|
|
||||||
const CTransaction& get() override { return *m_tx; }
|
const CTransaction& get() override { return *m_tx; }
|
||||||
|
|
||||||
int64_t getVirtualSize() override { return GetVirtualTransactionSize(*m_tx); }
|
|
||||||
|
|
||||||
bool commit(WalletValueMap value_map,
|
bool commit(WalletValueMap value_map,
|
||||||
WalletOrderForm order_form,
|
WalletOrderForm order_form,
|
||||||
std::string& reject_reason) override
|
std::string& reject_reason) override
|
||||||
|
@ -99,12 +97,8 @@ WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, co
|
||||||
//! Construct wallet tx status struct.
|
//! Construct wallet tx status struct.
|
||||||
WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx)
|
WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx)
|
||||||
{
|
{
|
||||||
LockAnnotation lock(::cs_main); // Temporary, for mapBlockIndex below. Removed in upcoming commit.
|
|
||||||
|
|
||||||
WalletTxStatus result;
|
WalletTxStatus result;
|
||||||
auto mi = ::mapBlockIndex.find(wtx.hashBlock);
|
result.block_height = locked_chain.getBlockHeight(wtx.hashBlock).get_value_or(std::numeric_limits<int>::max());
|
||||||
CBlockIndex* block = mi != ::mapBlockIndex.end() ? mi->second : nullptr;
|
|
||||||
result.block_height = (block ? block->nHeight : std::numeric_limits<int>::max());
|
|
||||||
result.blocks_to_maturity = wtx.GetBlocksToMaturity(locked_chain);
|
result.blocks_to_maturity = wtx.GetBlocksToMaturity(locked_chain);
|
||||||
result.depth_in_main_chain = wtx.GetDepthInMainChain(locked_chain);
|
result.depth_in_main_chain = wtx.GetDepthInMainChain(locked_chain);
|
||||||
result.time_received = wtx.nTimeReceived;
|
result.time_received = wtx.nTimeReceived;
|
||||||
|
@ -514,7 +508,7 @@ public:
|
||||||
: m_chain(chain), m_wallet_filenames(std::move(wallet_filenames))
|
: m_chain(chain), m_wallet_filenames(std::move(wallet_filenames))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
void registerRpcs() override { return RegisterWalletRPCCommands(::tableRPC); }
|
void registerRpcs() override { return RegisterWalletRPCCommands(m_chain, m_rpc_handlers); }
|
||||||
bool verify() override { return VerifyWallets(m_chain, m_wallet_filenames); }
|
bool verify() override { return VerifyWallets(m_chain, m_wallet_filenames); }
|
||||||
bool load() override { return LoadWallets(m_chain, m_wallet_filenames); }
|
bool load() override { return LoadWallets(m_chain, m_wallet_filenames); }
|
||||||
void start(CScheduler& scheduler) override { return StartWallets(scheduler); }
|
void start(CScheduler& scheduler) override { return StartWallets(scheduler); }
|
||||||
|
@ -524,6 +518,7 @@ public:
|
||||||
|
|
||||||
Chain& m_chain;
|
Chain& m_chain;
|
||||||
std::vector<std::string> m_wallet_filenames;
|
std::vector<std::string> m_wallet_filenames;
|
||||||
|
std::vector<std::unique_ptr<Handler>> m_rpc_handlers;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -292,9 +292,6 @@ public:
|
||||||
//! Get transaction data.
|
//! Get transaction data.
|
||||||
virtual const CTransaction& get() = 0;
|
virtual const CTransaction& get() = 0;
|
||||||
|
|
||||||
//! Get virtual transaction size.
|
|
||||||
virtual int64_t getVirtualSize() = 0;
|
|
||||||
|
|
||||||
//! Send pending transaction and commit to wallet.
|
//! Send pending transaction and commit to wallet.
|
||||||
virtual bool commit(WalletValueMap value_map,
|
virtual bool commit(WalletValueMap value_map,
|
||||||
WalletOrderForm order_form,
|
WalletOrderForm order_form,
|
||||||
|
|
22
src/node/coin.cpp
Normal file
22
src/node/coin.cpp
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
// Copyright (c) 2019 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <node/coin.h>
|
||||||
|
|
||||||
|
#include <txmempool.h>
|
||||||
|
#include <validation.h>
|
||||||
|
|
||||||
|
void FindCoins(std::map<COutPoint, Coin>& coins)
|
||||||
|
{
|
||||||
|
LOCK2(cs_main, ::mempool.cs);
|
||||||
|
assert(pcoinsTip);
|
||||||
|
CCoinsViewCache& chain_view = *::pcoinsTip;
|
||||||
|
CCoinsViewMemPool mempool_view(&chain_view, ::mempool);
|
||||||
|
for (auto& coin : coins) {
|
||||||
|
if (!mempool_view.GetCoin(coin.first, coin.second)) {
|
||||||
|
// Either the coin is not in the CCoinsViewCache or is spent. Clear it.
|
||||||
|
coin.second.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
src/node/coin.h
Normal file
22
src/node/coin.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
// Copyright (c) 2019 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_NODE_COIN_H
|
||||||
|
#define BITCOIN_NODE_COIN_H
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class COutPoint;
|
||||||
|
class Coin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up unspent output information. Returns coins in the mempool and in the
|
||||||
|
* current chain UTXO set. Iterates through all the keys in the map and
|
||||||
|
* populates the values.
|
||||||
|
*
|
||||||
|
* @param[in,out] coins map to fill
|
||||||
|
*/
|
||||||
|
void FindCoins(std::map<COutPoint, Coin>& coins);
|
||||||
|
|
||||||
|
#endif // BITCOIN_NODE_COIN_H
|
|
@ -29,7 +29,7 @@ std::unique_ptr<interfaces::PendingWalletTx>& WalletModelTransaction::getWtx()
|
||||||
|
|
||||||
unsigned int WalletModelTransaction::getTransactionSize()
|
unsigned int WalletModelTransaction::getTransactionSize()
|
||||||
{
|
{
|
||||||
return wtx ? wtx->getVirtualSize() : 0;
|
return wtx ? GetVirtualTransactionSize(wtx->get()) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CAmount WalletModelTransaction::getTransactionFee() const
|
CAmount WalletModelTransaction::getTransactionFee() const
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <core_io.h>
|
#include <core_io.h>
|
||||||
#include <index/txindex.h>
|
#include <index/txindex.h>
|
||||||
#include <init.h>
|
#include <init.h>
|
||||||
|
#include <interfaces/chain.h>
|
||||||
#include <key_io.h>
|
#include <key_io.h>
|
||||||
#include <keystore.h>
|
#include <keystore.h>
|
||||||
#include <merkleblock.h>
|
#include <merkleblock.h>
|
||||||
|
@ -791,23 +792,20 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
|
||||||
return EncodeHexTx(CTransaction(mergedTx));
|
return EncodeHexTx(CTransaction(mergedTx));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(https://github.com/bitcoin/bitcoin/pull/10973#discussion_r267084237):
|
||||||
|
// This function is called from both wallet and node rpcs
|
||||||
|
// (signrawtransactionwithwallet and signrawtransactionwithkey). It should be
|
||||||
|
// moved to a util file so wallet code doesn't need to link against node code.
|
||||||
|
// Also the dependency on interfaces::Chain should be removed, so
|
||||||
|
// signrawtransactionwithkey doesn't need access to a Chain instance.
|
||||||
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
|
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
|
||||||
{
|
{
|
||||||
// Fetch previous transactions (inputs):
|
// Fetch previous transactions (inputs):
|
||||||
CCoinsView viewDummy;
|
std::map<COutPoint, Coin> coins;
|
||||||
CCoinsViewCache view(&viewDummy);
|
for (const CTxIn& txin : mtx.vin) {
|
||||||
{
|
coins[txin.prevout]; // Create empty map entry keyed by prevout.
|
||||||
LOCK2(cs_main, mempool.cs);
|
|
||||||
CCoinsViewCache &viewChain = *pcoinsTip;
|
|
||||||
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
|
||||||
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
|
|
||||||
|
|
||||||
for (const CTxIn& txin : mtx.vin) {
|
|
||||||
view.AccessCoin(txin.prevout); // Load entries from viewChain into view; can fail.
|
|
||||||
}
|
|
||||||
|
|
||||||
view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
|
|
||||||
}
|
}
|
||||||
|
chain.findCoins(coins);
|
||||||
|
|
||||||
// Add previous txouts given in the RPC call:
|
// Add previous txouts given in the RPC call:
|
||||||
if (!prevTxsUnival.isNull()) {
|
if (!prevTxsUnival.isNull()) {
|
||||||
|
@ -839,10 +837,10 @@ UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, con
|
||||||
CScript scriptPubKey(pkData.begin(), pkData.end());
|
CScript scriptPubKey(pkData.begin(), pkData.end());
|
||||||
|
|
||||||
{
|
{
|
||||||
const Coin& coin = view.AccessCoin(out);
|
auto coin = coins.find(out);
|
||||||
if (!coin.IsSpent() && coin.out.scriptPubKey != scriptPubKey) {
|
if (coin != coins.end() && !coin->second.IsSpent() && coin->second.out.scriptPubKey != scriptPubKey) {
|
||||||
std::string err("Previous output scriptPubKey mismatch:\n");
|
std::string err("Previous output scriptPubKey mismatch:\n");
|
||||||
err = err + ScriptToAsmStr(coin.out.scriptPubKey) + "\nvs:\n"+
|
err = err + ScriptToAsmStr(coin->second.out.scriptPubKey) + "\nvs:\n"+
|
||||||
ScriptToAsmStr(scriptPubKey);
|
ScriptToAsmStr(scriptPubKey);
|
||||||
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
|
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
|
||||||
}
|
}
|
||||||
|
@ -853,7 +851,7 @@ UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, con
|
||||||
newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
|
newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
|
||||||
}
|
}
|
||||||
newcoin.nHeight = 1;
|
newcoin.nHeight = 1;
|
||||||
view.AddCoin(out, std::move(newcoin), true);
|
coins[out] = std::move(newcoin);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
|
// if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
|
||||||
|
@ -897,15 +895,15 @@ UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, con
|
||||||
// Sign what we can:
|
// Sign what we can:
|
||||||
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
|
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
|
||||||
CTxIn& txin = mtx.vin[i];
|
CTxIn& txin = mtx.vin[i];
|
||||||
const Coin& coin = view.AccessCoin(txin.prevout);
|
auto coin = coins.find(txin.prevout);
|
||||||
if (coin.IsSpent()) {
|
if (coin == coins.end() || coin->second.IsSpent()) {
|
||||||
TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
|
TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const CScript& prevPubKey = coin.out.scriptPubKey;
|
const CScript& prevPubKey = coin->second.out.scriptPubKey;
|
||||||
const CAmount& amount = coin.out.nValue;
|
const CAmount& amount = coin->second.out.nValue;
|
||||||
|
|
||||||
SignatureData sigdata = DataFromTransaction(mtx, i, coin.out);
|
SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
|
||||||
// Only sign SIGHASH_SINGLE if there's a corresponding output:
|
// Only sign SIGHASH_SINGLE if there's a corresponding output:
|
||||||
if (!fHashSingle || (i < mtx.vout.size())) {
|
if (!fHashSingle || (i < mtx.vout.size())) {
|
||||||
ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, nHashType), prevPubKey, sigdata);
|
ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, nHashType), prevPubKey, sigdata);
|
||||||
|
@ -915,7 +913,7 @@ UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, con
|
||||||
|
|
||||||
// amount must be specified for valid segwit signature
|
// amount must be specified for valid segwit signature
|
||||||
if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
|
if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
|
||||||
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coin.out.ToString()));
|
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coin->second.out.ToString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptError serror = SCRIPT_ERR_OK;
|
ScriptError serror = SCRIPT_ERR_OK;
|
||||||
|
|
|
@ -30,6 +30,7 @@ static std::string rpcWarmupStatus GUARDED_BY(cs_rpcWarmup) = "RPC server starte
|
||||||
static RPCTimerInterface* timerInterface = nullptr;
|
static RPCTimerInterface* timerInterface = nullptr;
|
||||||
/* Map of name to timer. */
|
/* Map of name to timer. */
|
||||||
static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers;
|
static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers;
|
||||||
|
static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler);
|
||||||
|
|
||||||
struct RPCCommandExecutionInfo
|
struct RPCCommandExecutionInfo
|
||||||
{
|
{
|
||||||
|
@ -173,11 +174,11 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
|
||||||
{
|
{
|
||||||
std::string strRet;
|
std::string strRet;
|
||||||
std::string category;
|
std::string category;
|
||||||
std::set<rpcfn_type> setDone;
|
std::set<intptr_t> setDone;
|
||||||
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
|
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
|
||||||
|
|
||||||
for (const auto& entry : mapCommands)
|
for (const auto& entry : mapCommands)
|
||||||
vCommands.push_back(make_pair(entry.second->category + entry.first, entry.second));
|
vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front()));
|
||||||
sort(vCommands.begin(), vCommands.end());
|
sort(vCommands.begin(), vCommands.end());
|
||||||
|
|
||||||
JSONRPCRequest jreq(helpreq);
|
JSONRPCRequest jreq(helpreq);
|
||||||
|
@ -193,9 +194,9 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
|
||||||
jreq.strMethod = strMethod;
|
jreq.strMethod = strMethod;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
rpcfn_type pfn = pcmd->actor;
|
UniValue unused_result;
|
||||||
if (setDone.insert(pfn).second)
|
if (setDone.insert(pcmd->unique_id).second)
|
||||||
(*pfn)(jreq);
|
pcmd->actor(jreq, unused_result, true /* last_handler */);
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
|
@ -337,32 +338,32 @@ CRPCTable::CRPCTable()
|
||||||
const CRPCCommand *pcmd;
|
const CRPCCommand *pcmd;
|
||||||
|
|
||||||
pcmd = &vRPCCommands[vcidx];
|
pcmd = &vRPCCommands[vcidx];
|
||||||
mapCommands[pcmd->name] = pcmd;
|
mapCommands[pcmd->name].push_back(pcmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const CRPCCommand *CRPCTable::operator[](const std::string &name) const
|
|
||||||
{
|
|
||||||
std::map<std::string, const CRPCCommand*>::const_iterator it = mapCommands.find(name);
|
|
||||||
if (it == mapCommands.end())
|
|
||||||
return nullptr;
|
|
||||||
return (*it).second;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CRPCTable::appendCommand(const std::string& name, const CRPCCommand* pcmd)
|
bool CRPCTable::appendCommand(const std::string& name, const CRPCCommand* pcmd)
|
||||||
{
|
{
|
||||||
if (IsRPCRunning())
|
if (IsRPCRunning())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// don't allow overwriting for now
|
mapCommands[name].push_back(pcmd);
|
||||||
std::map<std::string, const CRPCCommand*>::const_iterator it = mapCommands.find(name);
|
|
||||||
if (it != mapCommands.end())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
mapCommands[name] = pcmd;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CRPCTable::removeCommand(const std::string& name, const CRPCCommand* pcmd)
|
||||||
|
{
|
||||||
|
auto it = mapCommands.find(name);
|
||||||
|
if (it != mapCommands.end()) {
|
||||||
|
auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
|
||||||
|
if (it->second.end() != new_end) {
|
||||||
|
it->second.erase(new_end, it->second.end());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void StartRPC()
|
void StartRPC()
|
||||||
{
|
{
|
||||||
LogPrint(BCLog::RPC, "Starting RPC\n");
|
LogPrint(BCLog::RPC, "Starting RPC\n");
|
||||||
|
@ -543,18 +544,28 @@ UniValue CRPCTable::execute(const JSONRPCRequest &request) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find method
|
// Find method
|
||||||
const CRPCCommand *pcmd = tableRPC[request.strMethod];
|
auto it = mapCommands.find(request.strMethod);
|
||||||
if (!pcmd)
|
if (it != mapCommands.end()) {
|
||||||
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
|
UniValue result;
|
||||||
|
for (const auto& command : it->second) {
|
||||||
|
if (ExecuteCommand(*command, request, result, &command == &it->second.back())) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
RPCCommandExecution execution(request.strMethod);
|
RPCCommandExecution execution(request.strMethod);
|
||||||
// Execute, convert arguments to array if necessary
|
// Execute, convert arguments to array if necessary
|
||||||
if (request.params.isObject()) {
|
if (request.params.isObject()) {
|
||||||
return pcmd->actor(transformNamedArguments(request, pcmd->argNames));
|
return command.actor(transformNamedArguments(request, command.argNames), result, last_handler);
|
||||||
} else {
|
} else {
|
||||||
return pcmd->actor(request);
|
return command.actor(request, result, last_handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
|
|
|
@ -131,10 +131,31 @@ typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
|
||||||
class CRPCCommand
|
class CRPCCommand
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
//! RPC method handler reading request and assigning result. Should return
|
||||||
|
//! true if request is fully handled, false if it should be passed on to
|
||||||
|
//! subsequent handlers.
|
||||||
|
using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
|
||||||
|
|
||||||
|
//! Constructor taking Actor callback supporting multiple handlers.
|
||||||
|
CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
|
||||||
|
: category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
|
||||||
|
unique_id(unique_id)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Simplified constructor taking plain rpcfn_type function pointer.
|
||||||
|
CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
|
||||||
|
: CRPCCommand(category, name,
|
||||||
|
[fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn(request); return true; },
|
||||||
|
{args.begin(), args.end()}, intptr_t(fn))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
std::string category;
|
std::string category;
|
||||||
std::string name;
|
std::string name;
|
||||||
rpcfn_type actor;
|
Actor actor;
|
||||||
std::vector<std::string> argNames;
|
std::vector<std::string> argNames;
|
||||||
|
intptr_t unique_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,10 +164,9 @@ public:
|
||||||
class CRPCTable
|
class CRPCTable
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::map<std::string, const CRPCCommand*> mapCommands;
|
std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
|
||||||
public:
|
public:
|
||||||
CRPCTable();
|
CRPCTable();
|
||||||
const CRPCCommand* operator[](const std::string& name) const;
|
|
||||||
std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
|
std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,9 +189,7 @@ public:
|
||||||
*
|
*
|
||||||
* Returns false if RPC server is already running (dump concurrency protection).
|
* Returns false if RPC server is already running (dump concurrency protection).
|
||||||
*
|
*
|
||||||
* Commands cannot be overwritten (returns false).
|
* Commands with different method names but the same unique_id will
|
||||||
*
|
|
||||||
* Commands with different method names but the same callback function will
|
|
||||||
* be considered aliases, and only the first registered method name will
|
* be considered aliases, and only the first registered method name will
|
||||||
* show up in the help text command listing. Aliased commands do not have
|
* show up in the help text command listing. Aliased commands do not have
|
||||||
* to have the same behavior. Server and client code can distinguish
|
* to have the same behavior. Server and client code can distinguish
|
||||||
|
@ -179,6 +197,7 @@ public:
|
||||||
* register different names, types, and numbers of parameters.
|
* register different names, types, and numbers of parameters.
|
||||||
*/
|
*/
|
||||||
bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
|
bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
|
||||||
|
bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
|
||||||
};
|
};
|
||||||
|
|
||||||
bool IsDeprecatedRPCEnabled(const std::string& method);
|
bool IsDeprecatedRPCEnabled(const std::string& method);
|
||||||
|
|
|
@ -31,10 +31,9 @@ UniValue CallRPC(std::string args)
|
||||||
request.strMethod = strMethod;
|
request.strMethod = strMethod;
|
||||||
request.params = RPCConvertValues(strMethod, vArgs);
|
request.params = RPCConvertValues(strMethod, vArgs);
|
||||||
request.fHelp = false;
|
request.fHelp = false;
|
||||||
BOOST_CHECK(tableRPC[strMethod]);
|
if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();
|
||||||
rpcfn_type method = tableRPC[strMethod]->actor;
|
|
||||||
try {
|
try {
|
||||||
UniValue result = (*method)(request);
|
UniValue result = tableRPC.execute(request);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (const UniValue& objError) {
|
catch (const UniValue& objError) {
|
||||||
|
|
|
@ -123,16 +123,17 @@ Result CreateTransaction(const CWallet* wallet, const uint256& txid, const CCoin
|
||||||
// The wallet uses a conservative WALLET_INCREMENTAL_RELAY_FEE value to
|
// The wallet uses a conservative WALLET_INCREMENTAL_RELAY_FEE value to
|
||||||
// future proof against changes to network wide policy for incremental relay
|
// future proof against changes to network wide policy for incremental relay
|
||||||
// fee that our node may not be aware of.
|
// fee that our node may not be aware of.
|
||||||
|
CFeeRate nodeIncrementalRelayFee = wallet->chain().relayIncrementalFee();
|
||||||
CFeeRate walletIncrementalRelayFee = CFeeRate(WALLET_INCREMENTAL_RELAY_FEE);
|
CFeeRate walletIncrementalRelayFee = CFeeRate(WALLET_INCREMENTAL_RELAY_FEE);
|
||||||
if (::incrementalRelayFee > walletIncrementalRelayFee) {
|
if (nodeIncrementalRelayFee > walletIncrementalRelayFee) {
|
||||||
walletIncrementalRelayFee = ::incrementalRelayFee;
|
walletIncrementalRelayFee = nodeIncrementalRelayFee;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (total_fee > 0) {
|
if (total_fee > 0) {
|
||||||
CAmount minTotalFee = nOldFeeRate.GetFee(maxNewTxSize) + ::incrementalRelayFee.GetFee(maxNewTxSize);
|
CAmount minTotalFee = nOldFeeRate.GetFee(maxNewTxSize) + nodeIncrementalRelayFee.GetFee(maxNewTxSize);
|
||||||
if (total_fee < minTotalFee) {
|
if (total_fee < minTotalFee) {
|
||||||
errors.push_back(strprintf("Insufficient totalFee, must be at least %s (oldFee %s + incrementalFee %s)",
|
errors.push_back(strprintf("Insufficient totalFee, must be at least %s (oldFee %s + incrementalFee %s)",
|
||||||
FormatMoney(minTotalFee), FormatMoney(nOldFeeRate.GetFee(maxNewTxSize)), FormatMoney(::incrementalRelayFee.GetFee(maxNewTxSize))));
|
FormatMoney(minTotalFee), FormatMoney(nOldFeeRate.GetFee(maxNewTxSize)), FormatMoney(nodeIncrementalRelayFee.GetFee(maxNewTxSize))));
|
||||||
return Result::INVALID_PARAMETER;
|
return Result::INVALID_PARAMETER;
|
||||||
}
|
}
|
||||||
CAmount requiredFee = GetRequiredFee(*wallet, maxNewTxSize);
|
CAmount requiredFee = GetRequiredFee(*wallet, maxNewTxSize);
|
||||||
|
@ -159,9 +160,10 @@ Result CreateTransaction(const CWallet* wallet, const uint256& txid, const CCoin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that in all cases the new fee doesn't violate maxTxFee
|
// Check that in all cases the new fee doesn't violate maxTxFee
|
||||||
if (new_fee > maxTxFee) {
|
const CAmount max_tx_fee = wallet->chain().maxTxFee();
|
||||||
|
if (new_fee > max_tx_fee) {
|
||||||
errors.push_back(strprintf("Specified or calculated fee %s is too high (cannot be higher than maxTxFee %s)",
|
errors.push_back(strprintf("Specified or calculated fee %s is too high (cannot be higher than maxTxFee %s)",
|
||||||
FormatMoney(new_fee), FormatMoney(maxTxFee)));
|
FormatMoney(new_fee), FormatMoney(max_tx_fee)));
|
||||||
return Result::WALLET_ERROR;
|
return Result::WALLET_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,9 @@ CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinC
|
||||||
{
|
{
|
||||||
CAmount fee_needed = GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
|
CAmount fee_needed = GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
|
||||||
// Always obey the maximum
|
// Always obey the maximum
|
||||||
if (fee_needed > maxTxFee) {
|
const CAmount max_tx_fee = wallet.chain().maxTxFee();
|
||||||
fee_needed = maxTxFee;
|
if (fee_needed > max_tx_fee) {
|
||||||
|
fee_needed = max_tx_fee;
|
||||||
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
|
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
|
||||||
}
|
}
|
||||||
return fee_needed;
|
return fee_needed;
|
||||||
|
@ -31,7 +32,7 @@ CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinC
|
||||||
|
|
||||||
CFeeRate GetRequiredFeeRate(const CWallet& wallet)
|
CFeeRate GetRequiredFeeRate(const CWallet& wallet)
|
||||||
{
|
{
|
||||||
return std::max(wallet.m_min_fee, ::minRelayTxFee);
|
return std::max(wallet.m_min_fee, wallet.chain().relayMinFee());
|
||||||
}
|
}
|
||||||
|
|
||||||
CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, FeeCalculation* feeCalc)
|
CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, FeeCalculation* feeCalc)
|
||||||
|
@ -96,6 +97,6 @@ CFeeRate GetDiscardRate(const CWallet& wallet)
|
||||||
// Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
|
// Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
|
||||||
discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
|
discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
|
||||||
// Discard rate must be at least dustRelayFee
|
// Discard rate must be at least dustRelayFee
|
||||||
discard_rate = std::max(discard_rate, ::dustRelayFee);
|
discard_rate = std::max(discard_rate, wallet.chain().relayDustFee());
|
||||||
return discard_rate;
|
return discard_rate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,7 +153,7 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
|
||||||
|
|
||||||
LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
|
LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
|
||||||
|
|
||||||
uiInterface.InitMessage(_("Verifying wallet(s)..."));
|
chain.initMessage(_("Verifying wallet(s)..."));
|
||||||
|
|
||||||
// Parameter interaction code should have thrown an error if -salvagewallet
|
// Parameter interaction code should have thrown an error if -salvagewallet
|
||||||
// was enabled with more than wallet file, so the wallet_files size check
|
// was enabled with more than wallet file, so the wallet_files size check
|
||||||
|
|
|
@ -595,11 +595,11 @@ UniValue importwallet(const JSONRPCRequest& request)
|
||||||
|
|
||||||
// Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
|
// Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
|
||||||
// we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
|
// we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
|
||||||
uiInterface.ShowProgress(strprintf("%s " + _("Importing..."), pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
|
pwallet->chain().showProgress(strprintf("%s " + _("Importing..."), pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
|
||||||
std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
|
std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
|
||||||
std::vector<std::pair<CScript, int64_t>> scripts;
|
std::vector<std::pair<CScript, int64_t>> scripts;
|
||||||
while (file.good()) {
|
while (file.good()) {
|
||||||
uiInterface.ShowProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
|
pwallet->chain().showProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
|
||||||
std::string line;
|
std::string line;
|
||||||
std::getline(file, line);
|
std::getline(file, line);
|
||||||
if (line.empty() || line[0] == '#')
|
if (line.empty() || line[0] == '#')
|
||||||
|
@ -637,13 +637,13 @@ UniValue importwallet(const JSONRPCRequest& request)
|
||||||
file.close();
|
file.close();
|
||||||
// We now know whether we are importing private keys, so we can error if private keys are disabled
|
// We now know whether we are importing private keys, so we can error if private keys are disabled
|
||||||
if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||||
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
|
pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled");
|
throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled");
|
||||||
}
|
}
|
||||||
double total = (double)(keys.size() + scripts.size());
|
double total = (double)(keys.size() + scripts.size());
|
||||||
double progress = 0;
|
double progress = 0;
|
||||||
for (const auto& key_tuple : keys) {
|
for (const auto& key_tuple : keys) {
|
||||||
uiInterface.ShowProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
|
pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
|
||||||
const CKey& key = std::get<0>(key_tuple);
|
const CKey& key = std::get<0>(key_tuple);
|
||||||
int64_t time = std::get<1>(key_tuple);
|
int64_t time = std::get<1>(key_tuple);
|
||||||
bool has_label = std::get<2>(key_tuple);
|
bool has_label = std::get<2>(key_tuple);
|
||||||
|
@ -668,7 +668,7 @@ UniValue importwallet(const JSONRPCRequest& request)
|
||||||
progress++;
|
progress++;
|
||||||
}
|
}
|
||||||
for (const auto& script_pair : scripts) {
|
for (const auto& script_pair : scripts) {
|
||||||
uiInterface.ShowProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
|
pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
|
||||||
const CScript& script = script_pair.first;
|
const CScript& script = script_pair.first;
|
||||||
int64_t time = script_pair.second;
|
int64_t time = script_pair.second;
|
||||||
CScriptID id(script);
|
CScriptID id(script);
|
||||||
|
@ -687,10 +687,10 @@ UniValue importwallet(const JSONRPCRequest& request)
|
||||||
}
|
}
|
||||||
progress++;
|
progress++;
|
||||||
}
|
}
|
||||||
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
|
pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
|
||||||
pwallet->UpdateTimeFirstKey(nTimeBegin);
|
pwallet->UpdateTimeFirstKey(nTimeBegin);
|
||||||
}
|
}
|
||||||
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
|
pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
|
||||||
RescanWallet(*pwallet, reserver, nTimeBegin, false /* update */);
|
RescanWallet(*pwallet, reserver, nTimeBegin, false /* update */);
|
||||||
pwallet->MarkDirty();
|
pwallet->MarkDirty();
|
||||||
|
|
||||||
|
|
|
@ -2367,8 +2367,8 @@ static UniValue settxfee(const JSONRPCRequest& request)
|
||||||
CFeeRate tx_fee_rate(nAmount, 1000);
|
CFeeRate tx_fee_rate(nAmount, 1000);
|
||||||
if (tx_fee_rate == 0) {
|
if (tx_fee_rate == 0) {
|
||||||
// automatic selection
|
// automatic selection
|
||||||
} else if (tx_fee_rate < ::minRelayTxFee) {
|
} else if (tx_fee_rate < pwallet->chain().relayMinFee()) {
|
||||||
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than min relay tx fee (%s)", ::minRelayTxFee.ToString()));
|
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than min relay tx fee (%s)", pwallet->chain().relayMinFee().ToString()));
|
||||||
} else if (tx_fee_rate < pwallet->m_min_fee) {
|
} else if (tx_fee_rate < pwallet->m_min_fee) {
|
||||||
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than wallet min fee (%s)", pwallet->m_min_fee.ToString()));
|
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than wallet min fee (%s)", pwallet->m_min_fee.ToString()));
|
||||||
}
|
}
|
||||||
|
@ -4156,8 +4156,8 @@ static const CRPCCommand commands[] =
|
||||||
};
|
};
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
void RegisterWalletRPCCommands(CRPCTable &t)
|
void RegisterWalletRPCCommands(interfaces::Chain& chain, std::vector<std::unique_ptr<interfaces::Handler>>& handlers)
|
||||||
{
|
{
|
||||||
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
|
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
|
||||||
t.appendCommand(commands[vcidx].name, &commands[vcidx]);
|
handlers.emplace_back(chain.handleRpc(commands[vcidx]));
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@
|
||||||
#ifndef BITCOIN_WALLET_RPCWALLET_H
|
#ifndef BITCOIN_WALLET_RPCWALLET_H
|
||||||
#define BITCOIN_WALLET_RPCWALLET_H
|
#define BITCOIN_WALLET_RPCWALLET_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class CRPCTable;
|
class CRPCTable;
|
||||||
class CWallet;
|
class CWallet;
|
||||||
|
@ -14,7 +16,12 @@ class UniValue;
|
||||||
struct PartiallySignedTransaction;
|
struct PartiallySignedTransaction;
|
||||||
class CTransaction;
|
class CTransaction;
|
||||||
|
|
||||||
void RegisterWalletRPCCommands(CRPCTable &t);
|
namespace interfaces {
|
||||||
|
class Chain;
|
||||||
|
class Handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterWalletRPCCommands(interfaces::Chain& chain, std::vector<std::unique_ptr<interfaces::Handler>>& handlers);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Figures out what wallet, if any, to use for a JSONRPCRequest.
|
* Figures out what wallet, if any, to use for a JSONRPCRequest.
|
||||||
|
|
|
@ -13,12 +13,7 @@ WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
|
||||||
{
|
{
|
||||||
bool fFirstRun;
|
bool fFirstRun;
|
||||||
m_wallet.LoadWallet(fFirstRun);
|
m_wallet.LoadWallet(fFirstRun);
|
||||||
RegisterValidationInterface(&m_wallet);
|
m_wallet.m_chain_notifications_handler = m_chain->handleNotifications(m_wallet);
|
||||||
|
|
||||||
RegisterWalletRPCCommands(tableRPC);
|
m_chain_client->registerRpcs();
|
||||||
}
|
|
||||||
|
|
||||||
WalletTestingSetup::~WalletTestingSetup()
|
|
||||||
{
|
|
||||||
UnregisterValidationInterface(&m_wallet);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,9 +17,9 @@
|
||||||
*/
|
*/
|
||||||
struct WalletTestingSetup: public TestingSetup {
|
struct WalletTestingSetup: public TestingSetup {
|
||||||
explicit WalletTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
|
explicit WalletTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
|
||||||
~WalletTestingSetup();
|
|
||||||
|
|
||||||
std::unique_ptr<interfaces::Chain> m_chain = interfaces::MakeChain();
|
std::unique_ptr<interfaces::Chain> m_chain = interfaces::MakeChain();
|
||||||
|
std::unique_ptr<interfaces::ChainClient> m_chain_client = interfaces::MakeWalletClient(*m_chain, {});
|
||||||
CWallet m_wallet;
|
CWallet m_wallet;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,7 @@ static void ReleaseWallet(CWallet* wallet)
|
||||||
wallet->WalletLogPrintf("Releasing wallet\n");
|
wallet->WalletLogPrintf("Releasing wallet\n");
|
||||||
wallet->BlockUntilSyncedToCurrentChain();
|
wallet->BlockUntilSyncedToCurrentChain();
|
||||||
wallet->Flush();
|
wallet->Flush();
|
||||||
UnregisterValidationInterface(wallet);
|
wallet->m_chain_notifications_handler.reset();
|
||||||
delete wallet;
|
delete wallet;
|
||||||
// Wallet is now released, notify UnloadWallet, if any.
|
// Wallet is now released, notify UnloadWallet, if any.
|
||||||
{
|
{
|
||||||
|
@ -1243,7 +1243,8 @@ void CWallet::TransactionRemovedFromMempool(const CTransactionRef &ptx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) {
|
void CWallet::BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& vtxConflicted) {
|
||||||
|
const uint256& block_hash = block.GetHash();
|
||||||
auto locked_chain = chain().lock();
|
auto locked_chain = chain().lock();
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
// TODO: Temporarily ensure that mempool removals are notified before
|
// TODO: Temporarily ensure that mempool removals are notified before
|
||||||
|
@ -1258,19 +1259,19 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
|
||||||
SyncTransaction(ptx, {} /* block hash */, 0 /* position in block */);
|
SyncTransaction(ptx, {} /* block hash */, 0 /* position in block */);
|
||||||
TransactionRemovedFromMempool(ptx);
|
TransactionRemovedFromMempool(ptx);
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < pblock->vtx.size(); i++) {
|
for (size_t i = 0; i < block.vtx.size(); i++) {
|
||||||
SyncTransaction(pblock->vtx[i], pindex->GetBlockHash(), i);
|
SyncTransaction(block.vtx[i], block_hash, i);
|
||||||
TransactionRemovedFromMempool(pblock->vtx[i]);
|
TransactionRemovedFromMempool(block.vtx[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_last_block_processed = pindex->GetBlockHash();
|
m_last_block_processed = block_hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) {
|
void CWallet::BlockDisconnected(const CBlock& block) {
|
||||||
auto locked_chain = chain().lock();
|
auto locked_chain = chain().lock();
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
|
|
||||||
for (const CTransactionRef& ptx : pblock->vtx) {
|
for (const CTransactionRef& ptx : block.vtx) {
|
||||||
SyncTransaction(ptx, {} /* block hash */, 0 /* position in block */);
|
SyncTransaction(ptx, {} /* block hash */, 0 /* position in block */);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1297,7 +1298,7 @@ void CWallet::BlockUntilSyncedToCurrentChain() {
|
||||||
// ...otherwise put a callback in the validation interface queue and wait
|
// ...otherwise put a callback in the validation interface queue and wait
|
||||||
// for the queue to drain enough to execute it (indicating we are caught up
|
// for the queue to drain enough to execute it (indicating we are caught up
|
||||||
// at least with the time we entered this function).
|
// at least with the time we entered this function).
|
||||||
SyncWithValidationInterfaceQueue();
|
chain().waitForNotifications();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1790,7 +1791,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
|
||||||
progress_end = chain().guessVerificationProgress(stop_block.IsNull() ? tip_hash : stop_block);
|
progress_end = chain().guessVerificationProgress(stop_block.IsNull() ? tip_hash : stop_block);
|
||||||
}
|
}
|
||||||
double progress_current = progress_begin;
|
double progress_current = progress_begin;
|
||||||
while (block_height && !fAbortRescan && !ShutdownRequested()) {
|
while (block_height && !fAbortRescan && !chain().shutdownRequested()) {
|
||||||
if (*block_height % 100 == 0 && progress_end - progress_begin > 0.0) {
|
if (*block_height % 100 == 0 && progress_end - progress_begin > 0.0) {
|
||||||
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), std::max(1, std::min(99, (int)((progress_current - progress_begin) / (progress_end - progress_begin) * 100))));
|
ShowProgress(strprintf("%s " + _("Rescanning..."), GetDisplayName()), std::max(1, std::min(99, (int)((progress_current - progress_begin) / (progress_end - progress_begin) * 100))));
|
||||||
}
|
}
|
||||||
|
@ -1852,7 +1853,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
|
||||||
if (block_height && fAbortRescan) {
|
if (block_height && fAbortRescan) {
|
||||||
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", *block_height, progress_current);
|
WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", *block_height, progress_current);
|
||||||
result.status = ScanResult::USER_ABORT;
|
result.status = ScanResult::USER_ABORT;
|
||||||
} else if (block_height && ShutdownRequested()) {
|
} else if (block_height && chain().shutdownRequested()) {
|
||||||
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", *block_height, progress_current);
|
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", *block_height, progress_current);
|
||||||
result.status = ScanResult::USER_ABORT;
|
result.status = ScanResult::USER_ABORT;
|
||||||
}
|
}
|
||||||
|
@ -2137,7 +2138,7 @@ std::vector<uint256> CWallet::ResendWalletTransactionsBefore(interfaces::Chain::
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CWallet::ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman)
|
void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime)
|
||||||
{
|
{
|
||||||
// Do this infrequently and randomly to avoid giving away
|
// Do this infrequently and randomly to avoid giving away
|
||||||
// that these are our transactions.
|
// that these are our transactions.
|
||||||
|
@ -2155,8 +2156,7 @@ void CWallet::ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman
|
||||||
|
|
||||||
// Rebroadcast unconfirmed txes older than 5 minutes before the last
|
// Rebroadcast unconfirmed txes older than 5 minutes before the last
|
||||||
// block was found:
|
// block was found:
|
||||||
auto locked_chain = chain().assumeLocked(); // Temporary. Removed in upcoming lock cleanup
|
std::vector<uint256> relayed = ResendWalletTransactionsBefore(locked_chain, nBestBlockTime-5*60);
|
||||||
std::vector<uint256> relayed = ResendWalletTransactionsBefore(*locked_chain, nBestBlockTime-5*60);
|
|
||||||
if (!relayed.empty())
|
if (!relayed.empty())
|
||||||
WalletLogPrintf("%s: rebroadcast %u unconfirmed transactions\n", __func__, relayed.size());
|
WalletLogPrintf("%s: rebroadcast %u unconfirmed transactions\n", __func__, relayed.size());
|
||||||
}
|
}
|
||||||
|
@ -2310,7 +2310,6 @@ CAmount CWallet::GetAvailableBalance(const CCoinControl* coinControl) const
|
||||||
|
|
||||||
void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<COutput> &vCoins, bool fOnlySafe, const CCoinControl *coinControl, const CAmount &nMinimumAmount, const CAmount &nMaximumAmount, const CAmount &nMinimumSumAmount, const uint64_t nMaximumCount, const int nMinDepth, const int nMaxDepth) const
|
void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<COutput> &vCoins, bool fOnlySafe, const CCoinControl *coinControl, const CAmount &nMinimumAmount, const CAmount &nMaximumAmount, const CAmount &nMinimumSumAmount, const uint64_t nMaximumCount, const int nMinDepth, const int nMaxDepth) const
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_main);
|
|
||||||
AssertLockHeld(cs_wallet);
|
AssertLockHeld(cs_wallet);
|
||||||
|
|
||||||
vCoins.clear();
|
vCoins.clear();
|
||||||
|
@ -2420,7 +2419,6 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
|
||||||
|
|
||||||
std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins(interfaces::Chain::Lock& locked_chain) const
|
std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins(interfaces::Chain::Lock& locked_chain) const
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_main);
|
|
||||||
AssertLockHeld(cs_wallet);
|
AssertLockHeld(cs_wallet);
|
||||||
|
|
||||||
std::map<CTxDestination, std::vector<COutput>> result;
|
std::map<CTxDestination, std::vector<COutput>> result;
|
||||||
|
@ -2692,13 +2690,13 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nC
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsCurrentForAntiFeeSniping(interfaces::Chain::Lock& locked_chain)
|
static bool IsCurrentForAntiFeeSniping(interfaces::Chain& chain, interfaces::Chain::Lock& locked_chain)
|
||||||
{
|
{
|
||||||
if (IsInitialBlockDownload()) {
|
if (chain.isInitialBlockDownload()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60; // in seconds
|
constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60; // in seconds
|
||||||
if (chainActive.Tip()->GetBlockTime() < (GetTime() - MAX_ANTI_FEE_SNIPING_TIP_AGE)) {
|
if (locked_chain.getBlockTime(*locked_chain.getHeight()) < (GetTime() - MAX_ANTI_FEE_SNIPING_TIP_AGE)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -2708,7 +2706,7 @@ static bool IsCurrentForAntiFeeSniping(interfaces::Chain::Lock& locked_chain)
|
||||||
* Return a height-based locktime for new transactions (uses the height of the
|
* Return a height-based locktime for new transactions (uses the height of the
|
||||||
* current chain tip unless we are not synced with the current chain
|
* current chain tip unless we are not synced with the current chain
|
||||||
*/
|
*/
|
||||||
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain::Lock& locked_chain)
|
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain& chain, interfaces::Chain::Lock& locked_chain)
|
||||||
{
|
{
|
||||||
uint32_t const height = locked_chain.getHeight().get_value_or(-1);
|
uint32_t const height = locked_chain.getHeight().get_value_or(-1);
|
||||||
uint32_t locktime;
|
uint32_t locktime;
|
||||||
|
@ -2732,7 +2730,7 @@ static uint32_t GetLocktimeForNewTransaction(interfaces::Chain::Lock& locked_cha
|
||||||
// enough, that fee sniping isn't a problem yet, but by implementing a fix
|
// enough, that fee sniping isn't a problem yet, but by implementing a fix
|
||||||
// now we ensure code won't be written that makes assumptions about
|
// now we ensure code won't be written that makes assumptions about
|
||||||
// nLockTime that preclude a fix later.
|
// nLockTime that preclude a fix later.
|
||||||
if (IsCurrentForAntiFeeSniping(locked_chain)) {
|
if (IsCurrentForAntiFeeSniping(chain, locked_chain)) {
|
||||||
locktime = height;
|
locktime = height;
|
||||||
|
|
||||||
// Secondly occasionally randomly pick a nLockTime even further back, so
|
// Secondly occasionally randomly pick a nLockTime even further back, so
|
||||||
|
@ -2806,7 +2804,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
|
||||||
|
|
||||||
CMutableTransaction txNew;
|
CMutableTransaction txNew;
|
||||||
|
|
||||||
txNew.nLockTime = GetLocktimeForNewTransaction(locked_chain);
|
txNew.nLockTime = GetLocktimeForNewTransaction(chain(), locked_chain);
|
||||||
|
|
||||||
FeeCalculation feeCalc;
|
FeeCalculation feeCalc;
|
||||||
CAmount nFeeNeeded;
|
CAmount nFeeNeeded;
|
||||||
|
@ -2902,7 +2900,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
|
||||||
// Include the fee cost for outputs. Note this is only used for BnB right now
|
// Include the fee cost for outputs. Note this is only used for BnB right now
|
||||||
coin_selection_params.tx_noinputs_size += ::GetSerializeSize(txout, PROTOCOL_VERSION);
|
coin_selection_params.tx_noinputs_size += ::GetSerializeSize(txout, PROTOCOL_VERSION);
|
||||||
|
|
||||||
if (IsDust(txout, ::dustRelayFee))
|
if (IsDust(txout, chain().relayDustFee()))
|
||||||
{
|
{
|
||||||
if (recipient.fSubtractFeeFromAmount && nFeeRet > 0)
|
if (recipient.fSubtractFeeFromAmount && nFeeRet > 0)
|
||||||
{
|
{
|
||||||
|
@ -3003,7 +3001,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
|
||||||
|
|
||||||
// If we made it here and we aren't even able to meet the relay fee on the next pass, give up
|
// If we made it here and we aren't even able to meet the relay fee on the next pass, give up
|
||||||
// because we must be at the maximum allowed fee.
|
// because we must be at the maximum allowed fee.
|
||||||
if (nFeeNeeded < ::minRelayTxFee.GetFee(nBytes))
|
if (nFeeNeeded < chain().relayMinFee().GetFee(nBytes))
|
||||||
{
|
{
|
||||||
strFailReason = _("Transaction too large for fee policy");
|
strFailReason = _("Transaction too large for fee policy");
|
||||||
return false;
|
return false;
|
||||||
|
@ -4280,9 +4278,9 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
||||||
_("This is the transaction fee you will pay if you send a transaction."));
|
_("This is the transaction fee you will pay if you send a transaction."));
|
||||||
}
|
}
|
||||||
walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000);
|
walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000);
|
||||||
if (walletInstance->m_pay_tx_fee < ::minRelayTxFee) {
|
if (walletInstance->m_pay_tx_fee < chain.relayMinFee()) {
|
||||||
chain.initError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
|
chain.initError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
|
||||||
gArgs.GetArg("-paytxfee", ""), ::minRelayTxFee.ToString()));
|
gArgs.GetArg("-paytxfee", ""), chain.relayMinFee().ToString()));
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4384,8 +4382,8 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
||||||
|
|
||||||
chain.loadWallet(interfaces::MakeWallet(walletInstance));
|
chain.loadWallet(interfaces::MakeWallet(walletInstance));
|
||||||
|
|
||||||
// Register with the validation interface. It's ok to do this after rescan since we're still holding cs_main.
|
// Register with the validation interface. It's ok to do this after rescan since we're still holding locked_chain.
|
||||||
RegisterValidationInterface(walletInstance.get());
|
walletInstance->m_chain_notifications_handler = chain.handleNotifications(*walletInstance);
|
||||||
|
|
||||||
walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
|
walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
|
||||||
|
|
||||||
|
@ -4445,8 +4443,6 @@ int CMerkleTx::GetDepthInMainChain(interfaces::Chain::Lock& locked_chain) const
|
||||||
if (hashUnset())
|
if (hashUnset())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
AssertLockHeld(cs_main);
|
|
||||||
|
|
||||||
return locked_chain.getBlockDepth(hashBlock) * (nIndex == -1 ? -1 : 1);
|
return locked_chain.getBlockDepth(hashBlock) * (nIndex == -1 ? -1 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <amount.h>
|
#include <amount.h>
|
||||||
#include <interfaces/chain.h>
|
#include <interfaces/chain.h>
|
||||||
|
#include <interfaces/handler.h>
|
||||||
#include <outputtype.h>
|
#include <outputtype.h>
|
||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
|
@ -505,7 +506,7 @@ public:
|
||||||
CAmount GetCredit(interfaces::Chain::Lock& locked_chain, const isminefilter& filter) const;
|
CAmount GetCredit(interfaces::Chain::Lock& locked_chain, const isminefilter& filter) const;
|
||||||
CAmount GetImmatureCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache=true) const;
|
CAmount GetImmatureCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache=true) const;
|
||||||
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
|
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
|
||||||
// annotation "EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwallet->cs_wallet)". The
|
// annotation "EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)". The
|
||||||
// annotation "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid
|
// annotation "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid
|
||||||
// having to resolve the issue of member access into incomplete type CWallet.
|
// having to resolve the issue of member access into incomplete type CWallet.
|
||||||
CAmount GetAvailableCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const NO_THREAD_SAFETY_ANALYSIS;
|
CAmount GetAvailableCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const NO_THREAD_SAFETY_ANALYSIS;
|
||||||
|
@ -637,7 +638,7 @@ class WalletRescanReserver; //forward declarations for ScanForWalletTransactions
|
||||||
* A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
|
* A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
|
||||||
* and provides the ability to create new transactions.
|
* and provides the ability to create new transactions.
|
||||||
*/
|
*/
|
||||||
class CWallet final : public CCryptoKeyStore, public CValidationInterface
|
class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notifications
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::atomic<bool> fAbortRescan{false};
|
std::atomic<bool> fAbortRescan{false};
|
||||||
|
@ -808,6 +809,9 @@ public:
|
||||||
|
|
||||||
std::set<COutPoint> setLockedCoins GUARDED_BY(cs_wallet);
|
std::set<COutPoint> setLockedCoins GUARDED_BY(cs_wallet);
|
||||||
|
|
||||||
|
/** Registered interfaces::Chain::Notifications handler. */
|
||||||
|
std::unique_ptr<interfaces::Handler> m_chain_notifications_handler;
|
||||||
|
|
||||||
/** Interface for accessing chain state. */
|
/** Interface for accessing chain state. */
|
||||||
interfaces::Chain& chain() const { return m_chain; }
|
interfaces::Chain& chain() const { return m_chain; }
|
||||||
|
|
||||||
|
@ -920,8 +924,8 @@ public:
|
||||||
bool AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose=true);
|
bool AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose=true);
|
||||||
void LoadToWallet(const CWalletTx& wtxIn) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
void LoadToWallet(const CWalletTx& wtxIn) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
void TransactionAddedToMempool(const CTransactionRef& tx) override;
|
void TransactionAddedToMempool(const CTransactionRef& tx) override;
|
||||||
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) override;
|
void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& vtxConflicted) override;
|
||||||
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) override;
|
void BlockDisconnected(const CBlock& block) override;
|
||||||
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
|
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
|
||||||
|
|
||||||
struct ScanResult {
|
struct ScanResult {
|
||||||
|
@ -942,7 +946,7 @@ public:
|
||||||
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
|
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
|
||||||
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;
|
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;
|
||||||
void ReacceptWalletTransactions();
|
void ReacceptWalletTransactions();
|
||||||
void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime) override;
|
||||||
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
|
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
|
||||||
std::vector<uint256> ResendWalletTransactionsBefore(interfaces::Chain::Lock& locked_chain, int64_t nTime);
|
std::vector<uint256> ResendWalletTransactionsBefore(interfaces::Chain::Lock& locked_chain, int64_t nTime);
|
||||||
CAmount GetBalance(const isminefilter& filter=ISMINE_SPENDABLE, const int min_depth=0) const;
|
CAmount GetBalance(const isminefilter& filter=ISMINE_SPENDABLE, const int min_depth=0) const;
|
||||||
|
@ -1220,6 +1224,8 @@ public:
|
||||||
|
|
||||||
/** Add a KeyOriginInfo to the wallet */
|
/** Add a KeyOriginInfo to the wallet */
|
||||||
bool AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info);
|
bool AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info);
|
||||||
|
|
||||||
|
friend struct WalletTestingSetup;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A key allocated from the key pool. */
|
/** A key allocated from the key pool. */
|
||||||
|
|
Loading…
Reference in a new issue