Skip to content

Commit

Permalink
Fix the instantiate command for Substrate 0.9.42-based chains (use-in…
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed authored Apr 8, 2024
1 parent a241868 commit 2ca6db0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Fix the `instantiate` command for Substrate `0.9.42` based chains - [#1564](https://github.com/paritytech/cargo-contract/pull/1564)

### Added
- Add `cargo contract storage --version` command - [#1564](https://github.com/paritytech/cargo-contract/pull/1564)
- Verify raw Wasm in cargo contract verify - [#1551](https://github.com/paritytech/cargo-contract/pull/1551)
- Specify prod chain URL with names and check for the verifiable build upon upload - [#1290](https://github.com/paritytech/cargo-contract/pull/1290)

Expand Down
4 changes: 2 additions & 2 deletions crates/cargo-contract/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub const MAX_KEY_COL_WIDTH: usize = STORAGE_DEPOSIT_KEY.len() + 1;

/// Print to stdout the fields of the result of a `instantiate` or `call` dry-run via RPC.
pub fn display_contract_exec_result<R, const WIDTH: usize, Balance>(
result: &ContractResult<R, Balance, ()>,
result: &ContractResult<R, Balance>,
) -> Result<()>
where
Balance: Debug,
Expand Down Expand Up @@ -226,7 +226,7 @@ where
}

pub fn display_contract_exec_result_debug<R, const WIDTH: usize, Balance>(
result: &ContractResult<R, Balance, ()>,
result: &ContractResult<R, Balance>,
) -> Result<()> {
let mut debug_message_lines = std::str::from_utf8(&result.debug_message)
.context("Error decoding UTF8 debug message bytes")?
Expand Down
28 changes: 24 additions & 4 deletions crates/cargo-contract/src/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ use super::{
#[clap(name = "storage", about = "Inspect contract storage")]
pub struct StorageCommand {
/// The address of the contract to inspect storage of.
#[clap(name = "contract", long, env = "CONTRACT")]
contract: String,
#[clap(
name = "contract",
long,
env = "CONTRACT",
required_unless_present = "version"
)]
contract: Option<String>,
/// Fetch the "raw" storage keys and values for the contract.
#[clap(long)]
raw: bool,
Expand All @@ -65,6 +70,10 @@ pub struct StorageCommand {
/// Path to the `Cargo.toml` of the contract.
#[clap(long, value_parser)]
manifest_path: Option<PathBuf>,
/// Fetch the storage version of the pallet contracts (state query:
/// contracts::palletVersion()).
#[clap(long, short)]
version: bool,
/// Arguments required for communtacting with a substrate node.
#[clap(flatten)]
chain_cli_opts: CLIChainOpts,
Expand All @@ -86,8 +95,19 @@ impl StorageCommand {
let rpc =
ContractStorageRpc::<C>::new(&self.chain_cli_opts.chain().url()).await?;
let storage_layout = ContractStorage::<C, C>::new(rpc);
let contract = parse_account(&self.contract)
.map_err(|e| anyhow::anyhow!("Failed to parse contract option: {}", e))?;
if self.version {
println!("{}", storage_layout.version().await?);
return Ok(())
}

// Contract arg shall be always present in this case, it is enforced by
// clap configuration
let contract = self
.contract
.as_ref()
.map(|c| parse_account(c))
.transpose()?
.expect("Contract argument shall be present");

if self.raw {
let storage_data =
Expand Down
2 changes: 1 addition & 1 deletion crates/extrinsics/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ where
///
/// Returns the dry run simulation result of type [`ContractExecResult`], which
/// includes information about the simulated call, or an error in case of failure.
pub async fn call_dry_run(&self) -> Result<ContractExecResult<E::Balance, ()>> {
pub async fn call_dry_run(&self) -> Result<ContractExecResult<E::Balance>> {
let storage_deposit_limit = self.opts.storage_deposit_limit();
let call_request = CallRequest {
origin: self.opts.signer().account_id(),
Expand Down
18 changes: 17 additions & 1 deletion crates/extrinsics/src/contract_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ use sp_core::{
};
use std::{
collections::BTreeMap,
fmt,
fmt::{
self,
Display,
Formatter,
},
Expand Down Expand Up @@ -94,6 +94,22 @@ where
}
}

/// Fetch the storage version of the pallet contracts.
///
/// This is the result of a state query to the function `contracts::palletVersion())`.
pub async fn version(&self) -> Result<u16> {
self.rpc
.client
.storage()
.at_latest()
.await?
.storage_version("Contracts")
.await
.map_err(|e| {
anyhow!("The storage version for the contracts pallet could not be determined: {e}")
})
}

/// Load the raw key/value storage for a given contract.
pub async fn load_contract_storage_data(
&self,
Expand Down
4 changes: 2 additions & 2 deletions crates/extrinsics/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ where
/// Returns the decoded dry run result, or an error in case of failure.
pub async fn decode_instantiate_dry_run(
&self,
result: &ContractInstantiateResult<C::AccountId, E::Balance, ()>,
result: &ContractInstantiateResult<C::AccountId, E::Balance>,
) -> Result<InstantiateDryRunResult<E::Balance>, ErrorVariant> {
tracing::debug!("instantiate data {:?}", self.args.data);
match result.result {
Expand Down Expand Up @@ -319,7 +319,7 @@ where
/// Returns the dry run simulation result, or an error in case of failure.
pub async fn instantiate_dry_run(
&self,
) -> Result<ContractInstantiateResult<C::AccountId, E::Balance, ()>> {
) -> Result<ContractInstantiateResult<C::AccountId, E::Balance>> {
let storage_deposit_limit = self.args.storage_deposit_limit;
let call_request = InstantiateRequest::<C, E> {
origin: self.opts.signer().account_id(),
Expand Down
16 changes: 5 additions & 11 deletions crates/extrinsics/src/pallet_contracts_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use sp_weights::Weight;
/// the `ContractsApi` version. Therefore when SCALE decoding a `ContractResult` its
/// trailing data should be ignored to avoid any potential compatibility issues.
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct ContractResult<R, Balance, EventRecord> {
pub struct ContractResult<R, Balance> {
/// How much weight was consumed during execution.
pub gas_consumed: Weight,
/// How much weight is required as gas limit in order to execute this call.
Expand Down Expand Up @@ -78,21 +78,15 @@ pub struct ContractResult<R, Balance, EventRecord> {
pub debug_message: Vec<u8>,
/// The execution result of the wasm code.
pub result: R,
/// The events that were emitted during execution. It is an option as event
/// collection is optional.
pub events: Option<Vec<EventRecord>>,
}

/// Result type of a `bare_call` call as well as `ContractsApi::call`.
pub type ContractExecResult<Balance, EventRecord> =
ContractResult<Result<ExecReturnValue, DispatchError>, Balance, EventRecord>;
pub type ContractExecResult<Balance> =
ContractResult<Result<ExecReturnValue, DispatchError>, Balance>;

/// Result type of a `bare_instantiate` call as well as `ContractsApi::instantiate`.
pub type ContractInstantiateResult<AccountId, Balance, EventRecord> = ContractResult<
Result<InstantiateReturnValue<AccountId>, DispatchError>,
Balance,
EventRecord,
>;
pub type ContractInstantiateResult<AccountId, Balance> =
ContractResult<Result<InstantiateReturnValue<AccountId>, DispatchError>, Balance>;

/// Result type of a `bare_code_upload` call.
pub type CodeUploadResult<CodeHash, Balance> =
Expand Down

0 comments on commit 2ca6db0

Please sign in to comment.