Skip to content

Commit

Permalink
domain instantiation accept endowed accounts for the domain at genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Feb 12, 2024
1 parent 4f84bed commit 6f917e9
Show file tree
Hide file tree
Showing 23 changed files with 344 additions and 174 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/pallet-domains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "h
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
log = { version = "0.4.20", default-features = false }
pallet-balances = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
sp-consensus-slots = { version = "0.10.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
Expand All @@ -32,7 +33,7 @@ subspace-runtime-primitives = { version = "0.1.0", default-features = false, pat

[dev-dependencies]
domain-pallet-executive = { version = "0.1.0", default-features = false, path = "../../domains/pallets/executive" }
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
hex-literal = "0.4.1"
pallet-timestamp = { version = "4.0.0-dev", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
pallet-block-fees = { version = "0.1.0", default-features = false, path = "../../domains/pallets/block-fees" }
sp-externalities = { version = "0.19.0", git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" }
Expand All @@ -48,6 +49,7 @@ std = [
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"scale-info/std",
"sp-consensus-slots/std",
"sp-core/std",
Expand Down
2 changes: 2 additions & 0 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mod benchmarks {
bundle_slot_probability: (1, 1),
target_bundles_per_block: 10,
operator_allow_list: OperatorAllowList::Anyone,
initial_balances: Default::default(),
};

#[extrinsic_call]
Expand Down Expand Up @@ -434,6 +435,7 @@ mod benchmarks {
bundle_slot_probability: (1, 1),
target_bundles_per_block: 10,
operator_allow_list: OperatorAllowList::Anyone,
initial_balances: Default::default(),
};

assert_ok!(Domains::<T>::instantiate_domain(
Expand Down
108 changes: 99 additions & 9 deletions crates/pallet-domains/src/domain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use crate::pallet::{DomainStakingSummary, NextEVMChainId};
use crate::runtime_registry::DomainRuntimeInfo;
use crate::staking::StakingSummary;
use crate::{
Config, DomainHashingFor, DomainRegistry, ExecutionReceiptOf, HoldIdentifier, NextDomainId,
RuntimeRegistry,
BalanceOf, Config, DomainHashingFor, DomainRegistry, ExecutionReceiptOf, HoldIdentifier,
NextDomainId, RuntimeRegistry,
};
use alloc::string::String;
use codec::{Decode, Encode};
use domain_runtime_primitives::MultiAccountId;
use frame_support::traits::fungible::{Inspect, MutateHold};
use frame_support::traits::tokens::{Fortitude, Preservation};
use frame_support::weights::Weight;
Expand All @@ -25,6 +26,7 @@ use sp_runtime::traits::{CheckedAdd, Zero};
use sp_runtime::DigestItem;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::vec::Vec;

/// Domain registry specific errors
#[derive(TypeInfo, Encode, Decode, PalletError, Debug, PartialEq)]
Expand All @@ -42,10 +44,11 @@ pub enum Error {
FailedToGenerateGenesisStateRoot,
DomainNotFound,
NotDomainOwner,
FailedToGenerateRawGenesis(crate::runtime_registry::Error),
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct DomainConfig<AccountId: Ord> {
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.
Expand All @@ -61,25 +64,27 @@ pub struct DomainConfig<AccountId: Ord> {
pub target_bundles_per_block: u32,
/// Allowed operators to operate for this domain.
pub operator_allow_list: OperatorAllowList<AccountId>,
// Initial balances for Domain.
pub initial_balances: Vec<(MultiAccountId, Balance)>,
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct DomainObject<Number, ReceiptHash, AccountId: Ord> {
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>,
pub domain_config: DomainConfig<AccountId, Balance>,
/// Domain runtime specific information.
pub domain_runtime_info: DomainRuntimeInfo,
}

pub(crate) fn can_instantiate_domain<T: Config>(
owner_account_id: &T::AccountId,
domain_config: &DomainConfig<T::AccountId>,
domain_config: &DomainConfig<T::AccountId, BalanceOf<T>>,
) -> Result<(), Error> {
ensure!(
domain_config.domain_name.len() as u32 <= T::MaxDomainNameLength::get(),
Expand Down Expand Up @@ -120,7 +125,7 @@ pub(crate) fn can_instantiate_domain<T: Config>(
}

pub(crate) fn do_instantiate_domain<T: Config>(
domain_config: DomainConfig<T::AccountId>,
domain_config: DomainConfig<T::AccountId, BalanceOf<T>>,
owner_account_id: T::AccountId,
created_at: BlockNumberFor<T>,
) -> Result<DomainId, Error> {
Expand All @@ -141,9 +146,16 @@ pub(crate) fn do_instantiate_domain<T: Config>(
}
};

// TODO: burn the total initial balance for domain from the domain owner account
let genesis_receipt = {
let state_version = runtime_obj.version.state_version();
let raw_genesis = runtime_obj.into_complete_raw_genesis(domain_id, domain_runtime_info);
let raw_genesis = runtime_obj
.into_complete_raw_genesis::<T>(
domain_id,
domain_runtime_info,
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(),
Expand Down Expand Up @@ -222,12 +234,16 @@ mod tests {
use crate::pallet::{DomainRegistry, NextDomainId, RuntimeRegistry};
use crate::runtime_registry::RuntimeObject;
use crate::tests::{new_test_ext, Test};
use frame_support::assert_ok;
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_runtime::traits::Convert;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::vec;
use sp_version::RuntimeVersion;
use subspace_runtime_primitives::SSC;

type Balances = pallet_balances::Pallet<Test>;

Expand All @@ -244,6 +260,7 @@ mod tests {
bundle_slot_probability: (0, 0),
target_bundles_per_block: 0,
operator_allow_list: OperatorAllowList::Anyone,
initial_balances: Default::default(),
};

let mut ext = new_test_ext();
Expand Down Expand Up @@ -381,4 +398,77 @@ mod tests {
);
});
}

#[test]
fn test_domain_instantiation_evm_accounts() {
let creator = 1u128;
let created_at = 0u64;
// Construct an invalid domain config initially
let mut domain_config = DomainConfig {
domain_name: "evm-domain".to_owned(),
runtime_id: 0,
max_block_size: 10,
max_block_weight: Weight::from_parts(1, 0),
bundle_slot_probability: (1, 1),
target_bundles_per_block: 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.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(),
},
);

// Set enough fund to creator
Balances::make_free_balance_be(
&creator,
<Test as Config>::DomainInstantiationDeposit::get()
+ <Test as pallet_balances::Config>::ExistentialDeposit::get(),
);

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

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

// should be successful
let domain_id =
do_instantiate_domain::<Test>(domain_config.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, domain_config);
});
}
}
22 changes: 13 additions & 9 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod pallet {
AtLeast32BitUnsigned, BlockNumberProvider, CheckEqual, CheckedAdd, Header as HeaderT,
MaybeDisplay, One, SimpleBitOps, Zero,
};
use sp_runtime::{SaturatedConversion, Saturating};
use sp_runtime::Saturating;
use sp_std::boxed::Box;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::collections::btree_set::BTreeSet;
Expand Down Expand Up @@ -457,7 +457,7 @@ mod pallet {
_,
Identity,
DomainId,
DomainObject<BlockNumberFor<T>, ReceiptHashFor<T>, T::AccountId>,
DomainObject<BlockNumberFor<T>, ReceiptHashFor<T>, T::AccountId, BalanceOf<T>>,
OptionQuery,
>;

Expand Down Expand Up @@ -1139,7 +1139,7 @@ mod pallet {
#[pallet::weight(T::WeightInfo::instantiate_domain())]
pub fn instantiate_domain(
origin: OriginFor<T>,
domain_config: DomainConfig<T::AccountId>,
domain_config: DomainConfig<T::AccountId, BalanceOf<T>>,
) -> DispatchResult {
ensure_root(origin)?;

Expand Down Expand Up @@ -1280,7 +1280,7 @@ mod pallet {

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub genesis_domain: Option<GenesisDomain<T::AccountId>>,
pub genesis_domain: Option<GenesisDomain<T::AccountId, BalanceOf<T>>>,
}

impl<T: Config> Default for GenesisConfig<T> {
Expand Down Expand Up @@ -1314,6 +1314,7 @@ mod pallet {
bundle_slot_probability: genesis_domain.bundle_slot_probability,
target_bundles_per_block: genesis_domain.target_bundles_per_block,
operator_allow_list: genesis_domain.operator_allow_list,
initial_balances: genesis_domain.initial_balances,
};
let domain_owner = genesis_domain.owner_account_id;
let domain_id =
Expand All @@ -1323,9 +1324,7 @@ mod pallet {
// Register domain_owner as the genesis operator.
let operator_config = OperatorConfig {
signing_key: genesis_domain.signing_key.clone(),
minimum_nominator_stake: genesis_domain
.minimum_nominator_stake
.saturated_into(),
minimum_nominator_stake: genesis_domain.minimum_nominator_stake,
nomination_tax: genesis_domain.nomination_tax,
};
let operator_stake = T::MinOperatorStake::get();
Expand Down Expand Up @@ -1498,8 +1497,13 @@ impl<T: Config> Pallet<T> {
let domain_obj = DomainRegistry::<T>::get(domain_id)?;
let runtime_object = RuntimeRegistry::<T>::get(domain_obj.domain_config.runtime_id)?;
let runtime_type = runtime_object.runtime_type.clone();
let raw_genesis =
runtime_object.into_complete_raw_genesis(domain_id, domain_obj.domain_runtime_info);
let raw_genesis = runtime_object
.into_complete_raw_genesis::<T>(
domain_id,
domain_obj.domain_runtime_info,
domain_obj.domain_config.initial_balances,
)
.ok()?;
Some((
DomainInstanceData {
runtime_type,
Expand Down
Loading

0 comments on commit 6f917e9

Please sign in to comment.