Add $isStorageFallback on Codec interface (#5421)

* Add $isStorageFallback on Codec interface

* Rename in CHANGELOG

* Update specs with new naming

* Small formatting adjustments

* Abstract values

* Adjust initialU8a expectation

* Mark old interface as deprecated (& type to never)

* Adjust @deprecated comments

* Add deprecated interfaces to all
This commit is contained in:
Jaco
2023-01-18 21:58:06 +02:00
committed by GitHub
parent 263fce1e7d
commit 923f569d77
42 changed files with 261 additions and 156 deletions
@@ -17,7 +17,7 @@ export function subscribeFinalizedBlocks (instanceId: string, api: DeriveApi): (
return memo(instanceId, (): Observable<SignedBlockExtended> =>
api.derive.chain.subscribeFinalizedHeads().pipe(
switchMap((header) =>
api.derive.chain.getBlock(header.createdAtHash || header.hash)
api.derive.chain.getBlock(header.$createdAtHash || header.hash)
)
)
);
@@ -41,7 +41,7 @@ export function subscribeFinalizedHeads (instanceId: string, api: DeriveApi): ()
const endHash = prevHash;
const startHash = header.parentHash;
prevHash = header.createdAtHash = header.hash;
prevHash = header.$createdAtHash = header.hash;
return endHash === null || startHash.eq(endHash)
? of(header)
@@ -17,7 +17,7 @@ export function subscribeNewBlocks (instanceId: string, api: DeriveApi): () => O
return memo(instanceId, (): Observable<SignedBlockExtended> =>
api.derive.chain.subscribeNewHeads().pipe(
switchMap((header) =>
api.derive.chain.getBlock(header.createdAtHash || header.hash)
api.derive.chain.getBlock(header.$createdAtHash || header.hash)
)
)
);
@@ -37,7 +37,7 @@ export function subscribeNewHeads (instanceId: string, api: DeriveApi): () => Ob
getAuthorDetails(header, queryAt)
),
map(([header, validators, author]): HeaderExtended => {
header.createdAtHash = header.hash;
header.$createdAtHash = header.hash;
return createHeaderExtended(header.registry, header, validators, author);
})
@@ -33,7 +33,7 @@ function _getUpdates (api: DeriveApi, paraId: string | number | BN): Observable<
added = added.concat(...changes.added);
removed = removed.concat(...changes.removed);
return of({ added, addedDelta: changes.added, blockHash: events.createdAtHash?.toHex() || '-', removed, removedDelta: changes.removed });
return of({ added, addedDelta: changes.added, blockHash: events.$createdAtHash?.toHex() || '-', removed, removedDelta: changes.removed });
}
return EMPTY;
@@ -52,7 +52,7 @@ function _eventTriggerAll (api: DeriveApi, paraId: string | number | BN): Observ
);
return items.length
? of(events.createdAtHash?.toHex() || '-')
? of(events.$createdAtHash?.toHex() || '-')
: EMPTY;
}),
startWith('-')
+1 -1
View File
@@ -29,5 +29,5 @@ export function extractContributed (paraId: string | number | BN, events: Vec<Fr
}
return result;
}, { added, blockHash: events.createdAtHash?.toHex() || '-', removed });
}, { added, blockHash: events.$createdAtHash?.toHex() || '-', removed });
}
@@ -18,7 +18,7 @@ export function createHeaderExtended (registry: Registry, header?: Header, valid
super(registry, header);
this.#author = author || extractAuthor(this.digest, validators || []);
this.createdAtHash = header?.createdAtHash;
this.$createdAtHash = header?.$createdAtHash;
}
/**
@@ -46,7 +46,7 @@ export function createSignedBlockExtended (registry: Registry, block?: SignedBlo
this.#author = author || extractAuthor(this.block.header.digest, validators || []);
this.#events = events || ([] as EventRecord[]);
this.#extrinsics = mapExtrinsics(this.block.extrinsics, this.#events);
this.createdAtHash = block?.createdAtHash;
this.$createdAtHash = block?.$createdAtHash;
}
/**
+1 -1
View File
@@ -483,7 +483,7 @@ export class RpcCore {
: meta.modifier.isOptional
? registry.createTypeUnsafe(type, [input], { blockHash, isPedantic: true })
: input
], { blockHash, isOptional: meta.modifier.isOptional, isPedantic: !meta.modifier.isOptional });
], { blockHash, isFallback: isEmpty && !!meta.fallback, isOptional: meta.modifier.isOptional, isPedantic: !meta.modifier.isOptional });
} catch (error) {
throw new Error(`Unable to decode storage ${key.section || 'unknown'}.${key.method || 'unknown'}:${entryNum}: ${(error as Error).message}`);
}
+2 -1
View File
@@ -20,7 +20,8 @@ class LRUNode {
// https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
export class LRUCache {
readonly capacity: number;
public readonly capacity: number;
readonly #data: Map<string, unknown> = new Map();
readonly #refs: Map<string, LRUNode> = new Map();
+8 -1
View File
@@ -16,9 +16,16 @@ import { compareArray } from '../utils/compareArray';
* @noInheritDoc
*/
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
/**
* @description This ensures that operators such as clice, filter, map, etc. return
+9 -4
View File
@@ -9,17 +9,22 @@ import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types
* @description A type extends the Base class, when it holds a value
*/
export abstract class AbstractBase<T extends Codec> implements Codec {
public createdAtHash?: IU8a;
public readonly initialU8aLength?: number;
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #raw: T;
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
this.$initialU8aLength = initialU8aLength;
this.#raw = value;
this.initialU8aLength = initialU8aLength;
this.registry = registry;
}
+10 -4
View File
@@ -72,14 +72,19 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
* @noInheritDoc
*/
export abstract class AbstractInt extends BN implements INumber {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public readonly encodedLength: number;
public readonly isUnsigned: boolean;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #bitLength: UIntBitLength;
constructor (registry: Registry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
@@ -98,6 +103,7 @@ export abstract class AbstractInt extends BN implements INumber {
this.registry = registry;
this.#bitLength = bitLength;
this.encodedLength = this.#bitLength / 8;
this.$initialU8aLength = this.#bitLength / 8;
this.isUnsigned = !isSigned;
const isNegative = this.isNeg();
+9 -5
View File
@@ -47,14 +47,18 @@ function decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<
* a number and making the compact representation thereof
*/
export class Compact<T extends INumber> implements ICompact<T> {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #Type: CodecClass<T>;
readonly #raw: T;
constructor (registry: Registry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
@@ -63,7 +67,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
const [raw, decodedLength] = decodeCompact<T>(registry, this.#Type, value);
this.initialU8aLength = decodedLength;
this.$initialU8aLength = decodedLength;
this.#raw = raw;
}
@@ -10,9 +10,15 @@ import type { AnyJson, Codec, CodecClass, Inspect, IU8a, Registry } from '../typ
* An unknown type that fails on construction with the type info
*/
export class DoNotConstruct implements Codec {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $isStorageFallback?: boolean;
#neverError: Error;
+10 -10
View File
@@ -183,22 +183,22 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?:
* an extension to enum where the value type is determined by the actual index.
*/
export class Enum implements IEnum {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #def: TypesDef;
readonly #entryIndex: number;
readonly initialU8aLength?: number;
readonly #indexes: number[];
readonly #isBasic: boolean;
readonly #isIndexed: boolean;
readonly #raw: Codec;
constructor (registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number, { definition, setDefinition = noopSetDefinition }: Options = {}) {
@@ -217,8 +217,8 @@ export class Enum implements IEnum {
this.#entryIndex = this.#indexes.indexOf(decoded.index);
this.#raw = decoded.value;
if (this.#raw.initialU8aLength) {
this.initialU8aLength = 1 + this.#raw.initialU8aLength;
if (this.#raw.$initialU8aLength) {
this.$initialU8aLength = 1 + this.#raw.$initialU8aLength;
}
}
+8 -6
View File
@@ -12,16 +12,18 @@ import { isNull } from '@polkadot/util';
* Implements a type that does not contain anything (apart from `null`)
*/
export class Null implements Codec {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly encodedLength = 0;
public readonly isEmpty = true;
public readonly registry: Registry;
public createdAtHash?: IU8a;
// Added for compatibility reasons, e.g. see Option
public readonly initialU8aLength = 0;
public $createdAtHash?: IU8a;
public $initialU8aLength = 0;
public $isStorageFallback?: boolean;
constructor (registry: Registry) {
this.registry = registry;
+10 -6
View File
@@ -66,14 +66,18 @@ function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): C
* with a value if/as required/found.
*/
export class Option<T extends Codec> implements IOption<T> {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #Type: CodecClass<T>;
readonly #raw: T;
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
@@ -88,8 +92,8 @@ export class Option<T extends Codec> implements IOption<T> {
this.#Type = Type;
this.#raw = decoded as T;
if (decoded && decoded.initialU8aLength) {
this.initialU8aLength = 1 + decoded.initialU8aLength;
if (decoded?.$initialU8aLength) {
this.$initialU8aLength = 1 + decoded.$initialU8aLength;
}
}
+1 -3
View File
@@ -55,8 +55,6 @@ function decodeTuple (registry: Registry, result: Codec[], value: Exclude<AnyTup
* own type. It extends the base JS `Array` object.
*/
export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
readonly initialU8aLength?: number;
#Types: Definition;
constructor (registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition = noopSetDefinition }: Options = {}) {
@@ -70,7 +68,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
super(registry, Classes[0].length);
this.initialU8aLength = (
this.$initialU8aLength = (
isU8a(value)
? decodeU8a(registry, this, value, Classes)
: decodeTuple(registry, this, value, Classes)
+1 -3
View File
@@ -71,8 +71,6 @@ export function decodeVec<T extends Codec> (registry: Registry, result: T[], val
* specific encoding/decoding on top of the base type.
*/
export class Vec<T extends Codec> extends AbstractArray<T> {
readonly initialU8aLength?: number;
#Type: CodecClass<T>;
constructor (registry: Registry, Type: CodecClass<T> | string, value: Uint8Array | HexString | unknown[] = [], { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
@@ -82,7 +80,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
this.initialU8aLength = (
this.$initialU8aLength = (
isU8a(decodeFrom)
? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type)
: decodeVec(registry, this, decodeFrom, startAt, this.#Type)
+1 -3
View File
@@ -25,8 +25,6 @@ function noopSetDefinition <T extends Codec> (d: CodecClass<T>): CodecClass<T> {
* This manages codec arrays of a fixed length
*/
export class VecFixed<T extends Codec> extends AbstractArray<T> {
readonly initialU8aLength?: number;
#Type: CodecClass<T>;
constructor (registry: Registry, Type: CodecClass<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[], { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
@@ -34,7 +32,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
this.initialU8aLength = (
this.$initialU8aLength = (
isU8a(value)
? decodeU8aVec(registry, this, value, 0, this.#Type)
: decodeVec(registry, this, value, 0, this.#Type)
@@ -141,7 +141,7 @@ describe('BTreeSet', (): void => {
expect(none.toHex()).toEqual('0x00');
expect(none.encodedLength).toEqual(1);
expect(
(new (BTreeSet.with(U32))(registry, none.toHex())).initialU8aLength
(new (BTreeSet.with(U32))(registry, none.toHex())).$initialU8aLength
).toEqual(none.encodedLength);
});
@@ -152,7 +152,7 @@ describe('BTreeSet', (): void => {
expect(some.toHex()).toEqual('0x080100000002000000');
expect(some.encodedLength).toEqual(1 + (4 * 2));
expect(
(new (BTreeSet.with(U32))(registry, some.toHex())).initialU8aLength
(new (BTreeSet.with(U32))(registry, some.toHex())).$initialU8aLength
).toEqual(some.encodedLength);
});
});
@@ -70,11 +70,16 @@ function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V>
}
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #ValClass: CodecClass<V>;
@@ -84,7 +89,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
super(sortSet(values));
this.registry = registry;
this.initialU8aLength = decodedLength;
this.$initialU8aLength = decodedLength;
this.#ValClass = ValClass;
}
@@ -83,7 +83,7 @@ describe('CodecMap', (): void => {
expect(none.toHex()).toEqual('0x00');
expect(none.encodedLength).toEqual(1);
expect(
new CodecMap(registry, Text, Text, none.toHex()).initialU8aLength
new CodecMap(registry, Text, Text, none.toHex()).$initialU8aLength
).toEqual(none.encodedLength);
});
@@ -97,7 +97,7 @@ describe('CodecMap', (): void => {
expect(some.toHex()).toEqual('0x0804310c666f6f04320c626172');
expect(some.encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2));
expect(
new CodecMap(registry, Text, Text, some.toHex()).initialU8aLength
new CodecMap(registry, Text, Text, some.toHex()).$initialU8aLength
).toEqual(some.encodedLength);
});
});
+9 -6
View File
@@ -94,16 +94,19 @@ function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyTyp
}
export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements IMap<K, V> {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #KeyClass: CodecClass<K>;
readonly #ValClass: CodecClass<V>;
readonly initialU8aLength?: number;
readonly #type: string;
constructor (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
@@ -112,7 +115,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
this.registry = registry;
this.initialU8aLength = decodedLength;
this.$initialU8aLength = decodedLength;
this.#KeyClass = KeyClass;
this.#ValClass = ValClass;
this.#type = type;
@@ -33,7 +33,7 @@ export class OptionBool extends Option<Bool> {
: value
);
this.initialU8aLength = 1;
this.$initialU8aLength = 1;
}
/**
@@ -39,9 +39,7 @@ function decodeRaw<T extends Codec> (registry: Registry, typeName: CodecClass<T>
export class WrapperKeepOpaque<T extends Codec> extends Bytes {
readonly #Type: CodecClass<T>;
readonly #decoded: T | null;
readonly #opaqueName: OpaqueName;
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { opaqueName = 'WrapperKeepOpaque' }: Options = {}) {
+8 -1
View File
@@ -13,9 +13,16 @@ import { isU8a, u8aToHex } from '@polkadot/util';
* @noInheritDoc
*/
export class bool extends Boolean implements Codec {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $initialU8aLength = 1;
public $isStorageFallback?: boolean;
constructor (registry: Registry, value: bool | AnyBool | Uint8Array | number = false) {
super(
+8 -1
View File
@@ -34,9 +34,16 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
* @noInheritDoc
*/
export class CodecDate extends Date implements INumber {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $initialU8aLength = BITLENGTH / 8;
public $isStorageFallback?: boolean;
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
super(decodeDate(value));
+13 -9
View File
@@ -19,16 +19,20 @@ interface Options {
* in some eth_* RPCs
*/
export class Float extends Number implements IFloat {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly encodedLength: number;
public readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #bitLength: 32 | 64;
public createdAtHash?: IU8a;
readonly encodedLength: number;
readonly initialU8aLength?: number;
readonly registry: Registry;
constructor (registry: Registry, value?: AnyFloat, { bitLength = 32 }: Options = {}) {
super(
isU8a(value) || isHex(value)
@@ -40,7 +44,7 @@ export class Float extends Number implements IFloat {
this.#bitLength = bitLength;
this.encodedLength = bitLength / 8;
this.initialU8aLength = this.encodedLength;
this.$initialU8aLength = this.encodedLength;
this.registry = registry;
}
+8 -1
View File
@@ -22,9 +22,16 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
* @noInheritDoc
*/
export class Json extends Map<string, any> implements Codec {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
constructor (registry: Registry, value?: Record<string, unknown> | null) {
const decoded = decodeJson(value);
+9 -4
View File
@@ -16,12 +16,17 @@ import { isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@
* @noInheritDoc
*/
export class Raw extends Uint8Array implements IU8a {
public createdAtHash?: IU8a;
public readonly initialU8aLength?: number;
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
/**
* @description This ensures that operators such as clice, filter, map, etc. return
* new Array instances (without this we need to apply overrides)
@@ -34,7 +39,7 @@ export class Raw extends Uint8Array implements IU8a {
super(u8aToU8a(value));
this.registry = registry;
this.initialU8aLength = initialU8aLength;
this.$initialU8aLength = initialU8aLength;
}
/**
+8 -1
View File
@@ -92,9 +92,16 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
* a bitwise representation of the values.
*/
export class CodecSet extends Set<string> implements ISet<string> {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public createdAtHash?: IU8a;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #allowed: SetValues;
+9 -5
View File
@@ -103,14 +103,18 @@ export class Struct<
V extends { [K in keyof S]: any } = { [K in keyof S]: any },
// type names, mapped by key, name of Class in S
E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
public createdAtHash?: IU8a;
public readonly initialU8aLength?: number;
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
readonly #jsonMap: Map<keyof S, string>;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly #jsonMap: Map<keyof S, string>;
readonly #Types: Definition;
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>(), { definition, setDefinition = noopSetDefinition }: Options = {}) {
@@ -123,7 +127,7 @@ export class Struct<
super(decoded);
this.initialU8aLength = decodedLength;
this.$initialU8aLength = decodedLength;
this.registry = registry;
this.#jsonMap = jsonMap;
this.#Types = typeMap;
+9 -4
View File
@@ -49,12 +49,17 @@ function decodeText (value?: null | AnyString | AnyU8a | { toString: () => strin
* @noInheritDoc
*/
export class Text extends String implements IText {
public createdAtHash?: IU8a;
public readonly initialU8aLength?: number;
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
public initialU8aLength?: never;
public readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
#override: string | null = null;
constructor (registry: Registry, value?: null | AnyString | AnyU8a | { toString: () => string }) {
@@ -63,7 +68,7 @@ export class Text extends String implements IText {
super(str);
this.registry = registry;
this.initialU8aLength = decodedLength;
this.$initialU8aLength = decodedLength;
}
/**
+36 -13
View File
@@ -17,11 +17,44 @@ export interface Inspect {
/**
* @name Codec
* @description
* The base Codec interface. All types implement the interface provided here. Additionally
* implementors can add their own specific interfaces and helpers with getters and functions.
* The Codec Base is however required for operating as an encoding/decoding layer
* The base Codec interface. All types implement the interface provided here.
* Additionally implementors can add their own specific interfaces and helpers
* with getters and functions. The Codec Base is however required for operating
* as an encoding/decoding layer
*/
export interface Codec {
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
createdAtHash?: never;
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
initialU8aLength?: never;
/**
* @description
* The block at which this value was retrieved/created (set to non-empty when
* retrieved from storage)
*/
$createdAtHash?: IU8a;
/**
* @description
* The length of the initial encoded value (Only available when the value was
* constructed from a Uint8Array input)
*/
$initialU8aLength?: number;
/**
* @description
* (internal usage) Indicates that the value was created via a fallback. This
* is used when with data specified in the metadata when the storage entry is
* empty.
*
* With metadata fallback values (available as defaults on most storage entries)
* any empty storage item should erturn the default. (This is the same as the
* implementation on the Substrate runtime)
*/
$isStorageFallback?: boolean;
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -32,11 +65,6 @@ export interface Codec {
*/
readonly hash: IU8a;
/**
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
*/
readonly initialU8aLength?: number;
/**
* @description Checks if the value is an empty value
*/
@@ -47,11 +75,6 @@ export interface Codec {
*/
readonly registry: Registry;
/**
* @description The block at which this value was retrieved/created (set to non-empty when retrieved from storage)
*/
createdAtHash?: IU8a;
/**
* @description Compares the value of the input to see if there is a match
*/
+2 -6
View File
@@ -32,6 +32,7 @@ export type RegistryTypes =
export interface CodecCreateOptions {
blockHash?: Uint8Array | string | null;
isFallback?: boolean;
isOptional?: boolean;
isPedantic?: boolean;
}
@@ -51,13 +52,8 @@ export interface Registry {
readonly chainDecimals: number[];
readonly chainSS58: number | undefined;
readonly chainTokens: string[];
// readonly knownTypes: RegisteredTypes;
// readonly lookup: PortableRegistry;
// readonly metadata: MetadataLatest;
// readonly unknownTypes: string[];
// readonly signedExtensions: string[];
createdAtHash?: IU8a;
$createdAtHash?: IU8a;
findMetaCall (callIndex: Uint8Array): CallFunction;
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN } | { error: BN | Uint8Array, index: BN }): RegistryError;
+3 -3
View File
@@ -41,7 +41,7 @@ export function decodeU8a <T extends Codec = Codec> (registry: Registry, result:
while (i < count) {
const value = new Types[i](registry, u8a.subarray(offset));
offset += value.initialU8aLength || value.encodedLength;
offset += value.$initialU8aLength || value.encodedLength;
result[i] = value;
i++;
}
@@ -67,7 +67,7 @@ export function decodeU8aStruct (registry: Registry, result: [string, Codec][],
while (i < count) {
const value = new Types[i](registry, u8a.subarray(offset));
offset += value.initialU8aLength || value.encodedLength;
offset += value.$initialU8aLength || value.encodedLength;
result[i] = [keys[i], value];
i++;
}
@@ -93,7 +93,7 @@ export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, resu
while (i < count) {
const value = new Type(registry, u8a.subarray(offset));
offset += value.initialU8aLength || value.encodedLength;
offset += value.$initialU8aLength || value.encodedLength;
result[i] = value;
i++;
}
+11 -3
View File
@@ -44,13 +44,21 @@ function checkPedantic (created: Codec, [value]: unknown[]): void {
// Initializes a type with a value. This also checks for fallbacks and in the cases
// where isPedantic is specified (storage decoding), also check the format/structure
function initType<T extends Codec> (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isOptional, isPedantic }: CreateOptions = {}): T {
const created = new (isOptional ? Option.with(Type) : Type)(registry, ...params);
function initType<T extends Codec> (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isFallback, isOptional, isPedantic }: CreateOptions = {}): T {
const created = new (
isOptional
? Option.with(Type)
: Type
)(registry, ...params);
isPedantic && checkPedantic(created, params);
if (blockHash) {
created.createdAtHash = createTypeUnsafe<IU8a>(registry, 'Hash', [blockHash]);
created.$createdAtHash = createTypeUnsafe<IU8a>(registry, 'BlockHash', [blockHash]);
}
if (isFallback) {
created.$isStorageFallback = true;
}
return created as T;
+18 -31
View File
@@ -166,43 +166,30 @@ function extractProperties (registry: TypeRegistry, metadata: Metadata): ChainPr
}
export class TypeRegistry implements Registry {
#classes = new Map<string, CodecClass>();
#definitions = new Map<string, string>();
#firstCallIndex: Uint8Array | null = null;
#lookup?: PortableRegistry;
#metadata?: MetadataLatest;
#metadataVersion = 0;
readonly #metadataCalls: Record<string, Record<string, CallFunction>> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError>> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
readonly #moduleMap: Record<string, string[]> = {};
#unknownTypes = new Map<string, boolean>();
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
public createdAtHash?: never;
#chainProperties?: ChainProperties;
#classes = new Map<string, CodecClass>();
#definitions = new Map<string, string>();
#firstCallIndex: Uint8Array | null = null;
#hasher: (data: Uint8Array) => Uint8Array = blake2AsU8a;
readonly #knownDefaults: Record<string, CodecClass>;
readonly #knownDefinitions: Record<string, Definitions>;
#knownTypes: RegisteredTypes = {};
#lookup?: PortableRegistry;
#metadata?: MetadataLatest;
#metadataVersion = 0;
#signedExtensions: string[] = fallbackExtensions;
#unknownTypes = new Map<string, boolean>();
#userExtensions?: ExtDef;
public createdAtHash?: Hash;
readonly #knownDefaults: Record<string, CodecClass>;
readonly #knownDefinitions: Record<string, Definitions>;
readonly #metadataCalls: Record<string, Record<string, CallFunction>> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError>> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
readonly #moduleMap: Record<string, string[]> = {};
public $createdAtHash?: Hash;
constructor (createdAtHash?: Hash | Uint8Array | string) {
this.#knownDefaults = objectSpread({ Json, Metadata, PortableRegistry, Raw }, baseTypes);
@@ -215,7 +202,7 @@ export class TypeRegistry implements Registry {
}
if (createdAtHash) {
this.createdAtHash = this.createType('Hash', createdAtHash);
this.$createdAtHash = this.createType('BlockHash', createdAtHash);
}
}
@@ -23,6 +23,7 @@ describe('ExtrinsicPayload', (): void => {
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
const b = new ExtrinsicPayload(registry, a.toU8a(), { version: 4 });
expect(a.inspect()).toEqual(b.inspect());
expect(a.toJSON()).toEqual(b.toJSON());
});
@@ -30,6 +31,7 @@ describe('ExtrinsicPayload', (): void => {
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
const b = new ExtrinsicPayload(registry, a.toHex(), { version: 4 });
expect(a.inspect()).toEqual(b.inspect());
expect(a.toJSON()).toEqual(b.toJSON());
});