Skip to content

Commit 0627b1e

Browse files
authored
Detect INK_STATIC_BUFFER_SIZE env var (#1310)
1 parent b731f54 commit 0627b1e

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- Adds workflow for publishing docker images for the verifiable builds - [#1267](https://github.com/paritytech/cargo-contract/pull/1267)
11+
- Detect `INK_STATIC_BUFFER_SIZE` env var - [#1310](https://github.com/paritytech/cargo-contract/pull/1310)
1112
- Add `verify` command - [#1306](https://github.com/paritytech/cargo-contract/pull/1306)
1213

1314
## [4.0.0-alpha]

crates/build/src/lib.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![doc = include_str!("../README.md")]
1818
#![deny(unused_crate_dependencies)]
1919

20+
use contract_metadata::ContractMetadata;
2021
use which as _;
2122

2223
mod args;
@@ -370,6 +371,81 @@ fn exec_cargo_for_onchain_target(
370371
Ok(())
371372
}
372373

374+
/// Check if the `INK_STATIC_BUFFER_SIZE` is set.
375+
/// If so, then checks if the current contract has already been compiled with a new value.
376+
/// If not, or metadata is not present, we need to clean binaries and rebuild.
377+
fn check_buffer_size_invoke_cargo_clean(
378+
crate_metadata: &CrateMetadata,
379+
verbosity: &Verbosity,
380+
) -> Result<()> {
381+
if let Ok(buffer_size) = std::env::var("INK_STATIC_BUFFER_SIZE") {
382+
let buffer_size_value: u64 = buffer_size
383+
.parse()
384+
.context("`INK_STATIC_BUFFER_SIZE` must have an integer value.")?;
385+
386+
let extract_buffer_size = |metadata_path: PathBuf| -> Result<u64> {
387+
let size = ContractMetadata::load(metadata_path)
388+
.context("Metadata is not present")?
389+
.abi
390+
// get `spec` field
391+
.get("spec")
392+
.context("spec field should be present in ABI.")?
393+
// get `environment` field
394+
.get("environment")
395+
.context("environment field should be present in ABI.")?
396+
// get `staticBufferSize` field
397+
.get("staticBufferSize")
398+
.context("`staticBufferSize` must be specified.")?
399+
// convert to u64
400+
.as_u64()
401+
.context("`staticBufferSize` value must be an integer.")?;
402+
403+
Ok(size)
404+
};
405+
406+
let cargo = util::cargo_cmd(
407+
"clean",
408+
Vec::<&str>::new(),
409+
crate_metadata.manifest_path.directory(),
410+
*verbosity,
411+
vec![],
412+
);
413+
414+
match extract_buffer_size(crate_metadata.metadata_path()) {
415+
Ok(contract_buffer_size) if contract_buffer_size == buffer_size_value => {
416+
verbose_eprintln!(
417+
verbosity,
418+
"{} {}",
419+
"info:".green().bold(),
420+
"Detected a configured buffer size, but the value is already specified."
421+
.bold()
422+
);
423+
}
424+
Ok(_) => {
425+
verbose_eprintln!(
426+
verbosity,
427+
"{} {}",
428+
"warning:".yellow().bold(),
429+
"Detected a change in the configured buffer size. Rebuilding the project."
430+
.bold()
431+
);
432+
invoke_cargo_and_scan_for_error(cargo)?;
433+
}
434+
Err(_) => {
435+
verbose_eprintln!(
436+
verbosity,
437+
"{} {}",
438+
"warning:".yellow().bold(),
439+
"Cannot find the previous size of the static buffer. Rebuilding the project."
440+
.bold()
441+
);
442+
invoke_cargo_and_scan_for_error(cargo)?;
443+
}
444+
}
445+
}
446+
Ok(())
447+
}
448+
373449
/// Executes the supplied cargo command, reading the output and scanning for known errors.
374450
/// Writes the captured stderr back to stderr and maintains the cargo tty progress bar.
375451
fn invoke_cargo_and_scan_for_error(cargo: duct::Expression) -> Result<()> {
@@ -880,6 +956,7 @@ fn local_build(
880956
"[==]".bold(),
881957
"Building cargo project".bright_green().bold()
882958
);
959+
check_buffer_size_invoke_cargo_clean(crate_metadata, verbosity)?;
883960
exec_cargo_for_onchain_target(
884961
crate_metadata,
885962
"build",

0 commit comments

Comments
 (0)