From 14f018d57ff847773b9c7a929b343250270e0b3b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 30 Jul 2019 14:24:41 +0200 Subject: [PATCH] Fix session/era calculations for Babe (#1235) --- packages/api-derive/src/session/info.ts | 34 +++++++++++++++++-------- packages/api-derive/src/types.ts | 1 + 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/api-derive/src/session/info.ts b/packages/api-derive/src/session/info.ts index 14f26e6bc8..6322841241 100644 --- a/packages/api-derive/src/session/info.ts +++ b/packages/api-derive/src/session/info.ts @@ -2,20 +2,20 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { BlockNumber } from '@polkadot/types/interfaces'; +import { BlockNumber, EraIndex, SessionIndex } from '@polkadot/types/interfaces'; import { DerivedSessionInfo } from '../types'; import BN from 'bn.js'; import { Observable, combineLatest, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; -import { Option } from '@polkadot/types'; +import { Option, u64 } from '@polkadot/types'; import { drr } from '../util/drr'; import { bestNumber } from '../chain'; -type Result0to94 = [BN, [BN, Option, BN, BN, BN]]; -type Result = [BN, [BN, BN]]; +type Result0to94 = [BlockNumber, [SessionIndex, Option, BN, BN, SessionIndex]]; +type Result = [[u64, SessionIndex], [u64, u64, u64, SessionIndex, EraIndex]]; const ZERO = new BN(0); @@ -41,6 +41,7 @@ function createDerived0to94 ([bestNumber, [currentIndex, _lastLengthChange, sess currentIndex, eraLength, eraProgress, + isEpoch: false, lastEraLengthChange, lastLengthChange, sessionLength, @@ -49,19 +50,24 @@ function createDerived0to94 ([bestNumber, [currentIndex, _lastLengthChange, sess } as unknown as DerivedSessionInfo; } -function createDerived ([sessionsPerEra, [currentIndex, currentEra]]: Result): DerivedSessionInfo { - const eraProgress = (currentIndex).mod(sessionsPerEra); +function createDerived ([[epochDuration, sessionsPerEra], [currentSlot, epochIndex, epochStartSlot, currentIndex, currentEra]]: Result): DerivedSessionInfo { + const sessionProgress = currentSlot.sub(epochStartSlot); + const eraLength = sessionsPerEra.mul(epochDuration); + const eraProgress = epochIndex.mod(sessionsPerEra).mul(epochDuration).add(sessionProgress); + // FIXME This alwasy assumes Babe, as per the substrate defaults - at least for + // aura the `isEpoch` should be false return { currentEra, currentIndex, - eraLength: ZERO, + eraLength, eraProgress, + isEpoch: true, lastEraLengthChange: ZERO, - lastLengthChange: ZERO, - sessionLength: ZERO, + lastLengthChange: epochStartSlot, + sessionLength: epochDuration, sessionsPerEra, - sessionProgress: ZERO + sessionProgress: sessionProgress } as unknown as DerivedSessionInfo; } @@ -94,8 +100,14 @@ export function info (api: ApiInterfaceRx): () => Observable // sessionsPerEra: // substrate spec_version >= 94 : get from parameter_types exposed as api.consts // https://github.com/paritytech/substrate/pull/2802/files#diff-5e5e1c3aec9ddfde0a9054d062ab3db9R156 - of(api.consts.staking.sessionsPerEra), + of([ + api.consts.babe.epochDuration, + api.consts.staking.sessionsPerEra + ]), api.queryMulti([ + api.query.babe.currentSlot, + api.query.babe.epochIndex, + api.query.babe.epochStartSlot, api.query.session.currentIndex, api.query.staking.currentEra ]) diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 94e1a5e03c..3cd55e8e33 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -62,6 +62,7 @@ export interface DerivedSessionInfo { currentIndex: BN; eraLength: BN; eraProgress: BN; + isEpoch: boolean; lastEraLengthChange: BN; lastLengthChange: BN; sessionLength: BN;