Skip to content

Commit

Permalink
Backporting #191, #176 from dev branch to substrate 0.9.30 (#193)
Browse files Browse the repository at this point in the history
Backporting the following PRs from the dev branch:
- [#191](#191)
- [#176](#176)

---------

Co-authored-by: Alisher A. Khassanov <a.khssnv@gmail.com>
  • Loading branch information
yahortsaryk and khssnv authored Dec 11, 2023
1 parent 974077a commit 2885ae9
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

39 changes: 39 additions & 0 deletions docs/genesis-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# State preset

Sometimes we use the blockchain as a part of a test environment.
Those tests typically want a certain state of the blockchain.
One option is to make a script which sends a number of transactions to put the blockchain into the desired state.
But those scripts may become large and execution may take significant time.
Another option is to make a chain specification, a JSON file with the desired genesis state of the blockchain.
And launch the chain with the desired state from the first block.

## Build a chain spec

Chain specification is a JSON file which contains network parameters with the the initial runtime and a genesis state of the database for each runtime module.
There are two forms of chain specification - a human-readable _plain_ form where each runtime module is represented by it's name.
And a _raw_ form with key-value pairs which will be written to the database.

1. Create a plain form chain spec.

```console
./target/release/cere build-spec --chain=dev --disable-default-bootnode > plain.json
```

1. Set genesis state for each module.
There is an example in `node/service/example.json`, you can copy everything except the `code` field value from it to the `plain.json`.

1. Create a raw form chain spec.

```console
./target/release/cere build-spec --chain=plain.json --disable-default-bootnode --raw > raw.json
```

## Launch the chain

```console
./target/release/cere --chain=raw.json --tmp --alice --unsafe-rpc-external --unsafe-ws-external --rpc-cors=all
```

## See also

[docs.substrate.io: Create a custom chain specification](https://docs.substrate.io/tutorials/build-a-blockchain/add-trusted-nodes/#create-a-custom-chain-specification)
14 changes: 4 additions & 10 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
{
"includes": [
"**/*.{toml}"
],
"excludes": [
"**/target"
],
"plugins": [
"https://plugins.dprint.dev/toml-0.5.3.wasm"
]
}
"includes": ["**/*.{toml}"],
"excludes": ["**/target"],
"plugins": ["https://plugins.dprint.dev/toml-0.5.3.wasm"]
}
232 changes: 232 additions & 0 deletions node/service/chain-specs/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
{
"name": "Development",
"id": "cere_dev",
"chainType": "Development",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": "cere",
"properties": {
"ss58Format": 54,
"tokenDecimals": 10,
"tokenSymbol": "CERE"
},
"forkBlocks": null,
"badBlocks": null,
"lightSyncState": null,
"codeSubstitutes": {},
"genesis": {
"runtime": {
"system": {
"code": "0x00"
},
"babe": {
"authorities": [],
"epochConfig": {
"c": [
1,
4
],
"allowed_slots": "PrimaryAndSecondaryPlainSlots"
}
},
"indices": {
"indices": []
},
"balances": {
"balances": [
[
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
10000000000000
],
[
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
10000000000000
],
[
"5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY",
10000000000000
],
[
"5HpG9w8EBLe5XCrbczpwq5TSXvedjrBGCwqxK1iQ7qUsSWFc",
10000000000000
],
[
"5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y",
10000000000000
]
]
},
"transactionPayment": {
"multiplier": "10000000000"
},
"staking": {
"validatorCount": 1,
"minimumValidatorCount": 1,
"invulnerables": [
"5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY"
],
"forceEra": "NotForcing",
"slashRewardFraction": 100000000,
"canceledPayout": 0,
"stakers": [
[
"5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY",
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
1000000000000,
"Validator"
]
],
"minNominatorBond": 0,
"minValidatorBond": 0,
"maxValidatorCount": null,
"maxNominatorCount": null
},
"session": {
"keys": [
[
"5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY",
"5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY",
{
"grandpa": "5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu",
"babe": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"im_online": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"authority_discovery": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
}
]
]
},
"democracy": {
"phantom": null
},
"council": {
"phantom": null,
"members": []
},
"technicalCommittee": {
"phantom": null,
"members": [
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
]
},
"elections": {
"members": [
[
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
10000000000
],
[
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
10000000000
]
]
},
"technicalMembership": {
"members": [],
"phantom": null
},
"grandpa": {
"authorities": []
},
"treasury": null,
"sudo": {
"key": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
},
"imOnline": {
"keys": []
},
"authorityDiscovery": {
"keys": []
},
"society": {
"pot": 0,
"members": [
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
],
"maxMembers": 999
},
"vesting": {
"vesting": []
},
"nominationPools": {
"minJoinBond": 0,
"minCreateBond": 0,
"maxPools": 16,
"maxMembersPerPool": 32,
"maxMembers": 512
},
"ddcStaking": {
"cdns": [],
"storages": []
},
"ddcCustomers": {
"buckets": [
[
"0x0000000000000000000000000000000000000001",
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
10000000000
]
]
},
"ddcNodes": {
"storageNodes": [
{
"pub_key": "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y",
"provider_id": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"cluster_id": "0x0000000000000000000000000000000000000001",
"props": {
"host": [],
"http_port": 8080,
"grpc_port": 8081,
"p2p_port": 8082
}
}
],
"cdnNodes": []
},
"ddcClusters": {
"clusters": [
{
"cluster_id": "0x0000000000000000000000000000000000000001",
"manager_id": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"reserve_id": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
"props": {
"node_provider_auth_contract": null
}
}
],
"clustersGovParams": [
[
"0x0000000000000000000000000000000000000001",
{
"treasury_share": 0,
"validators_share": 0,
"cluster_reserve_share": 0,
"cdn_bond_size": 0,
"cdn_chill_delay": 0,
"cdn_unbonding_delay": 0,
"storage_bond_size": 0,
"storage_chill_delay": 0,
"storage_unbonding_delay": 0,
"unit_per_mb_stored": 0,
"unit_per_mb_streamed": 0,
"unit_per_put_request": 0,
"unit_per_get_request": 0
}
]
],
"clustersNodes": [
[
"0x0000000000000000000000000000000000000001",
[
{
"StoragePubKey": "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y"
}
]
]
]
}
}
}
}
1 change: 1 addition & 0 deletions node/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ pub fn cere_dev_genesis(
ddc_customers: Default::default(),
nomination_pools: Default::default(),
ddc_clusters: Default::default(),
ddc_nodes: Default::default(),
}
}

Expand Down
44 changes: 39 additions & 5 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,52 @@ pub mod pallet {
}

#[pallet::genesis_config]
pub struct GenesisConfig;
pub struct GenesisConfig<T: Config> {
pub buckets: Vec<(ClusterId, T::AccountId, BalanceOf<T>)>,
}

#[cfg(feature = "std")]
impl Default for GenesisConfig {
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self
GenesisConfig { buckets: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {}
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
let account_id = <Pallet<T>>::account_id();
let min = <T as pallet::Config>::Currency::minimum_balance();
if <T as pallet::Config>::Currency::free_balance(&account_id) < min {
let _ = <T as pallet::Config>::Currency::make_free_balance_be(&account_id, min);
}

for &(ref cluster_id, ref owner_id, ref deposit) in &self.buckets {
let cur_bucket_id = <BucketsCount<T>>::get()
.checked_add(1)
.ok_or(Error::<T>::ArithmeticOverflow)
.unwrap();
<BucketsCount<T>>::set(cur_bucket_id);

let bucket = Bucket {
bucket_id: cur_bucket_id,
owner_id: owner_id.clone(),
cluster_id: *cluster_id,
};
<Buckets<T>>::insert(cur_bucket_id, bucket);

let ledger = AccountsLedger::<T> {
owner: owner_id.clone(),
total: *deposit,
active: *deposit,
unlocking: Default::default(),
};
<Ledger<T>>::insert(&ledger.owner, &ledger);

<T as pallet::Config>::Currency::deposit_into_existing(&account_id, *deposit)
.unwrap();
}
}
}

#[pallet::call]
Expand Down
2 changes: 1 addition & 1 deletion pallets/ddc-customers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ construct_runtime!(
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
DdcCustomers: pallet_ddc_customers::{Pallet, Call, Storage, Config, Event<T>},
DdcCustomers: pallet_ddc_customers::{Pallet, Call, Storage, Config<T>, Event<T>},
}
);

Expand Down
2 changes: 2 additions & 0 deletions pallets/ddc-nodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ frame-benchmarking = { default-features = false, git = "https://github.com/parit
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", default-features = false, features = ["derive"], optional = true }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
Expand All @@ -34,6 +35,7 @@ std = [
"sp-runtime/std",
"sp-std/std",
"sp-core/std",
"serde",
]
runtime-benchmarks = [
"ddc-primitives/runtime-benchmarks",
Expand Down
Loading

0 comments on commit 2885ae9

Please sign in to comment.