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

Check meta before adding new custom section #1707

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/crates/soroban-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ crate-type = ["rlib", "cdylib"]


[dependencies]
stellar-xdr = { workspace = true, features = ["cli"] }
soroban-spec = { workspace = true }
soroban-spec-tools = { workspace = true }
soroban-ledger-snapshot = { workspace = true }
Expand Down
89 changes: 57 additions & 32 deletions cmd/crates/soroban-test/tests/fixtures/workspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 51 additions & 10 deletions cmd/crates/soroban-test/tests/it/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use predicates::prelude::predicate;
use soroban_cli::wasm;
use soroban_spec_tools::contract::Spec;
use soroban_test::TestEnv;
use std::collections::HashMap;
use stellar_xdr::curr::{ScMetaEntry, ScMetaV0};

#[test]
fn build_all() {
Expand Down Expand Up @@ -135,18 +139,55 @@ fn build_with_metadata() {
.assert()
.success();

// verify that the metadata added in the contract code via contractmetadata! macro is present
// as well as the meta that is included on build
let wasm_path = fixture_path.join(&outdir).join("add.wasm");
sandbox
.new_assert_cmd("contract")
.current_dir(&fixture_path)
.arg("info")
.arg("meta")
.arg("--wasm")
.arg(wasm_path)
.arg("build")
.arg("--meta")
.arg("meta_replaced=some_new_meta")
.arg("--out-dir")
.arg(&outdir)
.assert()
.success()
.stdout(predicate::str::contains("Description: A test add contract"))
.stdout(predicate::str::contains("contract meta: added on build"));
.success();

// verify that the metadata added in the contract code via contractmetadata! macro is present
// as well as the meta that is included on build
let wasm_path = fixture_path.join(&outdir).join("add.wasm");

let wasm_bytes = wasm::Args {
wasm: wasm_path.clone(),
}
.read()
.unwrap();
let spec = Spec::new(&wasm_bytes).unwrap();
let mut expected_entries = HashMap::new();

expected_entries.insert("Description".to_string(), None);
expected_entries.insert("rsver".to_string(), None);
expected_entries.insert("rssdkver".to_string(), None);
expected_entries.insert("meta_replaced".to_string(), Some("some_new_meta"));

for meta_entry in &spec.meta {
match meta_entry {
ScMetaEntry::ScMetaV0(ScMetaV0 { key, val }) => {
let key = key.to_string();
if let Some(entry) = expected_entries.remove(&key) {
if let Some(str) = entry {
assert_eq!(
str,
val.to_string(),
"Unexpected value ${val} for key ${key} (expecting ${str})"
);
}
} else {
panic!("Unexpected key {key}");
}
}
}
}

assert!(
expected_entries.is_empty(),
"Some entries were not found in the metadata: {expected_entries:?}",
);
}
8 changes: 8 additions & 0 deletions cmd/soroban-cli/src/commands/contract/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo_metadata::{Metadata, MetadataCommand, Package};
use clap::Parser;
use itertools::Itertools;
use soroban_spec_tools::contract::Spec;
use std::{
borrow::Cow,
collections::HashSet,
Expand Down Expand Up @@ -283,6 +284,8 @@ impl Cmd {
}

let mut wasm_bytes = fs::read(target_file_path).map_err(Error::ReadingWasmFile)?;
let spec = Spec::new(&wasm_bytes).unwrap();
println!("spec: {:#?}", spec.meta);

for (k, v) in self.meta.clone() {
let key: StringM = k
Expand All @@ -299,6 +302,11 @@ impl Cmd {
.to_xdr(Limits::none())
.map_err(|e| Error::MetaArg(format!("failed to encode metadata entry: {e}")))?;

if spec.meta.contains(&meta_entry) {
println!("metadata entry already exists in wasm: {k}={v}");
continue;
}

wasm_gen::write_custom_section(&mut wasm_bytes, META_CUSTOM_SECTION_NAME, &xdr);
}

Expand Down
Loading