|
@@ -43,6 +43,10 @@ import {
|
|
|
Status,
|
|
|
} from '../types'
|
|
|
|
|
|
+import {AccountId, Moment, ActiveEraInfo} from "@polkadot/types/interfaces";
|
|
|
+import Option from "@polkadot/types/codec/Option";
|
|
|
+import {Vec} from "@polkadot/types";
|
|
|
+
|
|
|
// queuing
|
|
|
let lastUpdate = 0
|
|
|
const queue: any[] = []
|
|
@@ -95,16 +99,48 @@ const addBlock = async (
|
|
|
|
|
|
const currentEra = Number(await api.query.staking.currentEra())
|
|
|
const era = await save('era', { id: currentEra })
|
|
|
- era.addBlock(block)
|
|
|
+ await era.addBlock(block)
|
|
|
|
|
|
const handle = member ? member.handle : author
|
|
|
const queued = `(queued: ${queue.length})`
|
|
|
console.log(`[Joystream] block ${block.id} ${handle} ${queued}`)
|
|
|
|
|
|
- processEvents(api, id)
|
|
|
+ await processEvents(api, id)
|
|
|
return updateEra(api, io, status, currentEra)
|
|
|
}
|
|
|
|
|
|
+const addBlockRange = async (api: Api, startBlock: number, endBlock: number) => {
|
|
|
+
|
|
|
+ const previousHash = await api.rpc.chain.getBlockHash(startBlock - 1);
|
|
|
+ let previousEra = await api.query.staking.activeEra.at(previousHash) as Option<ActiveEraInfo>;
|
|
|
+
|
|
|
+ for (let i = startBlock; i < endBlock; i++) {
|
|
|
+ const hash = await api.rpc.chain.getBlockHash(i);
|
|
|
+ const blockEra = await api.query.staking.activeEra.at(hash) as Option<ActiveEraInfo>;
|
|
|
+ if (blockEra.unwrap().index.toNumber() === previousEra.unwrap().index.toNumber()){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ let totalValidators = await api.query.staking.snapshotValidators.at(hash) as Option<Vec<AccountId>>;
|
|
|
+ if (totalValidators.isEmpty) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ console.log(`found validators: ${totalValidators.unwrap().length}`)
|
|
|
+
|
|
|
+ const totalNrValidators = totalValidators.unwrap().length;
|
|
|
+ const maxSlots = Number((await api.query.staking.validatorCount.at(hash)).toString());
|
|
|
+ const actives = Math.min(maxSlots, totalNrValidators);
|
|
|
+ const waiting = totalNrValidators > maxSlots ? totalNrValidators - maxSlots : 0;
|
|
|
+
|
|
|
+ const timestamp = (await api.query.timestamp.now.at(hash)) as Moment;
|
|
|
+ const date = new Date(timestamp.toNumber());
|
|
|
+ await save('era', { id: blockEra.unwrap().index.toNumber(), waiting: waiting, actives: actives, maxSlots: maxSlots, timestamp: date})
|
|
|
+
|
|
|
+ previousEra = blockEra;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
const getBlockHash = (api: Api, blockId: number) =>
|
|
|
api.rpc.chain.getBlockHash(blockId)
|
|
|
|
|
@@ -147,7 +183,8 @@ const updateEra = async (api: Api, io: any, status: any, era: number) => {
|
|
|
console.debug(`fetching stakes`)
|
|
|
|
|
|
if (!stashes) return
|
|
|
- stashes.forEach(async (validator: string) => {
|
|
|
+
|
|
|
+ for (let validator of stashes){
|
|
|
try {
|
|
|
const prefs = await api.query.staking.erasValidatorPrefs(era, validator)
|
|
|
const commission = Number(prefs.commission) / 10000000
|
|
@@ -157,7 +194,7 @@ const updateEra = async (api: Api, io: any, status: any, era: number) => {
|
|
|
} catch (e) {
|
|
|
console.warn(`Failed to fetch stakes for ${validator} in era ${era}`, e)
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
|
|
|
return {
|
|
|
members: (await api.query.members.nextMemberId()) - 1,
|
|
@@ -485,4 +522,4 @@ const fetchGithubDir = async (url: string) => {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
-module.exports = { addBlock }
|
|
|
+module.exports = { addBlock, addBlockRange }
|