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

feat: allow user to choose build mode: debug (default) or release #202

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ Build the Smart Contract:
# Build an existing Smart Contract
pop build contract -p ./my_contract
```
By default the contract is compiled with `debug` functionality included.

This enables the contract to output debug messages, but increases the contract size and the amount of gas used.

For production builds, use the --release flag: `--release`:
```sh
pop build contract -p ./my_contract --release
```

To deploy a Smart Contract you need a chain running. For testing purposes you can simply spawn a contract node:

Expand Down
6 changes: 5 additions & 1 deletion crates/pop-cli/src/commands/build/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use pop_contracts::build_smart_contract;
pub struct BuildContractCommand {
#[arg(short = 'p', long, help = "Path for the contract project, [default: current directory]")]
pub(crate) path: Option<PathBuf>,
/// The default compilation includes debug functionality, increasing contract size and gas usage.
/// For production, always build in release mode to exclude debug features.
#[clap(long = "release")]
build_release: bool,
}

impl BuildContractCommand {
Expand All @@ -21,7 +25,7 @@ impl BuildContractCommand {
intro(format!("{}: Building your contract", style(" Pop CLI ").black().on_magenta()))?;
set_theme(Theme);

let result_build = build_smart_contract(&self.path)?;
let result_build = build_smart_contract(&self.path, self.build_release)?;
outro("Build completed successfully!")?;
log::success(result_build.to_string())?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/pop-cli/src/commands/up/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl UpContractCommand {
if !build_path.as_path().exists() {
log::warning(format!("NOTE: contract has not yet been built."))?;
intro(format!("{}: Building a contract", style(" Pop CLI ").black().on_magenta()))?;
// Directory exists, proceed with the rest of the code
let result = build_smart_contract(&self.path)?;
// Build the contract in release mode
let result = build_smart_contract(&self.path, true)?;
log::success(result.to_string())?;
}

Expand Down
3 changes: 2 additions & 1 deletion crates/pop-contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Build an existing Smart Contract:
use pop_contracts::build_smart_contract;

let contract_path = ...;
build_smart_contract(&contract_path)?;
let build_release = true; // Set to True for release mode, False for debug mode.
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
build_smart_contract(&contract_path, build_release)?;
```

Test an existing Smart Contract:
Expand Down
13 changes: 9 additions & 4 deletions crates/pop-contracts/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// SPDX-License-Identifier: GPL-3.0
use contract_build::{execute, ExecuteArgs};
use contract_build::{execute, BuildMode, ExecuteArgs};
use std::path::PathBuf;

use crate::utils::helpers::get_manifest_path;

/// Build the smart contract located in the specified `path`.
pub fn build_smart_contract(path: &Option<PathBuf>) -> anyhow::Result<String> {
/// Build the smart contract located at the specified `path` in `build_release` mode.
pub fn build_smart_contract(path: &Option<PathBuf>, build_release: bool) -> anyhow::Result<String> {
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
let manifest_path = get_manifest_path(path)?;

let build_mode = match build_release {
true => BuildMode::Release,
false => BuildMode::Debug,
};
// Default values
let args = ExecuteArgs { manifest_path, ..Default::default() };
let args = ExecuteArgs { manifest_path, build_mode, ..Default::default() };

// Execute the build and log the output of the build
let result = execute(args)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/pop-contracts/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod tests {
Ok(temp_dir)
}
fn build_smart_contract_test_environment(temp_dir: &TempDir) -> Result<(), Error> {
build_smart_contract(&Some(temp_dir.path().join("test_contract")))?;
build_smart_contract(&Some(temp_dir.path().join("test_contract")), true)?;
Ok(())
}

Expand Down
30 changes: 23 additions & 7 deletions crates/pop-contracts/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ fn setup_test_environment() -> std::result::Result<tempfile::TempDir, Error> {
Ok(temp_dir)
}

#[test]
fn test_contract_build() -> std::result::Result<(), Error> {
let temp_contract_dir = setup_test_environment()?;

build_smart_contract(&Some(temp_contract_dir.path().join("test_contract")))?;

fn verify_build_files(temp_contract_dir: TempDir) -> Result<()> {
// Verify that the folder target has been created
assert!(temp_contract_dir.path().join("test_contract/target").exists());
// Verify that all the artifacts has been generated
Expand All @@ -37,14 +32,35 @@ fn test_contract_build() -> std::result::Result<(), Error> {
.path()
.join("test_contract/target/ink/test_contract.json")
.exists());
Ok(())
}

#[test]
fn test_contract_build() -> std::result::Result<(), Error> {
// Test building in release mode
let temp_contract_dir = setup_test_environment()?;

let formatted_result =
build_smart_contract(&Some(temp_contract_dir.path().join("test_contract")), true)?;
assert!(formatted_result.contains("RELEASE"));

verify_build_files(temp_contract_dir)?;

let temp_debug_contract_dir = setup_test_environment()?;
// Test building in debug mode
let formatted_result_debug_mode =
build_smart_contract(&Some(temp_debug_contract_dir.path().join("test_contract")), false)?;
assert!(formatted_result_debug_mode.contains("DEBUG"));

verify_build_files(temp_debug_contract_dir)?;

Ok(())
}

const CONTRACTS_NETWORK_URL: &str = "wss://rococo-contracts-rpc.polkadot.io";

fn build_smart_contract_test_environment(temp_dir: &TempDir) -> Result<(), Error> {
build_smart_contract(&Some(temp_dir.path().join("test_contract")))?;
build_smart_contract(&Some(temp_dir.path().join("test_contract")), true)?;
Ok(())
}

Expand Down
Loading