Skip to content

Commit

Permalink
sc-meta all proxy --compare impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed May 23, 2024
1 parent 27635af commit 6ed9826
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/proxy-compare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 --path framework/meta
- name: Run proxy compare
run: |
cd contracts
sc-meta all proxy --compare
22 changes: 20 additions & 2 deletions framework/meta/src/cli_args/cli_args_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub enum ContractCliAction {
name = "proxy",
about = "Generates a proxy, based on the contract ABI."
)]
GenerateProxies,
GenerateProxies(GenerateProxyArgs),
}

impl CliArgsToRaw for ContractCliAction {
Expand Down Expand Up @@ -103,8 +103,9 @@ impl CliArgsToRaw for ContractCliAction {
raw.push("snippets".to_string());
raw.append(&mut args.to_raw());
},
ContractCliAction::GenerateProxies => {
ContractCliAction::GenerateProxies(args) => {
raw.push("proxy".to_string());
raw.append(&mut args.to_raw());
},
}
raw
Expand All @@ -127,3 +128,20 @@ impl CliArgsToRaw for GenerateSnippetsArgs {
raw
}
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct GenerateProxyArgs {
/// Runs proxy comparison (newly generated vs already present on disk).
#[arg(long, verbatim_doc_comment)]
pub compare: bool,
}

impl CliArgsToRaw for GenerateProxyArgs {
fn to_raw(&self) -> Vec<String> {
let mut raw = Vec::new();
if self.compare {
raw.push("--compare".to_string());
}
raw
}
}
8 changes: 7 additions & 1 deletion framework/meta/src/cmd/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ pub fn cli_main<AbiObj: ContractAbiProvider>() {
meta_config_opt.generate_rust_snippets(&gs_arg);
meta_config_opt.generate_proxy()
},
ContractCliAction::GenerateProxies => meta_config_opt.generate_proxy(),
ContractCliAction::GenerateProxies(proxy_args) => {
if proxy_args.compare {
meta_config_opt.compare_proxy()
} else {
meta_config_opt.generate_proxy()
}
},
}
}

Expand Down
27 changes: 27 additions & 0 deletions framework/meta/src/cmd/contract/generate_proxy/proxy_gen_main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use colored::Colorize;
use std::fs;

use multiversx_sc::abi::ContractAbi;

use crate::cmd::contract::sc_config::ProxyConfigSerde;
Expand All @@ -6,6 +9,8 @@ use super::{
super::meta_config::MetaConfig, proxy_crate_gen::create_file, proxy_generator::ProxyGenerator,
};

const PROXY_COMPARE_ERR_MSG: &str = "Contract has been modified and proxies have not been updated. Regenerate proxies to avoid inconsistencies.";

impl MetaConfig {
pub fn generate_proxy(&mut self) {
let default_proxy = ProxyConfigSerde::new();
Expand All @@ -14,6 +19,28 @@ 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 = Vec::<u8>::new();
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 existent_proxy = fs::read_to_string(existent_proxy_path).unwrap();
let newly_gen_proxy = String::from_utf8(temp).unwrap();

if existent_proxy != newly_gen_proxy {
panic!("{}", PROXY_COMPARE_ERR_MSG.to_string().red());
}
}

fn write_proxy_with_explicit_path(proxy_config: &ProxyConfigSerde, meta_config: &MetaConfig) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Display, fs::File, io::Write};
use std::fmt::Display;

use multiversx_sc::abi::{
ContractAbi, EndpointAbi, EnumVariantDescription, InputAbi, OutputAbi, StructFieldDescription,
Expand Down Expand Up @@ -41,15 +41,15 @@ const TYPES_FROM_FRAMEWORK: &[&str] = &[

pub struct ProxyGenerator<'a> {
pub meta_config: &'a MetaConfig,
pub file: Option<&'a mut File>,
pub file: Option<&'a mut dyn std::io::Write>,
pub proxy_config: &'a ProxyConfigSerde,
pub contract_abi: &'a ContractAbi,
}

impl<'a> ProxyGenerator<'a> {
pub fn new(
meta_config: &'a MetaConfig,
file: &'a mut File,
file: &'a mut dyn std::io::Write,
proxy_config: &'a ProxyConfigSerde,
contract_abi: &'a ContractAbi,
) -> Self {
Expand All @@ -62,8 +62,8 @@ impl<'a> ProxyGenerator<'a> {
}

fn write(&mut self, s: impl Display) {
let file = self.file.as_mut().expect("output not configured");
write!(*file, "{s}").unwrap();
let file = self.file.as_mut().unwrap();
file.write_all(s.to_string().as_bytes()).unwrap();
}

fn writeln(&mut self, s: impl Display) {
Expand Down

0 comments on commit 6ed9826

Please sign in to comment.