Skip to content

build SCs on std #2019

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ members = [
"contracts/feature-tests/rust-testing-framework-tester/meta",
"contracts/feature-tests/scenario-tester",
"contracts/feature-tests/scenario-tester/meta",
"contracts/feature-tests/std-contract",
"contracts/feature-tests/std-contract/meta",
"contracts/feature-tests/use-module",
"contracts/feature-tests/use-module/meta",
]
7 changes: 7 additions & 0 deletions contracts/feature-tests/std-contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
*/target/

# The mxpy output
/output*/
20 changes: 20 additions & 0 deletions contracts/feature-tests/std-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "std-contract"
version = "0.0.0"
authors = ["you"]
edition = "2021"
publish = false

[lib]
path = "src/lib.rs"

[dependencies.multiversx-sc]
version = "0.57.0"
path = "../../../framework/base"

[dev-dependencies.multiversx-sc-scenario]
version = "0.57.0"
path = "../../../framework/scenario"

[dev-dependencies]
num-bigint = "0.4"
13 changes: 13 additions & 0 deletions contracts/feature-tests/std-contract/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "std-contract-meta"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.std-contract]
path = ".."

[dependencies.multiversx-sc-meta-lib]
version = "0.57.0"
path = "../../../../framework/meta-lib"
default-features = false
3 changes: 3 additions & 0 deletions contracts/feature-tests/std-contract/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta_lib::cli_main::<std_contract::AbiProvider>();
}
3 changes: 3 additions & 0 deletions contracts/feature-tests/std-contract/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
2 changes: 2 additions & 0 deletions contracts/feature-tests/std-contract/sc-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[contracts.std-contract]
std = true
39 changes: 39 additions & 0 deletions contracts/feature-tests/std-contract/scenarios/empty.scen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "empty",
"steps": [
{
"step": "setState",
"accounts": {
"address:owner": {
"nonce": "1",
"balance": "0"
}
},
"newAddresses": [
{
"creatorAddress": "address:owner",
"creatorNonce": "1",
"newAddress": "sc:empty"
}
]
},
{
"step": "scDeploy",
"id": "deploy",
"tx": {
"from": "address:owner",
"contractCode": "mxsc:../output/std-contract.mxsc.json",
"arguments": [],
"gasLimit": "5,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "",
"logs": [],
"gas": "*",
"refund": "*"
}
}
]
}
19 changes: 19 additions & 0 deletions contracts/feature-tests/std-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::sync::Arc;

#[allow(unused_imports)]
use multiversx_sc::imports::*;

pub struct Config {
pub settings: Arc<Vec<u8>>,
pub allowed: bool,
}

/// An empty contract. To be used as a template when starting a new contract from scratch.
#[multiversx_sc::contract]
pub trait StdContract {
#[init]
fn init(&self) {}

#[upgrade]
fn upgrade(&self) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use multiversx_sc_scenario::*;

fn world() -> ScenarioWorld {
ScenarioWorld::vm_go()
}

#[test]
fn empty_go() {
world().run("scenarios/empty.scen.json");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use multiversx_sc_scenario::*;

fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();

blockchain.set_current_dir_from_workspace("contracts/feature-tests/std-contract");
blockchain.register_contract(
"mxsc:output/std-contract.mxsc.json",
std_contract::ContractBuilder,
);
blockchain
}

#[test]
fn empty_rs() {
world().run("scenarios/empty.scen.json");
}
35 changes: 35 additions & 0 deletions contracts/feature-tests/std-contract/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Code generated by the multiversx-sc build system. DO NOT EDIT.

# ##########################################
# ############## AUTO-GENERATED #############
# ##########################################

[package]
name = "std-contract-wasm"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[profile.release]
codegen-units = 1
opt-level = "z"
lto = true
debug = false
panic = "abort"
overflow-checks = false

[profile.dev]
panic = "abort"

[dependencies.std-contract]
path = ".."

[dependencies.multiversx-sc-wasm-adapter]
version = "0.57.0"
path = "../../../../framework/wasm-adapter"

[workspace]
members = ["."]
24 changes: 24 additions & 0 deletions contracts/feature-tests/std-contract/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Code generated by the multiversx-sc build system. DO NOT EDIT.

////////////////////////////////////////////////////
////////////////// AUTO-GENERATED //////////////////
////////////////////////////////////////////////////

// Init: 1
// Upgrade: 1
// Endpoints: 0
// Async Callback (empty): 1
// Total number of exported functions: 3

multiversx_sc_wasm_adapter::allocator!();


multiversx_sc_wasm_adapter::endpoints! {
std_contract
(
init => init
upgrade => upgrade
)
}

multiversx_sc_wasm_adapter::async_callback_empty! {}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl ContractVariantBuilder {
default_features: cms.default_features,
kill_legacy_callback: cms.kill_legacy_callback,
profile: ContractVariantProfile::from_serde(&cms.profile),
std: cms.std.unwrap_or(default.settings.std),
},
..default
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct ContractVariantSettings {
pub kill_legacy_callback: bool,

pub profile: ContractVariantProfile,

/// Allows the contract to be built with std.
pub std: bool,
}

impl Default for ContractVariantSettings {
Expand All @@ -50,6 +53,7 @@ impl Default for ContractVariantSettings {
default_features: None,
kill_legacy_callback: false,
profile: Default::default(),
std: false,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions framework/meta-lib/src/contract/sc_config/sc_config_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub struct ContractVariantSerde {

#[serde(default)]
pub profile: Option<ContractVariantProfileSerde>,

#[serde(default)]
pub std: Option<bool>,
}

#[derive(Deserialize, Default, Debug)]
Expand Down
40 changes: 25 additions & 15 deletions framework/meta-lib/src/contract/sc_config/wasm_crate_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ impl ContractVariant {
}

fn panic_handler_macro_invocation(&self) -> &'static str {
if self.settings.panic_message {
"multiversx_sc_wasm_adapter::panic_handler_with_message!();"
} else {
"multiversx_sc_wasm_adapter::panic_handler!();"
if !self.settings.std {
if self.settings.panic_message {
return "multiversx_sc_wasm_adapter::panic_handler_with_message!();";
} else {
return "multiversx_sc_wasm_adapter::panic_handler!();";
}
}
""
}

fn endpoint_macro_name(&self) -> &'static str {
Expand All @@ -83,7 +86,7 @@ impl ContractVariant {
fn write_wasm_src_lib_contents(&self, wasm_lib_file: &mut File) {
writeln!(wasm_lib_file, "{PREFIX_AUTO_GENERATED}").unwrap();
self.write_stat_comments(wasm_lib_file);
write_prefix(wasm_lib_file);
write_prefix(wasm_lib_file, self.select_features());
writeln!(wasm_lib_file, "{}", self.allocator_macro_invocation()).unwrap();
writeln!(wasm_lib_file, "{}", self.panic_handler_macro_invocation()).unwrap();

Expand Down Expand Up @@ -137,21 +140,28 @@ impl ContractVariant {

write_stat_comment(wasm_lib_file, "Total number of exported functions:", total);
}
}

fn select_features() -> &'static str {
let meta = rustc_version::version_meta().unwrap();
if meta.semver < Version::from_str(VER_1_71).unwrap() {
FEATURES_PRE_RUSTC_1_71
} else if meta.semver < Version::from_str(VER_1_73).unwrap() {
FEATURES_PRE_RUSTC_1_73
} else {
fn select_features(&self) -> &'static str {
if self.settings.std {
return "";
}

let meta = rustc_version::version_meta().unwrap();

if meta.semver < Version::from_str(VER_1_71).unwrap() {
return FEATURES_PRE_RUSTC_1_71;
}

if meta.semver < Version::from_str(VER_1_73).unwrap() {
return FEATURES_PRE_RUSTC_1_73;
}

FEATURES_DEFAULT
}
}

fn write_prefix(wasm_lib_file: &mut File) {
writeln!(wasm_lib_file, "{}", select_features()).unwrap();
fn write_prefix(wasm_lib_file: &mut File, features: &str) {
writeln!(wasm_lib_file, "{}", features).unwrap();
}

fn write_endpoints_macro<'a, I>(
Expand Down
Loading