From 0464d9d077fc8cceccdb21c422a45114f245d69a Mon Sep 17 00:00:00 2001 From: Ravi Shankar Date: Mon, 28 Nov 2022 15:09:05 +0530 Subject: [PATCH] Fix MC startup and lambda return --- src/defid.cpp | 21 ++++++++++++++------- src/dmc_handler.cpp | 23 ++++++++++++++++------- src/dmc_handler.h | 3 ++- src/init.cpp | 5 +++-- src/init.h | 2 +- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/defid.cpp b/src/defid.cpp index 192061568e5..9869a066a00 100644 --- a/src/defid.cpp +++ b/src/defid.cpp @@ -73,12 +73,15 @@ static bool AppInit(int argc, char* argv[]) // // If Qt is used, parameters/defi.conf are parsed in qt/defi.cpp's main() SetupServerArgs(); - auto mcRes = InitMetachain(&argc, argv); - if (mcRes.is_help || !mcRes.daemon) { - return true; - } - if (!mcRes.success) { - return InitError("Failed to initialize metachain\n"); + bool isMetaEmbedded = false; + auto mcRes = InitMetachain(&argc, argv, isMetaEmbedded); + if (isMetaEmbedded) { + if (mcRes.is_help || !mcRes.daemon) { + return true; + } + if (!mcRes.success) { + return InitError("Failed to initialize metachain\n"); + } } std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { @@ -105,7 +108,11 @@ static bool AppInit(int argc, char* argv[]) try { - SetupMetachainHandler(); + bool hasRpc = SetupMetachainHandler(); + if (!isMetaEmbedded && !hasRpc) { + return InitError("Expected one of '-meta' or '-meta_rpc' parameters to initialize metachain"); + } + if (!CheckDataDirOption()) { return InitError(strprintf("Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""))); } diff --git a/src/dmc_handler.cpp b/src/dmc_handler.cpp index 1bcbcb2bfb9..61113a953a2 100644 --- a/src/dmc_handler.cpp +++ b/src/dmc_handler.cpp @@ -10,7 +10,7 @@ std::string rpc_url; bool is_ffi = true; // FIXME: Set this to `false` if both -meta and -meta_rpc is missing -void SetupMetachainHandler() { +bool SetupMetachainHandler() { auto url = gArgs.GetArg("-meta_rpc", ""); if (!url.empty()) { // Test the instantiation of client for this URL. Calling this multiple @@ -19,6 +19,8 @@ void SetupMetachainHandler() { rpc_url = url; is_ffi = false; } + + return !url.empty(); } Res MintDmcBlock(CBlock& pblock) { @@ -37,30 +39,37 @@ Res MintDmcBlock(CBlock& pblock) { call(mcMsg.from.GetHex(), mcMsg.to.GetHex(), int64_t(mcMsg.amount)); } } + return Res::Ok(); }; if (is_ffi) { std::vector txs; - dmcTxIter([&](std::string from, std::string to, int64_t amount) { + auto res = dmcTxIter([&](std::string from, std::string to, int64_t amount) { txs.push_back(DmcTx{from, to, amount}); }); + if (!res) { + return res; + } try { - auto res = mint_block(txs); - std::copy(res.payload.begin(), res.payload.end(), std::back_inserter(pblock.dmc_payload)); + auto block = mint_block(txs); + std::copy(block.payload.begin(), block.payload.end(), std::back_inserter(pblock.dmc_payload)); } catch (const std::exception& e) { return Res::Err(e.what()); } } else { auto inp = MakeMetaBlockInput(); - dmcTxIter([&](std::string from, std::string to, int64_t amount) { + auto res = dmcTxIter([&](std::string from, std::string to, int64_t amount) { inp.txs.push_back(MetaTransaction{from, to, amount}); }); + if (!res) { + return res; + } try { auto client = NewClient(rpc_url); - auto res = CallMetaMintBlock(client, inp); - std::copy(res.payload.begin(), res.payload.end(), std::back_inserter(pblock.dmc_payload)); + auto block = CallMetaMintBlock(client, inp); + std::copy(block.payload.begin(), block.payload.end(), std::back_inserter(pblock.dmc_payload)); } catch (const std::exception& e) { return Res::Err(e.what()); } diff --git a/src/dmc_handler.h b/src/dmc_handler.h index faaed5547d8..a9b0d9f1987 100644 --- a/src/dmc_handler.h +++ b/src/dmc_handler.h @@ -8,10 +8,11 @@ * Initializes metachain handler * * @throws Exception on failure to instantiate RPC client + * @returns true if RPC handler has been setup and false otherwise * * NOTE: This depends on ArgsManager, so it must be called only after parsing arguments */ -void SetupMetachainHandler(); +bool SetupMetachainHandler(); /** * Instructs the metachain node to mint a block using the newly minted miner block diff --git a/src/init.cpp b/src/init.cpp index db75b9b6fee..f10a8ad4c41 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -378,7 +378,7 @@ static void OnRPCStopped() LogPrint(BCLog::RPC, "RPC stopped.\n"); } -ExecResult InitMetachain(int* argc, const char* const argv[]) +ExecResult InitMetachain(int* argc, const char* const argv[], bool &embedded) { bool found = false; std::vector new_argv; @@ -392,7 +392,8 @@ ExecResult InitMetachain(int* argc, const char* const argv[]) if (found) new_argv.push_back(key); } - if (!found) return ExecResult{false, true}; // Remove this line to force metachain options + embedded = found; + if (!found) return ExecResult{}; return parse_args_and_run(new_argv); } diff --git a/src/init.h b/src/init.h index 23cb4cfbd3c..da57179e6c8 100644 --- a/src/init.h +++ b/src/init.h @@ -36,7 +36,7 @@ void InitLogging(); //!Parameter interaction: change current parameters depending on various rules void InitParameterInteraction(); //!Initialize metachain using the arguments -ExecResult InitMetachain(int* argc, const char* const argv[]); +ExecResult InitMetachain(int* argc, const char* const argv[], bool &embedded); /** Initialize DeFi Blockchain: Basic context setup. * @note This can be done before daemonization. Do not call Shutdown() if this function fails.