Update metadata (Babe updates) (#1410)

* Update metadata (Babe updates)

* Bump with latest master

* Use genesisSlot as opposed to epochStartSlot (wrongly)

* Calculate session from genesis start

* Re-create after master merge

* Kusama CC1 chain type overrides
This commit is contained in:
Jaco Greeff
2019-09-24 16:47:04 +02:00
committed by GitHub
parent 539b26ed87
commit 8de45cb09e
8 changed files with 231 additions and 115 deletions
+13 -6
View File
@@ -15,7 +15,7 @@ import { drr } from '../util/drr';
import { bestNumber } from '../chain';
type Result0to94 = [BlockNumber, [SessionIndex, Option<BlockNumber>, BN, BN, SessionIndex]];
type Result = [[u64, SessionIndex], [u64, u64, u64], [SessionIndex, EraIndex]];
type Result = [[boolean, boolean, u64, SessionIndex], [u64, u64, u64], [SessionIndex, EraIndex]];
const ZERO = new BN(0);
const ONE = new BN(1);
@@ -49,21 +49,24 @@ function createDerived0to94 ([bestNumber, [currentIndex, _lastLengthChange, sess
} as unknown as DerivedSessionInfo;
}
function createDerived ([[epochDuration, sessionsPerEra], [currentSlot, epochIndex, epochStartSlot], [currentIndex, currentEra]]: Result): DerivedSessionInfo {
function createDerived ([[hasBabe, isGenesisSlot, epochDuration, sessionsPerEra], [currentSlot, epochIndex, epochOrGenesisStartSlot], [currentIndex, currentEra]]: Result): DerivedSessionInfo {
const epochStartSlot = isGenesisSlot
? epochIndex
.mul(epochDuration)
.add(epochOrGenesisStartSlot)
: epochOrGenesisStartSlot;
const sessionProgress = currentSlot.sub(epochStartSlot);
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: sessionsPerEra.mul(epochDuration),
eraProgress,
isEpoch: true,
isEpoch: hasBabe,
lastEraLengthChange: ZERO,
lastLengthChange: epochStartSlot,
sessionLength: epochDuration,
@@ -104,6 +107,8 @@ export function info (api: ApiInterfaceRx): () => Observable<DerivedSessionInfo>
// substrate spec_version >= 94 : get from parameter_types exposed as api.consts
// https://github.com/paritytech/substrate/pull/2802/files#diff-5e5e1c3aec9ddfde0a9054d062ab3db9R156
of([
hasBabe,
!!api.query.babe.genesisSlot,
hasBabe
? api.consts.babe.epochDuration
: ONE,
@@ -113,7 +118,9 @@ export function info (api: ApiInterfaceRx): () => Observable<DerivedSessionInfo>
? api.queryMulti([
api.query.babe.currentSlot,
api.query.babe.epochIndex,
api.query.babe.epochStartSlot
api.query.babe.genesisSlot
? api.query.babe.genesisSlot
: api.query.babe.epochStartSlot
])
: of([ONE, ONE, ONE]),
api.queryMulti([