From 1bc1b02fdfbe8cb84224f770bd814fd3a434a668 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Mon, 20 May 2024 19:40:31 +0300 Subject: [PATCH] sc-meta compare feature and github actions --- .github/workflows/proxy-compare.yml | 37 +++++++++++++++++++ contracts/examples/adder/wasm/src/lib.rs | 5 +-- .../meta/src/cli_args/cli_args_contract.rs | 8 ++++ framework/meta/src/cmd/contract.rs | 1 + .../contract/generate_proxy/proxy_gen_main.rs | 30 +++++++++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/proxy-compare.yml diff --git a/.github/workflows/proxy-compare.yml b/.github/workflows/proxy-compare.yml new file mode 100644 index 0000000000..12a7f57745 --- /dev/null +++ b/.github/workflows/proxy-compare.yml @@ -0,0 +1,37 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + +jobs: + proxy_compare: + name: Proxy compare - newly generated vs present in file tree + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install rust + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + default: true + toolchain: stable + target: wasm32-unknown-unknown + + - name: Install prerequisites + run: | + cargo install wasm-opt + cargo install --path framework/meta + sc-meta install mx-scenario-go --tag v2.0.0 + + which wasm-opt + which mx-scenario-go + mx-scenario-go --version + + - name: Run proxy compare + run: | + cd framework/meta + cargo run compare diff --git a/contracts/examples/adder/wasm/src/lib.rs b/contracts/examples/adder/wasm/src/lib.rs index 19ea33c7e1..ae4ff8ef8b 100644 --- a/contracts/examples/adder/wasm/src/lib.rs +++ b/contracts/examples/adder/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 2 +// Endpoints: 1 // Async Callback (empty): 1 -// Total number of exported functions: 5 +// Total number of exported functions: 4 #![no_std] @@ -21,7 +21,6 @@ multiversx_sc_wasm_adapter::endpoints! { init => init upgrade => upgrade getSum => sum - add => add ) } diff --git a/framework/meta/src/cli_args/cli_args_contract.rs b/framework/meta/src/cli_args/cli_args_contract.rs index e05146a9bc..c0c65642c2 100644 --- a/framework/meta/src/cli_args/cli_args_contract.rs +++ b/framework/meta/src/cli_args/cli_args_contract.rs @@ -72,6 +72,11 @@ pub enum ContractCliAction { about = "Generates a proxy, based on the contract ABI." )] GenerateProxies, + #[command( + name = "compare", + about = "Compares a newly generated proxy with the proxies already on disk." + )] + Compare } impl CliArgsToRaw for ContractCliAction { @@ -106,6 +111,9 @@ impl CliArgsToRaw for ContractCliAction { ContractCliAction::GenerateProxies => { raw.push("proxy".to_string()); }, + ContractCliAction::Compare => { + raw.push("compare".to_string()); + }, } raw } diff --git a/framework/meta/src/cmd/contract.rs b/framework/meta/src/cmd/contract.rs index 3e1008a3a3..f91cd8e997 100644 --- a/framework/meta/src/cmd/contract.rs +++ b/framework/meta/src/cmd/contract.rs @@ -34,6 +34,7 @@ pub fn cli_main() { meta_config_opt.generate_proxy() }, ContractCliAction::GenerateProxies => meta_config_opt.generate_proxy(), + ContractCliAction::Compare => meta_config_opt.compare_proxy(), } } diff --git a/framework/meta/src/cmd/contract/generate_proxy/proxy_gen_main.rs b/framework/meta/src/cmd/contract/generate_proxy/proxy_gen_main.rs index 0619a65cb5..4aee431238 100644 --- a/framework/meta/src/cmd/contract/generate_proxy/proxy_gen_main.rs +++ b/framework/meta/src/cmd/contract/generate_proxy/proxy_gen_main.rs @@ -1,3 +1,6 @@ +use colored::Colorize; +use std::fs; + use multiversx_sc::abi::ContractAbi; use crate::cmd::contract::sc_config::ProxyConfigSerde; @@ -14,6 +17,33 @@ impl MetaConfig { write_proxy_with_explicit_path(&proxy_config, self); } } + + pub fn compare_proxy(&mut self) { + for proxy_config in self.sc_config.proxy_configs.clone() { + compare_proxy_explicit_path(&proxy_config, self); + } + } +} + +fn compare_proxy_explicit_path(proxy_config: &ProxyConfigSerde, meta_config: &MetaConfig) { + let contract_abi = extract_contract_abi(proxy_config, meta_config); + let mut temp = create_file("temp"); + let mut proxy_generator = + ProxyGenerator::new(meta_config, &mut temp, proxy_config, contract_abi); + proxy_generator.write_proxy_to_file(); + + let existent_proxy_path = format!("../{}", proxy_config.path); + let temp_path = "../temp"; + + let existent_proxy = fs::read_to_string(existent_proxy_path).unwrap(); + let temp = fs::read_to_string(temp_path).unwrap(); + + if existent_proxy != temp { + fs::remove_file(temp_path).unwrap(); + panic!("{}", format!("Contract has been modified and proxies have not been updated. Regenerate proxies to avoid inconsistencies.").red()); + } + + fs::remove_file(temp_path).unwrap(); } fn write_proxy_with_explicit_path(proxy_config: &ProxyConfigSerde, meta_config: &MetaConfig) {