From 006c686c1860c078a22c83061d7d493da9608d8b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 17 Dec 2019 15:33:45 +0100 Subject: [PATCH] Fix linked maps with optional entries (#1676) * Fix linked maps with optional entries * Revert entries() change for linked maps --- package.json | 2 +- packages/api/src/base/Decorate.ts | 20 +++++++++++++------- yarn.lock | 8 ++++---- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 9bfd24dc88..cdaec11243 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "@babel/core": "^7.7.5", "@babel/register": "^7.7.4", "@babel/runtime": "^7.7.6", - "@polkadot/dev": "^0.32.0-beta.19", + "@polkadot/dev": "^0.32.13", "@polkadot/ts": "^0.1.88" } } diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 5a0c1301f1..e7c876eeaa 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -5,7 +5,7 @@ import { Constants, Storage } from '@polkadot/metadata/Decorated/types'; import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types'; import { Call, Hash, RuntimeVersion } from '@polkadot/types/interfaces'; -import { AnyFunction, CallFunction, Codec, CodecArg as Arg, ModulesWithCalls, Registry } from '@polkadot/types/types'; +import { AnyFunction, CallFunction, Codec, CodecArg as Arg, ITuple, ModulesWithCalls, Registry } from '@polkadot/types/types'; import { SubmittableExtrinsic } from '../submittable/types'; import { ApiInterfaceRx, ApiOptions, ApiTypes, @@ -20,7 +20,7 @@ import { map, switchMap } from 'rxjs/operators'; import decorateDerive, { ExactDerive } from '@polkadot/api-derive'; import RpcCore from '@polkadot/rpc-core'; import { WsProvider } from '@polkadot/rpc-provider'; -import { Metadata, Null, TypeRegistry, u64, Vec } from '@polkadot/types'; +import { Metadata, Null, Option, TypeRegistry, u64, Vec } from '@polkadot/types'; import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage'; import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/primitive/Extrinsic/constants'; import StorageKey, { StorageEntry } from '@polkadot/types/primitive/StorageKey'; @@ -38,6 +38,8 @@ interface MetaDecoration { toJSON: () => any; } +type LinkageData = ITuple<[Codec, Linkage]>; + export default abstract class Decorate extends Events { public readonly registry: Registry; @@ -298,7 +300,7 @@ export default abstract class Decorate extends Events } private decorateStorageLinked (creator: StorageEntry, decorateMethod: DecorateMethod): ReturnType> { - const result: Map]> = new Map(); + const result: Map]> | null> = new Map(); let subject: BehaviorSubject; let head: Codec | null = null; @@ -306,13 +308,17 @@ export default abstract class Decorate extends Events // entries can be re-linked in the middle of a list, we subscribe here to make // sure we catch any updates, no matter the list position const getNext = (key: Codec): Observable => - this._rpcCore.state.subscribeStorage<[[Codec, Linkage]]>([[creator, key]]).pipe( - switchMap(([data]: [[Codec, Linkage]]): Observable => { + this._rpcCore.state.subscribeStorage<[LinkageData | Option]>([[creator, key]]).pipe( + switchMap(([_data]: [LinkageData | Option]): Observable => { + const data = creator.meta.modifier.isOptional + ? (_data as Option).unwrapOr(null) + : _data as LinkageData; + result.set(key, data); // iterate from this key to the linkages, constructing entries for all // those found and available - if (data[1].next.isSome) { + if (data && data[1].next.isSome) { return getNext(data[1].next.unwrap()); } @@ -334,7 +340,7 @@ export default abstract class Decorate extends Events keys.push(nextKey); vals.push(item); - nextKey = linkage.next && linkage.next.unwrapOr(null); + nextKey = linkage.next?.unwrapOr(null); } const nextResult = vals.length diff --git a/yarn.lock b/yarn.lock index e4e8149b40..c70add6bfe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1832,10 +1832,10 @@ dependencies: "@types/node" ">= 8" -"@polkadot/dev@^0.32.0-beta.19": - version "0.32.0-beta.19" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.32.0-beta.19.tgz#9f4e9a78a559f4e050842a6a06365d16d7003188" - integrity sha512-Ozftw1CLtu/K4c8039/b2qBvvrLDSA8GsZi9MdwN0t17AuGmkWruiZ8apd3M4Cqfc7q7NbiwGfOvCdk/7tpTfw== +"@polkadot/dev@^0.32.13": + version "0.32.13" + resolved "https://registry.npmjs.org/@polkadot/dev/-/dev-0.32.13.tgz#b6f60f48539bd533e84cec7949d340fb66b6f183" + integrity sha512-huHQmHiPxi735lVdnjrNggVnX6FpGz2sikoQzw80HwMN90/nX3QiQeE44CQA4IhHMxQtFAQWfKBDfExFTHJjVQ== dependencies: "@babel/cli" "^7.7.5" "@babel/core" "^7.7.5"