signAndSend with finalized hash (depending on lag) (#2104)

* signAndSend with finalized hash (depending on drift)

* Extract lag calc

* mock update

* getSigningHeader

* api.derive.tx (with submittable cleanups)

* Adjust mortal calculations

* Cleanup new tx derives

* Optimize calcs

* Comment cleanups, next-up testing

* Adjust mocks

* Adjust sig (period is now based on metadata)

* Remove extra fn, tested and it works
This commit is contained in:
Jaco Greeff
2020-03-27 13:26:12 +01:00
committed by GitHub
parent 54efead9d7
commit 21a2b5f0b7
9 changed files with 145 additions and 60 deletions
+1
View File
@@ -1,5 +1,6 @@
# 1.9.0-beta.x
- Tx signing via `signAndSend` & `signAsync` now tries to use the last finalized block (depending on finalized drift)
- Adjust `Error.message` on RPC failures to not include the (already known) method signature
- Additional cleanups and rework of democracy derives, including support for new Polkadot/Substrate vote retrievals
- Internal adjustment of interface types to align with modules (no external impact via `@polkadot/types/interfaces`)
+2 -1
View File
@@ -21,10 +21,11 @@ import * as society from './society';
import * as staking from './staking';
import * as technicalCommittee from './technicalCommittee';
import * as treasury from './treasury';
import * as tx from './tx';
export * from './type';
export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, parachains, session, society, staking, technicalCommittee, treasury };
export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, parachains, session, society, staking, technicalCommittee, treasury, tx };
type DeriveSection<Section> = {
[Method in keyof Section]: Section[Method] extends AnyFunction
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
export const MAX_FINALITY_LAG = new BN(5);
+30
View File
@@ -0,0 +1,30 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventRecord, Hash, SignedBlock } from '@polkadot/types/interfaces';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { memo } from '../util';
interface Result {
block: SignedBlock;
events: EventRecord[];
}
export function events (api: ApiInterfaceRx): (at: Hash) => Observable<Result> {
return memo((at: Hash): Observable<Result> =>
combineLatest([
api.query.system.events.at(at),
api.rpc.chain.getBlock(at)
]).pipe(
map(([events, block]) => ({
block,
events
}))
)
);
}
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export * from './events';
export * from './signingInfo';
+69
View File
@@ -0,0 +1,69 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Header, Index } from '@polkadot/types/interfaces';
import { AnyNumber, Codec, IExtrinsicEra } from '@polkadot/types/types';
import BN from 'bn.js';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { isNumber, isUndefined } from '@polkadot/util';
interface Result {
header: Header | null;
mortalLength: number;
nonce: Index;
}
// default here to 5 min eras, adjusted based on the actual blocktime
const FALLBACK_PERIOD = new BN(6 * 1000);
const MAX_FINALITY_LAG = new BN(5);
const MORTAL_PERIOD = new BN(5 * 60 * 1000);
function latestNonce (api: ApiInterfaceRx, address: string): Observable<Index> {
return api.derive.balances.account(address).pipe(
map(({ accountNonce }): Index => accountNonce)
);
}
function signingHeader (api: ApiInterfaceRx): Observable<Header> {
return combineLatest([
api.rpc.chain.getHeader(),
api.rpc.chain.getFinalizedHead().pipe(
switchMap((hash) => api.rpc.chain.getHeader(hash))
)
]).pipe(
map(([current, finalized]): Header =>
// determine the hash to use, current when lag > max, else finalized
current.number.unwrap().sub(finalized.number.unwrap()).gt(MAX_FINALITY_LAG)
? current
: finalized
)
);
}
export function signingInfo (api: ApiInterfaceRx): (address: string, nonce?: AnyNumber | Codec, era?: IExtrinsicEra | number) => Observable<Result> {
// no memo, we want to do this fresh on each run
return (address: string, nonce?: AnyNumber | Codec, era?: IExtrinsicEra | number): Observable<Result> =>
combineLatest([
// retrieve nonce if none was specified
isUndefined(nonce)
? latestNonce(api, address)
: of(api.registry.createType('Index', nonce)),
// if no era (create) or era > 0 (mortal), do block retrieval
(isUndefined(era) || (isNumber(era) && era > 0))
? signingHeader(api)
: of(null)
]).pipe(
map(([nonce, header]) => ({
header,
nonce,
mortalLength: MORTAL_PERIOD
.div(api.consts.babe?.expectedBlockTime || api.consts.timestamp?.minimumPeriod.muln(2) || FALLBACK_PERIOD)
.add(MAX_FINALITY_LAG)
.toNumber()
}))
);
}
+1 -1
View File
@@ -13,7 +13,7 @@ import { hexToU8a } from '@polkadot/util';
import { SingleAccountSigner } from '../../test/util';
import ApiPromise from './Api';
const TRANSFER_SIG = '0x62bfcd07a9f50ba32ed9e2c41ca11267ac62a24c95949e25efdccc4989d48573f84754dbbaa1d0937ceb888ad8a2cf71daced1545a839d9d6cceac33a5771401';
const TRANSFER_SIG = '0xa1d493ab6b74550da434fbc153a739429be68c9d30e59e631fb5d08f348504deefb5577f4824e138741ad102f479109827e8fa9552812a3e5f505a24d10e820f';
describe('ApiPromise', (): void => {
const registry = new TypeRegistry();
+27 -57
View File
@@ -8,9 +8,9 @@ import { Callback, Codec, Constructor, IKeyringPair, Registry, SignatureOptions,
import { ApiInterfaceRx, ApiTypes, SignerResult } from '../types';
import { AddressOrPair, SignerOptions, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types';
import { Observable, combineLatest, of } from 'rxjs';
import { Observable, of } from 'rxjs';
import { first, map, mapTo, mergeMap, switchMap, tap } from 'rxjs/operators';
import { assert, isBn, isFunction, isNumber, isUndefined } from '@polkadot/util';
import { assert, isBn, isFunction, isNumber } from '@polkadot/util';
import { filterEvents, isKeyringPair } from '../util';
import ApiBase from '../base';
@@ -22,13 +22,6 @@ interface SubmittableOptions<ApiType extends ApiTypes> {
decorateMethod: ApiBase<ApiType>['decorateMethod'];
}
// The default for 6s allowing for 5min eras. When translating this to faster blocks -
// - 4s = (10 / 15) * 5 = 3.33m
// - 2s = (10 / 30) * 5 = 1.66m
const BLOCKTIME = 6;
const ONE_MINUTE = 60 / BLOCKTIME;
const DEFAULT_MORTAL_LENGTH = 5 * ONE_MINUTE;
export default function createClass <ApiType extends ApiTypes> ({ api, apiType, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> {
// an instance of the base extrinsic for us to extend
const ExtrinsicBase = api.registry.createClass('Extrinsic');
@@ -49,11 +42,11 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
return decorateMethod(
(): Observable<RuntimeDispatchInfo> =>
this.#getPrelimState(address, allOptions).pipe(
api.derive.tx.signingInfo(address, allOptions.nonce, allOptions.era).pipe(
first(),
switchMap(([nonce, header]): Observable<RuntimeDispatchInfo> => {
switchMap((signingInfo): Observable<RuntimeDispatchInfo> => {
// setup our options (same way as in signAndSend)
const eraOptions = this.#makeEraOptions(allOptions, { header, nonce });
const eraOptions = this.#makeEraOptions(allOptions, signingInfo);
const signOptions = this.#makeSignOptions(eraOptions, {});
// add a fake signature to the extrinsic
@@ -125,23 +118,7 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
)(statusCb);
}
#getPrelimState = (address: string, options: Partial<SignerOptions>): Observable<[Index, Header | null]> => {
return combineLatest([
// if we have a nonce already, don't retrieve the latest, use what is there
isUndefined(options.nonce)
? api.derive.balances.account(address).pipe(
map(({ accountNonce }): Index => accountNonce)
)
: of(this.registry.createType('Index', options.nonce)),
// if we have an era provided already or eraLength is <= 0 (immortal)
// don't get the latest block, just pass null, handle in mergeMap
(isUndefined(options.era) || (isNumber(options.era) && options.era > 0))
? api.rpc.chain.getHeader()
: of(null)
]);
}
#makeEraOptions = (options: Partial<SignerOptions>, { header, nonce }: { header: Header | null; nonce: Index }): SignatureOptions => {
#makeEraOptions = (options: Partial<SignerOptions>, { header, mortalLength, nonce }: { header: Header | null; mortalLength: number; nonce: Index }): SignatureOptions => {
if (!header) {
if (isNumber(options.era)) {
// since we have no header, it is immortal, remove any option overrides
@@ -157,7 +134,7 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
blockHash: header.hash,
era: this.registry.createType('ExtrinsicEra', {
current: header.number,
period: options.era || DEFAULT_MORTAL_LENGTH
period: options.era || mortalLength
}),
nonce
});
@@ -191,15 +168,15 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
const options = this.#optionsOrNonce(optionsOrNonce);
let updateId: number | undefined;
return this.#getPrelimState(address, options).pipe(
return api.derive.tx.signingInfo(address, options.nonce, options.era).pipe(
first(),
mergeMap(async ([nonce, header]): Promise<void> => {
const eraOptions = this.#makeEraOptions(options, { header, nonce });
mergeMap(async (signingInfo): Promise<void> => {
const eraOptions = this.#makeEraOptions(options, signingInfo);
if (isKeyringPair(account)) {
this.sign(account, eraOptions);
} else {
updateId = await this.#signViaSigner(address, eraOptions, header);
updateId = await this.#signViaSigner(address, eraOptions, signingInfo.header);
}
}),
mapTo(updateId)
@@ -215,13 +192,10 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
? status.asInBlock
: status.asFinalized;
return combineLatest([
api.rpc.chain.getBlock(blockHash),
api.query.system.events.at(blockHash)
]).pipe(
map(([signedBlock, allEvents]): ISubmittableResult =>
return api.derive.tx.events(blockHash).pipe(
map(({ block, events }): ISubmittableResult =>
new SubmittableResult({
events: filterEvents(this.hash, signedBlock, allEvents, status),
events: filterEvents(this.hash, block, events, status),
status
})
)
@@ -229,26 +203,22 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
}
#observeSend = (updateId = -1): Observable<Hash> => {
return api.rpc.author
.submitExtrinsic(this)
.pipe(
tap((hash): void => {
this.#updateSigner(updateId, hash);
})
);
return api.rpc.author.submitExtrinsic(this).pipe(
tap((hash): void => {
this.#updateSigner(updateId, hash);
})
);
}
#observeSubscribe = (updateId = -1): Observable<ISubmittableResult> => {
return api.rpc.author
.submitAndWatchExtrinsic(this)
.pipe(
switchMap((status): Observable<ISubmittableResult> =>
this.#observeStatus(status)
),
tap((status): void => {
this.#updateSigner(updateId, status);
})
);
return api.rpc.author.submitAndWatchExtrinsic(this).pipe(
switchMap((status): Observable<ISubmittableResult> =>
this.#observeStatus(status)
),
tap((status): void => {
this.#updateSigner(updateId, status);
})
);
}
// NOTE here we actually override nonce if it was specified (backwards compat for
+2 -1
View File
@@ -54,7 +54,8 @@ export default class Mock implements ProviderInterface {
chain_getBlock: (hash: string): any => this.registry.createType('SignedBlock', rpcSignedBlock.result).toJSON(),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
chain_getBlockHash: (blockNumber: number): string => '0x1234',
chain_getHeader: (): any => this.registry.createType('Header', rpcHeader.result).toJSON(),
chain_getFinalizedHead: () => this.registry.createType('Header', rpcHeader.result).hash,
chain_getHeader: () => this.registry.createType('Header', rpcHeader.result).toJSON(),
state_getKeys: (): string[] => [],
state_getKeysPaged: (): string[] => [],
state_getRuntimeVersion: (): string => this.registry.createType('RuntimeVersion').toHex(),