Skip to content

Commit 5efe3ce

Browse files
Append custom meta entries in one section
1 parent aae8105 commit 5efe3ce

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

  • cmd/soroban-cli/src/commands/contract

cmd/soroban-cli/src/commands/contract/build.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use std::{
1010
env,
1111
ffi::OsStr,
1212
fmt::Debug,
13-
fs, io,
13+
fs, io::{self, Cursor},
1414
path::{self, Path, PathBuf},
1515
process::{Command, ExitStatus, Stdio},
1616
};
17-
use stellar_xdr::curr::{Limits, ScMetaEntry, ScMetaV0, StringM, WriteXdr};
17+
use stellar_xdr::curr::{Limits, ScMetaEntry, ScMetaV0, StringM, WriteXdr, Limited};
1818

1919
use crate::{commands::global, print::Print};
2020

@@ -114,6 +114,8 @@ pub enum Error {
114114
MetaArg(String),
115115
#[error("use rust 1.81 or 1.84+ to build contracts (got {0})")]
116116
RustVersion(String),
117+
#[error(transparent)]
118+
Xdr(#[from] stellar_xdr::curr::Error),
117119
}
118120

119121
const WASM_TARGET: &str = "wasm32v1-none";
@@ -296,7 +298,17 @@ impl Cmd {
296298
}
297299

298300
let mut wasm_bytes = fs::read(target_file_path).map_err(Error::ReadingWasmFile)?;
301+
let xdr = self.encoded_new_meta()?;
302+
wasm_gen::write_custom_section(&mut wasm_bytes, META_CUSTOM_SECTION_NAME, &xdr);
303+
304+
// Deleting .wasm file effectively unlinking it from /release/deps/.wasm preventing from overwrite
305+
// See https://github.com/stellar/stellar-cli/issues/1694#issuecomment-2709342205
306+
fs::remove_file(target_file_path).map_err(Error::DeletingArtifact)?;
307+
fs::write(target_file_path, wasm_bytes).map_err(Error::WritingWasmFile)
308+
}
299309

310+
fn encoded_new_meta(&self) -> Result<Vec<u8>, Error> {
311+
let mut new_meta: Vec<ScMetaEntry> = Vec::new();
300312
for (k, v) in self.meta.clone() {
301313
let key: StringM = k
302314
.clone()
@@ -308,17 +320,15 @@ impl Cmd {
308320
.try_into()
309321
.map_err(|e| Error::MetaArg(format!("{v} is an invalid metadata value: {e}")))?;
310322
let meta_entry = ScMetaEntry::ScMetaV0(ScMetaV0 { key, val });
311-
let xdr: Vec<u8> = meta_entry
312-
.to_xdr(Limits::none())
313-
.map_err(|e| Error::MetaArg(format!("failed to encode metadata entry: {e}")))?;
314-
315-
wasm_gen::write_custom_section(&mut wasm_bytes, META_CUSTOM_SECTION_NAME, &xdr);
323+
new_meta.push(meta_entry);
316324
}
317325

318-
// Deleting .wasm file effectively unlinking it from /release/deps/.wasm preventing from overwrite
319-
// See https://github.com/stellar/stellar-cli/issues/1694#issuecomment-2709342205
320-
fs::remove_file(target_file_path).map_err(Error::DeletingArtifact)?;
321-
fs::write(target_file_path, wasm_bytes).map_err(Error::WritingWasmFile)
326+
let mut buffer = Vec::new();
327+
let mut writer = Limited::new(Cursor::new(&mut buffer), Limits::none());
328+
for entry in new_meta {
329+
entry.write_xdr(&mut writer)?;
330+
}
331+
Ok(buffer)
322332
}
323333

324334
fn print_build_summary(print: &Print, target_file_path: &PathBuf) -> Result<(), Error> {

0 commit comments

Comments
 (0)