Skip to content

Commit

Permalink
Add intention validators in Polkadot network (#468)
Browse files Browse the repository at this point in the history
* Add timer to getAllValidators()

* Fetch intention addresses for current session

* wip

* Add validators

* working! getAllValidators exec time less than 10s

* fix status

* Cleanup
  • Loading branch information
mariopino authored Mar 18, 2020
1 parent f309d0e commit 3c9745f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10,536 deletions.
3 changes: 1 addition & 2 deletions data/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ module.exports = [
title: 'Kusama',
chain_id: 'kusama-cc3',
api_url: 'https://host-01.polkascan.io/kusama/api/v1/',
rpc_url:
'wss://kusama-rpc.polkadot.io/',
rpc_url: 'wss://kusama-rpc.polkadot.io/',
bech32_prefix: ' ',
address_prefix: ' ',
ledger_app: 'polkadot',
Expand Down
2 changes: 1 addition & 1 deletion lib/reducers/polkadotV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function validatorReducer(network, validator) {
commission: (validator.validatorPrefs.commission / 1000000000).toFixed(9), // Returns commission in 0-1 range (1 = 100%)
maxCommission: undefined,
maxChangeCommission: undefined,
status: `ACTIVE`, // We are fetching current session active validators only (not intentions)
status: validator.status,
statusDetailed: ``, // TODO: Include validator heartbeat messages
delegatorShares: undefined,
selfStake:
Expand Down
48 changes: 30 additions & 18 deletions lib/source/polkadotV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,38 +110,50 @@ class polkadotAPI {
}

async getAllValidators() {
console.time(`getAllValidators`)

const api = await this.getAPI()

// Fetch validator addresses for current session
// Fetch all stash addresses for current session (including validators and intentions)
const allStashAddresses = await api.derive.staking.stashes()

// Fetch active validator addresses for current session.
const validatorAddresses = await api.query.session.validators()

// Fetch validator's staking info
let validators = await Promise.all(
validatorAddresses.map(authorityId =>
// Fetch all validators staking info
let allValidators = await Promise.all(
allStashAddresses.map(authorityId =>
api.derive.staking.account(authorityId)
)
)
validators = JSON.parse(JSON.stringify(validators))
allValidators = JSON.parse(JSON.stringify(allValidators))

// Calculate and update total active staked funds
let networkTotalStake = new BigNumber(0)
validators.forEach(validator => {
if (validator.exposure) {
const accum = new BigNumber(validator.exposure.total)
networkTotalStake = networkTotalStake.plus(accum)
}
})
allValidators
.filter(validator => validatorAddresses.includes(validator.accountId))
.forEach(validator => {
if (validator.exposure) {
const accum = new BigNumber(validator.exposure.total)
networkTotalStake = networkTotalStake.plus(accum)
}
})

// Fetch identity info
let validatorIdentity = await Promise.all(
validatorAddresses.map(authorityId =>
let allValidatorsIdentity = await Promise.all(
allStashAddresses.map(authorityId =>
api.derive.accounts.info(authorityId)
)
)
validatorIdentity = JSON.parse(JSON.stringify(validatorIdentity))
allValidatorsIdentity = JSON.parse(JSON.stringify(allValidatorsIdentity))

validators.forEach(validator => {
const identity = validatorIdentity.find(
allValidators.forEach(validator => {
if (validatorAddresses.includes(validator.accountId)) {
validator.status = `ACTIVE`
} else {
validator.status = `INACTIVE`
}
const identity = allValidatorsIdentity.find(
validatorIdentity => validatorIdentity.accountId === validator.accountId
)
validator.identity = JSON.parse(JSON.stringify(identity.identity))
Expand All @@ -158,8 +170,8 @@ class polkadotAPI {
validator.votingPower = 0
}
})

return validators.map(validator =>
console.timeEnd(`getAllValidators`)
return allValidators.map(validator =>
this.reducers.validatorReducer(this.network, validator)
)
}
Expand Down
Loading

0 comments on commit 3c9745f

Please sign in to comment.