From 8ad81c803181143c2dd0f32c9b6438f20cfecfb4 Mon Sep 17 00:00:00 2001 From: Chalith Desaman Date: Tue, 18 Oct 2022 13:52:05 +0530 Subject: [PATCH] Convert environment config to a key value map (#370) --- src/conf.cpp | 27 +++++++++++++++++++-------- src/conf.hpp | 22 +++++++++++----------- test/local-cluster/cluster-create.sh | 2 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index af825a5a..1a34506b 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -1031,6 +1031,23 @@ namespace conf return -1; } + contract.bin_path = jdoc["bin_path"].as(); + contract.bin_args = jdoc["bin_args"].as(); + contract.max_input_ledger_offset = jdoc["max_input_ledger_offset"].as(); + + jpath = "contract.environment"; + for (const auto &obj : jdoc["environment"].object_range()) + { + // Environment variable values should be strings. + if (!obj.value().is_string()) + { + std::cerr << jpath << "." << obj.key() << " environment variable should be a string.\n"; + return -1; + } + + contract.environment.emplace(obj.key(), obj.value().as()); + } + jpath = "contract.unl"; contract.unl.clear(); for (auto &nodepk : jdoc["unl"].array_range()) @@ -1050,12 +1067,6 @@ namespace conf return -1; } - contract.bin_path = jdoc["bin_path"].as(); - contract.bin_args = jdoc["bin_args"].as(); - contract.environment = jdoc["environment"].as(); - contract.max_input_ledger_offset = jdoc["max_input_ledger_offset"].as(); - - jpath = "contract.consensus"; contract.consensus.roundtime = jdoc["consensus"]["roundtime"].as(); if (contract.consensus.roundtime < 1 || contract.consensus.roundtime > MAX_ROUND_TIME) @@ -1109,8 +1120,8 @@ namespace conf contract.runtime_env_args.clear(); // Populate runtime environment args. - if (!contract.environment.empty()) - util::split_string(contract.runtime_env_args, contract.environment, " "); + for (const auto &[key, val] : contract.environment) + contract.runtime_env_args.push_back(key + "=" + val); contract.runtime_binexec_args.clear(); // Populate runtime contract execution args. diff --git a/src/conf.hpp b/src/conf.hpp index 11062794..970b5b38 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -125,7 +125,7 @@ namespace conf }; // Broadcasting mode of consensus and npl - enum MODE + enum MODE { PUBLIC, PRIVATE @@ -171,15 +171,15 @@ namespace conf struct consensus_config { - MODE mode; // If PUBLIC, consensus are broadcasted to non-unl nodes as well. - std::atomic roundtime = 0; // Consensus round time in ms (max: 3,600,000). - std::atomic stage_slice = 0; // Percentage slice of round time that stages 0,1,2 get (max: 33). + MODE mode; // If PUBLIC, consensus are broadcasted to non-unl nodes as well. + std::atomic roundtime = 0; // Consensus round time in ms (max: 3,600,000). + std::atomic stage_slice = 0; // Percentage slice of round time that stages 0,1,2 get (max: 33). uint16_t threshold = 0; }; struct npl_config { - MODE mode; // If PUBLIC, npl messages are broadcasted to non-unl nodes as well. + MODE mode; // If PUBLIC, npl messages are broadcasted to non-unl nodes as well. }; struct contract_config @@ -189,12 +189,12 @@ namespace conf ugid run_as; // The user/groups id to execute the contract as. contract_log_config log; // Contract log related settings. - std::string version; // Contract version string. - std::set unl; // Unique node list (list of binary public keys). - std::string bin_path; // Full path to the contract binary. - std::string bin_args; // CLI arguments to pass to the contract binary. - std::string environment; // Environment variables to be passed into contract. - uint16_t max_input_ledger_offset; // Maximum ledger sequence number offset that can be specified in the input. + std::string version; // Contract version string. + std::set unl; // Unique node list (list of binary public keys). + std::string bin_path; // Full path to the contract binary. + std::string bin_args; // CLI arguments to pass to the contract binary. + std::map environment; // Environment variables to be passed into contract. + uint16_t max_input_ledger_offset; // Maximum ledger sequence number offset that can be specified in the input. consensus_config consensus; npl_config npl; round_limits_config round_limits; diff --git a/test/local-cluster/cluster-create.sh b/test/local-cluster/cluster-create.sh index 784399df..ac9d5b07 100755 --- a/test/local-cluster/cluster-create.sh +++ b/test/local-cluster/cluster-create.sh @@ -111,7 +111,7 @@ do id: '3c349abe-4d70-4f50-9fa6-018f1f2530ab', \ bin_path: '$binary', \ bin_args: '$binargs', \ - environment: '', \ + environment: {}, \ consensus: { \ ...require('./tmp.json').contract.consensus, \ mode: 'public', \