From 2c7b904c37f3131163b3cbed9905a6aad47b441e Mon Sep 17 00:00:00 2001 From: Javier Viola <363911+pepoviola@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:21:00 -0300 Subject: [PATCH] fix: check if minimumPeriod is zero (#5827) * fix: check if minimumPeriod is zero * changes for https://github.com/paritytech/polkadot-sdk/pull/3732/files * guard expectedBlockTime * typo * clean code and add aura interface * Update signingInfo.ts Co-authored-by: Tarik Gul <47201679+TarikGul@users.noreply.github.com> * Update packages/api-derive/src/tx/signingInfo.ts Co-authored-by: Tarik Gul <47201679+TarikGul@users.noreply.github.com> * lint --------- Co-authored-by: Tarik Gul <47201679+TarikGul@users.noreply.github.com> --- packages/api-derive/src/tx/signingInfo.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/api-derive/src/tx/signingInfo.ts b/packages/api-derive/src/tx/signingInfo.ts index eb583af57c..b0ec632bb4 100644 --- a/packages/api-derive/src/tx/signingInfo.ts +++ b/packages/api-derive/src/tx/signingInfo.ts @@ -2,8 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; +import type { AugmentedConsts } from '@polkadot/api-base/types'; +import type { u64 } from '@polkadot/types'; import type { Header, Index } from '@polkadot/types/interfaces'; import type { AnyNumber, Codec, IExtrinsicEra } from '@polkadot/types/types'; +import type { BN } from '@polkadot/util'; import type { DeriveApi } from '../types.js'; import { catchError, combineLatest, map, of, switchMap } from 'rxjs'; @@ -63,6 +66,19 @@ function signingHeader (api: DeriveApi): Observable
{ ); } +interface Aura { + slotDuration: u64 & AugmentedConsts<'rxjs'>; +} + +function babeOrAuraPeriod (api: DeriveApi): BN | undefined { + const period = api.consts.babe?.expectedBlockTime || + // this will be present ones https://github.com/paritytech/polkadot-sdk/pull/3732 is merged + (api.consts['aura'] as unknown as Aura)?.slotDuration || + api.consts.timestamp?.minimumPeriod; + + return !period.isZero() ? period : undefined; +} + export function signingInfo (_instanceId: string, api: DeriveApi): (address: string, nonce?: AnyNumber | Codec, era?: IExtrinsicEra | number) => Observable { // no memo, we want to do this fresh on each run return (address: string, nonce?: AnyNumber | Codec, era?: IExtrinsicEra | number): Observable => @@ -83,11 +99,7 @@ export function signingInfo (_instanceId: string, api: DeriveApi): (address: str mortalLength: Math.min( api.consts.system?.blockHashCount?.toNumber() || FALLBACK_MAX_HASH_COUNT, MORTAL_PERIOD - .div( - api.consts.babe?.expectedBlockTime || - api.consts.timestamp?.minimumPeriod.muln(2) || - FALLBACK_PERIOD - ) + .div(babeOrAuraPeriod(api) || FALLBACK_PERIOD) .iadd(MAX_FINALITY_LAG) .toNumber() ),