Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: add devnet custom genesis, aura and grandpa support (#935)
Browse files Browse the repository at this point in the history
Co-authored-by: Harry Solovay <harrysolovay@gmail.com>
  • Loading branch information
kratico and harrysolovay authored May 8, 2023
1 parent d69ff2e commit f645482
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 28 deletions.
3 changes: 3 additions & 0 deletions nets/DevNetSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ export interface DevNetProps {
bin: BinaryResolver
chain: string
nodeCount?: number
customize?: (chainSpec: Record<string, unknown>) => void
}

export abstract class DevNetSpec extends NetSpec {
readonly binary
readonly chain
readonly nodeCount
readonly customize
constructor(props: DevNetProps) {
super()
this.binary = props.bin
this.chain = props.chain
this.nodeCount = props.nodeCount
this.customize = props.customize
}

abstract relay: DevRelaySpec
Expand Down
7 changes: 5 additions & 2 deletions nets/DevParachainSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ export class DevParachainSpec extends DevNetSpec {
(chainSpec) => {
chainSpec.para_id = this.id
const genesisConfig = getGenesisConfig(chainSpec)
genesisConfig.parachainInfo.parachainId = this.id
addDevUsers(genesisConfig.balances.balances)
if (genesisConfig.parachainInfo) {
genesisConfig.parachainInfo.parachainId = this.id
}
addDevUsers(genesisConfig)
this.customize?.(chainSpec)
},
)
const genesis = await exportParachainGenesis(binary, chainSpecPath, signal)
Expand Down
13 changes: 7 additions & 6 deletions nets/DevRelaySpec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Narrow } from "../deps/scale.ts"
import {
addAuthorities,
addDevUsers,
addXcmHrmpChannels,
createCustomChainSpec,
GenesisConfig,
getGenesisConfig,
} from "./chain_spec/mod.ts"
import { DevNet, DevNetSpec, spawnDevNet } from "./DevNetSpec.ts"
Expand Down Expand Up @@ -39,15 +39,16 @@ export class DevRelaySpec extends DevNetSpec {
return createCustomChainSpec(tempDir, binary, this.chain, (chainSpec) => {
const genesisConfig = getGenesisConfig(chainSpec)
if (parachainInfo.length) {
genesisConfig.paras.paras.push(
...parachainInfo.map((
{ id, genesis },
): GenesisConfig["paras"]["paras"][number] => [id, [...genesis, true]]),
genesisConfig.paras?.paras.push(
...parachainInfo.map(({ id, genesis: [state, wasm] }) =>
[id, [state, wasm, true]] satisfies Narrow
),
)
addXcmHrmpChannels(genesisConfig, parachainInfo.map(({ id }) => id))
}
addAuthorities(genesisConfig, minValidators)
addDevUsers(genesisConfig.balances.balances)
addDevUsers(genesisConfig)
this.customize?.(chainSpec)
})
}

Expand Down
13 changes: 10 additions & 3 deletions nets/chain_spec/ChainSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@ export interface ChainSpec {
genesis: {
runtime: GenesisConfig | { runtime_genesis_config: GenesisConfig }
}
[key: string]: unknown
}

export interface GenesisConfig {
runtime_genesis_config?: never
paras: {
paras?: {
paras: [[
parachainId: number,
genesis: [state: string, wasm: string, kind: boolean],
]]
}
parachainInfo: {
parachainInfo?: {
parachainId: number
}
balances: {
balances?: {
balances: [account: string, initialBalance: number][]
}
session?: {
keys: [account: string, account: string, key: SessionKey][]
}
aura?: {
authorities: string[]
}
grandpa?: {
authorities: [string, 1][]
}
hrmp?: {
preopenHrmpChannels: [
senderParaId: number,
Expand Down
38 changes: 23 additions & 15 deletions nets/chain_spec/addAuthorities.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import { GenesisConfig } from "./ChainSpec.ts"

// TODO: #889 add support for pallet_session, pallet_aura and pallet_grandpa

export function addAuthorities(genesisConfig: GenesisConfig, count: number) {
if (count > authorities.length) {
throw new Error(`authorities count should be <= ${authorities.length}`)
}
if (!genesisConfig.session) return
genesisConfig.session.keys.length = 0
authorities.slice(0, count).forEach(({ srAccount, srStash, edAccount, ecAccount }) =>
genesisConfig.session!.keys.push([srStash, srStash, {
grandpa: edAccount,
babe: srAccount,
im_online: srAccount,
para_validator: srAccount,
para_assignment: srAccount,
authority_discovery: srAccount,
beefy: ecAccount,
}])
)
const genesisAuthorities = authorities.slice(0, count)
if (genesisConfig.session) {
genesisConfig.session.keys = genesisAuthorities.map((
{ srAccount, srStash, edAccount, ecAccount },
) => [
srStash,
srStash,
{
grandpa: edAccount,
babe: srAccount,
im_online: srAccount,
para_validator: srAccount,
para_assignment: srAccount,
authority_discovery: srAccount,
beefy: ecAccount,
},
])
} else if (genesisConfig.aura && genesisConfig.grandpa) {
genesisConfig.aura.authorities = genesisAuthorities.map(({ srAccount }) => srAccount)
genesisConfig.grandpa.authorities = genesisAuthorities.map((
{ edAccount },
) => [edAccount, 1])
}
}

export const authorities = [
Expand Down
5 changes: 4 additions & 1 deletion nets/chain_spec/addDevUsers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { blake2_256, Sr25519, ss58 } from "../../crypto/mod.ts"
import * as $ from "../../deps/scale.ts"
import { devUserPublicKeysData } from "../../util/_artifacts/devUserPublicKeysData.ts"
import { GenesisConfig } from "./ChainSpec.ts"

const devUserInitialFunds = 1_000_000_000_000_000_000

export const devUserPublicKeys = $.array($.sizedUint8Array(32)).decode(devUserPublicKeysData)

export function addDevUsers(balances: [string, number][]) {
export function addDevUsers(genesisConfig: GenesisConfig) {
if (!genesisConfig.balances) return
const { balances } = genesisConfig.balances
const networkPrefix = ss58.decode(balances[0]![0])[0]
for (const publicKey of devUserPublicKeys) {
balances.push([ss58.encode(networkPrefix, publicKey), devUserInitialFunds])
Expand Down
2 changes: 1 addition & 1 deletion nets/chain_spec/addXcmHrmpChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GenesisConfig } from "./ChainSpec.ts"
const hrmpChannelMaxCapacity = 8
const hrmpChannelMaxMessageSize = 512
export function addXcmHrmpChannels(genesisConfig: GenesisConfig, paraIds: number[]) {
genesisConfig.hrmp ??= { preopenHrmpChannels: [] }
if (!genesisConfig.hrmp) return
for (const senderParaId of paraIds) {
for (const recipientParaId of paraIds) {
if (senderParaId === recipientParaId) continue
Expand Down

0 comments on commit f645482

Please sign in to comment.