Skip to content

Commit

Permalink
feat: dynamic council threshold (#1890)
Browse files Browse the repository at this point in the history
Signed-off-by: Cheng JIANG <[email protected]>

Signed-off-by: Cheng JIANG <[email protected]>
  • Loading branch information
GopherJ authored Oct 7, 2022
1 parent 3dfb1a8 commit ee7d41e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
4 changes: 2 additions & 2 deletions scripts/helper/src/commands/democracy/runtime-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import https from 'https'
import { Command, CreateCommandParameters, program } from '@caporal/core'
import { Keyring } from '@polkadot/api'
import { blake2AsHex } from '@polkadot/util-crypto'
import { getApi, nextNonce } from '../../utils'
import { getApi, getCouncilThreshold, nextNonce } from '../../utils'

export default function ({ createCommand }: CreateCommandParameters): Command {
return createCommand('runtime upgrade via democracy')
Expand Down Expand Up @@ -50,7 +50,7 @@ export default function ({ createCommand }: CreateCommandParameters): Command {

const tx = api.tx.utility.batchAll([
api.tx.democracy.notePreimage(encoded),
api.tx.generalCouncil.propose(3, external, external.length)
api.tx.generalCouncil.propose(await getCouncilThreshold(api), external, external.length)
])
if (dryRun) {
return logger.info(`hex-encoded call: ${tx.toHex()}`)
Expand Down
15 changes: 13 additions & 2 deletions scripts/helper/src/commands/hrmp/accept.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createXcm, getApi, getRelayApi, nextNonce, sovereignRelayOf } from '../../utils'
import {
createXcm,
getApi,
getCouncilThreshold,
getRelayApi,
nextNonce,
sovereignRelayOf
} from '../../utils'
import { Command, CreateCommandParameters, program } from '@caporal/core'
import { Keyring } from '@polkadot/api'

Expand Down Expand Up @@ -41,7 +48,11 @@ export default function ({ createCommand }: CreateCommandParameters): Command {
},
createXcm(`0x${encoded.slice(6)}`, sovereignRelayOf(target.valueOf() as number))
)
const tx = api.tx.generalCouncil.propose(3, proposal, proposal.length)
const tx = api.tx.generalCouncil.propose(
await getCouncilThreshold(api),
proposal,
proposal.length
)

if (dryRun) {
return logger.info(`hex-encoded call: ${tx.toHex()}`)
Expand Down
9 changes: 7 additions & 2 deletions scripts/helper/src/commands/hrmp/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
nextNonce,
sovereignRelayOf,
getDefaultRelayChainWsUrl,
getDefaultParachainWsUrl
getDefaultParachainWsUrl,
getCouncilThreshold
} from '../../utils'
import { Command, CreateCommandParameters, program } from '@caporal/core'
import { Keyring } from '@polkadot/api'
Expand Down Expand Up @@ -60,7 +61,11 @@ export default function ({ createCommand }: CreateCommandParameters): Command {
},
createXcm(`0x${encoded.slice(6)}`, sovereignRelayOf(source.valueOf() as number))
)
const tx = api.tx.generalCouncil.propose(3, proposal, proposal.length)
const tx = api.tx.generalCouncil.propose(
await getCouncilThreshold(api),
proposal,
proposal.length
)

if (dryRun) {
return logger.info(`hex-encoded call: ${tx.toHex()}`)
Expand Down
8 changes: 6 additions & 2 deletions scripts/helper/src/commands/set/add-market.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getApi, nextNonce } from '../../utils'
import { getApi, getCouncilThreshold, nextNonce } from '../../utils'
import { Command, CreateCommandParameters, program } from '@caporal/core'
import { Keyring } from '@polkadot/api'
import { readFile } from '../../utils'
Expand Down Expand Up @@ -115,7 +115,11 @@ export default function ({ createCommand }: CreateCommandParameters): Command {
)
)

const tx = api.tx.generalCouncil.propose(3, proposal, proposal.length)
const tx = api.tx.generalCouncil.propose(
await getCouncilThreshold(api),
proposal,
proposal.length
)

if (dryRun) {
return logger.info(`hex-encoded call: ${tx.toHex()}`)
Expand Down
8 changes: 6 additions & 2 deletions scripts/helper/src/commands/set/market-reward.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getApi, nextNonce } from '../../utils'
import { getApi, getCouncilThreshold, nextNonce } from '../../utils'
import { Command, CreateCommandParameters, program } from '@caporal/core'
import { Keyring } from '@polkadot/api'
import { readFile } from '../../utils'
Expand Down Expand Up @@ -48,7 +48,11 @@ export default function ({ createCommand }: CreateCommandParameters): Command {
})
)

const tx = api.tx.generalCouncil.propose(3, proposal, proposal.length)
const tx = api.tx.generalCouncil.propose(
await getCouncilThreshold(api),
proposal,
proposal.length
)

if (dryRun) {
return logger.info(`hex-encoded call: ${tx.toHex()}`)
Expand Down
8 changes: 6 additions & 2 deletions scripts/helper/src/commands/ump/transact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createXcm, getApi, nextNonce, sovereignRelayOf } from '../../utils'
import { createXcm, getApi, getCouncilThreshold, nextNonce, sovereignRelayOf } from '../../utils'
import { Command, CreateCommandParameters, program } from '@caporal/core'
import { Keyring } from '@polkadot/api'
import { u32 } from '@polkadot/types'
Expand Down Expand Up @@ -38,7 +38,11 @@ export default function ({ createCommand }: CreateCommandParameters): Command {
'SovereignAccount'
)
)
const tx = api.tx.generalCouncil.propose(3, proposal, proposal.length)
const tx = api.tx.generalCouncil.propose(
await getCouncilThreshold(api),
proposal,
proposal.length
)

if (dryRun) {
return logger.info(`hex-encoded call: ${tx.toHex()}`)
Expand Down
8 changes: 7 additions & 1 deletion scripts/helper/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { blake2AsU8a } from '@polkadot/util-crypto'
import { stringToU8a, bnToU8a, u8aConcat, u8aToHex } from '@polkadot/util'
import { decodeAddress, encodeAddress } from '@polkadot/keyring'
import { KeyringPair } from '@polkadot/keyring/types'
import { Index } from '@polkadot/types/interfaces'
import { AccountId, Index } from '@polkadot/types/interfaces'
import { promisify } from 'util'
import { Vec } from '@polkadot/types'

const EMPTY_U8A_32 = new Uint8Array(32)

Expand Down Expand Up @@ -153,6 +154,11 @@ export const getRelayApi = async (endpoint: string): Promise<ApiPromise> => {
})
}

export const getCouncilThreshold = async (api: ApiPromise): Promise<number> => {
const members = (await api.query.generalCouncilMembership.members()) as unknown as Vec<AccountId>
return Math.ceil(members.length / 2)
}

export const calcWeightPerSecond = (precision: number, price: number): number => {
const WEIGHT_PER_SECOND = 10 ** 12
// for fixed weigher always 600_000_000
Expand Down

0 comments on commit ee7d41e

Please sign in to comment.