Add createdAtHash to Codec interface (#3276)

* Add createdAtHash to Codec interface

* Adjust linkedHash (also on )

* createdAtHash
This commit is contained in:
Jaco Greeff
2021-03-07 16:00:30 +01:00
committed by GitHub
parent 22e5488f16
commit 3c9b7de6c7
26 changed files with 154 additions and 96 deletions
@@ -2,8 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { AccountId, EventRecord, SignedBlock } from '@polkadot/types/interfaces';
import type { Vec } from '@polkadot/types';
import type { EventRecord, SignedBlock } from '@polkadot/types/interfaces';
import type { Observable } from '@polkadot/x-rxjs';
import type { HeaderExtended } from '../type';
import { combineLatest, of } from '@polkadot/x-rxjs';
import { map, switchMap } from '@polkadot/x-rxjs/operators';
@@ -18,17 +20,17 @@ import { memo } from '../util';
export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): () => Observable<SignedBlockExtended> {
return memo(instanceId, (): Observable<SignedBlockExtended> =>
api.derive.chain.subscribeNewHeads().pipe(
switchMap((header): Observable<[SignedBlock, EventRecord[], AccountId[] | undefined]> => {
const blockHash = header.hash;
switchMap((header): Observable<[SignedBlock, Vec<EventRecord>, HeaderExtended]> => {
const blockHash = header.createdAtHash || header.hash;
return combineLatest(
api.rpc.chain.getBlock(blockHash),
api.query.system.events.at(blockHash),
of(header.validators)
of(header)
);
}),
map(([block, events, validators]) =>
new SignedBlockExtended(api.registry, block, events, validators)
map(([block, events, header]) =>
new SignedBlockExtended(api.registry, block, events, header.validators)
)
)
);
@@ -29,10 +29,13 @@ export function subscribeNewHeads (instanceId: string, api: ApiInterfaceRx): ()
api.rpc.chain.subscribeNewHeads(),
api.query.session
? api.query.session.validators()
: of([])
: of(undefined)
]).pipe(
map(([header, validators]): HeaderExtended =>
new HeaderExtended(api.registry, header, validators)
)
));
map(([header, validators]): HeaderExtended => {
header.createdAtHash = header.hash;
return new HeaderExtended(api.registry, header, validators);
})
)
);
}
@@ -26,6 +26,7 @@ export class HeaderExtended extends _Header {
this.#author = extractAuthor(this.digest, validators);
this.#validators = validators;
this.createdAtHash = header?.createdAtHash;
}
/**
@@ -53,12 +53,13 @@ export class SignedBlockExtended extends _SignedBlock {
readonly #events: EventRecord[];
readonly #extrinsics: TxWithEvent[];
constructor (registry: Registry, block?: SignedBlock, events?: EventRecord[], sessionValidators?: AccountId[]) {
constructor (registry: Registry, block?: SignedBlock, events?: EventRecord[], validators?: AccountId[]) {
super(registry, block);
this.#author = extractAuthor(this.block.header.digest, sessionValidators);
this.#author = extractAuthor(this.block.header.digest, validators);
this.#events = events || ([] as EventRecord[]);
this.#extrinsics = mapExtrinsics(this.block.extrinsics, this.#events);
this.createdAtHash = block?.createdAtHash;
}
/**