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 committed Nov 5, 2024
1 parent 3d74a06 commit b0e0b5d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ 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]
[Unreleased]

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

## [5.0.0-alpha]

### Changed
- Updated the toolchain version used by `ink_linting` - [#1616](https://github.com/paritytech/cargo-contract/pull/1616)
- Bump the version of `subxt` and `subxt-signer` - [#1722](https://github.com/use-ink/cargo-contract/pull/1722)

### Removed
- Remove support for `V11` metadata [#1722](https://github.com/use-ink/cargo-contract/pull/1722)

## [4.1.1]

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 b0e0b5d

Please sign in to comment.