pallet_domains/
domain_registry.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Domain registry for domains

#[cfg(not(feature = "std"))]
extern crate alloc;

use crate::block_tree::import_genesis_receipt;
use crate::pallet::{DomainStakingSummary, NextEVMChainId};
use crate::runtime_registry::DomainRuntimeInfo;
use crate::staking::StakingSummary;
use crate::{
    into_complete_raw_genesis, BalanceOf, Config, DomainHashingFor, DomainRegistry,
    DomainSudoCalls, ExecutionReceiptOf, HoldIdentifier, NextDomainId, RuntimeRegistry,
};
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Decode, Encode};
use domain_runtime_primitives::MultiAccountId;
use frame_support::traits::fungible::{Inspect, Mutate, MutateHold};
use frame_support::traits::tokens::{Fortitude, Precision, Preservation};
use frame_support::weights::Weight;
use frame_support::{ensure, PalletError};
use frame_system::pallet_prelude::*;
use scale_info::TypeInfo;
use sp_core::Get;
use sp_domains::{
    calculate_max_bundle_weight_and_size, derive_domain_block_hash, DomainBundleLimit, DomainId,
    DomainSudoCall, DomainsDigestItem, DomainsTransfersTracker, OnDomainInstantiated,
    OperatorAllowList, RuntimeId, RuntimeType,
};
use sp_runtime::traits::{CheckedAdd, Zero};
use sp_runtime::DigestItem;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::collections::btree_set::BTreeSet;

/// Domain registry specific errors
#[derive(TypeInfo, Encode, Decode, PalletError, Debug, PartialEq)]
pub enum Error {
    ExceedMaxDomainBlockWeight,
    ExceedMaxDomainBlockSize,
    MaxDomainId,
    MaxEVMChainId,
    InvalidSlotProbability,
    RuntimeNotFound,
    InsufficientFund,
    DomainNameTooLong,
    BalanceFreeze,
    FailedToGenerateGenesisStateRoot,
    DomainNotFound,
    NotDomainOwner,
    InitialBalanceOverflow,
    TransfersTracker,
    MinInitialAccountBalance,
    MaxInitialDomainAccounts,
    DuplicateInitialAccounts,
    FailedToGenerateRawGenesis(crate::runtime_registry::Error),
    BundleLimitCalculationOverflow,
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct DomainConfig<AccountId: Ord, Balance> {
    /// A user defined name for this domain, should be a human-readable UTF-8 encoded string.
    pub domain_name: String,
    /// A pointer to the `RuntimeRegistry` entry for this domain.
    pub runtime_id: RuntimeId,
    /// The max bundle size for this domain, may not exceed the system-wide `MaxDomainBlockSize` limit.
    pub max_bundle_size: u32,
    /// The max bundle weight for this domain, may not exceed the system-wide `MaxDomainBlockWeight` limit.
    pub max_bundle_weight: Weight,
    /// The probability of successful bundle in a slot (active slots coefficient). This defines the
    /// expected bundle production rate, must be `> 0` and `≤ 1`.
    pub bundle_slot_probability: (u64, u64),
    /// Allowed operators to operate for this domain.
    pub operator_allow_list: OperatorAllowList<AccountId>,
    // Initial balances for Domain.
    pub initial_balances: Vec<(MultiAccountId, Balance)>,
}

/// Parameters of the `instantiate_domain` call, it is similar to `DomainConfig` except the `max_bundle_size/weight`
/// is replaced by a optional `maybe_bundle_limit`.
///
/// It is used to derive `DomainConfig`, and if `maybe_bundle_limit` is `None` a default `max_bundle_size/weight`
/// derived from the `bundle_slot_probability` and other system-wide parameters will be used.
#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct DomainConfigParams<AccountId: Ord, Balance> {
    pub domain_name: String,
    pub runtime_id: RuntimeId,
    pub maybe_bundle_limit: Option<DomainBundleLimit>,
    pub bundle_slot_probability: (u64, u64),
    pub operator_allow_list: OperatorAllowList<AccountId>,
    pub initial_balances: Vec<(MultiAccountId, Balance)>,
}

pub fn into_domain_config<T: Config>(
    domain_config_params: DomainConfigParams<T::AccountId, BalanceOf<T>>,
) -> Result<DomainConfig<T::AccountId, BalanceOf<T>>, Error> {
    let DomainConfigParams {
        domain_name,
        runtime_id,
        maybe_bundle_limit,
        bundle_slot_probability,
        operator_allow_list,
        initial_balances,
    } = domain_config_params;

    let DomainBundleLimit {
        max_bundle_size,
        max_bundle_weight,
    } = match maybe_bundle_limit {
        Some(b) => b,
        None => calculate_max_bundle_weight_and_size(
            T::MaxDomainBlockSize::get(),
            T::MaxDomainBlockWeight::get(),
            T::ConsensusSlotProbability::get(),
            bundle_slot_probability,
        )
        .ok_or(Error::BundleLimitCalculationOverflow)?,
    };

    Ok(DomainConfig {
        domain_name,
        runtime_id,
        max_bundle_size,
        max_bundle_weight,
        bundle_slot_probability,
        operator_allow_list,
        initial_balances,
    })
}

impl<AccountId, Balance> DomainConfig<AccountId, Balance>
where
    AccountId: Ord,
    Balance: Zero + CheckedAdd + PartialOrd,
{
    pub(crate) fn total_issuance(&self) -> Option<Balance> {
        self.initial_balances
            .iter()
            .try_fold(Balance::zero(), |total, (_, balance)| {
                total.checked_add(balance)
            })
    }

    pub(crate) fn check_initial_balances<T: Config>(&self) -> Result<(), Error>
    where
        Balance: From<BalanceOf<T>>,
    {
        let accounts: BTreeSet<MultiAccountId> = self
            .initial_balances
            .iter()
            .map(|(acc, _)| acc)
            .cloned()
            .collect();

        ensure!(
            accounts.len() == self.initial_balances.len(),
            Error::DuplicateInitialAccounts
        );

        ensure!(
            self.initial_balances.len() as u32 <= T::MaxInitialDomainAccounts::get(),
            Error::MaxInitialDomainAccounts
        );

        for (_, balance) in &self.initial_balances {
            ensure!(
                *balance >= T::MinInitialDomainAccountBalance::get().into(),
                Error::MinInitialAccountBalance
            );
        }

        Ok(())
    }
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct DomainObject<Number, ReceiptHash, AccountId: Ord, Balance> {
    /// The address of the domain creator, used to validate updating the domain config.
    pub owner_account_id: AccountId,
    /// The consensus chain block number when the domain first instantiated.
    pub created_at: Number,
    /// The hash of the genesis execution receipt for this domain.
    pub genesis_receipt_hash: ReceiptHash,
    /// The domain config.
    pub domain_config: DomainConfig<AccountId, Balance>,
    /// Domain runtime specific information.
    pub domain_runtime_info: DomainRuntimeInfo,
    /// The amount of balance hold on the domain owner account
    pub domain_instantiation_deposit: Balance,
}

pub(crate) fn can_instantiate_domain<T: Config>(
    owner_account_id: &T::AccountId,
    domain_config_params: DomainConfigParams<T::AccountId, BalanceOf<T>>,
) -> Result<DomainConfig<T::AccountId, BalanceOf<T>>, Error> {
    // `bundle_slot_probability` must be `> 0` and `≤ 1`
    let (numerator, denominator) = domain_config_params.bundle_slot_probability;
    ensure!(
        numerator != 0 && denominator != 0 && numerator <= denominator,
        Error::InvalidSlotProbability
    );

    let domain_config = into_domain_config::<T>(domain_config_params)?;

    ensure!(
        domain_config.domain_name.len() as u32 <= T::MaxDomainNameLength::get(),
        Error::DomainNameTooLong,
    );
    ensure!(
        RuntimeRegistry::<T>::contains_key(domain_config.runtime_id),
        Error::RuntimeNotFound
    );
    ensure!(
        domain_config.max_bundle_size <= T::MaxDomainBlockSize::get(),
        Error::ExceedMaxDomainBlockSize
    );
    ensure!(
        domain_config
            .max_bundle_weight
            .all_lte(T::MaxDomainBlockWeight::get()),
        Error::ExceedMaxDomainBlockWeight
    );

    ensure!(
        T::Currency::reducible_balance(owner_account_id, Preservation::Protect, Fortitude::Polite)
            >= T::DomainInstantiationDeposit::get(),
        Error::InsufficientFund
    );

    domain_config.check_initial_balances::<T>()?;

    Ok(domain_config)
}

pub(crate) fn do_instantiate_domain<T: Config>(
    domain_config_params: DomainConfigParams<T::AccountId, BalanceOf<T>>,
    owner_account_id: T::AccountId,
    created_at: BlockNumberFor<T>,
) -> Result<DomainId, Error> {
    let domain_config = can_instantiate_domain::<T>(&owner_account_id, domain_config_params)?;

    let domain_instantiation_deposit = T::DomainInstantiationDeposit::get();
    let domain_id = NextDomainId::<T>::get();
    let runtime_obj = RuntimeRegistry::<T>::mutate(domain_config.runtime_id, |maybe_runtime_obj| {
        let mut runtime_object = maybe_runtime_obj
            .take()
            .expect("Runtime object must exist as checked in `can_instantiate_domain`; qed");
        runtime_object.instance_count = runtime_object.instance_count.saturating_add(1);
        *maybe_runtime_obj = Some(runtime_object.clone());
        runtime_object
    });

    let domain_runtime_info = match runtime_obj.runtime_type {
        RuntimeType::Evm => {
            let evm_chain_id = NextEVMChainId::<T>::get();
            let next_evm_chain_id = evm_chain_id.checked_add(1).ok_or(Error::MaxEVMChainId)?;
            NextEVMChainId::<T>::set(next_evm_chain_id);
            DomainRuntimeInfo::EVM {
                chain_id: evm_chain_id,
            }
        }
        RuntimeType::AutoId => DomainRuntimeInfo::AutoId,
    };

    // burn total issuance on domain from owners account and track the domain balance
    let total_issuance = domain_config
        .total_issuance()
        .ok_or(Error::InitialBalanceOverflow)?;

    T::Currency::burn_from(
        &owner_account_id,
        total_issuance,
        Preservation::Expendable,
        Precision::Exact,
        Fortitude::Polite,
    )
    .map_err(|_| Error::InsufficientFund)?;

    T::DomainsTransfersTracker::initialize_domain_balance(domain_id, total_issuance)
        .map_err(|_| Error::TransfersTracker)?;

    let genesis_receipt = {
        let state_version = runtime_obj.version.state_version();
        let raw_genesis = into_complete_raw_genesis::<T>(
            runtime_obj,
            domain_id,
            domain_runtime_info,
            total_issuance,
            domain_config.initial_balances.clone(),
        )
        .map_err(Error::FailedToGenerateRawGenesis)?;
        let state_root = raw_genesis.state_root::<DomainHashingFor<T>>(state_version);
        let genesis_block_hash = derive_domain_block_hash::<T::DomainHeader>(
            Zero::zero(),
            sp_domains::EMPTY_EXTRINSIC_ROOT.into(),
            state_root,
            Default::default(),
            Default::default(),
        );

        ExecutionReceiptOf::<T>::genesis(
            state_root,
            sp_domains::EMPTY_EXTRINSIC_ROOT.into(),
            genesis_block_hash,
        )
    };
    let genesis_receipt_hash = genesis_receipt.hash::<DomainHashingFor<T>>();

    let domain_obj = DomainObject {
        owner_account_id: owner_account_id.clone(),
        created_at,
        genesis_receipt_hash,
        domain_config,
        domain_runtime_info,
        domain_instantiation_deposit,
    };
    DomainRegistry::<T>::insert(domain_id, domain_obj);

    let next_domain_id = domain_id.checked_add(&1.into()).ok_or(Error::MaxDomainId)?;
    NextDomainId::<T>::set(next_domain_id);

    // Lock up `domain_instantiation_deposit` amount of fund of the domain instance creator
    T::Currency::hold(
        &T::HoldIdentifier::domain_instantiation_id(),
        &owner_account_id,
        domain_instantiation_deposit,
    )
    .map_err(|_| Error::BalanceFreeze)?;

    DomainStakingSummary::<T>::insert(
        domain_id,
        StakingSummary {
            current_epoch_index: 0,
            current_total_stake: Zero::zero(),
            current_operators: BTreeMap::new(),
            next_operators: BTreeSet::new(),
            current_epoch_rewards: BTreeMap::new(),
        },
    );

    import_genesis_receipt::<T>(domain_id, genesis_receipt);
    T::OnDomainInstantiated::on_domain_instantiated(domain_id);

    DomainSudoCalls::<T>::insert(domain_id, DomainSudoCall { maybe_call: None });

    frame_system::Pallet::<T>::deposit_log(DigestItem::domain_instantiation(domain_id));

    Ok(domain_id)
}

pub(crate) fn do_update_domain_allow_list<T: Config>(
    domain_owner: T::AccountId,
    domain_id: DomainId,
    updated_operator_allow_list: OperatorAllowList<T::AccountId>,
) -> Result<(), Error> {
    DomainRegistry::<T>::try_mutate(domain_id, |maybe_domain_object| {
        let domain_obj = maybe_domain_object.as_mut().ok_or(Error::DomainNotFound)?;
        ensure!(
            domain_obj.owner_account_id == domain_owner,
            Error::NotDomainOwner
        );

        domain_obj.domain_config.operator_allow_list = updated_operator_allow_list;
        Ok(())
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::{new_test_ext, Test};
    use domain_runtime_primitives::{AccountId20, AccountId20Converter};
    use frame_support::traits::Currency;
    use frame_support::{assert_err, assert_ok};
    use hex_literal::hex;
    use sp_domains::storage::RawGenesis;
    use sp_domains::RuntimeObject;
    use sp_runtime::traits::Convert;
    use sp_std::vec;
    use sp_version::RuntimeVersion;
    use subspace_runtime_primitives::SSC;

    type Balances = pallet_balances::Pallet<Test>;

    #[test]
    fn test_domain_instantiation() {
        let creator = 1u128;
        let created_at = 0u64;
        // Construct an invalid domain config initially
        let mut domain_config_params = DomainConfigParams {
            domain_name: String::from_utf8(vec![0; 1024]).unwrap(),
            runtime_id: 0,
            maybe_bundle_limit: Some(DomainBundleLimit {
                max_bundle_size: u32::MAX,
                max_bundle_weight: Weight::MAX,
            }),
            bundle_slot_probability: (0, 0),
            operator_allow_list: OperatorAllowList::Anyone,
            initial_balances: Default::default(),
        };

        let mut ext = new_test_ext();
        ext.execute_with(|| {
            assert_eq!(NextDomainId::<Test>::get(), 0.into());

            // Failed to instantiate domain due to invalid `bundle_slot_probability`
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::InvalidSlotProbability)
            );
            domain_config_params.bundle_slot_probability = (1, 0);
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::InvalidSlotProbability)
            );
            domain_config_params.bundle_slot_probability = (0, 1);
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::InvalidSlotProbability)
            );
            domain_config_params.bundle_slot_probability = (2, 1);
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::InvalidSlotProbability)
            );
            // Recorrect `bundle_slot_probability`
            domain_config_params.bundle_slot_probability = (1, 1);

            // Failed to instantiate domain due to `domain_name` too long
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::DomainNameTooLong)
            );
            // Recorrect `domain_name`
            "evm-domain".clone_into(&mut domain_config_params.domain_name);

            // Failed to instantiate domain due to using unregistered runtime id
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::RuntimeNotFound)
            );
            // Register runtime id
            RuntimeRegistry::<Test>::insert(
                domain_config_params.runtime_id,
                RuntimeObject {
                    runtime_name: "evm".to_owned(),
                    runtime_type: Default::default(),
                    runtime_upgrades: 0,
                    hash: Default::default(),
                    raw_genesis: RawGenesis::dummy(vec![1, 2, 3, 4]),
                    version: RuntimeVersion {
                        spec_name: "test".into(),
                        spec_version: 1,
                        impl_version: 1,
                        transaction_version: 1,
                        ..Default::default()
                    },
                    created_at: Default::default(),
                    updated_at: Default::default(),
                    instance_count: 0,
                },
            );

            // Failed to instantiate domain due to exceed max domain block size limit
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::ExceedMaxDomainBlockSize)
            );
            // Recorrect `max_bundle_size`
            domain_config_params
                .maybe_bundle_limit
                .as_mut()
                .unwrap()
                .max_bundle_size = 1;

            // Failed to instantiate domain due to exceed max domain block weight limit
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::ExceedMaxDomainBlockWeight)
            );
            // Recorrect `max_bundle_weight`
            domain_config_params
                .maybe_bundle_limit
                .as_mut()
                .unwrap()
                .max_bundle_weight = Weight::from_parts(1, 0);

            // Failed to instantiate domain due to creator don't have enough fund
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Err(Error::InsufficientFund)
            );
            // Set enough fund to creator
            Balances::make_free_balance_be(
                &creator,
                <Test as Config>::DomainInstantiationDeposit::get()
                    + <Test as pallet_balances::Config>::ExistentialDeposit::get(),
            );
            // Set `maybe_bundle_limit` to use the default bundle limit
            domain_config_params.maybe_bundle_limit = None;

            // `instantiate_domain` must success now
            let domain_id =
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at)
                    .unwrap();
            let domain_obj = DomainRegistry::<Test>::get(domain_id).unwrap();

            assert_eq!(domain_obj.owner_account_id, creator);
            assert_eq!(domain_obj.created_at, created_at);
            assert_eq!(
                domain_obj.domain_config,
                into_domain_config::<Test>(domain_config_params.clone()).unwrap()
            );
            assert_eq!(NextDomainId::<Test>::get(), 1.into());
            // Fund locked up thus can't withdraw, and usable balance is zero since ED is 1
            assert_eq!(Balances::usable_balance(creator), Zero::zero());

            // instance count must be incremented
            let runtime_obj =
                RuntimeRegistry::<Test>::get(domain_config_params.runtime_id).unwrap();
            assert_eq!(runtime_obj.instance_count, 1);

            // cannot use the locked funds to create a new domain instance
            assert_eq!(
                do_instantiate_domain::<Test>(domain_config_params, creator, created_at),
                Err(Error::InsufficientFund)
            );

            // update operator allow list
            let updated_operator_allow_list =
                OperatorAllowList::Operators(BTreeSet::from_iter(vec![1, 2, 3]));
            assert_ok!(do_update_domain_allow_list::<Test>(
                creator,
                domain_id,
                updated_operator_allow_list.clone()
            ));
            let domain_obj = DomainRegistry::<Test>::get(domain_id).unwrap();
            assert_eq!(
                domain_obj.domain_config.operator_allow_list,
                updated_operator_allow_list
            );
        });
    }

    #[test]
    fn test_domain_instantiation_evm_accounts() {
        let creator = 1u128;
        let created_at = 0u64;
        // Construct an invalid domain config initially
        let mut domain_config_params = DomainConfigParams {
            domain_name: "evm-domain".to_owned(),
            runtime_id: 0,
            maybe_bundle_limit: None,
            bundle_slot_probability: (1, 1),
            operator_allow_list: OperatorAllowList::Anyone,
            initial_balances: vec![(MultiAccountId::Raw(vec![0, 1, 2, 3, 4, 5]), 1_000_000 * SSC)],
        };

        let mut ext = new_test_ext();
        ext.execute_with(|| {
            assert_eq!(NextDomainId::<Test>::get(), 0.into());
            // Register runtime id
            RuntimeRegistry::<Test>::insert(
                domain_config_params.runtime_id,
                RuntimeObject {
                    runtime_name: "evm".to_owned(),
                    runtime_type: Default::default(),
                    runtime_upgrades: 0,
                    hash: Default::default(),
                    raw_genesis: RawGenesis::dummy(vec![1, 2, 3, 4]),
                    version: RuntimeVersion {
                        spec_name: "test".into(),
                        spec_version: 1,
                        impl_version: 1,
                        transaction_version: 1,
                        ..Default::default()
                    },
                    created_at: Default::default(),
                    updated_at: Default::default(),
                    instance_count: 0,
                },
            );

            // Set enough fund to creator
            Balances::make_free_balance_be(
                &creator,
                <Test as Config>::DomainInstantiationDeposit::get()
                    // for domain total issuance
                    + 1_000_000 * SSC
                    + <Test as pallet_balances::Config>::ExistentialDeposit::get(),
            );

            // should fail due to invalid account ID type
            assert_err!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Error::FailedToGenerateRawGenesis(
                    crate::runtime_registry::Error::InvalidAccountIdType
                )
            );

            // duplicate accounts
            domain_config_params.initial_balances = vec![
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
                    ))),
                    1_000_000 * SSC,
                ),
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
                    ))),
                    1_000_000 * SSC,
                ),
            ];

            assert_err!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Error::DuplicateInitialAccounts
            );

            // max accounts
            domain_config_params.initial_balances = vec![
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
                    ))),
                    1_000_000 * SSC,
                ),
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cbc"
                    ))),
                    1_000_000 * SSC,
                ),
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566ccc"
                    ))),
                    1_000_000 * SSC,
                ),
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cdc"
                    ))),
                    1_000_000 * SSC,
                ),
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cec"
                    ))),
                    1_000_000 * SSC,
                ),
                (
                    AccountId20Converter::convert(AccountId20::from(hex!(
                        "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cfc"
                    ))),
                    1_000_000 * SSC,
                ),
            ];

            assert_err!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Error::MaxInitialDomainAccounts
            );

            // min balance accounts
            domain_config_params.initial_balances = vec![(
                AccountId20Converter::convert(AccountId20::from(hex!(
                    "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
                ))),
                1,
            )];

            assert_err!(
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at),
                Error::MinInitialAccountBalance
            );

            domain_config_params.initial_balances = vec![(
                AccountId20Converter::convert(AccountId20::from(hex!(
                    "f24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"
                ))),
                1_000_000 * SSC,
            )];

            // Set enough fund to creator
            Balances::make_free_balance_be(
                &creator,
                <Test as Config>::DomainInstantiationDeposit::get()
                    // for domain total issuance
                    + 1_000_000 * SSC
                    + <Test as pallet_balances::Config>::ExistentialDeposit::get(),
            );

            // should be successful
            let domain_id =
                do_instantiate_domain::<Test>(domain_config_params.clone(), creator, created_at)
                    .unwrap();
            let domain_obj = DomainRegistry::<Test>::get(domain_id).unwrap();

            assert_eq!(domain_obj.owner_account_id, creator);
            assert_eq!(domain_obj.created_at, created_at);
            assert_eq!(
                domain_obj.domain_config,
                into_domain_config::<Test>(domain_config_params).unwrap()
            );
        });
    }
}