Changeset View
Changeset View
Standalone View
Standalone View
src/interfaces/wallet.cpp
// Copyright (c) 2018 The Bitcoin Core developers | // Copyright (c) 2018 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <interfaces/wallet.h> | #include <interfaces/wallet.h> | ||||
#include <amount.h> | #include <amount.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <config.h> | |||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <interfaces/chain.h> | #include <interfaces/chain.h> | ||||
#include <interfaces/handler.h> | #include <interfaces/handler.h> | ||||
#include <policy/fees.h> | #include <policy/fees.h> | ||||
#include <primitives/transaction.h> | #include <primitives/transaction.h> | ||||
#include <rpc/server.h> | |||||
#include <script/standard.h> | #include <script/standard.h> | ||||
#include <support/allocators/secure.h> | #include <support/allocators/secure.h> | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <ui_interface.h> | #include <ui_interface.h> | ||||
#include <util/check.h> | #include <util/check.h> | ||||
#include <util/ref.h> | |||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <wallet/context.h> | |||||
#include <wallet/fees.h> | #include <wallet/fees.h> | ||||
#include <wallet/ismine.h> | #include <wallet/ismine.h> | ||||
#include <wallet/load.h> | #include <wallet/load.h> | ||||
#include <wallet/rpcdump.h> | #include <wallet/rpcdump.h> | ||||
#include <wallet/wallet.h> | #include <wallet/wallet.h> | ||||
namespace interfaces { | namespace interfaces { | ||||
namespace { | namespace { | ||||
▲ Show 20 Lines • Show All 428 Lines • ▼ Show 20 Lines | public: | ||||
std::shared_ptr<CWallet> m_wallet; | std::shared_ptr<CWallet> m_wallet; | ||||
}; | }; | ||||
class WalletClientImpl : public ChainClient { | class WalletClientImpl : public ChainClient { | ||||
public: | public: | ||||
WalletClientImpl(Chain &chain, | WalletClientImpl(Chain &chain, | ||||
std::vector<std::string> wallet_filenames) | std::vector<std::string> wallet_filenames) | ||||
: m_chain(chain), m_wallet_filenames(std::move(wallet_filenames)) {} | : m_wallet_filenames(std::move(wallet_filenames)) { | ||||
m_context.chain = &chain; | |||||
} | |||||
void registerRpcs(const Span<const CRPCCommand> &commands) { | |||||
for (const CRPCCommand &command : commands) { | |||||
m_rpc_commands.emplace_back( | |||||
command.category, command.name, | |||||
[this, &command](Config &config, | |||||
const JSONRPCRequest &request, | |||||
UniValue &result, bool last_handler) { | |||||
return command.actor(config, {request, m_context}, | |||||
result, last_handler); | |||||
}, | |||||
command.argNames, command.unique_id); | |||||
m_rpc_handlers.emplace_back( | |||||
m_context.chain->handleRpc(m_rpc_commands.back())); | |||||
} | |||||
} | |||||
void registerRpcs() override { | void registerRpcs() override { | ||||
g_rpc_chain = &m_chain; | registerRpcs(GetWalletRPCCommands()); | ||||
RegisterWalletRPCCommands(m_chain, m_rpc_handlers); | registerRpcs(GetWalletDumpRPCCommands()); | ||||
RegisterDumpRPCCommands(m_chain, m_rpc_handlers); | |||||
} | } | ||||
bool verify(const CChainParams &chainParams) override { | bool verify(const CChainParams &chainParams) override { | ||||
return VerifyWallets(chainParams, m_chain, m_wallet_filenames); | return VerifyWallets(chainParams, *m_context.chain, | ||||
m_wallet_filenames); | |||||
} | } | ||||
bool load(const CChainParams &chainParams) override { | bool load(const CChainParams &chainParams) override { | ||||
return LoadWallets(chainParams, m_chain, m_wallet_filenames); | return LoadWallets(chainParams, *m_context.chain, | ||||
m_wallet_filenames); | |||||
} | } | ||||
void start(CScheduler &scheduler) override { | void start(CScheduler &scheduler) override { | ||||
return StartWallets(scheduler); | return StartWallets(scheduler); | ||||
} | } | ||||
void flush() override { return FlushWallets(); } | void flush() override { return FlushWallets(); } | ||||
void stop() override { return StopWallets(); } | void stop() override { return StopWallets(); } | ||||
void setMockTime(int64_t time) override { return SetMockTime(time); } | void setMockTime(int64_t time) override { return SetMockTime(time); } | ||||
std::vector<std::unique_ptr<Wallet>> getWallets() override { | std::vector<std::unique_ptr<Wallet>> getWallets() override { | ||||
std::vector<std::unique_ptr<Wallet>> wallets; | std::vector<std::unique_ptr<Wallet>> wallets; | ||||
for (const auto &wallet : GetWallets()) { | for (const auto &wallet : GetWallets()) { | ||||
wallets.emplace_back(MakeWallet(wallet)); | wallets.emplace_back(MakeWallet(wallet)); | ||||
} | } | ||||
return wallets; | return wallets; | ||||
} | } | ||||
~WalletClientImpl() override { UnloadWallets(); } | ~WalletClientImpl() override { UnloadWallets(); } | ||||
Chain &m_chain; | WalletContext m_context; | ||||
std::vector<std::string> m_wallet_filenames; | std::vector<std::string> m_wallet_filenames; | ||||
std::vector<std::unique_ptr<Handler>> m_rpc_handlers; | std::vector<std::unique_ptr<Handler>> m_rpc_handlers; | ||||
std::list<CRPCCommand> m_rpc_commands; | |||||
}; | }; | ||||
} // namespace | } // namespace | ||||
std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet> &wallet) { | std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet> &wallet) { | ||||
return wallet ? std::make_unique<WalletImpl>(wallet) : nullptr; | return wallet ? std::make_unique<WalletImpl>(wallet) : nullptr; | ||||
} | } | ||||
std::unique_ptr<ChainClient> | std::unique_ptr<ChainClient> | ||||
MakeWalletClient(Chain &chain, std::vector<std::string> wallet_filenames) { | MakeWalletClient(Chain &chain, std::vector<std::string> wallet_filenames) { | ||||
return std::make_unique<WalletClientImpl>(chain, | return std::make_unique<WalletClientImpl>(chain, | ||||
std::move(wallet_filenames)); | std::move(wallet_filenames)); | ||||
} | } | ||||
} // namespace interfaces | } // namespace interfaces |