Skip to content

Commit 85c7d1d

Browse files
Append custom meta entries in one section (#2229)
1 parent aae8105 commit 85c7d1d

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

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

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ use std::{
1010
env,
1111
ffi::OsStr,
1212
fmt::Debug,
13-
fs, io,
13+
fs,
14+
io::{self, Cursor},
1415
path::{self, Path, PathBuf},
1516
process::{Command, ExitStatus, Stdio},
1617
};
17-
use stellar_xdr::curr::{Limits, ScMetaEntry, ScMetaV0, StringM, WriteXdr};
18+
use stellar_xdr::curr::{Limited, Limits, ScMetaEntry, ScMetaV0, StringM, WriteXdr};
1819

1920
use crate::{commands::global, print::Print};
2021

@@ -114,6 +115,8 @@ pub enum Error {
114115
MetaArg(String),
115116
#[error("use rust 1.81 or 1.84+ to build contracts (got {0})")]
116117
RustVersion(String),
118+
#[error(transparent)]
119+
Xdr(#[from] stellar_xdr::curr::Error),
117120
}
118121

119122
const WASM_TARGET: &str = "wasm32v1-none";
@@ -296,7 +299,17 @@ impl Cmd {
296299
}
297300

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

311+
fn encoded_new_meta(&self) -> Result<Vec<u8>, Error> {
312+
let mut new_meta: Vec<ScMetaEntry> = Vec::new();
300313
for (k, v) in self.meta.clone() {
301314
let key: StringM = k
302315
.clone()
@@ -308,17 +321,15 @@ impl Cmd {
308321
.try_into()
309322
.map_err(|e| Error::MetaArg(format!("{v} is an invalid metadata value: {e}")))?;
310323
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);
324+
new_meta.push(meta_entry);
316325
}
317326

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)
327+
let mut buffer = Vec::new();
328+
let mut writer = Limited::new(Cursor::new(&mut buffer), Limits::none());
329+
for entry in new_meta {
330+
entry.write_xdr(&mut writer)?;
331+
}
332+
Ok(buffer)
322333
}
323334

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

0 commit comments

Comments
 (0)