Author extract using correct as-at state (#5598)

* Author extract using correct as-at state

* Align header retrieval

* Dedupe

* Deupe

* Adjust

* Remove deprecated events.at

* 10.3.4
This commit is contained in:
Jaco
2023-04-16 12:18:36 +03:00
committed by GitHub
parent 074e790bfc
commit 320135dbe5
22 changed files with 200 additions and 182 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ export function getBlock (instanceId: string, api: DeriveApi): (hash: Uint8Array
combineLatest([
of(signedBlock),
queryAt.system.events(),
getAuthorDetails(signedBlock.block.header, queryAt)
getAuthorDetails(api, signedBlock.block.header, blockHash)
])
),
map(([signedBlock, events, [, validators, author]]) =>
+4 -7
View File
@@ -5,7 +5,7 @@ import type { Observable } from 'rxjs';
import type { HeaderExtended } from '../type/types.js';
import type { DeriveApi } from '../types.js';
import { combineLatest, map, switchMap } from 'rxjs';
import { map, switchMap } from 'rxjs';
import { createHeaderExtended } from '../type/index.js';
import { memo } from '../util/index.js';
@@ -27,12 +27,9 @@ import { getAuthorDetails } from './util.js';
*/
export function getHeader (instanceId: string, api: DeriveApi): (blockHash: Uint8Array | string) => Observable<HeaderExtended> {
return memo(instanceId, (blockHash: Uint8Array | string): Observable<HeaderExtended> =>
combineLatest([
api.rpc.chain.getHeader(blockHash),
api.queryAt(blockHash)
]).pipe(
switchMap(([header, queryAt]) =>
getAuthorDetails(header, queryAt)
api.rpc.chain.getHeader(blockHash).pipe(
switchMap((header) =>
getAuthorDetails(api, header, blockHash)
),
map(([header, validators, author]) =>
createHeaderExtended((validators || header).registry, header, validators, author)
@@ -5,7 +5,7 @@ import type { Observable } from 'rxjs';
import type { HeaderExtended } from '../type/types.js';
import type { DeriveApi } from '../types.js';
import { combineLatest, map, of, switchMap } from 'rxjs';
import { map, switchMap } from 'rxjs';
import { createHeaderExtended } from '../type/index.js';
import { memo } from '../util/index.js';
@@ -28,13 +28,7 @@ export function subscribeNewHeads (instanceId: string, api: DeriveApi): () => Ob
return memo(instanceId, (): Observable<HeaderExtended> =>
api.rpc.chain.subscribeNewHeads().pipe(
switchMap((header) =>
combineLatest([
of(header),
api.queryAt(header.hash)
])
),
switchMap(([header, queryAt]) =>
getAuthorDetails(header, queryAt)
getAuthorDetails(api, header)
),
map(([header, validators, author]): HeaderExtended => {
header.createdAtHash = header.hash;
+22 -2
View File
@@ -9,7 +9,7 @@ import type { SpCoreSr25519Public } from '@polkadot/types/lookup';
import type { Codec, IOption } from '@polkadot/types/types';
import type { DeriveApi } from '../types.js';
import { combineLatest, map, mergeMap, of } from 'rxjs';
import { combineLatest, map, mergeMap, of, switchMap } from 'rxjs';
import { memo, unwrapBlockNumber } from '../util/index.js';
@@ -24,7 +24,8 @@ export function createBlockNumberDerive <T extends { number: Compact<BlockNumber
);
}
export function getAuthorDetails (header: Header, queryAt: QueryableStorage<'rxjs'>): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
/** @internal */
function getAuthorDetailsWithAt (header: Header, queryAt: QueryableStorage<'rxjs'>): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
const validators = queryAt.session
? queryAt.session.validators()
: of(null);
@@ -72,3 +73,22 @@ export function getAuthorDetails (header: Header, queryAt: QueryableStorage<'rxj
of(null)
]);
}
export function getAuthorDetails (api: DeriveApi, header: Header, blockHash?: Uint8Array | string): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
// For on-chain state, we need to retrieve it as per the start
// of the block being constructed, i.e. session validators would
// be at the point of the block construction, not when all operations
// has been supplied.
//
// However for the first block (no parentHash available), we would
// just use the as-is
return api.queryAt(
header.parentHash.isEmpty
? blockHash || header.hash
: header.parentHash
).pipe(
switchMap((queryAt) =>
getAuthorDetailsWithAt(header, queryAt)
)
);
}