Skip to content

Commit

Permalink
interfaces: Add schedulerMockForward method so mockscheduler RPC can …
Browse files Browse the repository at this point in the history
…work across processes

Needed to fix new wallet_groups.py and wallet_resendwallettransactions.py tests
with multiprocess bitcoin-node executable.
  • Loading branch information
ryanofsky committed Oct 20, 2023
1 parent 924327e commit 62a0cea
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ class ChainClient

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

//! Mock the scheduler to fast forward in time.
virtual void schedulerMockForward(std::chrono::seconds delta_seconds) = 0;
};

//! Return implementation of Chain interface.
Expand Down
3 changes: 3 additions & 0 deletions src/rpc/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ static RPCHelpMan mockscheduler()
const NodeContext& node_context{EnsureAnyNodeContext(request.context)};
CHECK_NONFATAL(node_context.scheduler)->MockForward(std::chrono::seconds{delta_seconds});
SyncWithValidationInterfaceQueue();
for (const auto& chain_client : node_context.chain_clients) {
chain_client->schedulerMockForward(std::chrono::seconds(delta_seconds));
}

return UniValue::VNULL;
},
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>

class ArgsManager;
class CScheduler;
namespace interfaces {
class Chain;
class Wallet;
Expand All @@ -34,6 +35,7 @@ using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wall
//! behavior.
struct WalletContext {
interfaces::Chain* chain{nullptr};
CScheduler* scheduler{nullptr};
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
// It is unsafe to lock this after locking a CWallet::cs_wallet mutex because
// this could introduce inconsistent lock ordering and cause deadlocks.
Expand Down
8 changes: 7 additions & 1 deletion src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <policy/fees.h>
#include <primitives/transaction.h>
#include <rpc/server.h>
#include <scheduler.h>
#include <support/allocators/secure.h>
#include <sync.h>
#include <uint256.h>
Expand Down Expand Up @@ -585,10 +586,15 @@ class WalletLoaderImpl : public WalletLoader
}
bool verify() override { return VerifyWallets(m_context); }
bool load() override { return LoadWallets(m_context); }
void start(CScheduler& scheduler) override { return StartWallets(m_context, scheduler); }
void start(CScheduler& scheduler) override
{
m_context.scheduler = &scheduler;
return StartWallets(m_context);
}
void flush() override { return FlushWallets(m_context); }
void stop() override { return StopWallets(m_context); }
void setMockTime(int64_t time) override { return SetMockTime(time); }
void schedulerMockForward(std::chrono::seconds delta) override { m_context.scheduler->MockForward(delta); }

//! WalletLoader methods
util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) override
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ bool LoadWallets(WalletContext& context)
}
}

void StartWallets(WalletContext& context, CScheduler& scheduler)
void StartWallets(WalletContext& context)
{
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
pwallet->postInitProcess();
}

// Schedule periodic wallet flushes and tx rebroadcasts
if (context.args->GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
scheduler.scheduleEvery([&context] { MaybeCompactWalletDB(context); }, std::chrono::milliseconds{500});
context.scheduler->scheduleEvery([&context] { MaybeCompactWalletDB(context); }, std::chrono::milliseconds{500});
}
scheduler.scheduleEvery([&context] { MaybeResendWalletTxs(context); }, 1min);
context.scheduler->scheduleEvery([&context] { MaybeResendWalletTxs(context); }, 1min);
}

void FlushWallets(WalletContext& context)
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool VerifyWallets(WalletContext& context);
bool LoadWallets(WalletContext& context);

//! Complete startup of wallets.
void StartWallets(WalletContext& context, CScheduler& scheduler);
void StartWallets(WalletContext& context);

//! Flush all wallets in preparation for shutdown.
void FlushWallets(WalletContext& context);
Expand Down

0 comments on commit 62a0cea

Please sign in to comment.