Skip to content

Commit

Permalink
Merge pull request #936 from multiversx/sc-upgrade
Browse files Browse the repository at this point in the history
SC upgrade tool
  • Loading branch information
andrei-marinica authored Jan 18, 2023
2 parents 1aa562d + e296215 commit 84e2bd0
Show file tree
Hide file tree
Showing 127 changed files with 1,045 additions and 553 deletions.
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ members = [
"sdk/core",
"sdk/scenario-format",

"tools/test-gen",
"tools/erdpy-snippet-generator",

"vm",

"contracts/modules",

"contracts/core/price-aggregator",
"contracts/core/price-aggregator/meta",
"contracts/core/wegld-swap",
"contracts/core/wegld-swap/meta",

"contracts/benchmarks/mappers/benchmark-common",
"contracts/benchmarks/mappers/linked-list-repeat",
"contracts/benchmarks/mappers/linked-list-repeat/meta",
Expand All @@ -34,11 +44,6 @@ members = [
"contracts/benchmarks/send-tx-repeat",
"contracts/benchmarks/send-tx-repeat/meta",

"contracts/core/price-aggregator",
"contracts/core/price-aggregator/meta",
"contracts/core/wegld-swap",
"contracts/core/wegld-swap/meta",

"contracts/examples/adder",
"contracts/examples/adder/meta",
"contracts/examples/esdt-transfer-with-fee",
Expand Down Expand Up @@ -153,10 +158,5 @@ members = [
"contracts/feature-tests/rust-testing-framework-tester/meta",
"contracts/feature-tests/use-module",
"contracts/feature-tests/use-module/meta",

"contracts/modules",

"tools/test-gen",
"tools/erdpy-snippet-generator",
]

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# The MultiversX Rust Tool Set


[![Build Status](https://img.shields.io/github/actions/workflow/status/multiversx/mx-sdk-rs/actions.yml?branch=master)](https://github.com/multiversx/mx-sdk-rs/actions/workflows/actions.yml?query=branch%3Amaster) [![Dependency Status](https://deps.rs/repo/github/multiversx/mx-sdk-rs/status.svg)](https://deps.rs/repo/github/multiversx/mx-sdk-rs) [![Contributors](https://img.shields.io/github/contributors/multiversx/mx-sdk-rs)](https://github.com/multiversx/mx-sdk-rs/graphs/contributors)

This repository contains a wide variety of tools, aimed primarily at smart contract developers.

The repo contains:
Expand Down
2 changes: 2 additions & 0 deletions contracts/modules/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Standard modules for Elrond smart contracts

[![Crates.io](https://img.shields.io/crates/v/multiversx-sc-modules)](https://crates.io/crates/multiversx-sc-modules)

This crate contains several smart contract modules developed by the Elrond team, which can be used to add standard functionality to any smart contract.

It is enough to add the module traits as supertraits of the contract traits to receive all endpoints and all functionality from the modules in contracts.
2 changes: 1 addition & 1 deletion framework/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ esdt-token-payment-legacy-decode = []

[dependencies]
wee_alloc = "0.4"
hashbrown = "0.11.2"
hashbrown = "0.13.2"
hex-literal = "0.3.1"
bitflags = "1.3.2"

Expand Down
2 changes: 2 additions & 0 deletions framework/base/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Smart contract base crate

[![crates.io](https://img.shields.io/crates/v/multiversx-sc.svg)](https://crates.io/crates/multiversx-sc)

This is the main crate for building smart contracts for the MultiversX blockchain.

It contains the interface that the smart contract sees and can use. No implementation details are available from this crate alone.
Expand Down
2 changes: 2 additions & 0 deletions framework/codec-derive/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Smart contract standard codec derive functionality

[![crates.io](https://img.shields.io/crates/v/multiversx-sc-codec-derive.svg)](https://crates.io/crates/multiversx-sc-codec-derive)

Crate that contains all macro code generation the `multiversx-sc-codec` serializer.

There are 4 derive macros currently provided:
Expand Down
2 changes: 2 additions & 0 deletions framework/codec/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Smart contract standard codec

[![crates.io](https://img.shields.io/crates/v/multiversx-sc-codec.svg)](https://crates.io/crates/multiversx-sc-codec)

Lightweight binary serializer/deserializer, written especially for MultiversX smart contracts.

Designed to:
Expand Down
2 changes: 1 addition & 1 deletion framework/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0"
hex = "0.4"
radix_trie = "0.1"
radix_trie = "0.2.1"

[features]
default = ["syn/full", "syn/parsing", "syn/extra-traits"]
Expand Down
7 changes: 7 additions & 0 deletions framework/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ description = "MultiversX smart contract meta-programming tools and build system
keywords = ["multiversx", "blockchain", "contract", "debug"]
categories = ["cryptography::cryptocurrencies", "development-tools::debugging"]

[[bin]]
name = "sc-meta"
path = "src/main.rs"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rustc_version = "0.4"
toml = { version = "0.5.8", features = ["preserve_order"] }
ruplacer = "0.8.1"
clap = { version = "4.1.0", features = ["derive", "cargo"] }
colored = "2.0"

[dependencies.multiversx-sc]
version = "=0.39.0"
Expand Down
2 changes: 2 additions & 0 deletions framework/meta/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Smart contract meta-programming

[![crates.io](https://img.shields.io/crates/v/multiversx-sc-meta.svg)](https://crates.io/crates/multiversx-sc-meta)

A meta-programming utility that works with smart contract code metadata.

It is responsible with creating the smart contract ABIs, generating the wasm crates, and, ultimately, building the contract binaries
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
use std::{fs, io::Write, path::Path};
use std::{
fs,
io::Write,
path::{Path, PathBuf},
};

use toml::{value::Table, Value};

pub const CARGO_TOML_DEPENDENCIES: &str = "dependencies";
pub const CARGO_TOML_DEV_DEPENDENCIES: &str = "dev-dependencies";

/// Contains an in-memory representation of a Cargo.toml file.
///
Expand All @@ -8,18 +17,23 @@ use std::{fs, io::Write, path::Path};
/// - It keeps an ordered representation, thanks to the `toml` `preserve_order` feature.
#[derive(Clone, Debug)]
pub struct CargoTomlContents {
toml_value: toml::Value,
pub path: PathBuf,
pub toml_value: toml::Value,
}

impl CargoTomlContents {
pub fn load_from_file<P: AsRef<Path>>(path: P) -> Self {
let cargo_toml_content = fs::read(path).expect("failed to open Cargo.toml file");
let path_ref = path.as_ref();
let cargo_toml_content = fs::read(path_ref).expect("failed to open Cargo.toml file");
let cargo_toml_content_str =
String::from_utf8(cargo_toml_content).expect("error decoding Cargo.toml utf-8");
let toml_value = cargo_toml_content_str
.parse::<toml::Value>()
.expect("failed to parse Cargo.toml toml format");
CargoTomlContents { toml_value }
CargoTomlContents {
path: path_ref.to_owned(),
toml_value,
}
}

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) {
Expand All @@ -40,4 +54,21 @@ impl CargoTomlContents {
.expect("malformed package in Cargo.toml")
.insert("name".to_string(), toml::Value::String(new_package_name));
}

pub fn dependency(&self, dep_name: &str) -> Option<&Value> {
if let Some(deps) = self.toml_value.get(CARGO_TOML_DEPENDENCIES) {
if let Some(deps_map) = deps.as_table() {
return deps_map.get(dep_name);
}
}
None
}

pub fn dependencies_mut(&mut self) -> &mut Table {
self.toml_value
.get_mut(CARGO_TOML_DEPENDENCIES)
.unwrap_or_else(|| panic!("no dependencies found in crate {}", self.path.display()))
.as_table_mut()
.expect("malformed crate Cargo.toml")
}
}
122 changes: 122 additions & 0 deletions framework/meta/src/cli_args/cli_args_build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use clap::{ArgAction, Args};

#[derive(Clone, PartialEq, Eq, Debug, Args)]
pub struct BuildArgs {
/// Adds debug symbols in the resulting WASM binary. Adds bloat, but helps with debugging. Do not use in production.
#[arg(long = "wasm-symbols", verbatim_doc_comment)]
pub wasm_symbols: bool,

/// Overrides the main contract output name.
#[arg(long = "wasm-name", verbatim_doc_comment)]
pub wasm_name_override: Option<String>,

/// Adds given suffix to all built contracts.
#[arg(long = "wasm-suffix", verbatim_doc_comment)]
pub wasm_name_suffix: Option<String>,

/// True if wasm-opt should be used.
#[arg(
long = "no-wasm-opt",
help = "Skips applying the wasm-opt tool after building the contracts.",
action = ArgAction::SetFalse,
)]
pub wasm_opt: bool,

/// Also generate a WAT file when building.
#[arg(long = "wat", verbatim_doc_comment)]
pub wat: bool,

#[arg(
long = "no-imports",
help = "Skips extracting the EI imports after building the contracts.",
action = ArgAction::SetFalse,
)]
pub extract_imports: bool,

/// Allows specifying the target directory where the Rust compiler will build the intermediary files.
/// Sharing the same target directory can speed up building multiple contract crates at once.
#[arg(long = "target-dir", verbatim_doc_comment)]
pub target_dir: Option<String>,

/// Generate a twiggy top report after building.
#[arg(long = "twiggy-top", verbatim_doc_comment)]
pub twiggy_top: bool,

/// Generate a twiggy paths report after building.
#[arg(long = "twiggy-paths", verbatim_doc_comment)]
pub twiggy_paths: bool,

/// Generate a twiggy monos report after building.
#[arg(long = "twiggy-monos", verbatim_doc_comment)]
pub twiggy_monos: bool,

/// Generate a twiggy dominators report after building.
#[arg(long = "twiggy-dominators", verbatim_doc_comment)]
pub twiggy_dominators: bool,

/// Backwards compatibility with mxpy, delete when github actions are fixed.
#[deprecated]
#[arg(long = "target", verbatim_doc_comment)]
pub target: Option<String>,

/// Backwards compatibility with mxpy, delete when github actions are fixed.
#[deprecated]
#[arg(long, verbatim_doc_comment)]
pub release: bool,

/// Backwards compatibility with mxpy, delete when github actions are fixed.
#[deprecated]
#[arg(long = "out-dir", verbatim_doc_comment)]
pub out_dir: Option<String>,
}

impl Default for BuildArgs {
#[allow(deprecated)]
fn default() -> Self {
BuildArgs {
wasm_symbols: false,
wasm_name_override: None,
wasm_name_suffix: None,
wasm_opt: true,
wat: false,
extract_imports: true,
target_dir: None,
twiggy_top: false,
twiggy_paths: false,
twiggy_monos: false,
twiggy_dominators: false,
target: None,
release: false,
out_dir: None,
}
}
}

impl BuildArgs {
/// Base config when calling `cargo run build-dbg`, with no additional configs.
pub fn into_dbg(self) -> Self {
BuildArgs {
wasm_symbols: true,
wasm_name_suffix: Some("dbg".to_string()),
wasm_opt: false,
wat: true,
extract_imports: false,
..self
}
}

/// Base config when calling `cargo run build-dbg`, with no additional configs.
pub fn into_twiggy(self) -> Self {
BuildArgs {
twiggy_top: true,
twiggy_paths: true,
twiggy_monos: true,
twiggy_dominators: true,
..self.into_dbg()
}
}

pub fn has_twiggy_call(&self) -> bool {
self.twiggy_top || self.twiggy_paths || self.twiggy_monos || self.twiggy_dominators
}
}
73 changes: 73 additions & 0 deletions framework/meta/src/cli_args/cli_args_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use clap::{ArgAction, Args, Parser, Subcommand};

use super::BuildArgs;

/// Parsed arguments of the meta crate CLI.
#[derive(Default, PartialEq, Eq, Debug, Parser)]
#[command(
version,
about,
after_help = "
The MultiversX smart contract Meta crate can be used in two ways:
A. Import it into a contract's specific meta-crate.
There it will receive access to the contract ABI generator.
Based on that it is able to build the contract and apply various tools.
This part also contains the multi-contract config infrastructure.
B. Use it as a standalone tool.
It can be used to automatically upgrade contracts from one version to the next.
You are currently using the contract tool (A).
"
)]
#[command(propagate_version = true)]
pub struct ContractCliArgs {
#[command(subcommand)]
pub command: ContractCliAction,

#[arg(
long = "no-abi-git-version",
help = "Skips loading the Git version into the ABI",
action = ArgAction::SetFalse
)]
#[clap(global = true)]
pub load_abi_git_version: bool,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Subcommand)]
pub enum ContractCliAction {
#[default]
#[command(name = "abi", about = "Generates the contract ABI and nothing else.")]
Abi,

#[command(
name = "build",
about = "Builds contract(s) for deploy on the blockchain."
)]
Build(BuildArgs),

#[command(name = "build-dbg", about = "Builds contract(s) with symbols and WAT.")]
BuildDbg(BuildArgs),

#[command(
name = "twiggy",
about = "Builds contract(s) and generate twiggy reports."
)]
Twiggy(BuildArgs),

#[command(about = "Clean the Rust project and the output folder.")]
Clean,

#[command(
name = "snippets",
about = "Generates a snippets project, based on the contract ABI."
)]
GenerateSnippets(GenerateSnippetsArgs),
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct GenerateSnippetsArgs {
/// Override snippets project if it already exists.
#[arg(long, verbatim_doc_comment)]
pub overwrite: bool,
}
Loading

0 comments on commit 84e2bd0

Please sign in to comment.