Skip to content

Commit

Permalink
Feat/add meta on build (#1684)
Browse files Browse the repository at this point in the history
* Refactor to use wasmgen and not re-parse entire wasm file when adding new meta

* Add metadata arg to build command

---------

Co-authored-by: Willem Wyndham <[email protected]>
Co-authored-by: Leigh McCulloch <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 1c3f4d2 commit 1fb8559
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 41 deletions.
71 changes: 47 additions & 24 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 FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ To view the commands that will be executed, without executing them, use the --pr

If ommitted, wasm files are written only to the cargo target directory.
* `--print-commands-only` — Print commands to build without executing them
* `--meta <META>` — Add key-value to contract meta (adds the meta to the `contractmetav0` custom section)



Expand Down
20 changes: 13 additions & 7 deletions cmd/crates/soroban-spec-tools/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ pub enum Error {

impl Spec {
pub fn new(bytes: &[u8]) -> Result<Self, Error> {
let mut env_meta: Option<&[u8]> = None;
let mut meta: Option<&[u8]> = None;
let mut spec: Option<&[u8]> = None;
let mut env_meta: Option<Vec<u8>> = None;
let mut meta: Option<Vec<u8>> = None;
let mut spec: Option<Vec<u8>> = None;
for payload in wasmparser::Parser::new(0).parse_all(bytes) {
let payload = payload?;
if let wasmparser::Payload::CustomSection(section) = payload {
Expand All @@ -52,13 +52,19 @@ impl Spec {
"contractspecv0" => &mut spec,
_ => continue,
};
*out = Some(section.data());

if let Some(existing_data) = out {
let combined_data = [existing_data, section.data()].concat();
*out = Some(combined_data);
} else {
*out = Some(section.data().to_vec());
}
};
}

let mut env_meta_base64 = None;
let env_meta = if let Some(env_meta) = env_meta {
env_meta_base64 = Some(base64.encode(env_meta));
env_meta_base64 = Some(base64.encode(&env_meta));
let cursor = Cursor::new(env_meta);
let mut read = Limited::new(cursor, Limits::none());
ScEnvMetaEntry::read_xdr_iter(&mut read).collect::<Result<Vec<_>, xdr::Error>>()?
Expand All @@ -68,7 +74,7 @@ impl Spec {

let mut meta_base64 = None;
let meta = if let Some(meta) = meta {
meta_base64 = Some(base64.encode(meta));
meta_base64 = Some(base64.encode(&meta));
let cursor = Cursor::new(meta);
let mut depth_limit_read = Limited::new(cursor, Limits::none());
ScMetaEntry::read_xdr_iter(&mut depth_limit_read)
Expand All @@ -78,7 +84,7 @@ impl Spec {
};

let (spec_base64, spec) = if let Some(spec) = spec {
let (spec_base64, spec) = Spec::spec_to_base64(spec)?;
let (spec_base64, spec) = Spec::spec_to_base64(&spec)?;
(Some(spec_base64), spec)
} else {
(None, vec![])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![no_std]
use soroban_sdk::{contract, contractimpl};
use soroban_sdk::{contract, contractimpl, contractmeta};

#[contract]
pub struct Contract;

contractmeta!(key = "Description", val = "A test add contract");

#[contractimpl]
impl Contract {
pub fn add(x: u64, y: u64) -> u128 {
Expand Down
34 changes: 34 additions & 0 deletions cmd/crates/soroban-test/tests/it/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,37 @@ cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --targe
",
));
}

#[test]
fn build_with_metadata() {
let sandbox = TestEnv::default();
let cargo_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let fixture_path = cargo_dir.join("tests/fixtures/workspace/contracts/add");
let outdir = sandbox.dir().join("out");

sandbox
.new_assert_cmd("contract")
.current_dir(&fixture_path)
.arg("build")
.arg("--meta")
.arg("contract meta=added on build")
.arg("--out-dir")
.arg(&outdir)
.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)
.assert()
.success()
.stdout(predicate::str::contains("Description: A test add contract"))
.stdout(predicate::str::contains("contract meta: added on build"));
}
1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ glob = "0.3.1"
fqdn = "0.3.12"
open = "5.3.0"
url = "2.5.2"
wasm-gen = "0.1.4"

[build-dependencies]
crate-git-revision = "0.0.4"
Expand Down
Loading

0 comments on commit 1fb8559

Please sign in to comment.