Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-853p-5678-hv8f
* Handle `LangError` from `DelegateCall` * Add basic delegate E2E test * Get test working with integration flipper * Use `incrementer` for test instead * Add tests to make sure that `LangError`s can be handled * Get rid of references to the interation Flipper
- Loading branch information
Showing
8 changed files
with
269 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
integration-tests/lang-err-integration-tests/call-builder-delegate/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "call_builder_delegate" | ||
version = "4.2.0" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
ink = { path = "../../../crates/ink", default-features = false } | ||
|
||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } | ||
|
||
incrementer = { path = "../../incrementer", default-features = false, features = ["ink-as-dependency"] } | ||
|
||
[dev-dependencies] | ||
ink_e2e = { path = "../../../crates/e2e" } | ||
|
||
[lib] | ||
path = "lib.rs" | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ink/std", | ||
"scale/std", | ||
"scale-info/std", | ||
|
||
"incrementer/std", | ||
] | ||
ink-as-dependency = [] | ||
e2e-tests = [] |
211 changes: 211 additions & 0 deletions
211
integration-tests/lang-err-integration-tests/call-builder-delegate/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
//! # Integration Tests for `LangError` | ||
//! | ||
//! This contract is used to ensure that the behavior around `LangError`s works as | ||
//! expected. | ||
//! | ||
//! In particular, it exercises the codepaths that stem from the usage of the | ||
//! [`CallBuilder`](`ink::env::call::CallBuilder`) and | ||
//! [`CreateBuilder`](`ink::env::call::CreateBuilder`) structs. | ||
//! | ||
//! This differs from the codepath used by external tooling, such as `cargo-contract` or | ||
//! the `Contracts-UI` which instead depend on methods from the Contracts pallet which are | ||
//! exposed via RPC. | ||
//! | ||
//! Note that during testing we make use of ink!'s end-to-end testing features, so ensure | ||
//! that you have a node which includes the Contracts pallet running alongside your tests. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std, no_main)] | ||
|
||
#[ink::contract] | ||
mod call_builder { | ||
use ink::env::{ | ||
call::{ | ||
build_call, | ||
ExecutionInput, | ||
Selector, | ||
}, | ||
DefaultEnvironment, | ||
}; | ||
|
||
#[ink(storage)] | ||
#[derive(Default)] | ||
pub struct CallBuilderDelegateTest { | ||
/// Since we're going to `DelegateCall` into the `incrementer` contract, we need | ||
/// to make sure our storage layout matches. | ||
value: i32, | ||
} | ||
|
||
impl CallBuilderDelegateTest { | ||
#[ink(constructor)] | ||
pub fn new(value: i32) -> Self { | ||
Self { value } | ||
} | ||
|
||
/// Call a contract using the `CallBuilder`. | ||
/// | ||
/// Since we can't use the `CallBuilder` in a test environment directly we need | ||
/// this wrapper to test things like crafting calls with invalid | ||
/// selectors. | ||
/// | ||
/// We also wrap the output in an `Option` since we can't return a `Result` | ||
/// directly from a contract message without erroring out ourselves. | ||
#[ink(message)] | ||
pub fn delegate( | ||
&mut self, | ||
code_hash: Hash, | ||
selector: [u8; 4], | ||
) -> Option<ink::LangError> { | ||
let result = build_call::<DefaultEnvironment>() | ||
.delegate(code_hash) | ||
.exec_input(ExecutionInput::new(Selector::new(selector))) | ||
.returns::<bool>() | ||
.try_invoke() | ||
.expect("Error from the Contracts pallet."); | ||
|
||
match result { | ||
Ok(_) => None, | ||
Err(e @ ink::LangError::CouldNotReadInput) => Some(e), | ||
Err(_) => { | ||
unimplemented!("No other `LangError` variants exist at the moment.") | ||
} | ||
} | ||
} | ||
|
||
/// Call a contract using the `CallBuilder`. | ||
/// | ||
/// Since we can't use the `CallBuilder` in a test environment directly we need | ||
/// this wrapper to test things like crafting calls with invalid | ||
/// selectors. | ||
/// | ||
/// This message does not allow the caller to handle any `LangErrors`, for that | ||
/// use the `call` message instead. | ||
#[ink(message)] | ||
pub fn invoke(&mut self, code_hash: Hash, selector: [u8; 4]) -> i32 { | ||
use ink::env::call::build_call; | ||
|
||
build_call::<DefaultEnvironment>() | ||
.delegate(code_hash) | ||
.exec_input(ExecutionInput::new(Selector::new(selector))) | ||
.returns::<i32>() | ||
.invoke() | ||
} | ||
} | ||
|
||
#[cfg(all(test, feature = "e2e-tests"))] | ||
mod e2e_tests { | ||
use super::*; | ||
|
||
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; | ||
|
||
#[ink_e2e::test] | ||
async fn e2e_call_builder_delegate_returns_correct_value( | ||
mut client: ink_e2e::Client<C, E>, | ||
) -> E2EResult<()> { | ||
let origin = client | ||
.create_and_fund_account(&ink_e2e::alice(), 10_000_000_000_000) | ||
.await; | ||
|
||
let expected_value = 42; | ||
let constructor = CallBuilderDelegateTestRef::new(expected_value); | ||
let call_builder = client | ||
.instantiate("call_builder_delegate", &origin, constructor, 0, None) | ||
.await | ||
.expect("instantiate failed"); | ||
let mut call_builder_call = call_builder.call::<CallBuilderDelegateTest>(); | ||
|
||
let code_hash = client | ||
.upload("incrementer", &origin, None) | ||
.await | ||
.expect("upload `incrementer` failed") | ||
.code_hash; | ||
|
||
let selector = ink::selector_bytes!("get"); | ||
let call = call_builder_call.invoke(code_hash, selector); | ||
let call_result = client | ||
.call(&origin, &call, 0, None) | ||
.await | ||
.expect("Client failed to call `call_builder::invoke`.") | ||
.return_value(); | ||
|
||
assert_eq!( | ||
call_result, expected_value, | ||
"Decoded an unexpected value from the call." | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[ink_e2e::test] | ||
async fn e2e_invalid_message_selector_can_be_handled( | ||
mut client: ink_e2e::Client<C, E>, | ||
) -> E2EResult<()> { | ||
let origin = client | ||
.create_and_fund_account(&ink_e2e::bob(), 10_000_000_000_000) | ||
.await; | ||
|
||
let constructor = CallBuilderDelegateTestRef::new(Default::default()); | ||
let call_builder_contract = client | ||
.instantiate("call_builder_delegate", &origin, constructor, 0, None) | ||
.await | ||
.expect("instantiate failed"); | ||
let mut call_builder_call = | ||
call_builder_contract.call::<CallBuilderDelegateTest>(); | ||
|
||
let code_hash = client | ||
.upload("incrementer", &origin, None) | ||
.await | ||
.expect("upload `incrementer` failed") | ||
.code_hash; | ||
|
||
let selector = ink::selector_bytes!("invalid_selector"); | ||
let call = call_builder_call.delegate(code_hash, selector); | ||
let call_result = client | ||
.call(&origin, &call, 0, None) | ||
.await | ||
.expect("Calling `call_builder::delegate` failed"); | ||
|
||
assert!(matches!( | ||
call_result.return_value(), | ||
Some(ink::LangError::CouldNotReadInput) | ||
)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[ink_e2e::test] | ||
async fn e2e_invalid_message_selector_panics_on_invoke( | ||
mut client: ink_e2e::Client<C, E>, | ||
) -> E2EResult<()> { | ||
let origin = client | ||
.create_and_fund_account(&ink_e2e::charlie(), 10_000_000_000_000) | ||
.await; | ||
|
||
let constructor = CallBuilderDelegateTestRef::new(Default::default()); | ||
let call_builder_contract = client | ||
.instantiate("call_builder_delegate", &origin, constructor, 0, None) | ||
.await | ||
.expect("instantiate failed"); | ||
let mut call_builder_call = | ||
call_builder_contract.call::<CallBuilderDelegateTest>(); | ||
|
||
let code_hash = client | ||
.upload("incrementer", &origin, None) | ||
.await | ||
.expect("upload `incrementer` failed") | ||
.code_hash; | ||
|
||
// Since `LangError`s can't be handled by the `CallBuilder::invoke()` method | ||
// we expect this to panic. | ||
let selector = ink::selector_bytes!("invalid_selector"); | ||
let call = call_builder_call.invoke(code_hash, selector); | ||
let call_result = client.call_dry_run(&origin, &call, 0, None).await; | ||
|
||
assert!(call_result.is_err()); | ||
assert!(call_result | ||
.debug_message() | ||
.contains("Cross-contract call failed with CouldNotReadInput")); | ||
|
||
Ok(()) | ||
} | ||
} | ||
} |