Skip to content

Commit 320135d

Browse files
authored
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
1 parent 074e790 commit 320135d

File tree

22 files changed

+200
-182
lines changed

22 files changed

+200
-182
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# CHANGELOG
22

3-
## master
3+
## 10.3.4 Apr 16, 2023
4+
5+
Changes:
6+
7+
- Ensure block author extract uses the correct as-at block construction state
8+
9+
10+
## 10.3.3 Apr 15, 2023
411

512
Contributed:
613

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"sideEffects": false,
1616
"type": "module",
17-
"version": "10.3.3",
17+
"version": "10.3.4",
1818
"versions": {
1919
"git": "10.3.3",
2020
"npm": "10.3.3"

packages/api-augment/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
"./detectPackage.cjs"
1919
],
2020
"type": "module",
21-
"version": "10.3.3",
21+
"version": "10.3.4",
2222
"main": "index.js",
2323
"dependencies": {
24-
"@polkadot/api-base": "10.3.3",
25-
"@polkadot/rpc-augment": "10.3.3",
26-
"@polkadot/types": "10.3.3",
27-
"@polkadot/types-augment": "10.3.3",
28-
"@polkadot/types-codec": "10.3.3",
24+
"@polkadot/api-base": "10.3.4",
25+
"@polkadot/rpc-augment": "10.3.4",
26+
"@polkadot/types": "10.3.4",
27+
"@polkadot/types-augment": "10.3.4",
28+
"@polkadot/types-codec": "10.3.4",
2929
"@polkadot/util": "^11.1.3",
3030
"tslib": "^2.5.0"
3131
}

packages/api-base/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
"./detectPackage.cjs"
1919
],
2020
"type": "module",
21-
"version": "10.3.3",
21+
"version": "10.3.4",
2222
"main": "index.js",
2323
"dependencies": {
24-
"@polkadot/rpc-core": "10.3.3",
25-
"@polkadot/types": "10.3.3",
24+
"@polkadot/rpc-core": "10.3.4",
25+
"@polkadot/types": "10.3.4",
2626
"@polkadot/util": "^11.1.3",
2727
"rxjs": "^7.8.0",
2828
"tslib": "^2.5.0"

packages/api-contract/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
"./detectPackage.cjs"
1919
],
2020
"type": "module",
21-
"version": "10.3.3",
21+
"version": "10.3.4",
2222
"main": "index.js",
2323
"dependencies": {
24-
"@polkadot/api": "10.3.3",
25-
"@polkadot/types": "10.3.3",
26-
"@polkadot/types-codec": "10.3.3",
27-
"@polkadot/types-create": "10.3.3",
24+
"@polkadot/api": "10.3.4",
25+
"@polkadot/types": "10.3.4",
26+
"@polkadot/types-codec": "10.3.4",
27+
"@polkadot/types-create": "10.3.4",
2828
"@polkadot/util": "^11.1.3",
2929
"@polkadot/util-crypto": "^11.1.3",
3030
"rxjs": "^7.8.0",
3131
"tslib": "^2.5.0"
3232
},
3333
"devDependencies": {
34-
"@polkadot/api-augment": "10.3.3",
34+
"@polkadot/api-augment": "10.3.4",
3535
"@polkadot/keyring": "^11.1.3"
3636
}
3737
}

packages/api-derive/package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818
"./detectPackage.cjs"
1919
],
2020
"type": "module",
21-
"version": "10.3.3",
21+
"version": "10.3.4",
2222
"main": "index.js",
2323
"dependencies": {
24-
"@polkadot/api": "10.3.3",
25-
"@polkadot/api-augment": "10.3.3",
26-
"@polkadot/api-base": "10.3.3",
27-
"@polkadot/rpc-core": "10.3.3",
28-
"@polkadot/types": "10.3.3",
29-
"@polkadot/types-codec": "10.3.3",
24+
"@polkadot/api": "10.3.4",
25+
"@polkadot/api-augment": "10.3.4",
26+
"@polkadot/api-base": "10.3.4",
27+
"@polkadot/rpc-core": "10.3.4",
28+
"@polkadot/types": "10.3.4",
29+
"@polkadot/types-codec": "10.3.4",
3030
"@polkadot/util": "^11.1.3",
3131
"@polkadot/util-crypto": "^11.1.3",
3232
"rxjs": "^7.8.0",
3333
"tslib": "^2.5.0"
3434
},
3535
"devDependencies": {
36-
"@polkadot/api": "10.3.3",
37-
"@polkadot/api-augment": "10.3.3",
38-
"@polkadot/rpc-augment": "10.3.3",
39-
"@polkadot/rpc-provider": "10.3.3",
40-
"@polkadot/types-support": "10.3.3"
36+
"@polkadot/api": "10.3.4",
37+
"@polkadot/api-augment": "10.3.4",
38+
"@polkadot/rpc-augment": "10.3.4",
39+
"@polkadot/rpc-provider": "10.3.4",
40+
"@polkadot/types-support": "10.3.4"
4141
}
4242
}

packages/api-derive/src/chain/getBlock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function getBlock (instanceId: string, api: DeriveApi): (hash: Uint8Array
3434
combineLatest([
3535
of(signedBlock),
3636
queryAt.system.events(),
37-
getAuthorDetails(signedBlock.block.header, queryAt)
37+
getAuthorDetails(api, signedBlock.block.header, blockHash)
3838
])
3939
),
4040
map(([signedBlock, events, [, validators, author]]) =>

packages/api-derive/src/chain/getHeader.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Observable } from 'rxjs';
55
import type { HeaderExtended } from '../type/types.js';
66
import type { DeriveApi } from '../types.js';
77

8-
import { combineLatest, map, switchMap } from 'rxjs';
8+
import { map, switchMap } from 'rxjs';
99

1010
import { createHeaderExtended } from '../type/index.js';
1111
import { memo } from '../util/index.js';
@@ -27,12 +27,9 @@ import { getAuthorDetails } from './util.js';
2727
*/
2828
export function getHeader (instanceId: string, api: DeriveApi): (blockHash: Uint8Array | string) => Observable<HeaderExtended> {
2929
return memo(instanceId, (blockHash: Uint8Array | string): Observable<HeaderExtended> =>
30-
combineLatest([
31-
api.rpc.chain.getHeader(blockHash),
32-
api.queryAt(blockHash)
33-
]).pipe(
34-
switchMap(([header, queryAt]) =>
35-
getAuthorDetails(header, queryAt)
30+
api.rpc.chain.getHeader(blockHash).pipe(
31+
switchMap((header) =>
32+
getAuthorDetails(api, header, blockHash)
3633
),
3734
map(([header, validators, author]) =>
3835
createHeaderExtended((validators || header).registry, header, validators, author)

packages/api-derive/src/chain/subscribeNewHeads.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Observable } from 'rxjs';
55
import type { HeaderExtended } from '../type/types.js';
66
import type { DeriveApi } from '../types.js';
77

8-
import { combineLatest, map, of, switchMap } from 'rxjs';
8+
import { map, switchMap } from 'rxjs';
99

1010
import { createHeaderExtended } from '../type/index.js';
1111
import { memo } from '../util/index.js';
@@ -28,13 +28,7 @@ export function subscribeNewHeads (instanceId: string, api: DeriveApi): () => Ob
2828
return memo(instanceId, (): Observable<HeaderExtended> =>
2929
api.rpc.chain.subscribeNewHeads().pipe(
3030
switchMap((header) =>
31-
combineLatest([
32-
of(header),
33-
api.queryAt(header.hash)
34-
])
35-
),
36-
switchMap(([header, queryAt]) =>
37-
getAuthorDetails(header, queryAt)
31+
getAuthorDetails(api, header)
3832
),
3933
map(([header, validators, author]): HeaderExtended => {
4034
header.createdAtHash = header.hash;

packages/api-derive/src/chain/util.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { SpCoreSr25519Public } from '@polkadot/types/lookup';
99
import type { Codec, IOption } from '@polkadot/types/types';
1010
import type { DeriveApi } from '../types.js';
1111

12-
import { combineLatest, map, mergeMap, of } from 'rxjs';
12+
import { combineLatest, map, mergeMap, of, switchMap } from 'rxjs';
1313

1414
import { memo, unwrapBlockNumber } from '../util/index.js';
1515

@@ -24,7 +24,8 @@ export function createBlockNumberDerive <T extends { number: Compact<BlockNumber
2424
);
2525
}
2626

27-
export function getAuthorDetails (header: Header, queryAt: QueryableStorage<'rxjs'>): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
27+
/** @internal */
28+
function getAuthorDetailsWithAt (header: Header, queryAt: QueryableStorage<'rxjs'>): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
2829
const validators = queryAt.session
2930
? queryAt.session.validators()
3031
: of(null);
@@ -72,3 +73,22 @@ export function getAuthorDetails (header: Header, queryAt: QueryableStorage<'rxj
7273
of(null)
7374
]);
7475
}
76+
77+
export function getAuthorDetails (api: DeriveApi, header: Header, blockHash?: Uint8Array | string): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
78+
// For on-chain state, we need to retrieve it as per the start
79+
// of the block being constructed, i.e. session validators would
80+
// be at the point of the block construction, not when all operations
81+
// has been supplied.
82+
//
83+
// However for the first block (no parentHash available), we would
84+
// just use the as-is
85+
return api.queryAt(
86+
header.parentHash.isEmpty
87+
? blockHash || header.hash
88+
: header.parentHash
89+
).pipe(
90+
switchMap((queryAt) =>
91+
getAuthorDetailsWithAt(header, queryAt)
92+
)
93+
);
94+
}

0 commit comments

Comments
 (0)