Skip to content

Commit

Permalink
Print type comparison warning only on --verbose (#1483)
Browse files Browse the repository at this point in the history
* add verbose warning when checking types

* add changelog

* fix typo
  • Loading branch information
Gherman authored Feb 1, 2024
1 parent 16b730a commit 552d7cf
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `rpc` command - [#1458](https://github.com/paritytech/cargo-contract/pull/1458)

### Changed
- Print type comparison warning only on `--verbose` - [#1483](https://github.com/paritytech/cargo-contract/pull/1483)
- Mandatory dylint-based lints - [#1412](https://github.com/paritytech/cargo-contract/pull/1412)
- Add a new tabular layout for the contract storage data - [#1485](https://github.com/paritytech/cargo-contract/pull/1485)

Expand Down
4 changes: 3 additions & 1 deletion crates/build/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ impl TryFrom<&VerbosityFlags> for Verbosity {
}

/// Denotes if output should be printed to stdout.
#[derive(Clone, Copy, Default, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
#[derive(
Clone, Copy, Default, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug,
)]
pub enum Verbosity {
/// Use default output
#[default]
Expand Down
1 change: 1 addition & 0 deletions crates/cargo-contract/src/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl CallCommand {
.url(self.extrinsic_cli_opts.url.clone())
.suri(self.extrinsic_cli_opts.suri.clone())
.storage_deposit_limit(self.extrinsic_cli_opts.storage_deposit_limit.clone())
.verbosity(self.extrinsic_cli_opts.verbosity()?)
.done();
let call_exec = CallCommandBuilder::default()
.contract(self.contract.clone())
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 @@ -189,7 +189,7 @@ impl CallCommandBuilder<state::Message, state::ExtrinsicOptions> {
let rpc = RpcClient::from_url(&url).await?;
let client = OnlineClient::from_rpc_client(rpc.clone()).await?;
let rpc = LegacyRpcMethods::new(rpc);
check_env_types(&client, &transcoder)?;
check_env_types(&client, &transcoder, self.opts.extrinsic_opts.verbosity())?;

let token_metadata = TokenMetadata::query(&rpc).await?;

Expand Down
25 changes: 19 additions & 6 deletions crates/extrinsics/src/env_check.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use colored::Colorize;
use contract_build::{
verbose_eprintln,
Verbosity,
};
use ink_metadata::InkProject;
use scale_info::{
form::PortableForm,
Expand All @@ -13,16 +18,20 @@ use anyhow::{

fn get_node_env_fields(
registry: &PortableRegistry,
verbosity: &Verbosity,
) -> Result<Option<Vec<Field<PortableForm>>>> {
let Some(env_type) = registry.types.iter().find(|t| {
let len = t.ty.path.segments.len();
let bound = len.saturating_sub(2);
t.ty.path.segments[bound..] == ["pallet_contracts", "Environment"]
}) else {
// if we can't find the type, then we use the old contract version.
eprintln!(
"The targeted version of `pallet-contracts` does not contain the `Environment` type. \
Therefore the check for compatible types cannot be performed, and your types may not match those of the target node"
verbose_eprintln!(
verbosity,
"{} {}",
"Warning:".yellow().bold(),
"This chain does not yet support checking for compatibility of your contract types (https://use.ink/faq#type-comparison)."
.yellow()
);
return Ok(None)
};
Expand Down Expand Up @@ -76,8 +85,9 @@ pub(crate) fn resolve_type_definition(
pub fn compare_node_env_with_contract(
node_registry: &PortableRegistry,
contract_metadata: &InkProject,
verbosity: &Verbosity,
) -> Result<()> {
let Some(env_fields) = get_node_env_fields(node_registry)? else {
let Some(env_fields) = get_node_env_fields(node_registry, verbosity)? else {
return Ok(())
};
for field in env_fields {
Expand Down Expand Up @@ -136,6 +146,7 @@ fn compare_type(

#[cfg(test)]
mod tests {
use contract_build::Verbosity;
use ink_metadata::{
layout::{
Layout,
Expand Down Expand Up @@ -362,7 +373,8 @@ mod tests {
Timestamp,
>();

let valid = compare_node_env_with_contract(&portable, &ink_project);
let valid =
compare_node_env_with_contract(&portable, &ink_project, &Verbosity::Default);
assert!(valid.is_ok(), "{}", valid.err().unwrap())
}

Expand All @@ -376,7 +388,8 @@ mod tests {
let ink_project =
generate_contract_ink_project::<AccountId, Balance, BlockNumber, Hash, u8>();

let result = compare_node_env_with_contract(&portable, &ink_project);
let result =
compare_node_env_with_contract(&portable, &ink_project, &Verbosity::Default);
assert_eq!(
result.err().unwrap().to_string(),
"Failed to validate the field: timestamp"
Expand Down
15 changes: 15 additions & 0 deletions crates/extrinsics/src/extrinsic_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use core::marker::PhantomData;

use contract_build::Verbosity;
use subxt_signer::{
sr25519::Keypair,
SecretUri,
Expand Down Expand Up @@ -47,6 +48,7 @@ pub struct ExtrinsicOpts {
url: url::Url,
suri: String,
storage_deposit_limit: Option<BalanceVariant>,
verbosity: Verbosity,
}

/// Type state for the extrinsics' commands to tell that some mandatory state has not yet
Expand Down Expand Up @@ -130,6 +132,13 @@ impl<S> ExtrinsicOptsBuilder<S> {
this.opts.storage_deposit_limit = storage_deposit_limit;
this
}

/// Set the verbosity level.
pub fn verbosity(self, verbosity: Verbosity) -> Self {
let mut this = self;
this.opts.verbosity = verbosity;
this
}
}

impl ExtrinsicOptsBuilder<state::Suri> {
Expand All @@ -150,6 +159,7 @@ impl ExtrinsicOpts {
url: url::Url::parse("ws://localhost:9944").unwrap(),
suri: String::new(),
storage_deposit_limit: None,
verbosity: Verbosity::Default,
},
marker: PhantomData,
}
Expand Down Expand Up @@ -206,6 +216,11 @@ impl ExtrinsicOpts {
.map(|bv| bv.denominate_balance(token_metadata))
.transpose()?)
}

/// Verbosity for message reporting.
pub fn verbosity(&self) -> &Verbosity {
&self.verbosity
}
}

impl Default for ExtrinsicOpts {
Expand Down
2 changes: 1 addition & 1 deletion crates/extrinsics/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl InstantiateCommandBuilder<state::ExtrinsicOptions> {

let rpc_cli = RpcClient::from_url(&url).await?;
let client = OnlineClient::from_rpc_client(rpc_cli.clone()).await?;
check_env_types(&client, &transcoder)?;
check_env_types(&client, &transcoder, self.opts.extrinsic_opts.verbosity())?;
let rpc = LegacyRpcMethods::new(rpc_cli);

let token_metadata = TokenMetadata::query(&rpc).await?;
Expand Down
8 changes: 7 additions & 1 deletion crates/extrinsics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use env_check::compare_node_env_with_contract;
use anyhow::Result;
use contract_build::{
CrateMetadata,
Verbosity,
DEFAULT_KEY_COL_WIDTH,
};
use scale::{
Expand Down Expand Up @@ -260,11 +261,16 @@ async fn get_best_block<C: Config>(
fn check_env_types<T>(
client: &OnlineClient<T>,
transcoder: &ContractMessageTranscoder,
verbosity: &Verbosity,
) -> Result<()>
where
T: Config,
{
compare_node_env_with_contract(client.metadata().types(), transcoder.metadata())
compare_node_env_with_contract(
client.metadata().types(),
transcoder.metadata(),
verbosity,
)
}

// Converts a Url into a String representation without excluding the default port.
Expand Down
2 changes: 1 addition & 1 deletion crates/extrinsics/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl UploadCommandBuilder<state::ExtrinsicOptions> {
let url = self.opts.extrinsic_opts.url();
let rpc_cli = RpcClient::from_url(&url).await?;
let client = OnlineClient::from_rpc_client(rpc_cli.clone()).await?;
check_env_types(&client, &transcoder)?;
check_env_types(&client, &transcoder, self.opts.extrinsic_opts.verbosity())?;
let rpc = LegacyRpcMethods::new(rpc_cli);

let token_metadata = TokenMetadata::query(&rpc).await?;
Expand Down

0 comments on commit 552d7cf

Please sign in to comment.