Skip to content

Commit e2d9900

Browse files
xermicusGerman
andauthored
Prevent submitting calls to messages marked as immutable (#1397)
Prevent submitting calls to messages marked as immutable. Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com> Co-authored-by: German <german@parity.io>
1 parent a7ef97e commit e2d9900

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Remove check for compatible `scale` and `scale-info` versions - [#1370](https://github.com/paritytech/cargo-contract/pull/1370)
2121
- Add workspace support -[#1358](https://github.com/paritytech/cargo-contract/pull/1358)
2222

23+
### Fixed
24+
- Do not allow to execute calls on immutable contract messages - [#1397](https://github.com/paritytech/cargo-contract/pull/1397)
25+
2326
## [4.0.0-alpha]
2427

2528
Replaces the yanked `3.1.0` due to issues with supporting *both* Rust versions < `1.70`

crates/extrinsics/src/call.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,23 @@ impl CallExec {
267267
&self,
268268
gas_limit: Option<Weight>,
269269
) -> Result<DisplayEvents, ErrorVariant> {
270+
if !self
271+
.transcoder()
272+
.metadata()
273+
.spec()
274+
.messages()
275+
.iter()
276+
.find(|msg| msg.label() == &self.message)
277+
.expect("message exist after calling CallExec::done()")
278+
.mutates()
279+
{
280+
let inner = anyhow!(
281+
"Tried to execute a call on the immutable contract message '{}'. Please do a dry-run instead.",
282+
&self.message
283+
);
284+
return Err(inner.into());
285+
}
286+
270287
// use user specified values where provided, otherwise estimate
271288
let gas_limit = match gas_limit {
272289
Some(gas_limit) => gas_limit,

crates/extrinsics/src/integration_tests.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ impl ContractsNodeProcess {
9696
);
9797
let result = OnlineClient::new().await;
9898
if let Ok(client) = result {
99-
break Ok(client)
99+
break Ok(client);
100100
}
101101
if attempts < MAX_ATTEMPTS {
102102
attempts += 1;
103-
continue
103+
continue;
104104
}
105105
if let Err(err) = result {
106-
break Err(err)
106+
break Err(err);
107107
}
108108
};
109109
match client {
@@ -509,6 +509,13 @@ async fn api_build_upload_instantiate_call() {
509509
.to_string();
510510
assert!(value.contains("true"), "{:#?}", value);
511511

512+
// call the contract on the immutable "get" message trying to execute
513+
// this should fail because "get" is immutable
514+
match call.call(None).await {
515+
Err(crate::ErrorVariant::Generic(_)) => {}
516+
_ => panic!("immutable call was not prevented"),
517+
}
518+
512519
// call the contract
513520
// flip the value
514521
let call = CallCommandBuilder::default()

0 commit comments

Comments
 (0)