Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use better singleton default pattern #1836

Merged
merged 9 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/defi-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ static int AppInitRPC(int argc, char* argv[])
tfm::format(std::cerr, "Error reading configuration file: %s\n", error.c_str());
return EXIT_FAILURE;
}
RPCMetadata::InitFromArgs(gArgs);

// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
try {
SelectBaseParams(gArgs.GetChainName());
Expand Down
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ bool AppInitMain(InitInterfaces& interfaces)

InitSignatureCache();
InitScriptExecutionCache();
RPCMetadata::InitFromArgs(gArgs);

int script_threads = gArgs.GetArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
if (script_threads <= 0) {
Expand Down
43 changes: 32 additions & 11 deletions src/masternodes/coinselect.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,43 @@ static const std::string& ARG_STR_WALLET_COIN_OPT_SKIP_SOLVABLE = "-walletcoinop
static const std::string& ARG_STR_WALLET_COIN_OPT_EAGER_SELECT = "-walletcoinopteagerselect";

struct CoinSelectionOptions {
private:
std::optional<bool> fastSelect{};
std::optional<bool> skipSolvable{};
std::optional<bool> eagerSelect{};
private:
std::optional<bool> fastSelect{};
std::optional<bool> skipSolvable{};
std::optional<bool> eagerSelect{};

public:
inline static std::unique_ptr<CoinSelectionOptions> DEFAULT;

public:
bool IsFastSelectEnabled() const { return fastSelect.value_or(false); }
bool IsSkipSolvableEnabled() const { return skipSolvable.value_or(false); }
bool IsEagerSelectEnabled() const { return eagerSelect.value_or(false); }

static void LogValues(const CoinSelectionOptions& m) {
struct V { const std::optional<bool> v; const std::string& arg; };
for (auto &[v, arg]: std::vector<V> {
{ m.fastSelect, ARG_STR_WALLET_FAST_SELECT },
{ m.skipSolvable, ARG_STR_WALLET_COIN_OPT_SKIP_SOLVABLE },
{ m.eagerSelect, ARG_STR_WALLET_COIN_OPT_EAGER_SELECT },
}) {
if (v) LogPrintf("conf: %s: %s\n", arg.substr(1), *v ? "true" : "false");
}
}

static void SetupArgs(ArgsManager& args) {
args.AddArg(ARG_STR_WALLET_FAST_SELECT, strprintf("Faster coin select - Enables walletcoinoptskipsolvable and walletcoinopteagerselect. This ends up in faster selection but has the disadvantage of not being able to pick complex input scripts (default: %u)", DEFAULT_COIN_SELECT_FAST_SELECT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
args.AddArg(ARG_STR_WALLET_COIN_OPT_SKIP_SOLVABLE, strprintf("Coin select option: Skips IsSolvable signable UTXO check (default: %u)", DEFAULT_COIN_SELECT_SKIP_SOLVABLE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
args.AddArg(ARG_STR_WALLET_COIN_OPT_EAGER_SELECT, strprintf("Coin select option: Take fast path and eagerly exit on match even without having through the entire set (default: %u)", DEFAULT_COIN_SELECT_EAGER_SELECT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
}

static CoinSelectionOptions CreateDefault() {
CoinSelectionOptions opts;
FromArgs(opts, gArgs);
return opts;
static void InitFromArgs(const ArgsManager& args) {
auto m = std::make_unique<CoinSelectionOptions>();
FromArgs(*m, args);
LogValues(*m);
CoinSelectionOptions::DEFAULT = std::move(m);
}


static void FromArgs(CoinSelectionOptions& m, ArgsManager& args) {
static void FromArgs(CoinSelectionOptions& m, const ArgsManager& args) {
struct V {
std::optional<bool>& target;
const std::string& arg;
Expand All @@ -66,6 +79,14 @@ struct CoinSelectionOptions {
}
}

static CoinSelectionOptions CreateDefault() {
// Default to basic init, so tests, benches or other scenarios
// before init still falls back to expected.
if (DEFAULT == nullptr) return CoinSelectionOptions{};
// Create a copy
return *DEFAULT;
}

static void FromHTTPHeader(CoinSelectionOptions &m, const HTTPHeaderQueryFunc headerFunc) {
struct V {
std::optional<bool>& target;
Expand Down
17 changes: 11 additions & 6 deletions src/rpc/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ struct RPCMetadata {
public:
CoinSelectionOptions coinSelectOpts;

static RPCMetadata CreateDefault() {
RPCMetadata m;
FromArgs(m, gArgs);
return m;
}

static void SetupArgs(ArgsManager& args) {
CoinSelectionOptions::SetupArgs(args);
}

static void FromArgs(RPCMetadata &m, ArgsManager& args) {
static void InitFromArgs(const ArgsManager& args) {
CoinSelectionOptions::InitFromArgs(args);
}

static void FromArgs(RPCMetadata &m, const ArgsManager& args) {
CoinSelectionOptions::FromArgs(m.coinSelectOpts, args);
}

static RPCMetadata CreateDefault() {
return RPCMetadata {
CoinSelectionOptions::CreateDefault(),
};
}

static void FromHTTPHeader(RPCMetadata &m, const HTTPHeaderQueryFunc headerFunc) {
CoinSelectionOptions::FromHTTPHeader(m.coinSelectOpts, headerFunc);
}
Expand Down