-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathlib.rs
executable file
·211 lines (183 loc) · 7.59 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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(())
}
}
}