Skip to content

Commit

Permalink
chore(benchmarks): Implement new Substrate-approach to build the gene…
Browse files Browse the repository at this point in the history
…sis (#4439)
  • Loading branch information
gshep authored Jan 17, 2025
1 parent 1656dd0 commit db08ac1
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 208 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ region = "3.0.2"
reqwest = { version = "0.12.8", default-features = false }
scale-info = { version = "2.5.0", default-features = false }
serde = { version = "^1", default-features = false }
serde_json = "^1"
serde_json = { version = "1.0.135", default-features = false, features = ["alloc"] }
serde_yaml = "0.8.26"
sha-1 = "0.10.1"
static_init = "1.0.3"
Expand Down Expand Up @@ -285,7 +285,6 @@ pallet-gear-bank = { path = "pallets/gear-bank", default-features = false }
pallet-gear-builtin = { path = "pallets/gear-builtin", default-features = false }
pallet-gear-builtin-rpc = { path = "pallets/gear-builtin/rpc" }
pallet-gear-builtin-rpc-runtime-api = { path = "pallets/gear-builtin/rpc/runtime-api", default-features = false }
runtime-common = { package = "gear-runtime-common", path = "runtime/common", default-features = false }
runtime-primitives = { package = "gear-runtime-primitives", path = "runtime/primitives", default-features = false }
service = { package = "gear-service", path = "node/service", default-features = false }
testing = { package = "gear-node-testing", path = "node/testing" }
Expand Down
2 changes: 1 addition & 1 deletion gsdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jsonrpsee = { workspace = true, features = [ "http-client", "ws-client" ] }
log.workspace = true
scale-value.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_json = { workspace = true, features = [ "std" ] }
subxt.workspace = true
thiserror.workspace = true
sp-runtime = { workspace = true, features = [ "std" ] }
Expand Down
2 changes: 1 addition & 1 deletion node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ hex-literal.workspace = true
jsonrpsee = { workspace = true, features = ["server"] }
log.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
serde_json = { workspace = true, features = [ "std" ] }

# Gear
common = { workspace = true, features = ["std"] }
Expand Down
17 changes: 0 additions & 17 deletions node/service/src/chain_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
pub use runtime_primitives::{AccountId, AccountPublic, Block};
use sc_chain_spec::ChainSpecExtension;
use sp_core::{Pair, Public};
use sp_runtime::traits::IdentifyAccount;

use serde::{Deserialize, Serialize};

Expand All @@ -45,18 +43,3 @@ pub struct Extensions {

/// General `ChainSpec` used as a basis for a specialized config.
pub type RawChainSpec = sc_service::GenericChainSpec<Extensions>;

/// Generate a crypto pair from seed.
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{seed}"), None)
.expect("static values are valid; qed")
.public()
}

/// Generate an account ID from seed.
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}
164 changes: 5 additions & 159 deletions node/service/src/chain_spec/vara.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::chain_spec::{get_account_id_from_seed, get_from_seed, AccountId, Extensions};
use crate::chain_spec::Extensions;
use gear_runtime_common::{
self,
constants::{BANK_ADDRESS, VARA_DECIMAL, VARA_SS58PREFIX, VARA_TESTNET_TOKEN_SYMBOL},
constants::{VARA_DECIMAL, VARA_SS58PREFIX, VARA_TESTNET_TOKEN_SYMBOL},
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_staking::Forcing;
use sc_chain_spec::Properties;
use sc_service::ChainType;
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_babe::AuthorityId as BabeId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
use sp_core::sr25519;
use sp_runtime::{Perbill, Perquintill};
use vara_runtime::{
constants::currency::{ECONOMIC_UNITS, EXISTENTIAL_DEPOSIT, UNITS as TOKEN},
SessionKeys, StakerStatus, WASM_BINARY,
};
use vara_runtime::{genesis_config_presets, WASM_BINARY};

/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<Extensions>;
Expand All @@ -49,62 +39,14 @@ pub fn vara_dev_properties() -> Properties {
p
}

/// Helper function that wraps a set of session keys.
fn session_keys(
babe: BabeId,
grandpa: GrandpaId,
im_online: ImOnlineId,
authority_discovery: AuthorityDiscoveryId,
) -> SessionKeys {
SessionKeys {
babe,
grandpa,
im_online,
authority_discovery,
}
}

/// Generate authority keys.
pub fn authority_keys_from_seed(
s: &str,
) -> (
AccountId,
AccountId,
BabeId,
GrandpaId,
ImOnlineId,
AuthorityDiscoveryId,
) {
(
get_account_id_from_seed::<sr25519::Public>(&format!("{s}//stash")),
get_account_id_from_seed::<sr25519::Public>(s),
get_from_seed::<BabeId>(s),
get_from_seed::<GrandpaId>(s),
get_from_seed::<ImOnlineId>(s),
get_from_seed::<AuthorityDiscoveryId>(s),
)
}

pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;

Ok(ChainSpec::builder(wasm_binary, Default::default())
.with_name("Development")
.with_id("vara_dev")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(testnet_genesis(
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
],
BANK_ADDRESS.into(),
true,
))
.with_genesis_config_patch(genesis_config_presets::development_genesis())
.with_properties(vara_dev_properties())
.build())
}
Expand All @@ -116,103 +58,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
.with_name("Vara Local Testnet")
.with_id("vara_local_testnet")
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(testnet_genesis(
// Initial PoA authorities
vec![
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
],
BANK_ADDRESS.into(),
true,
))
.with_genesis_config_patch(genesis_config_presets::local_testnet_genesis())
.with_properties(vara_dev_properties())
.build())
}

/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
initial_authorities: Vec<(
AccountId,
AccountId,
BabeId,
GrandpaId,
ImOnlineId,
AuthorityDiscoveryId,
)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
bank_account: AccountId,
_enable_println: bool,
) -> serde_json::Value {
const ENDOWMENT: u128 = 1_000_000 * TOKEN;
const STASH: u128 = 100 * TOKEN;
const MIN_NOMINATOR_BOND: u128 = 50 * TOKEN;

let _num_endowed_accounts = endowed_accounts.len();

let mut balances = endowed_accounts
.iter()
.map(|k: &AccountId| (k.clone(), ENDOWMENT))
.chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH)))
.collect::<Vec<_>>();

balances.push((bank_account, EXISTENTIAL_DEPOSIT));

serde_json::json!({
"balances": {
"balances": balances,
},

"session": {
"keys": initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
session_keys(x.2.clone(), x.3.clone(), x.4.clone(), x.5.clone()),
)
})
.collect::<Vec<_>>(),
},
"staking": {
"validatorCount": initial_authorities.len() as u32,
"minimumValidatorCount": 4,
"stakers": initial_authorities
.iter()
.map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::<AccountId>::Validator))
.collect::<Vec<_>>(),
"invulnerables": initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
"forceEra": Forcing::ForceNone,
"slashRewardFraction": Perbill::from_percent(10),
"minNominatorBond": MIN_NOMINATOR_BOND,
},
"nominationPools": {
"minCreateBond": 10 * ECONOMIC_UNITS,
"minJoinBond": ECONOMIC_UNITS,
},
"stakingRewards": {
"nonStakeable": Perquintill::from_rational(4108_u64, 10_000_u64), // 41.08%
"idealStake": Perquintill::from_percent(85), // 85%
"targetInflation": Perquintill::from_rational(578_u64, 10_000_u64), // 5.78%
},
"babe": {
"epochConfig": Some(vara_runtime::BABE_GENESIS_EPOCH_CONFIG),
},
"sudo": {
// Assign network admin rights.
"key": Some(root_key),
},
})
}
10 changes: 7 additions & 3 deletions runtime/vara/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const-str.workspace = true
log.workspace = true
parity-scale-codec.workspace = true
scale-info = { workspace = true, features = ["derive"] }
serde_json.workspace = true

# Frame deps
frame-support.workspace = true
Expand Down Expand Up @@ -67,6 +68,7 @@ sp-api.workspace = true
sp-authority-discovery.workspace = true
sp-block-builder.workspace = true
sp-consensus-babe.workspace = true
sp-consensus-grandpa.workspace = true
sp-core.workspace = true
sp-externalities = { workspace = true, optional = true }
sp-genesis-builder.workspace = true
Expand All @@ -93,7 +95,7 @@ hex-literal = { workspace = true, optional = true }

# Internal deps
common.workspace = true
runtime-common.workspace = true
gear-runtime-common.workspace = true
pallet-gear-scheduler.workspace = true
pallet-gear-messenger.workspace = true
pallet-gear-program.workspace = true
Expand Down Expand Up @@ -189,14 +191,16 @@ std = [
"pallet-utility/std",
"pallet-vesting/std",
"pallet-whitelist/std",
"runtime-common/std",
"gear-runtime-common/std",
"runtime-primitives/std",
"scale-info/std",
"serde_json/std",
"sp-api/std",
"sp-arithmetic/std",
"sp-authority-discovery/std",
"sp-block-builder/std",
"sp-consensus-babe/std",
"sp-consensus-grandpa/std",
"sp-core/std",
"sp-externalities",
"sp-inherents/std",
Expand Down Expand Up @@ -282,7 +286,7 @@ try-runtime = [
"pallet-vesting/try-runtime",
"pallet-whitelist/try-runtime",
"pallet-bags-list/try-runtime",
"runtime-common/try-runtime",
"gear-runtime-common/try-runtime",
]
dev = ["pallet-gear-debug", "pallet-gear-eth-bridge", "pallet-sudo"]
metadata-hash = ["substrate-wasm-builder?/metadata-hash"]
Loading

0 comments on commit db08ac1

Please sign in to comment.