-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
executable file
·312 lines (268 loc) · 10.5 KB
/
Copy pathlib.rs
File metadata and controls
executable file
·312 lines (268 loc) · 10.5 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod multisig_flipper {
/// Errors that can occur during contract execution.
#[derive(Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum Error {
/// The caller is not the admin (multisig account).
NotAdmin,
}
pub type Result<T> = core::result::Result<T, Error>;
/// Emitted when the boolean value is flipped.
#[ink(event)]
pub struct Flipped {
#[ink(topic)]
new_value: bool,
}
/// Emitted when the admin is changed.
#[ink(event)]
pub struct AdminChanged {
#[ink(topic)]
old_admin: AccountId,
#[ink(topic)]
new_admin: AccountId,
}
/// A flipper contract controlled by a multisig account via `pallet-multisig`.
///
/// Only the designated admin (expected to be a multisig account) can call `flip()`.
/// Multisig members coordinate through `Multisig.as_multi` / `approve_as_multi`
/// extrinsics wrapping a `Contracts.call(flipper, flip_selector)` inner call.
/// When the multisig threshold is met, pallet-multisig dispatches the call as the
/// multisig account, satisfying the admin check.
#[ink(storage)]
pub struct MultisigFlipper {
/// The boolean value that gets flipped.
value: bool,
/// The account authorized to call restricted messages.
/// Expected to be a deterministic multisig account address.
admin: AccountId,
}
impl MultisigFlipper {
/// Creates the contract with a given initial value and admin account.
///
/// The `admin` should be the precomputed deterministic multisig address
/// derived from the signatories and threshold.
#[ink(constructor)]
pub fn new(init_value: bool, admin: AccountId) -> Self {
Self {
value: init_value,
admin,
}
}
/// Creates the contract with `value = false` and the deployer as admin.
///
/// Use this when the multisig account itself deploys the contract
/// (by wrapping `Contracts.instantiate` in a multisig call).
#[ink(constructor)]
pub fn default() -> Self {
Self {
value: false,
admin: Self::env().caller(),
}
}
/// Flips the stored boolean value. Only callable by the admin.
///
/// When used with pallet-multisig, a member initiates via
/// `Multisig.as_multi(threshold, others, Contracts.call(this, flip()))`.
/// Other members approve. Once threshold is met, pallet-multisig
/// dispatches the call as the multisig account.
#[ink(message)]
pub fn flip(&mut self) -> Result<()> {
self.ensure_admin()?;
self.value = !self.value;
self.env().emit_event(Flipped {
new_value: self.value,
});
Ok(())
}
/// Returns the current boolean value.
#[ink(message)]
pub fn get(&self) -> bool {
self.value
}
/// Returns the current admin account.
#[ink(message)]
pub fn get_admin(&self) -> AccountId {
self.admin
}
/// Transfers admin rights to a new account. Only callable by the current admin.
///
/// Use this when the multisig composition changes (members added/removed,
/// threshold changed), since that produces a new multisig address.
#[ink(message)]
pub fn set_admin(&mut self, new_admin: AccountId) -> Result<()> {
self.ensure_admin()?;
let old_admin = self.admin;
self.admin = new_admin;
self.env().emit_event(AdminChanged {
old_admin,
new_admin,
});
Ok(())
}
/// Checks that the caller is the admin.
fn ensure_admin(&self) -> Result<()> {
if self.env().caller() != self.admin {
return Err(Error::NotAdmin);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn set_caller(caller: AccountId) {
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(caller);
}
fn default_accounts(
) -> ink::env::test::DefaultAccounts<ink::env::DefaultEnvironment> {
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>()
}
#[ink::test]
fn default_works() {
let accounts = default_accounts();
set_caller(accounts.alice);
let contract = MultisigFlipper::default();
assert_eq!(contract.get(), false);
assert_eq!(contract.get_admin(), accounts.alice);
}
#[ink::test]
fn new_with_admin() {
let accounts = default_accounts();
let contract = MultisigFlipper::new(true, accounts.bob);
assert_eq!(contract.get(), true);
assert_eq!(contract.get_admin(), accounts.bob);
}
#[ink::test]
fn admin_can_flip() {
let accounts = default_accounts();
set_caller(accounts.alice);
let mut contract = MultisigFlipper::default();
assert_eq!(contract.flip(), Ok(()));
assert_eq!(contract.get(), true);
}
#[ink::test]
fn non_admin_cannot_flip() {
let accounts = default_accounts();
set_caller(accounts.alice);
let mut contract = MultisigFlipper::default();
set_caller(accounts.bob);
assert_eq!(contract.flip(), Err(Error::NotAdmin));
assert_eq!(contract.get(), false);
}
#[ink::test]
fn admin_can_set_admin() {
let accounts = default_accounts();
set_caller(accounts.alice);
let mut contract = MultisigFlipper::default();
assert_eq!(contract.set_admin(accounts.bob), Ok(()));
assert_eq!(contract.get_admin(), accounts.bob);
}
#[ink::test]
fn non_admin_cannot_set_admin() {
let accounts = default_accounts();
set_caller(accounts.alice);
let mut contract = MultisigFlipper::default();
set_caller(accounts.bob);
assert_eq!(contract.set_admin(accounts.charlie), Err(Error::NotAdmin));
assert_eq!(contract.get_admin(), accounts.alice);
}
#[ink::test]
fn flip_toggles_correctly() {
let accounts = default_accounts();
set_caller(accounts.alice);
let mut contract = MultisigFlipper::default();
assert_eq!(contract.get(), false);
assert_eq!(contract.flip(), Ok(()));
assert_eq!(contract.get(), true);
assert_eq!(contract.flip(), Ok(()));
assert_eq!(contract.get(), false);
}
#[ink::test]
fn new_admin_can_flip() {
let accounts = default_accounts();
set_caller(accounts.alice);
let mut contract = MultisigFlipper::default();
// Transfer admin to bob
assert_eq!(contract.set_admin(accounts.bob), Ok(()));
// Old admin (alice) cannot flip
assert_eq!(contract.flip(), Err(Error::NotAdmin));
// New admin (bob) can flip
set_caller(accounts.bob);
assert_eq!(contract.flip(), Ok(()));
assert_eq!(contract.get(), true);
}
}
#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::ContractsBackend;
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[ink_e2e::test]
async fn default_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let mut constructor = MultisigFlipperRef::default();
let contract = client
.instantiate("multisig_flipper", &ink_e2e::alice(), &mut constructor)
.submit()
.await
.expect("instantiate failed");
let call_builder = contract.call_builder::<MultisigFlipper>();
let get = call_builder.get();
let get_result = client.call(&ink_e2e::alice(), &get).dry_run().await?;
assert!(matches!(get_result.return_value(), false));
Ok(())
}
#[ink_e2e::test]
async fn admin_can_flip(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// Deploy with bob — bob becomes admin via default()
let mut constructor = MultisigFlipperRef::default();
let contract = client
.instantiate("multisig_flipper", &ink_e2e::bob(), &mut constructor)
.submit()
.await
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<MultisigFlipper>();
// Bob (admin) flips
let flip = call_builder.flip();
let _flip_result = client
.call(&ink_e2e::bob(), &flip)
.submit()
.await
.expect("flip failed");
// Verify value flipped
let get = call_builder.get();
let get_result = client.call(&ink_e2e::bob(), &get).dry_run().await?;
assert!(matches!(get_result.return_value(), true));
Ok(())
}
#[ink_e2e::test]
async fn non_admin_cannot_flip(
mut client: ink_e2e::Client<C, E>,
) -> E2EResult<()> {
// Deploy with bob — bob becomes admin
let mut constructor = MultisigFlipperRef::default();
let contract = client
.instantiate("multisig_flipper", &ink_e2e::bob(), &mut constructor)
.submit()
.await
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<MultisigFlipper>();
// Alice (non-admin) tries to flip — should fail
let flip = call_builder.flip();
let flip_result = client
.call(&ink_e2e::alice(), &flip)
.dry_run()
.await;
assert!(flip_result.is_err() || {
let res = flip_result.unwrap();
res.return_value().is_err()
});
// Verify value is still false
let get = call_builder.get();
let get_result = client.call(&ink_e2e::bob(), &get).dry_run().await?;
assert!(matches!(get_result.return_value(), false));
Ok(())
}
}
}