diff options
Diffstat (limited to 'src/interfaces')
-rw-r--r-- | src/interfaces/chain.cpp | 417 | ||||
-rw-r--r-- | src/interfaces/node.cpp | 303 | ||||
-rw-r--r-- | src/interfaces/wallet.cpp | 574 |
3 files changed, 0 insertions, 1294 deletions
diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp deleted file mode 100644 index 4c5ebe66fc..0000000000 --- a/src/interfaces/chain.cpp +++ /dev/null @@ -1,417 +0,0 @@ -// Copyright (c) 2018-2020 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 <interfaces/chain.h> - -#include <chain.h> -#include <chainparams.h> -#include <interfaces/handler.h> -#include <interfaces/wallet.h> -#include <net.h> -#include <net_processing.h> -#include <node/coin.h> -#include <node/context.h> -#include <node/transaction.h> -#include <node/ui_interface.h> -#include <policy/fees.h> -#include <policy/policy.h> -#include <policy/rbf.h> -#include <policy/settings.h> -#include <primitives/block.h> -#include <primitives/transaction.h> -#include <rpc/protocol.h> -#include <rpc/server.h> -#include <shutdown.h> -#include <sync.h> -#include <timedata.h> -#include <txmempool.h> -#include <uint256.h> -#include <univalue.h> -#include <util/system.h> -#include <validation.h> -#include <validationinterface.h> - -#include <memory> -#include <utility> - -namespace interfaces { -namespace { - -bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock) -{ - if (!index) return false; - if (block.m_hash) *block.m_hash = index->GetBlockHash(); - if (block.m_height) *block.m_height = index->nHeight; - if (block.m_time) *block.m_time = index->GetBlockTime(); - if (block.m_max_time) *block.m_max_time = index->GetBlockTimeMax(); - if (block.m_mtp_time) *block.m_mtp_time = index->GetMedianTimePast(); - if (block.m_data) { - REVERSE_LOCK(lock); - if (!ReadBlockFromDisk(*block.m_data, index, Params().GetConsensus())) block.m_data->SetNull(); - } - return true; -} - -class NotificationsProxy : public CValidationInterface -{ -public: - explicit NotificationsProxy(std::shared_ptr<Chain::Notifications> notifications) - : m_notifications(std::move(notifications)) {} - virtual ~NotificationsProxy() = default; - void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) override - { - m_notifications->transactionAddedToMempool(tx, mempool_sequence); - } - void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override - { - m_notifications->transactionRemovedFromMempool(tx, reason, mempool_sequence); - } - void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override - { - m_notifications->blockConnected(*block, index->nHeight); - } - void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override - { - m_notifications->blockDisconnected(*block, index->nHeight); - } - void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override - { - m_notifications->updatedBlockTip(); - } - void ChainStateFlushed(const CBlockLocator& locator) override { m_notifications->chainStateFlushed(locator); } - std::shared_ptr<Chain::Notifications> m_notifications; -}; - -class NotificationsHandlerImpl : public Handler -{ -public: - explicit NotificationsHandlerImpl(std::shared_ptr<Chain::Notifications> notifications) - : m_proxy(std::make_shared<NotificationsProxy>(std::move(notifications))) - { - RegisterSharedValidationInterface(m_proxy); - } - ~NotificationsHandlerImpl() override { disconnect(); } - void disconnect() override - { - if (m_proxy) { - UnregisterSharedValidationInterface(m_proxy); - m_proxy.reset(); - } - } - std::shared_ptr<NotificationsProxy> m_proxy; -}; - -class RpcHandlerImpl : public Handler -{ -public: - explicit 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() 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 -{ -public: - explicit ChainImpl(NodeContext& node) : m_node(node) {} - Optional<int> getHeight() override - { - LOCK(::cs_main); - int height = ::ChainActive().Height(); - if (height >= 0) { - return height; - } - return nullopt; - } - Optional<int> getBlockHeight(const uint256& hash) override - { - LOCK(::cs_main); - CBlockIndex* block = LookupBlockIndex(hash); - if (block && ::ChainActive().Contains(block)) { - return block->nHeight; - } - return nullopt; - } - uint256 getBlockHash(int height) override - { - LOCK(::cs_main); - CBlockIndex* block = ::ChainActive()[height]; - assert(block); - return block->GetBlockHash(); - } - bool haveBlockOnDisk(int height) override - { - LOCK(cs_main); - CBlockIndex* block = ::ChainActive()[height]; - return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0; - } - Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) override - { - LOCK(cs_main); - CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time, height); - if (block) { - if (hash) *hash = block->GetBlockHash(); - return block->nHeight; - } - return nullopt; - } - CBlockLocator getTipLocator() override - { - LOCK(cs_main); - return ::ChainActive().GetLocator(); - } - bool checkFinalTx(const CTransaction& tx) override - { - LOCK(cs_main); - return CheckFinalTx(tx); - } - Optional<int> findLocatorFork(const CBlockLocator& locator) override - { - LOCK(cs_main); - if (CBlockIndex* fork = FindForkInGlobalIndex(::ChainActive(), locator)) { - return fork->nHeight; - } - return nullopt; - } - bool findBlock(const uint256& hash, const FoundBlock& block) override - { - WAIT_LOCK(cs_main, lock); - return FillBlock(LookupBlockIndex(hash), block, lock); - } - bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override - { - WAIT_LOCK(cs_main, lock); - return FillBlock(ChainActive().FindEarliestAtLeast(min_time, min_height), block, lock); - } - bool findNextBlock(const uint256& block_hash, int block_height, const FoundBlock& next, bool* reorg) override { - WAIT_LOCK(cs_main, lock); - CBlockIndex* block = ChainActive()[block_height]; - if (block && block->GetBlockHash() != block_hash) block = nullptr; - if (reorg) *reorg = !block; - return FillBlock(block ? ChainActive()[block_height + 1] : nullptr, next, lock); - } - bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out) override - { - WAIT_LOCK(cs_main, lock); - if (const CBlockIndex* block = LookupBlockIndex(block_hash)) { - if (const CBlockIndex* ancestor = block->GetAncestor(ancestor_height)) { - return FillBlock(ancestor, ancestor_out, lock); - } - } - return FillBlock(nullptr, ancestor_out, lock); - } - bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override - { - WAIT_LOCK(cs_main, lock); - const CBlockIndex* block = LookupBlockIndex(block_hash); - const CBlockIndex* ancestor = LookupBlockIndex(ancestor_hash); - if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr; - return FillBlock(ancestor, ancestor_out, lock); - } - bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override - { - WAIT_LOCK(cs_main, lock); - const CBlockIndex* block1 = LookupBlockIndex(block_hash1); - const CBlockIndex* block2 = LookupBlockIndex(block_hash2); - const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr; - // Using & instead of && below to avoid short circuiting and leaving - // output uninitialized. - return FillBlock(ancestor, ancestor_out, lock) & FillBlock(block1, block1_out, lock) & FillBlock(block2, block2_out, lock); - } - void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); } - double guessVerificationProgress(const uint256& block_hash) override - { - LOCK(cs_main); - return GuessVerificationProgress(Params().TxData(), LookupBlockIndex(block_hash)); - } - bool hasBlocks(const uint256& block_hash, int min_height, Optional<int> max_height) override - { - // hasBlocks returns true if all ancestors of block_hash in specified - // range have block data (are not pruned), false if any ancestors in - // specified range are missing data. - // - // For simplicity and robustness, min_height and max_height are only - // used to limit the range, and passing min_height that's too low or - // max_height that's too high will not crash or change the result. - LOCK(::cs_main); - if (CBlockIndex* block = LookupBlockIndex(block_hash)) { - if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height); - for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) { - // Check pprev to not segfault if min_height is too low - if (block->nHeight <= min_height || !block->pprev) return true; - } - } - return false; - } - RBFTransactionState isRBFOptIn(const CTransaction& tx) override - { - if (!m_node.mempool) return IsRBFOptInEmptyMempool(tx); - LOCK(m_node.mempool->cs); - return IsRBFOptIn(tx, *m_node.mempool); - } - bool hasDescendantsInMempool(const uint256& txid) override - { - if (!m_node.mempool) return false; - LOCK(m_node.mempool->cs); - auto it = m_node.mempool->GetIter(txid); - return it && (*it)->GetCountWithDescendants() > 1; - } - bool broadcastTransaction(const CTransactionRef& tx, - const CAmount& max_tx_fee, - bool relay, - std::string& err_string) override - { - const TransactionError err = BroadcastTransaction(m_node, tx, err_string, max_tx_fee, relay, /*wait_callback*/ false); - // Chain clients only care about failures to accept the tx to the mempool. Disregard non-mempool related failures. - // Note: this will need to be updated if BroadcastTransactions() is updated to return other non-mempool failures - // that Chain clients do not need to know about. - return TransactionError::OK == err; - } - void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) override - { - ancestors = descendants = 0; - if (!m_node.mempool) return; - m_node.mempool->GetTransactionAncestry(txid, ancestors, descendants); - } - void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) override - { - limit_ancestor_count = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT); - limit_descendant_count = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT); - } - bool checkChainLimits(const CTransactionRef& tx) override - { - if (!m_node.mempool) return true; - LockPoints lp; - CTxMemPoolEntry entry(tx, 0, 0, 0, false, 0, lp); - CTxMemPool::setEntries ancestors; - auto limit_ancestor_count = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT); - auto limit_ancestor_size = gArgs.GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT) * 1000; - auto limit_descendant_count = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT); - auto limit_descendant_size = gArgs.GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT) * 1000; - std::string unused_error_string; - LOCK(m_node.mempool->cs); - return m_node.mempool->CalculateMemPoolAncestors( - entry, ancestors, limit_ancestor_count, limit_ancestor_size, - limit_descendant_count, limit_descendant_size, unused_error_string); - } - CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override - { - return ::feeEstimator.estimateSmartFee(num_blocks, calc, conservative); - } - unsigned int estimateMaxBlocks() override - { - return ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE); - } - CFeeRate mempoolMinFee() override - { - if (!m_node.mempool) return {}; - return m_node.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; } - bool havePruned() override - { - LOCK(cs_main); - return ::fHavePruned; - } - bool isReadyToBroadcast() override { return !::fImporting && !::fReindex && !isInitialBlockDownload(); } - bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); } - bool shutdownRequested() override { return ShutdownRequested(); } - int64_t getAdjustedTime() override { return GetAdjustedTime(); } - void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); } - void initWarning(const bilingual_str& message) override { InitWarning(message); } - void initError(const bilingual_str& message) override { InitError(message); } - void showProgress(const std::string& title, int progress, bool resume_possible) override - { - ::uiInterface.ShowProgress(title, progress, resume_possible); - } - std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) override - { - return MakeUnique<NotificationsHandlerImpl>(std::move(notifications)); - } - void waitForNotificationsIfTipChanged(const uint256& old_tip) override - { - if (!old_tip.IsNull()) { - LOCK(::cs_main); - if (old_tip == ::ChainActive().Tip()->GetBlockHash()) return; - } - SyncWithValidationInterfaceQueue(); - } - std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override - { - return MakeUnique<RpcHandlerImpl>(command); - } - bool rpcEnableDeprecated(const std::string& method) override { return IsDeprecatedRPCEnabled(method); } - void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) override - { - RPCRunLater(name, std::move(fn), seconds); - } - int rpcSerializationFlags() override { return RPCSerializationFlags(); } - util::SettingsValue getRwSetting(const std::string& name) override - { - util::SettingsValue result; - gArgs.LockSettings([&](const util::Settings& settings) { - if (const util::SettingsValue* value = util::FindKey(settings.rw_settings, name)) { - result = *value; - } - }); - return result; - } - bool updateRwSetting(const std::string& name, const util::SettingsValue& value) override - { - gArgs.LockSettings([&](util::Settings& settings) { - if (value.isNull()) { - settings.rw_settings.erase(name); - } else { - settings.rw_settings[name] = value; - } - }); - return gArgs.WriteSettingsFile(); - } - void requestMempoolTransactions(Notifications& notifications) override - { - if (!m_node.mempool) return; - LOCK2(::cs_main, m_node.mempool->cs); - for (const CTxMemPoolEntry& entry : m_node.mempool->mapTx) { - notifications.transactionAddedToMempool(entry.GetSharedTx(), 0 /* mempool_sequence */); - } - } - NodeContext& m_node; -}; -} // namespace - -std::unique_ptr<Chain> MakeChain(NodeContext& node) { return MakeUnique<ChainImpl>(node); } - -} // namespace interfaces diff --git a/src/interfaces/node.cpp b/src/interfaces/node.cpp deleted file mode 100644 index 2c5f8627e6..0000000000 --- a/src/interfaces/node.cpp +++ /dev/null @@ -1,303 +0,0 @@ -// Copyright (c) 2018-2020 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 <interfaces/node.h> - -#include <addrdb.h> -#include <banman.h> -#include <chain.h> -#include <chainparams.h> -#include <init.h> -#include <interfaces/chain.h> -#include <interfaces/handler.h> -#include <interfaces/wallet.h> -#include <net.h> -#include <net_processing.h> -#include <netaddress.h> -#include <netbase.h> -#include <node/context.h> -#include <node/ui_interface.h> -#include <policy/feerate.h> -#include <policy/fees.h> -#include <policy/settings.h> -#include <primitives/block.h> -#include <rpc/server.h> -#include <shutdown.h> -#include <support/allocators/secure.h> -#include <sync.h> -#include <txmempool.h> -#include <util/check.h> -#include <util/ref.h> -#include <util/system.h> -#include <util/translation.h> -#include <validation.h> -#include <warnings.h> - -#if defined(HAVE_CONFIG_H) -#include <config/bitcoin-config.h> -#endif - -#include <univalue.h> - -#include <boost/signals2/signal.hpp> - -namespace interfaces { -namespace { - -class NodeImpl : public Node -{ -public: - NodeImpl(NodeContext* context) { setContext(context); } - void initLogging() override { InitLogging(*Assert(m_context->args)); } - void initParameterInteraction() override { InitParameterInteraction(*Assert(m_context->args)); } - bilingual_str getWarnings() override { return GetWarnings(true); } - uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); } - bool baseInitialize() override - { - return AppInitBasicSetup(gArgs) && AppInitParameterInteraction(gArgs) && AppInitSanityChecks() && - AppInitLockDataDirectory() && AppInitInterfaces(*m_context); - } - bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override - { - return AppInitMain(m_context_ref, *m_context, tip_info); - } - void appShutdown() override - { - Interrupt(*m_context); - Shutdown(*m_context); - } - void startShutdown() override - { - StartShutdown(); - // Stop RPC for clean shutdown if any of waitfor* commands is executed. - if (gArgs.GetBoolArg("-server", false)) { - InterruptRPC(); - StopRPC(); - } - } - bool shutdownRequested() override { return ShutdownRequested(); } - void mapPort(bool use_upnp) override - { - if (use_upnp) { - StartMapPort(); - } else { - InterruptMapPort(); - StopMapPort(); - } - } - bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); } - size_t getNodeCount(CConnman::NumConnections flags) override - { - return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0; - } - bool getNodesStats(NodesStats& stats) override - { - stats.clear(); - - if (m_context->connman) { - std::vector<CNodeStats> stats_temp; - m_context->connman->GetNodeStats(stats_temp); - - stats.reserve(stats_temp.size()); - for (auto& node_stats_temp : stats_temp) { - stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats()); - } - - // Try to retrieve the CNodeStateStats for each node. - TRY_LOCK(::cs_main, lockMain); - if (lockMain) { - for (auto& node_stats : stats) { - std::get<1>(node_stats) = - GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats)); - } - } - return true; - } - return false; - } - bool getBanned(banmap_t& banmap) override - { - if (m_context->banman) { - m_context->banman->GetBanned(banmap); - return true; - } - return false; - } - bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override - { - if (m_context->banman) { - m_context->banman->Ban(net_addr, ban_time_offset); - return true; - } - return false; - } - bool unban(const CSubNet& ip) override - { - if (m_context->banman) { - m_context->banman->Unban(ip); - return true; - } - return false; - } - bool disconnectByAddress(const CNetAddr& net_addr) override - { - if (m_context->connman) { - return m_context->connman->DisconnectNode(net_addr); - } - return false; - } - bool disconnectById(NodeId id) override - { - if (m_context->connman) { - return m_context->connman->DisconnectNode(id); - } - return false; - } - int64_t getTotalBytesRecv() override { return m_context->connman ? m_context->connman->GetTotalBytesRecv() : 0; } - int64_t getTotalBytesSent() override { return m_context->connman ? m_context->connman->GetTotalBytesSent() : 0; } - size_t getMempoolSize() override { return m_context->mempool ? m_context->mempool->size() : 0; } - size_t getMempoolDynamicUsage() override { return m_context->mempool ? m_context->mempool->DynamicMemoryUsage() : 0; } - bool getHeaderTip(int& height, int64_t& block_time) override - { - LOCK(::cs_main); - if (::pindexBestHeader) { - height = ::pindexBestHeader->nHeight; - block_time = ::pindexBestHeader->GetBlockTime(); - return true; - } - return false; - } - int getNumBlocks() override - { - LOCK(::cs_main); - return ::ChainActive().Height(); - } - uint256 getBestBlockHash() override - { - const CBlockIndex* tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip()); - return tip ? tip->GetBlockHash() : Params().GenesisBlock().GetHash(); - } - int64_t getLastBlockTime() override - { - LOCK(::cs_main); - if (::ChainActive().Tip()) { - return ::ChainActive().Tip()->GetBlockTime(); - } - return Params().GenesisBlock().GetBlockTime(); // Genesis block's time of current network - } - double getVerificationProgress() override - { - const CBlockIndex* tip; - { - LOCK(::cs_main); - tip = ::ChainActive().Tip(); - } - return GuessVerificationProgress(Params().TxData(), tip); - } - bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); } - bool getReindex() override { return ::fReindex; } - bool getImporting() override { return ::fImporting; } - void setNetworkActive(bool active) override - { - if (m_context->connman) { - m_context->connman->SetNetworkActive(active); - } - } - bool getNetworkActive() override { return m_context->connman && m_context->connman->GetNetworkActive(); } - CFeeRate estimateSmartFee(int num_blocks, bool conservative, int* returned_target = nullptr) override - { - FeeCalculation fee_calc; - CFeeRate result = ::feeEstimator.estimateSmartFee(num_blocks, &fee_calc, conservative); - if (returned_target) { - *returned_target = fee_calc.returnedTarget; - } - return result; - } - CFeeRate getDustRelayFee() override { return ::dustRelayFee; } - UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override - { - JSONRPCRequest req(m_context_ref); - req.params = params; - req.strMethod = command; - req.URI = uri; - return ::tableRPC.execute(req); - } - std::vector<std::string> listRpcCommands() override { return ::tableRPC.listCommands(); } - void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) override { RPCSetTimerInterfaceIfUnset(iface); } - void rpcUnsetTimerInterface(RPCTimerInterface* iface) override { RPCUnsetTimerInterface(iface); } - bool getUnspentOutput(const COutPoint& output, Coin& coin) override - { - LOCK(::cs_main); - return ::ChainstateActive().CoinsTip().GetCoin(output, coin); - } - WalletClient& walletClient() override - { - return *Assert(m_context->wallet_client); - } - std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override - { - return MakeHandler(::uiInterface.InitMessage_connect(fn)); - } - std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override - { - return MakeHandler(::uiInterface.ThreadSafeMessageBox_connect(fn)); - } - std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override - { - return MakeHandler(::uiInterface.ThreadSafeQuestion_connect(fn)); - } - std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override - { - return MakeHandler(::uiInterface.ShowProgress_connect(fn)); - } - std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override - { - return MakeHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn)); - } - std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override - { - return MakeHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn)); - } - std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override - { - return MakeHandler(::uiInterface.NotifyAlertChanged_connect(fn)); - } - std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override - { - return MakeHandler(::uiInterface.BannedListChanged_connect(fn)); - } - std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override - { - return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) { - fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()}, - GuessVerificationProgress(Params().TxData(), block)); - })); - } - std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override - { - return MakeHandler( - ::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) { - fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()}, - /* verification progress is unused when a header was received */ 0); - })); - } - NodeContext* context() override { return m_context; } - void setContext(NodeContext* context) override - { - m_context = context; - if (context) { - m_context_ref.Set(*context); - } else { - m_context_ref.Clear(); - } - } - NodeContext* m_context{nullptr}; - util::Ref m_context_ref; -}; - -} // namespace - -std::unique_ptr<Node> MakeNode(NodeContext* context) { return MakeUnique<NodeImpl>(context); } - -} // namespace interfaces diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp deleted file mode 100644 index f68016b557..0000000000 --- a/src/interfaces/wallet.cpp +++ /dev/null @@ -1,574 +0,0 @@ -// Copyright (c) 2018-2020 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 <interfaces/wallet.h> - -#include <amount.h> -#include <interfaces/chain.h> -#include <interfaces/handler.h> -#include <policy/fees.h> -#include <primitives/transaction.h> -#include <rpc/server.h> -#include <script/standard.h> -#include <support/allocators/secure.h> -#include <sync.h> -#include <uint256.h> -#include <util/check.h> -#include <util/ref.h> -#include <util/system.h> -#include <util/ui_change_type.h> -#include <wallet/context.h> -#include <wallet/feebumper.h> -#include <wallet/fees.h> -#include <wallet/ismine.h> -#include <wallet/load.h> -#include <wallet/rpcwallet.h> -#include <wallet/wallet.h> - -#include <memory> -#include <string> -#include <utility> -#include <vector> - -namespace interfaces { -namespace { - -//! Construct wallet tx struct. -WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx) -{ - LOCK(wallet.cs_wallet); - WalletTx result; - result.tx = wtx.tx; - result.txin_is_mine.reserve(wtx.tx->vin.size()); - for (const auto& txin : wtx.tx->vin) { - result.txin_is_mine.emplace_back(wallet.IsMine(txin)); - } - result.txout_is_mine.reserve(wtx.tx->vout.size()); - result.txout_address.reserve(wtx.tx->vout.size()); - result.txout_address_is_mine.reserve(wtx.tx->vout.size()); - for (const auto& txout : wtx.tx->vout) { - result.txout_is_mine.emplace_back(wallet.IsMine(txout)); - result.txout_address.emplace_back(); - result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ? - wallet.IsMine(result.txout_address.back()) : - ISMINE_NO); - } - result.credit = wtx.GetCredit(ISMINE_ALL); - result.debit = wtx.GetDebit(ISMINE_ALL); - result.change = wtx.GetChange(); - result.time = wtx.GetTxTime(); - result.value_map = wtx.mapValue; - result.is_coinbase = wtx.IsCoinBase(); - return result; -} - -//! Construct wallet tx status struct. -WalletTxStatus MakeWalletTxStatus(CWallet& wallet, const CWalletTx& wtx) -{ - WalletTxStatus result; - result.block_height = wtx.m_confirm.block_height > 0 ? wtx.m_confirm.block_height : std::numeric_limits<int>::max(); - result.blocks_to_maturity = wtx.GetBlocksToMaturity(); - result.depth_in_main_chain = wtx.GetDepthInMainChain(); - result.time_received = wtx.nTimeReceived; - result.lock_time = wtx.tx->nLockTime; - result.is_final = wallet.chain().checkFinalTx(*wtx.tx); - result.is_trusted = wtx.IsTrusted(); - result.is_abandoned = wtx.isAbandoned(); - result.is_coinbase = wtx.IsCoinBase(); - result.is_in_main_chain = wtx.IsInMainChain(); - return result; -} - -//! Construct wallet TxOut struct. -WalletTxOut MakeWalletTxOut(CWallet& wallet, - const CWalletTx& wtx, - int n, - int depth) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) -{ - WalletTxOut result; - result.txout = wtx.tx->vout[n]; - result.time = wtx.GetTxTime(); - result.depth_in_main_chain = depth; - result.is_spent = wallet.IsSpent(wtx.GetHash(), n); - return result; -} - -class WalletImpl : public Wallet -{ -public: - explicit WalletImpl(const std::shared_ptr<CWallet>& wallet) : m_wallet(wallet) {} - - bool encryptWallet(const SecureString& wallet_passphrase) override - { - return m_wallet->EncryptWallet(wallet_passphrase); - } - bool isCrypted() override { return m_wallet->IsCrypted(); } - bool lock() override { return m_wallet->Lock(); } - bool unlock(const SecureString& wallet_passphrase) override { return m_wallet->Unlock(wallet_passphrase); } - bool isLocked() override { return m_wallet->IsLocked(); } - bool changeWalletPassphrase(const SecureString& old_wallet_passphrase, - const SecureString& new_wallet_passphrase) override - { - return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase); - } - void abortRescan() override { m_wallet->AbortRescan(); } - bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); } - std::string getWalletName() override { return m_wallet->GetName(); } - bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) override - { - LOCK(m_wallet->cs_wallet); - std::string error; - return m_wallet->GetNewDestination(type, label, dest, error); - } - bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override - { - std::unique_ptr<SigningProvider> provider = m_wallet->GetSolvingProvider(script); - if (provider) { - return provider->GetPubKey(address, pub_key); - } - return false; - } - SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) override - { - return m_wallet->SignMessage(message, pkhash, str_sig); - } - bool isSpendable(const CTxDestination& dest) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->IsMine(dest) & ISMINE_SPENDABLE; - } - bool haveWatchOnly() override - { - auto spk_man = m_wallet->GetLegacyScriptPubKeyMan(); - if (spk_man) { - return spk_man->HaveWatchOnly(); - } - return false; - }; - bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) override - { - return m_wallet->SetAddressBook(dest, name, purpose); - } - bool delAddressBook(const CTxDestination& dest) override - { - return m_wallet->DelAddressBook(dest); - } - bool getAddress(const CTxDestination& dest, - std::string* name, - isminetype* is_mine, - std::string* purpose) override - { - LOCK(m_wallet->cs_wallet); - auto it = m_wallet->m_address_book.find(dest); - if (it == m_wallet->m_address_book.end() || it->second.IsChange()) { - return false; - } - if (name) { - *name = it->second.GetLabel(); - } - if (is_mine) { - *is_mine = m_wallet->IsMine(dest); - } - if (purpose) { - *purpose = it->second.purpose; - } - return true; - } - std::vector<WalletAddress> getAddresses() override - { - LOCK(m_wallet->cs_wallet); - std::vector<WalletAddress> result; - for (const auto& item : m_wallet->m_address_book) { - if (item.second.IsChange()) continue; - result.emplace_back(item.first, m_wallet->IsMine(item.first), item.second.GetLabel(), item.second.purpose); - } - return result; - } - bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override - { - LOCK(m_wallet->cs_wallet); - WalletBatch batch{m_wallet->GetDatabase()}; - return m_wallet->AddDestData(batch, dest, key, value); - } - bool eraseDestData(const CTxDestination& dest, const std::string& key) override - { - LOCK(m_wallet->cs_wallet); - WalletBatch batch{m_wallet->GetDatabase()}; - return m_wallet->EraseDestData(batch, dest, key); - } - std::vector<std::string> getDestValues(const std::string& prefix) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->GetDestValues(prefix); - } - void lockCoin(const COutPoint& output) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->LockCoin(output); - } - void unlockCoin(const COutPoint& output) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->UnlockCoin(output); - } - bool isLockedCoin(const COutPoint& output) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->IsLockedCoin(output.hash, output.n); - } - void listLockedCoins(std::vector<COutPoint>& outputs) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->ListLockedCoins(outputs); - } - CTransactionRef createTransaction(const std::vector<CRecipient>& recipients, - const CCoinControl& coin_control, - bool sign, - int& change_pos, - CAmount& fee, - bilingual_str& fail_reason) override - { - LOCK(m_wallet->cs_wallet); - CTransactionRef tx; - FeeCalculation fee_calc_out; - if (!m_wallet->CreateTransaction(recipients, tx, fee, change_pos, - fail_reason, coin_control, fee_calc_out, sign)) { - return {}; - } - return tx; - } - void commitTransaction(CTransactionRef tx, - WalletValueMap value_map, - WalletOrderForm order_form) override - { - LOCK(m_wallet->cs_wallet); - m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form)); - } - bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); } - bool abandonTransaction(const uint256& txid) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->AbandonTransaction(txid); - } - bool transactionCanBeBumped(const uint256& txid) override - { - return feebumper::TransactionCanBeBumped(*m_wallet.get(), txid); - } - bool createBumpTransaction(const uint256& txid, - const CCoinControl& coin_control, - std::vector<bilingual_str>& errors, - CAmount& old_fee, - CAmount& new_fee, - CMutableTransaction& mtx) override - { - return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx) == feebumper::Result::OK; - } - bool signBumpTransaction(CMutableTransaction& mtx) override { return feebumper::SignTransaction(*m_wallet.get(), mtx); } - bool commitBumpTransaction(const uint256& txid, - CMutableTransaction&& mtx, - std::vector<bilingual_str>& errors, - uint256& bumped_txid) override - { - return feebumper::CommitTransaction(*m_wallet.get(), txid, std::move(mtx), errors, bumped_txid) == - feebumper::Result::OK; - } - CTransactionRef getTx(const uint256& txid) override - { - LOCK(m_wallet->cs_wallet); - auto mi = m_wallet->mapWallet.find(txid); - if (mi != m_wallet->mapWallet.end()) { - return mi->second.tx; - } - return {}; - } - WalletTx getWalletTx(const uint256& txid) override - { - LOCK(m_wallet->cs_wallet); - auto mi = m_wallet->mapWallet.find(txid); - if (mi != m_wallet->mapWallet.end()) { - return MakeWalletTx(*m_wallet, mi->second); - } - return {}; - } - std::vector<WalletTx> getWalletTxs() override - { - LOCK(m_wallet->cs_wallet); - std::vector<WalletTx> result; - result.reserve(m_wallet->mapWallet.size()); - for (const auto& entry : m_wallet->mapWallet) { - result.emplace_back(MakeWalletTx(*m_wallet, entry.second)); - } - return result; - } - bool tryGetTxStatus(const uint256& txid, - interfaces::WalletTxStatus& tx_status, - int& num_blocks, - int64_t& block_time) override - { - TRY_LOCK(m_wallet->cs_wallet, locked_wallet); - if (!locked_wallet) { - return false; - } - auto mi = m_wallet->mapWallet.find(txid); - if (mi == m_wallet->mapWallet.end()) { - return false; - } - num_blocks = m_wallet->GetLastBlockHeight(); - block_time = -1; - CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time))); - tx_status = MakeWalletTxStatus(*m_wallet, mi->second); - return true; - } - WalletTx getWalletTxDetails(const uint256& txid, - WalletTxStatus& tx_status, - WalletOrderForm& order_form, - bool& in_mempool, - int& num_blocks) override - { - LOCK(m_wallet->cs_wallet); - auto mi = m_wallet->mapWallet.find(txid); - if (mi != m_wallet->mapWallet.end()) { - num_blocks = m_wallet->GetLastBlockHeight(); - in_mempool = mi->second.InMempool(); - order_form = mi->second.vOrderForm; - tx_status = MakeWalletTxStatus(*m_wallet, mi->second); - return MakeWalletTx(*m_wallet, mi->second); - } - return {}; - } - TransactionError fillPSBT(int sighash_type, - bool sign, - bool bip32derivs, - PartiallySignedTransaction& psbtx, - bool& complete, - size_t* n_signed) override - { - return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs, n_signed); - } - WalletBalances getBalances() override - { - const auto bal = m_wallet->GetBalance(); - WalletBalances result; - result.balance = bal.m_mine_trusted; - result.unconfirmed_balance = bal.m_mine_untrusted_pending; - result.immature_balance = bal.m_mine_immature; - result.have_watch_only = haveWatchOnly(); - if (result.have_watch_only) { - result.watch_only_balance = bal.m_watchonly_trusted; - result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending; - result.immature_watch_only_balance = bal.m_watchonly_immature; - } - return result; - } - bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override - { - TRY_LOCK(m_wallet->cs_wallet, locked_wallet); - if (!locked_wallet) { - return false; - } - block_hash = m_wallet->GetLastBlockHash(); - balances = getBalances(); - return true; - } - CAmount getBalance() override { return m_wallet->GetBalance().m_mine_trusted; } - CAmount getAvailableBalance(const CCoinControl& coin_control) override - { - return m_wallet->GetAvailableBalance(&coin_control); - } - isminetype txinIsMine(const CTxIn& txin) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->IsMine(txin); - } - isminetype txoutIsMine(const CTxOut& txout) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->IsMine(txout); - } - CAmount getDebit(const CTxIn& txin, isminefilter filter) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->GetDebit(txin, filter); - } - CAmount getCredit(const CTxOut& txout, isminefilter filter) override - { - LOCK(m_wallet->cs_wallet); - return m_wallet->GetCredit(txout, filter); - } - CoinsList listCoins() override - { - LOCK(m_wallet->cs_wallet); - CoinsList result; - for (const auto& entry : m_wallet->ListCoins()) { - auto& group = result[entry.first]; - for (const auto& coin : entry.second) { - group.emplace_back(COutPoint(coin.tx->GetHash(), coin.i), - MakeWalletTxOut(*m_wallet, *coin.tx, coin.i, coin.nDepth)); - } - } - return result; - } - std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) override - { - LOCK(m_wallet->cs_wallet); - std::vector<WalletTxOut> result; - result.reserve(outputs.size()); - for (const auto& output : outputs) { - result.emplace_back(); - auto it = m_wallet->mapWallet.find(output.hash); - if (it != m_wallet->mapWallet.end()) { - int depth = it->second.GetDepthInMainChain(); - if (depth >= 0) { - result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth); - } - } - } - return result; - } - CAmount getRequiredFee(unsigned int tx_bytes) override { return GetRequiredFee(*m_wallet, tx_bytes); } - CAmount getMinimumFee(unsigned int tx_bytes, - const CCoinControl& coin_control, - int* returned_target, - FeeReason* reason) override - { - FeeCalculation fee_calc; - CAmount result; - result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, &fee_calc); - if (returned_target) *returned_target = fee_calc.returnedTarget; - if (reason) *reason = fee_calc.reason; - return result; - } - unsigned int getConfirmTarget() override { return m_wallet->m_confirm_target; } - bool hdEnabled() override { return m_wallet->IsHDEnabled(); } - bool canGetAddresses() override { return m_wallet->CanGetAddresses(); } - bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); } - OutputType getDefaultAddressType() override { return m_wallet->m_default_address_type; } - CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; } - void remove() override - { - RemoveWallet(m_wallet, false /* load_on_start */); - } - bool isLegacy() override { return m_wallet->IsLegacy(); } - std::unique_ptr<Handler> handleUnload(UnloadFn fn) override - { - return MakeHandler(m_wallet->NotifyUnload.connect(fn)); - } - std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override - { - return MakeHandler(m_wallet->ShowProgress.connect(fn)); - } - std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override - { - return MakeHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); })); - } - std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override - { - return MakeHandler(m_wallet->NotifyAddressBookChanged.connect( - [fn](CWallet*, const CTxDestination& address, const std::string& label, bool is_mine, - const std::string& purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); })); - } - std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override - { - return MakeHandler(m_wallet->NotifyTransactionChanged.connect( - [fn](CWallet*, const uint256& txid, ChangeType status) { fn(txid, status); })); - } - std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) override - { - return MakeHandler(m_wallet->NotifyWatchonlyChanged.connect(fn)); - } - std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override - { - return MakeHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn)); - } - CWallet* wallet() override { return m_wallet.get(); } - - std::shared_ptr<CWallet> m_wallet; -}; - -class WalletClientImpl : public WalletClient -{ -public: - WalletClientImpl(Chain& chain, ArgsManager& args) - { - m_context.chain = &chain; - m_context.args = &args; - } - ~WalletClientImpl() override { UnloadWallets(); } - - //! ChainClient methods - void registerRpcs() override - { - for (const CRPCCommand& command : GetWalletRPCCommands()) { - m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) { - return command.actor({request, m_context}, result, last_handler); - }, command.argNames, command.unique_id); - m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); - } - } - bool verify() override { return VerifyWallets(*m_context.chain); } - bool load() override { return LoadWallets(*m_context.chain); } - void start(CScheduler& scheduler) override { return StartWallets(scheduler, *Assert(m_context.args)); } - void flush() override { return FlushWallets(); } - void stop() override { return StopWallets(); } - void setMockTime(int64_t time) override { return SetMockTime(time); } - - //! WalletClient methods - std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) override - { - std::shared_ptr<CWallet> wallet; - DatabaseOptions options; - DatabaseStatus status; - options.require_create = true; - options.create_flags = wallet_creation_flags; - options.create_passphrase = passphrase; - return MakeWallet(CreateWallet(*m_context.chain, name, true /* load_on_start */, options, status, error, warnings)); - } - std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override - { - DatabaseOptions options; - DatabaseStatus status; - options.require_existing = true; - return MakeWallet(LoadWallet(*m_context.chain, name, true /* load_on_start */, options, status, error, warnings)); - } - std::string getWalletDir() override - { - return GetWalletDir().string(); - } - std::vector<std::string> listWalletDir() override - { - std::vector<std::string> paths; - for (auto& path : ListWalletDir()) { - paths.push_back(path.string()); - } - return paths; - } - std::vector<std::unique_ptr<Wallet>> getWallets() override - { - std::vector<std::unique_ptr<Wallet>> wallets; - for (const auto& wallet : GetWallets()) { - wallets.emplace_back(MakeWallet(wallet)); - } - return wallets; - } - std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override - { - return HandleLoadWallet(std::move(fn)); - } - - WalletContext m_context; - const std::vector<std::string> m_wallet_filenames; - std::vector<std::unique_ptr<Handler>> m_rpc_handlers; - std::list<CRPCCommand> m_rpc_commands; -}; - -} // namespace - -std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet) { return wallet ? MakeUnique<WalletImpl>(wallet) : nullptr; } - -std::unique_ptr<WalletClient> MakeWalletClient(Chain& chain, ArgsManager& args) -{ - return MakeUnique<WalletClientImpl>(chain, args); -} - -} // namespace interfaces |