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

SC config updates # 1 #1237

Merged
merged 1 commit into from
Oct 13, 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
12 changes: 2 additions & 10 deletions contracts/feature-tests/abi-tester/tests/abi_tester_abi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ fn abi_tester_abi_generated_ok() {

// generate ABI
let multi_contract_config = multiversx_sc_meta::multi_contract_config::<abi_tester::AbiProvider>(
blockchain
.current_dir()
.join("multicontract.toml")
.to_str()
.unwrap(),
blockchain.current_dir().as_path(),
);

let main_contract = multi_contract_config.find_contract("abi-tester");
Expand Down Expand Up @@ -52,11 +48,7 @@ fn check_multi_contract_config() {
blockchain.set_current_dir_from_workspace("contracts/feature-tests/abi-tester");

let multi_contract_config = multiversx_sc_meta::multi_contract_config::<abi_tester::AbiProvider>(
blockchain
.current_dir()
.join("multicontract.toml")
.to_str()
.unwrap(),
blockchain.current_dir().as_path(),
);

let ev_contract = multi_contract_config.find_contract("abi-tester-ev");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Keeping one file called `multicontract.toml` to test the backwards compatibility.

[settings]
main = "multi-contract-main"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ fn use_module_abi_generated_ok() {

// generate ABI
let multi_contract_config = multiversx_sc_meta::multi_contract_config::<use_module::AbiProvider>(
blockchain
.current_dir()
.join("multicontract.toml")
.to_str()
.unwrap(),
blockchain.current_dir().as_path(),
);

let main_contract = multi_contract_config.find_contract("use-module");
Expand Down
16 changes: 9 additions & 7 deletions framework/meta/src/cmd/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod meta_abi;
mod meta_config;
pub mod output_contract;

use std::path::Path;

use crate::cli_args::{ContractCliAction, ContractCliArgs};
use clap::Parser;
use meta_config::MetaConfig;
Expand Down Expand Up @@ -39,16 +41,16 @@ fn process_original_abi<AbiObj: ContractAbiProvider>(cli_args: &ContractCliArgs)
meta_config
}

pub fn multi_contract_config<AbiObj: ContractAbiProvider>(
multi_contract_config_toml_path: &str,
) -> OutputContractGlobalConfig {
pub fn multi_contract_config<AbiObj>(contract_crate_path: &Path) -> OutputContractGlobalConfig
where
AbiObj: ContractAbiProvider,
{
let original_contract_abi = <AbiObj as ContractAbiProvider>::abi();

let output_contracts = OutputContractGlobalConfig::load_from_file(
multi_contract_config_toml_path,
let output_contracts = OutputContractGlobalConfig::load_from_crate_or_default(
contract_crate_path,
&original_contract_abi,
)
.unwrap_or_else(|| panic!("could not find file {multi_contract_config_toml_path}"));
);
output_contracts.validate_output_contracts();
output_contracts
}
7 changes: 2 additions & 5 deletions framework/meta/src/cmd/contract/meta_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use super::output_contract::{OutputContract, OutputContractGlobalConfig};

const OUTPUT_RELATIVE_PATH: &str = "../output";
const SNIPPETS_RELATIVE_PATH: &str = "../interact-rs";
const MULTI_CONTRACT_CONFIG_RELATIVE_PATH: &str = "../multicontract.toml";
const WASM_LIB_PATH: &str = "../wasm/src/lib.rs";
const WASM_NO_MANAGED_EI: &str = "wasm-no-managed-ei";
const WASM_NO_MANAGED_EI_LIB_PATH: &str = "../wasm-no-managed-ei/src/lib.rs";
Expand All @@ -23,10 +22,8 @@ pub struct MetaConfig {

impl MetaConfig {
pub fn create(original_contract_abi: ContractAbi, load_abi_git_version: bool) -> MetaConfig {
let output_contracts = OutputContractGlobalConfig::load_from_file_or_default(
MULTI_CONTRACT_CONFIG_RELATIVE_PATH,
&original_contract_abi,
);
let output_contracts =
OutputContractGlobalConfig::load_from_crate_or_default("..", &original_contract_abi);

MetaConfig {
load_abi_git_version,
Expand Down
31 changes: 27 additions & 4 deletions framework/meta/src/cmd/contract/output_contract/oc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use multiversx_sc::abi::{ContractAbi, EndpointAbi};
use std::{
collections::{BTreeSet, HashMap, HashSet},
fs,
path::Path,
path::{Path, PathBuf},
};

use super::{
oc_global_config::SC_CONFIG_FILE_NAMES,
oc_settings::{parse_allocator, parse_check_ei, parse_stack_size},
MultiContractConfigSerde, OutputContract, OutputContractGlobalConfig, OutputContractSerde,
OutputContractSettings,
Expand Down Expand Up @@ -288,8 +289,30 @@ impl OutputContractGlobalConfig {
}

/// The standard way of loading a `multicontract.toml` configuration: read the file if present, use the default config otherwise.
pub fn load_from_file_or_default<P: AsRef<Path>>(path: P, original_abi: &ContractAbi) -> Self {
Self::load_from_file(path, original_abi)
.unwrap_or_else(|| Self::default_config(original_abi))
pub fn load_from_files_or_default<I, P>(paths: I, original_abi: &ContractAbi) -> Self
where
P: AsRef<Path>,
I: Iterator<Item = P>,
{
for path in paths {
if let Some(config) = Self::load_from_file(path.as_ref(), original_abi) {
return config;
}
}

Self::default_config(original_abi)
}

/// The standard way of loading a `multicontract.toml` configuration: read the file if present, use the default config otherwise.
pub fn load_from_crate_or_default<P>(contract_crate_path: P, original_abi: &ContractAbi) -> Self
where
P: AsRef<Path>,
{
Self::load_from_files_or_default(
SC_CONFIG_FILE_NAMES
.iter()
.map(|name| PathBuf::from(contract_crate_path.as_ref()).join(name)),
original_abi,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use super::{oc_validate::validate_output_contract, OutputContract};

/// Allowed file names for the SC config.
///
/// The current preferred name is `sc-config.toml`.
///
/// `multicontract.toml` is the legacy name.
/// Was changed because the config file broadened in scope, beyond multi-contract build.
pub const SC_CONFIG_FILE_NAMES: &[&str] = &["sc-config.toml", "multicontract.toml"];

/// An entire project configuration.
///
/// It can contain one or several output contracts.
Expand Down
8 changes: 2 additions & 6 deletions framework/scenario/src/facade/scenario_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,8 @@ impl ScenarioWorld {
Abi: ContractAbiProvider,
B: CallableContractBuilder,
{
let multi_contract_config = multiversx_sc_meta::multi_contract_config::<Abi>(
self.current_dir
.join("multicontract.toml")
.to_str()
.unwrap(),
);
let multi_contract_config =
multiversx_sc_meta::multi_contract_config::<Abi>(self.current_dir.as_path());
let sub_contract = multi_contract_config.find_contract(sub_contract_name);
let contract_obj = if sub_contract.settings.external_view {
contract_builder.new_contract_obj::<api::ExternalViewApi<DebugApi>>()
Expand Down
Loading