This PR is only a draft.
The description below will change over time.
This PR removes a potential circular dependency that was discovered in travis while building binaries for this PR #18827
The affected sources/includes in that PR are httprpc and rpc/server
My first "solution" was a forward declare of a single function in rpc/server.cpp. However, that merely defeats the linter and could not count as a proper solution.
After some discussion #18827#pullrequestreview-404116986 I tried to extract certain classes and function declarations into a new include file, rpc/interfaces.h
Now, the circular dependency in that PR is gone, but as this constitutes a much bigger change than the PR wanted to make, I think it should better get its own PR.
The following classes and functions got moved into a new source file pair rpc/interfaces.cpp and rpc/interfaces.h:
class RPCTimerBase
class RPCTimerInterface
class CRPCCommand
class CRPCTable
void SetRPCWarmupStatus(const std::string& newStatus);
void SetRPCWarmupFinished();
bool RPCIsInWarmup(std::string *outStatus);
bool IsRPCRunning();
int64_t GetStartupTime();
int RPCSerializationFlags();
std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
void RPCSetTimerInterface(RPCTimerInterface *iface);
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface);
void RPCUnsetTimerInterface(RPCTimerInterface *iface);
void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
bool IsDeprecatedRPCEnabled(const std::string& method);
In src/Makefile.am there is a new library defined: LIBBITCOIN_SERVER_INTERFACES that contains the implementation and is linked with bitcoind and bitcoin-qt (test and bench libraries are linked as well).
The changes reduce the size of rpc/server.cpp substantially. What remains there are pure server management functionalities:
namespace RPCServer
{
void OnStarted(std::function<void ()> slot);
void OnStopped(std::function<void ()> slot);
}
void StartRPC();
void InterruptRPC();
void StopRPC();
Another advantage coming from these changes is that we got rid of forward declarations like class CRPCTable, class CRPCCommand etc.
Possible future changes in class structure and function grouping
Although this PR isn't aiming for more complex engineering changes, like introducing new classes and other structures, there might be some advantage if we'd in future group the "extracted" functions into new classes. Just like we have CRPCTable that carries table-oriented functions, or CRPCCommand, we could introduce a new class called CRPCTimer:
class CRPCTimer {
public:
/** Set the factory function for timers */
void RPCSetTimerInterface(RPCTimerInterface *iface);
/** Set the factory function for timer, but only, if unset */
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface);
/** Unset factory function for timers */
void RPCUnsetTimerInterface(RPCTimerInterface *iface);
};
And server-status functions could be moved into a new class like this:
class CRPCServerStatus {
public:
void SetRPCWarmupStatus(const std::string& newStatus);
void SetRPCWarmupFinished();
bool RPCIsInWarmup(std::string *outStatus);
bool IsRPCRunning();
int64_t GetStartupTime();
private:
[...here come mutexes, guards, atomics etc. ]
};
Then we could give access to their instances the same way we do with CRPCtable tableRPC now.
extern CRPCTimer timerRPC;
extern CRPCServerStatus statusRPC;