odra-modules 2.10.0

Collection of reusable Odra modules.
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! A dapp registry owned by a single account.
use super::{DappMetadata, DappRegistryBase};
use crate::access::errors::Error as AccessError;
use crate::access::Ownable;
use odra::prelude::*;

/// A ready-to-deploy dapp registry built from [`Ownable`] and [`DappRegistryBase`].
///
/// The deployer becomes the owner. The owner sets the metadata and adds or removes contracts. A contract
/// the owner registered as a registrar may add contracts too, but not further registrars.
#[odra::module]
pub struct OwnedDappRegistry {
    ownable: SubModule<Ownable>,
    dapp: SubModule<DappRegistryBase>
}

#[odra::module]
impl OwnedDappRegistry {
    /// Initializes the registry, making the caller its owner.
    pub fn init(&mut self) {
        let owner = self.env().caller();
        self.ownable.init(owner);
        self.dapp.init();
    }

    /// Sets the dapp metadata. Only the owner may call it.
    pub fn set_dapp_metadata(&mut self, metadata: DappMetadata) {
        self.assert_owner();
        self.dapp.set_dapp_metadata(metadata);
    }

    /// Adds a contract to the dapp.
    ///
    /// The owner may add any contract. A registrar may add contracts that are not registrars.
    /// Anyone else is rejected with `CallerNotTheOwner`, and an unregistered contract with
    /// `CallerNotDappRegistrar`.
    pub fn add_dapp_contract(&mut self, dapp_contract: &Address, is_registrar: bool) {
        let caller = self.env().caller();
        if self.ownable.get_optional_owner() != Some(caller) {
            if is_registrar || !caller.is_contract() {
                self.env().revert(AccessError::CallerNotTheOwner);
            }
            self.dapp.assert_dapp_registrar(&caller);
        }
        self.dapp.add_dapp_contract(dapp_contract, is_registrar);
    }

    /// Removes a contract from the dapp. Only the owner may call it.
    pub fn remove_dapp_contract(&mut self, dapp_contract: &Address) {
        self.assert_owner();
        self.dapp.remove_dapp_contract(dapp_contract);
    }

    delegate! {
        to self.dapp {
            /// Returns the dapp metadata.
            fn get_dapp_metadata(&self) -> DappMetadata;
            /// Returns the contracts of the dapp, the registry first.
            fn get_dapp_contracts(&self) -> Vec<Address>;
            /// Returns true if the contract belongs to the dapp.
            fn is_dapp_contract(&self, dapp_contract: &Address) -> bool;
            /// Returns true if the contract is a registrar: it may add other contracts.
            fn is_dapp_registrar(&self, dapp_contract: &Address) -> bool;
            /// Returns the registry's own address.
            fn get_dapp_registry(&self) -> Address;
        }
        to self.ownable {
            /// Returns the owner.
            fn get_owner(&self) -> Address;
            /// Transfers ownership to `new_owner`. Only the owner may call it.
            fn transfer_ownership(&mut self, new_owner: &Address);
            /// Leaves the registry without an owner. Only the owner may call it.
            fn renounce_ownership(&mut self);
        }
    }
}

impl OwnedDappRegistry {
    fn assert_owner(&self) {
        self.ownable.assert_owner(&self.env().caller());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dapp::errors::Error;
    use crate::dapp::events::{
        DappContractAdded, DappContractRemoved, DappMetadataChanged, DappRegistryChanged
    };
    use crate::dapp::utils::{MockDappContract, MockDappContractHostRef, MockDappContractInitArgs};
    use odra::host::{Deployer, HostEnv, NoArgs};

    fn setup() -> (HostEnv, OwnedDappRegistryHostRef) {
        let env = odra_test::env();
        let registry = OwnedDappRegistry::deploy(&env, NoArgs);
        (env, registry)
    }

    /// A member contract pointing to `registry`.
    fn member(env: &HostEnv, registry: Address) -> MockDappContractHostRef {
        MockDappContract::deploy(env, MockDappContractInitArgs { registry })
    }

    fn metadata() -> DappMetadata {
        DappMetadata {
            name: "Dapp".to_string(),
            description: "A dapp".to_string(),
            website_url: "https://dapp.example".to_string(),
            icon_url: "https://dapp.example/icon.png".to_string()
        }
    }

    #[test]
    fn init() {
        let (env, registry) = setup();
        let address = registry.address();
        assert_eq!(registry.get_owner(), env.get_account(0));
        assert_eq!(registry.get_dapp_contracts(), vec![address]);
        assert!(registry.is_dapp_contract(&address));
        assert_eq!(registry.get_dapp_registry(), address);
        assert_eq!(registry.get_dapp_metadata(), DappMetadata::default());
    }

    #[test]
    fn set_metadata() {
        let (env, mut registry) = setup();
        registry.set_dapp_metadata(metadata());
        assert_eq!(registry.get_dapp_metadata(), metadata());
        assert!(env.emitted_event(
            &registry,
            DappMetadataChanged {
                name: "Dapp".to_string(),
                description: "A dapp".to_string(),
                website_url: "https://dapp.example".to_string(),
                icon_url: "https://dapp.example/icon.png".to_string()
            }
        ));

        env.set_caller(env.get_account(1));
        assert_eq!(
            registry.try_set_dapp_metadata(DappMetadata::default()),
            Err(AccessError::CallerNotTheOwner.into())
        );
    }

    #[test]
    fn owner_adds_contract() {
        let (env, mut registry) = setup();
        let member = member(&env, registry.address());
        registry.add_dapp_contract(&member.address(), false);

        assert!(registry.is_dapp_contract(&member.address()));
        assert!(!registry.is_dapp_registrar(&member.address()));
        assert_eq!(
            registry.get_dapp_contracts(),
            vec![registry.address(), member.address()]
        );
        assert!(env.emitted_event(
            &registry,
            DappContractAdded {
                contract: member.address(),
                is_registrar: false,
                registrar: env.get_account(0)
            }
        ));
    }

    #[test]
    fn cannot_add_account() {
        let (env, mut registry) = setup();
        assert_eq!(
            registry.try_add_dapp_contract(&env.get_account(1), false),
            Err(Error::NotAContract.into())
        );
    }

    #[test]
    fn cannot_add_contract_of_another_registry() {
        let (env, mut registry) = setup();
        let other = OwnedDappRegistry::deploy(&env, NoArgs);
        let member = member(&env, other.address());
        assert_eq!(
            registry.try_add_dapp_contract(&member.address(), false),
            Err(Error::DappRegistryMismatch.into())
        );
        // A registry points to itself.
        assert_eq!(
            registry.try_add_dapp_contract(&other.address(), false),
            Err(Error::DappRegistryMismatch.into())
        );
        assert_eq!(registry.get_dapp_contracts(), vec![registry.address()]);
        assert!(!registry.is_dapp_contract(&member.address()));
    }

    #[test]
    fn cannot_add_twice() {
        let (env, mut registry) = setup();
        let member = member(&env, registry.address());
        registry.add_dapp_contract(&member.address(), false);
        assert_eq!(
            registry.try_add_dapp_contract(&member.address(), true),
            Err(Error::DappContractAlreadyRegistered.into())
        );
        let address = registry.address();
        assert_eq!(
            registry.try_add_dapp_contract(&address, false),
            Err(Error::DappContractAlreadyRegistered.into())
        );
    }

    #[test]
    fn non_owner_cannot_add() {
        let (env, mut registry) = setup();
        let member = member(&env, registry.address());
        env.set_caller(env.get_account(1));
        assert_eq!(
            registry.try_add_dapp_contract(&member.address(), false),
            Err(AccessError::CallerNotTheOwner.into())
        );
    }

    #[test]
    fn registrar_adds_contracts() {
        let (env, mut registry) = setup();
        let mut registrar = member(&env, registry.address());
        let child = member(&env, registry.address());
        registry.add_dapp_contract(&registrar.address(), true);
        assert!(registry.is_dapp_registrar(&registrar.address()));

        registrar.register_in_dapp(&registry.address(), &child.address(), false);
        assert!(registry.is_dapp_contract(&child.address()));
        assert!(env.emitted_event(
            &registry,
            DappContractAdded {
                contract: child.address(),
                is_registrar: false,
                registrar: registrar.address()
            }
        ));

        // A registrar cannot add another registrar.
        let other = member(&env, registry.address());
        assert_eq!(
            registrar.try_register_in_dapp(&registry.address(), &other.address(), true),
            Err(AccessError::CallerNotTheOwner.into())
        );

        // A contract that is not a registrar cannot add anything.
        let mut not_registrar = member(&env, registry.address());
        assert_eq!(
            not_registrar.try_register_in_dapp(&registry.address(), &other.address(), false),
            Err(Error::CallerNotDappRegistrar.into())
        );
        registry.add_dapp_contract(&not_registrar.address(), false);
        assert_eq!(
            not_registrar.try_register_in_dapp(&registry.address(), &other.address(), false),
            Err(Error::CallerNotDappRegistrar.into())
        );
    }

    #[test]
    fn registrar_registers_in_its_registry() {
        let (env, mut registry) = setup();
        let mut registrar = member(&env, registry.address());
        registry.add_dapp_contract(&registrar.address(), true);
        assert_eq!(registrar.register_address(), registry.address());

        // A contract pointing to the registrar's registry joins it.
        let child = member(&env, registrar.register_address());
        registrar.register(&child.address());
        assert!(registry.is_dapp_contract(&child.address()));

        // One pointing elsewhere does not.
        let other = OwnedDappRegistry::deploy(&env, NoArgs);
        let stranger = member(&env, other.address());
        assert_eq!(
            registrar.try_register(&stranger.address()),
            Err(Error::DappRegistryMismatch.into())
        );
    }

    #[test]
    fn remove_contracts() {
        let (env, mut registry) = setup();
        let reg = registry.address();
        let a = member(&env, registry.address()).address();
        let b = member(&env, registry.address()).address();
        let c = member(&env, registry.address()).address();
        registry.add_dapp_contract(&a, true);
        registry.add_dapp_contract(&b, false);
        registry.add_dapp_contract(&c, false);

        // The other contracts keep their order.
        registry.remove_dapp_contract(&a);
        assert_eq!(registry.get_dapp_contracts(), vec![reg, b, c]);
        assert!(!registry.is_dapp_contract(&a));
        assert!(!registry.is_dapp_registrar(&a));
        assert!(env.emitted_event(
            &registry,
            DappContractRemoved {
                contract: a,
                registrar: env.get_account(0)
            }
        ));

        registry.remove_dapp_contract(&b);
        assert_eq!(registry.get_dapp_contracts(), vec![reg, c]);

        // A removed contract can be added again.
        registry.add_dapp_contract(&a, false);
        assert_eq!(registry.get_dapp_contracts(), vec![reg, c, a]);
        assert!(!registry.is_dapp_registrar(&a));
        registry.remove_dapp_contract(&c);
        assert_eq!(registry.get_dapp_contracts(), vec![reg, a]);
        assert!(registry.is_dapp_contract(&a));

        assert_eq!(
            registry.try_remove_dapp_contract(&b),
            Err(Error::DappContractNotRegistered.into())
        );
        assert_eq!(
            registry.try_remove_dapp_contract(&reg),
            Err(Error::CannotRemoveDappRegistry.into())
        );
        env.set_caller(env.get_account(1));
        assert_eq!(
            registry.try_remove_dapp_contract(&a),
            Err(AccessError::CallerNotTheOwner.into())
        );
    }

    #[test]
    fn transfer_ownership() {
        let (env, mut registry) = setup();
        let (owner, new_owner) = (env.get_account(0), env.get_account(1));
        registry.transfer_ownership(&new_owner);
        assert_eq!(registry.get_owner(), new_owner);

        env.set_caller(new_owner);
        registry.set_dapp_metadata(metadata());
        env.set_caller(owner);
        assert_eq!(
            registry.try_set_dapp_metadata(DappMetadata::default()),
            Err(AccessError::CallerNotTheOwner.into())
        );
    }

    /// Reads the item `key` of the `__dapp` dictionary of the contract at `address`.
    fn dapp_item<T: odra::casper_types::bytesrepr::FromBytes>(
        env: &HostEnv,
        address: &Address,
        key: &str
    ) -> T {
        use crate::dapp::storage::DAPP_KEY;
        let bytes = env
            .get_dictionary_value(address, DAPP_KEY, key.as_bytes())
            .unwrap_or_else(|| panic!("{DAPP_KEY}->{key} should be stored"));
        T::from_bytes(&bytes).unwrap().0
    }

    #[test]
    fn data_is_stored_in_the_dapp_dictionary() {
        use crate::dapp::storage::{CONTRACTS_KEY, DAPP_KEY, NAME_KEY, REGISTRARS_KEY};
        use odra::schema::{resolve_storage, SchemaStorageLayout, StorageLocation};

        let (env, mut registry) = setup();
        let address = registry.address();
        registry.set_dapp_metadata(metadata());
        let registrar = member(&env, registry.address()).address();
        let other = member(&env, registry.address()).address();
        registry.add_dapp_contract(&registrar, true);
        registry.add_dapp_contract(&other, false);

        // Every item holds a whole value; the collections are lists.
        assert_eq!(dapp_item::<String>(&env, &address, NAME_KEY), "Dapp");
        assert_eq!(
            dapp_item::<Vec<Address>>(&env, &address, CONTRACTS_KEY),
            vec![registrar, other]
        );
        assert_eq!(
            dapp_item::<Vec<Address>>(&env, &address, REGISTRARS_KEY),
            vec![registrar]
        );

        // The storage layout describes the `__dapp` dictionary; the item name is the key.
        let layout = OwnedDappRegistry::storage_kind();
        for (path, key) in [
            ("dapp.name", NAME_KEY),
            ("dapp.contracts", CONTRACTS_KEY),
            ("dapp.registrars", REGISTRARS_KEY)
        ] {
            let query = resolve_storage(&layout, path, &[item_key(key)]).unwrap();
            assert_eq!(
                query.location,
                StorageLocation::Dictionary {
                    name: DAPP_KEY.to_string(),
                    key: key.to_string()
                },
                "{path}"
            );
        }
    }

    #[test]
    fn member_registry() {
        let (env, registry) = setup();
        let mut member = member(&env, registry.address());
        assert_eq!(member.get_dapp_registry(), registry.address());
        assert!(env.emitted_event(
            &member,
            DappRegistryChanged {
                previous_registry: None,
                new_registry: registry.address()
            }
        ));

        // A contract belongs to one dapp at a time.
        let other = OwnedDappRegistry::deploy(&env, NoArgs);
        member.set_dapp_registry(&other.address());
        assert_eq!(member.get_dapp_registry(), other.address());
        assert!(env.emitted_event(
            &member,
            DappRegistryChanged {
                previous_registry: Some(registry.address()),
                new_registry: other.address()
            }
        ));

        assert_eq!(
            member.try_set_dapp_registry(&env.get_account(1)),
            Err(Error::NotAContract.into())
        );
        env.set_caller(env.get_account(1));
        assert_eq!(
            member.try_set_dapp_registry(&registry.address()),
            Err(AccessError::CallerNotTheOwner.into())
        );
    }

    #[test]
    fn member_registry_is_stored_in_the_dapp_dictionary() {
        use crate::dapp::storage::{DAPP_KEY, REGISTRY_KEY};
        use odra::schema::{resolve_storage, SchemaStorageLayout, StorageLocation};

        let (env, registry) = setup();
        let member = member(&env, registry.address());
        assert_eq!(
            dapp_item::<Address>(&env, &member.address(), REGISTRY_KEY),
            registry.address()
        );

        let layout = MockDappContract::storage_kind();
        let query = resolve_storage(&layout, "dapp.registry", &[item_key(REGISTRY_KEY)]).unwrap();
        assert_eq!(
            query.location,
            StorageLocation::Dictionary {
                name: DAPP_KEY.to_string(),
                key: REGISTRY_KEY.to_string()
            }
        );
    }

    /// A `__dapp` item name as a storage key: serialized like the contract serializes a `String`.
    fn item_key(key: &str) -> Vec<u8> {
        odra::casper_types::bytesrepr::ToBytes::to_bytes(&key.to_string()).unwrap()
    }
}