Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inject balances from json into chainspec #3114

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

1 change: 1 addition & 0 deletions crates/subspace-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sc-telemetry = { git = "https://github.com/subspace/polkadot-sdk", rev = "587181
sc-transaction-pool-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "5871818e1d736f1843eb9078f886290695165c42" }
sc-network-sync = { git = "https://github.com/subspace/polkadot-sdk", rev = "5871818e1d736f1843eb9078f886290695165c42" }
sc-utils = { git = "https://github.com/subspace/polkadot-sdk", rev = "5871818e1d736f1843eb9078f886290695165c42" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.128"
sp-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "5871818e1d736f1843eb9078f886290695165c42" }
sp-blockchain = { git = "https://github.com/subspace/polkadot-sdk", rev = "5871818e1d736f1843eb9078f886290695165c42" }
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::chain_spec_utils::{chain_spec_properties, get_account_id_from_seed};
use crate::domain::auto_id_chain_spec;
use crate::domain::cli::{GenesisDomain, SpecId};
use crate::domain::evm_chain_spec::{self};
use crate::genesis_allocations::{GENESIS_ALLOCATIONS, get_genesis_allocations};
use sc_chain_spec::GenericChainSpec;
use sc_service::ChainType;
use sc_subspace_chain_specs::DEVNET_CHAIN_SPEC;
Expand Down Expand Up @@ -86,7 +87,7 @@ pub fn gemini_3h_compiled() -> Result<GenericChainSpec, String> {
AccountId::from_ss58check("5DNwQTHfARgKoa2NdiUM51ZUow7ve5xG9S2yYdSbVQcnYxBA")
.expect("Wrong root account address");

let balances = vec![(sudo_account.clone(), 1_000 * SSC)];
let balances=get_genesis_allocations(GENESIS_ALLOCATIONS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should make rustfmt happier

Suggested change
let balances=get_genesis_allocations(GENESIS_ALLOCATIONS);
let balances = get_genesis_allocations(GENESIS_ALLOCATIONS);

serde_json::to_value(subspace_genesis_config(
sudo_account.clone(),
balances,
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-node/src/genesis_allocations.json

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions crates/subspace-node/src/genesis_allocations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::Deserialize;
use core::num::NonZeroU128;
use subspace_runtime_primitives::{AccountId, Balance};

/// Genesis allocations JSON content
pub const GENESIS_ALLOCATIONS: &str = include_str!("genesis_allocations.json");

#[derive(Deserialize)]
struct GenesisAllocation(
AccountId,
#[serde(with = "balance_string")] NonZeroU128
);

pub fn get_genesis_allocations(contents: &str) -> Vec<(AccountId, Balance)> {
let allocations: Vec<GenesisAllocation> = serde_json::from_str(contents)
.expect("Failed to parse genesis allocations JSON");

allocations.into_iter()
.map(|GenesisAllocation(account, balance)| (account, balance.get() as Balance))
.collect()
}

mod balance_string {
use super::*;
use serde::{de, Deserializer};

pub fn deserialize<'de, D>(deserializer: D) -> Result<NonZeroU128, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse::<u128>()
.map_err(de::Error::custom)
.and_then(|n| NonZeroU128::new(n).ok_or_else(|| de::Error::custom("Balance must be non-zero")))
}
}
1 change: 1 addition & 0 deletions crates/subspace-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod chain_spec;
mod chain_spec_utils;
mod cli;
mod domain;
mod genesis_allocations;

use crate::cli::{Cli, SubspaceCliPlaceholder};
use crate::domain::cli::DomainKey;
Expand Down
Loading