Skip to content

Commit

Permalink
Fix "chain configuration not found" error (#1786)
Browse files Browse the repository at this point in the history
* Account for spaced and non-spaced behavior of `stringify!`

* For user output: remove spaces from object path

* Output the chain configuration that was searched

* Apply `cargo fmt`

* Add changelog entry

* Switch spaces clean-up

* Fix string handling

* Apply `cargo fmt`

* Improve comment

* Improve formatting

* Improve assert error message

* Shorten code
  • Loading branch information
cmichi authored Nov 4, 2024
1 parent e83ebe0 commit 7fddfd9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

[Unreleased]

### Fixed
- Fix "chain configuration not found" error - [#1786](https://github.com/paritytech/cargo-contract/pull/1786)

## [5.0.0-alpha]

### Changed
Expand Down
42 changes: 36 additions & 6 deletions crates/cargo-contract/src/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,29 +218,59 @@ macro_rules! call_with_config_internal {
stringify!($config) => $obj.$function::<$config>().await,
)*
_ => {

let configs = vec![$(stringify!($config)),*].iter()
.map(|s| s.trim_start_matches("crate::cmd::config::"))
let configs = vec![$(stringify!($config)),*].iter()
.map(|s| s.replace(" ", ""))
.map(|s| s.trim_start_matches("$crate::cmd::config::").to_string())
.collect::<Vec<_>>()
.join(", ");
Err(ErrorVariant::Generic(
contract_extrinsics::GenericError::from_message(
format!("Chain configuration not found, Allowed configurations: {configs}")
format!("Chain configuration {} not found, allowed configurations: {configs}", $config_name)
)))
},
}
};
}

/// Macro that allows calling the command member function with chain configuration
///
/// # Developer Note
///
/// In older Rust versions the macro `stringify!($crate::foo)` expanded to
/// `"$crate::foo"`. This behavior changed with https://github.com/rust-lang/rust/pull/125174,
/// `stringify!` expands to `"$crate :: foo"` now. In order to support both older and
/// newer Rust versions our macro has to handle both cases, spaced and non-spaced.
///
/// # Known Limitation
///
/// The `$config_name:expr` has to be in the `$crate::cmd::config` crate and cannot
/// contain another `::` sub-path.
#[macro_export]
macro_rules! call_with_config {
($obj:tt, $function:ident, $config_name:expr) => {{
let config_name = format!("crate::cmd::config::{}", $config_name);
assert!(
!format!("{}", $config_name).contains("::"),
"The supplied config name `{}` is not allowed to contain `::`.",
$config_name
);

let res_nonspaced = $crate::call_with_config_internal!(
$obj,
$function,
format!("$crate::cmd::config::{}", $config_name).as_str(),
// All available chain configs need to be specified here
$crate::cmd::config::Polkadot,
$crate::cmd::config::Substrate,
$crate::cmd::config::Ecdsachain
);
if !res_nonspaced.is_err() {
return res_nonspaced
}

$crate::call_with_config_internal!(
$obj,
$function,
config_name.as_str(),
format!("$crate :: cmd :: config :: {}", $config_name).as_str(),
// All available chain configs need to be specified here
$crate::cmd::config::Polkadot,
$crate::cmd::config::Substrate,
Expand Down

0 comments on commit 7fddfd9

Please sign in to comment.