Skip to content

Commit

Permalink
Add raw_genesis_config to the genesis domain
Browse files Browse the repository at this point in the history
Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed Jul 26, 2023
1 parent 146c92a commit 7503802
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 85 deletions.
35 changes: 22 additions & 13 deletions crates/pallet-domains/src/domain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ pub struct DomainObject<Number, AccountId> {
pub genesis_receipt_hash: ReceiptHash,
/// The domain config.
pub domain_config: DomainConfig,
/// The genesis config of the domain, encoded in json format.
//
/// NOTE: the WASM code in the `system-pallet` genesis config should be empty to avoid
/// redundancy with the `RuntimeRegistry`. Currently, this value only set to `Some` for
/// the genesis domain instance.
pub raw_genesis_config: Option<Vec<u8>>,
}

pub(crate) fn can_instantiate_domain<T: Config>(
Expand Down Expand Up @@ -130,6 +136,7 @@ pub(crate) fn do_instantiate_domain<T: Config>(
domain_config: DomainConfig,
owner_account_id: T::AccountId,
created_at: T::BlockNumber,
raw_genesis_config: Option<Vec<u8>>,
) -> Result<DomainId, Error> {
can_instantiate_domain::<T>(&owner_account_id, &domain_config)?;

Expand All @@ -147,6 +154,7 @@ pub(crate) fn do_instantiate_domain<T: Config>(
created_at,
genesis_receipt_hash,
domain_config,
raw_genesis_config,
};
DomainRegistry::<T>::insert(domain_id, domain_obj);

Expand Down Expand Up @@ -237,15 +245,15 @@ mod tests {

// Failed to instantiate domain due to `domain_name` too long
assert_eq!(
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at),
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at, None),
Err(Error::DomainNameTooLong)
);
// Recorrect `domain_name`
domain_config.domain_name = b"evm-domain".to_vec();

// Failed to instantiate domain due to using unregistered runtime id
assert_eq!(
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at),
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at, None),
Err(Error::RuntimeNotFound)
);
// Register runtime id
Expand All @@ -271,59 +279,59 @@ mod tests {

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

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

// Failed to instantiate domain due to invalid `target_bundles_per_block`
assert_eq!(
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at),
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at, None),
Err(Error::InvalidBundlesPerBlock)
);
domain_config.target_bundles_per_block = u32::MAX;
assert_eq!(
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at),
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at, None),
Err(Error::InvalidBundlesPerBlock)
);
// Recorrect `target_bundles_per_block`
domain_config.target_bundles_per_block = 1;

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

// Failed to instantiate domain due to creator don't have enough fund
assert_eq!(
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at),
do_instantiate_domain::<Test>(domain_config.clone(), creator, created_at, None),
Err(Error::InsufficientFund)
);
// Set enough fund to creator
Expand All @@ -335,7 +343,8 @@ mod tests {

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

assert_eq!(domain_obj.owner_account_id, creator);
Expand All @@ -347,7 +356,7 @@ mod tests {

// cannot use the locked funds to create a new domain instance
assert!(
do_instantiate_domain::<Test>(domain_config, creator, created_at)
do_instantiate_domain::<Test>(domain_config, creator, created_at, None)
== Err(Error::InsufficientFund)
)
});
Expand Down
3 changes: 2 additions & 1 deletion crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ mod pallet {
let who = ensure_signed(origin)?;
let created_at = frame_system::Pallet::<T>::current_block_number();

let domain_id = do_instantiate_domain::<T>(domain_config, who, created_at)
let domain_id = do_instantiate_domain::<T>(domain_config, who, created_at, None)
.map_err(Error::<T>::from)?;

Self::deposit_event(Event::DomainInstantiated { domain_id });
Expand Down Expand Up @@ -917,6 +917,7 @@ mod pallet {
domain_config,
domain_owner.clone(),
Zero::zero(),
Some(genesis_domain.raw_genesis_config),
)
.expect("Genesis domain instantiation must always succeed");

Expand Down
1 change: 1 addition & 0 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ fn test_bundle_fromat_verification() {
created_at: Default::default(),
genesis_receipt_hash: Default::default(),
domain_config,
raw_genesis_config: None,
};
DomainRegistry::<Test>::insert(domain_id, domain_obj);

Expand Down
1 change: 1 addition & 0 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ pub struct GenesisDomain<AccountId> {
pub max_block_weight: Weight,
pub bundle_slot_probability: (u64, u64),
pub target_bundles_per_block: u32,
pub raw_genesis_config: Vec<u8>,

// Genesis operator
pub signing_key: OperatorPublicKey,
Expand Down
15 changes: 15 additions & 0 deletions crates/subspace-node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use crate::chain_spec_utils::{
chain_spec_properties, get_account_id_from_seed, get_public_key_from_seed,
};
use crate::domain::evm_chain_spec::{self, SpecId};
use sc_service::{ChainType, NoExtension};
use sc_subspace_chain_specs::ConsensusChainSpec;
use sc_telemetry::TelemetryEndpoints;
Expand Down Expand Up @@ -137,6 +138,7 @@ pub fn gemini_3e_compiled() -> Result<ConsensusChainSpec<GenesisConfig>, String>
})
.collect::<Vec<_>>();
subspace_genesis_config(
SpecId::Gemini,
WASM_BINARY.expect("Wasm binary must be built for Gemini"),
sudo_account,
balances,
Expand Down Expand Up @@ -230,6 +232,7 @@ pub fn devnet_config_compiled() -> Result<ConsensusChainSpec<GenesisConfig>, Str
})
.collect::<Vec<_>>();
subspace_genesis_config(
SpecId::DevNet,
WASM_BINARY.expect("Wasm binary must be built for Gemini"),
sudo_account,
balances,
Expand Down Expand Up @@ -272,6 +275,7 @@ pub fn dev_config() -> Result<ConsensusChainSpec<GenesisConfig>, String> {
ChainType::Development,
|| {
subspace_genesis_config(
SpecId::Dev,
wasm_binary,
// Sudo account
get_account_id_from_seed("Alice"),
Expand Down Expand Up @@ -318,6 +322,7 @@ pub fn local_config() -> Result<ConsensusChainSpec<GenesisConfig>, String> {
ChainType::Local,
|| {
subspace_genesis_config(
SpecId::Local,
wasm_binary,
// Sudo account
get_account_id_from_seed("Alice"),
Expand Down Expand Up @@ -363,6 +368,7 @@ pub fn local_config() -> Result<ConsensusChainSpec<GenesisConfig>, String> {

/// Configure initial storage state for FRAME modules.
fn subspace_genesis_config(
spec_id: SpecId,
wasm_binary: &[u8],
sudo_account: AccountId,
balances: Vec<(AccountId, Balance)>,
Expand All @@ -379,6 +385,14 @@ fn subspace_genesis_config(
confirmation_depth_k,
} = genesis_params;

let raw_domain_genesis_config = {
let mut domain_genesis_config = evm_chain_spec::get_testnet_genesis_by_spec_id(spec_id);
// Clear the WASM code of the genesis config since it is duplicated with `GenesisDomain::code`
domain_genesis_config.system = Default::default();
serde_json::to_vec(&domain_genesis_config)
.expect("Genesis config serialization never fails; qed")
};

GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
Expand Down Expand Up @@ -417,6 +431,7 @@ fn subspace_genesis_config(
max_block_weight: MaxDomainBlockWeight::get(),
bundle_slot_probability: (1, 1),
target_bundles_per_block: 10,
raw_genesis_config: raw_domain_genesis_config,

// TODO: Configurable genesis operator signing key.
signing_key: get_public_key_from_seed::<OperatorPublicKey>("Alice"),
Expand Down
146 changes: 75 additions & 71 deletions crates/subspace-node/src/domain/evm_chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,82 +147,86 @@ pub fn devnet_config<F: Fn() -> GenesisConfig + 'static + Send + Sync>(
}

pub fn load_chain_spec(spec_id: &str) -> Result<Box<dyn sc_cli::ChainSpec>, String> {
let accounts = get_dev_accounts();
let chain_spec = match spec_id {
"dev" => {
let constructor = move || {
testnet_genesis(
accounts.clone(),
// Alith is Sudo
Some(accounts[0]),
vec![(
accounts[0],
AccountId32ToAccountId20Converter::convert(
get_from_seed::<sr25519::Public>("Alice").into(),
),
)],
1000,
)
};
development_config(constructor)
"dev" => development_config(move || get_testnet_genesis_by_spec_id(SpecId::Dev)),
"gemini-3e" => gemini_3e_config(move || get_testnet_genesis_by_spec_id(SpecId::Gemini)),
"devnet" => devnet_config(move || get_testnet_genesis_by_spec_id(SpecId::DevNet)),
"" | "local" => local_testnet_config(move || get_testnet_genesis_by_spec_id(SpecId::Local)),
path => ChainSpec::from_json_file(std::path::PathBuf::from(path))?,
};
Ok(Box::new(chain_spec))
}

pub enum SpecId {
Dev,
Gemini,
DevNet,
Local,
}

pub fn get_testnet_genesis_by_spec_id(spec_id: SpecId) -> GenesisConfig {
let accounts = get_dev_accounts();
match spec_id {
SpecId::Dev => {
testnet_genesis(
accounts.clone(),
// Alith is Sudo
Some(accounts[0]),
vec![(
accounts[0],
AccountId32ToAccountId20Converter::convert(
get_from_seed::<sr25519::Public>("Alice").into(),
),
)],
1000,
)
}
"gemini-3e" => {
let constructor = move || {
let sudo_account = AccountId::from_str("f31e60022e290708c17d6997c34de6a30d09438f")
.expect("Invalid Sudo account");
testnet_genesis(
vec![
// Genesis operator
AccountId::from_str("2ac6c70c106138c8cd80da6b6a0e886b7eeee249")
.expect("Wrong executor account address"),
// Sudo account
sudo_account,
],
Some(sudo_account),
Default::default(),
1002,
)
};
gemini_3e_config(constructor)
SpecId::Gemini => {
let sudo_account = AccountId::from_str("f31e60022e290708c17d6997c34de6a30d09438f")
.expect("Invalid Sudo account");
testnet_genesis(
vec![
// Genesis operator
AccountId::from_str("2ac6c70c106138c8cd80da6b6a0e886b7eeee249")
.expect("Wrong executor account address"),
// Sudo account
sudo_account,
],
Some(sudo_account),
Default::default(),
1002,
)
}
"devnet" => {
let constructor = move || {
let sudo_account = AccountId::from_str("b66a91845249464309fad766fd0ece8144547736")
.expect("Invalid Sudo account");
testnet_genesis(
vec![
// Genesis operator
AccountId::from_str("cfdf9f58d9e532c3807ce62a5489cb19cfa6942d")
.expect("Wrong executor account address"),
// Sudo account
sudo_account,
],
Some(sudo_account),
vec![(
sudo_account,
AccountId::from_str("5b267fd1ba3ace6e3c3234f9576c49c877b5beb9")
.expect("Wrong relayer account address"),
)],
1003,
)
};
devnet_config(constructor)
SpecId::DevNet => {
let sudo_account = AccountId::from_str("b66a91845249464309fad766fd0ece8144547736")
.expect("Invalid Sudo account");
testnet_genesis(
vec![
// Genesis operator
AccountId::from_str("cfdf9f58d9e532c3807ce62a5489cb19cfa6942d")
.expect("Wrong executor account address"),
// Sudo account
sudo_account,
],
Some(sudo_account),
vec![(
sudo_account,
AccountId::from_str("5b267fd1ba3ace6e3c3234f9576c49c877b5beb9")
.expect("Wrong relayer account address"),
)],
1003,
)
}
"" | "local" => {
let constructor = move || {
testnet_genesis(
accounts.clone(),
// Alith is sudo
Some(accounts[0]),
vec![(accounts[0], accounts[0]), (accounts[1], accounts[1])],
1001,
)
};
local_testnet_config(constructor)
SpecId::Local => {
testnet_genesis(
accounts.clone(),
// Alith is sudo
Some(accounts[0]),
vec![(accounts[0], accounts[0]), (accounts[1], accounts[1])],
1001,
)
}
path => ChainSpec::from_json_file(std::path::PathBuf::from(path))?,
};
Ok(Box::new(chain_spec))
}
}

// HACK: `ChainSpec::from_genesis` is only allow to create hardcoded spec and `GenesisConfig`
Expand Down
Loading

0 comments on commit 7503802

Please sign in to comment.