Skip to content

Commit

Permalink
[backport#17999 1/2] Add ChainClient setMockTime, getWallets methods
Browse files Browse the repository at this point in the history
Summary:
Needed to set mock times, and get wallet interface pointers correctly when
wallet code is running in a different process from node code.

---

bitcoin/bitcoin@1dde238

Partial backport of Core [[bitcoin/bitcoin#17999 | PR17999]]

Test Plan:
  ninja check check-functional

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D6995
  • Loading branch information
ryanofsky authored and majcosta committed Oct 16, 2020
1 parent 12d862c commit a4ab5f7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ class ChainClient {

//! Shut down client.
virtual void stop() = 0;

//! Set mock time.
virtual void setMockTime(int64_t time) = 0;

//! Return interfaces for accessing wallets (if any).
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
};

//! Return implementation of Chain interface.
Expand Down
6 changes: 4 additions & 2 deletions src/interfaces/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,10 @@ namespace {
}
std::vector<std::unique_ptr<Wallet>> getWallets() override {
std::vector<std::unique_ptr<Wallet>> wallets;
for (const std::shared_ptr<CWallet> &wallet : GetWallets()) {
wallets.emplace_back(MakeWallet(wallet));
for (auto &client : m_context.chain_clients) {
auto client_wallets = client->getWallets();
std::move(client_wallets.begin(), client_wallets.end(),
std::back_inserter(wallets));
}
return wallets;
}
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ namespace {
}
void flush() override { return FlushWallets(); }
void stop() override { return StopWallets(); }
void setMockTime(int64_t time) override { return SetMockTime(time); }
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;
}
~WalletClientImpl() override { UnloadWallets(); }

Chain &m_chain;
Expand Down
12 changes: 9 additions & 3 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <chainparams.h>
#include <config.h>
#include <httpserver.h>
#include <interfaces/chain.h>
#include <key_io.h>
#include <logging.h>
#include <node/context.h>
Expand Down Expand Up @@ -448,12 +449,17 @@ static UniValue setmocktime(const Config &config,
LOCK(cs_main);

RPCTypeCheck(request.params, {UniValue::VNUM});
int64_t mockTime = request.params[0].get_int64();
if (mockTime < 0) {
int64_t time = request.params[0].get_int64();
if (time < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER,
"Timestamp must be 0 or greater");
}
SetMockTime(mockTime);
SetMockTime(time);
if (g_rpc_node) {
for (const auto &chain_client : g_rpc_node->chain_clients) {
chain_client->setMockTime(time);
}
}

return NullUniValue;
}
Expand Down

0 comments on commit a4ab5f7

Please sign in to comment.