Adjust democracy preimages retrieval (#5413)

* Adjust democracy preimages retrieval

* Remove log
This commit is contained in:
Jaco
2023-01-13 09:41:52 +02:00
committed by GitHub
parent 73b8dc3aff
commit 6ff3577853
2 changed files with 14 additions and 7 deletions
+12 -5
View File
@@ -76,7 +76,9 @@ function parseImage (api: DeriveApi, [proposalHash, status, bytes]: [HexString,
return { at: BN_ZERO, balance, proposal, proposalHash, proposer };
}
function getDemocracyImages (api: DeriveApi, hashes: (Hash | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> {
function getDemocracyImages (api: DeriveApi, bounded: (Hash | Uint8Array | string | FrameSupportPreimagesBounded)[]): Observable<(DeriveProposalImage | undefined)[]> {
const hashes = bounded.map((b) => getImageHashBounded(b));
return api.query.democracy.preimages.multi<Option<PreimageStatus>>(hashes).pipe(
map((images): (DeriveProposalImage | undefined)[] =>
images.map((imageOpt) => parseDemocracy(api, imageOpt))
@@ -86,6 +88,7 @@ function getDemocracyImages (api: DeriveApi, hashes: (Hash | Uint8Array | string
function getImages (api: DeriveApi, bounded: (FrameSupportPreimagesBounded | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> {
const hashes = bounded.map((b) => getImageHashBounded(b));
const bytesType = api.registry.lookup.getTypeDef(api.query.preimage.preimageFor.creator.meta.type.asMap.key).type;
return api.query.preimage.statusFor.multi(hashes).pipe(
switchMap((optStatus) => {
@@ -93,9 +96,13 @@ function getImages (api: DeriveApi, bounded: (FrameSupportPreimagesBounded | Uin
const keys = statuses
.map((s, i) =>
s
? s.isRequested
? [hashes[i], s.asRequested.len.unwrapOr(0)]
: [hashes[i], s.asUnrequested.len]
? bytesType === 'H256'
// first generation
? hashes[i]
// current generation (H256,u32)
: s.isRequested
? [hashes[i], s.asRequested.len.unwrapOr(0)]
: [hashes[i], s.asUnrequested.len]
: null
)
.filter((p) => !!p);
@@ -121,7 +128,7 @@ export function preimages (instanceId: string, api: DeriveApi): (hashes: (Hash |
return memo(instanceId, (hashes: (Hash | Uint8Array | string | FrameSupportPreimagesBounded)[]): Observable<(DeriveProposalImage | undefined)[]> =>
hashes.length
? isFunction(api.query.democracy.preimages)
? getDemocracyImages(api, hashes as string[])
? getDemocracyImages(api, hashes)
: isFunction(api.query.preimage.preimageFor)
? getImages(api, hashes)
: of([])
@@ -51,14 +51,14 @@ function parse ([proposals, images, optDepositors]: Result): DeriveProposal[] {
export function proposals (instanceId: string, api: DeriveApi): () => Observable<DeriveProposal[]> {
return memo(instanceId, (): Observable<DeriveProposal[]> =>
isFunction(api.query.democracy?.publicProps) && isFunction(api.query.democracy?.preimages)
isFunction(api.query.democracy?.publicProps)
? api.query.democracy.publicProps().pipe(
switchMap((proposals) =>
proposals.length
? combineLatest([
of(proposals),
api.derive.democracy.preimages(
proposals.map(([, hash]) => hash as unknown as Uint8Array)
proposals.map(([, hash]) => hash)
),
api.query.democracy.depositOf.multi(
proposals.map(([index]) => index)