Fix sorting circular dependencies (#3818)

This commit is contained in:
Jaco
2021-07-28 07:22:42 +02:00
committed by GitHub
parent ca0969b1eb
commit 77c45cf4c0
4 changed files with 30 additions and 17 deletions
+9
View File
@@ -1,5 +1,14 @@
# CHANGELOG
## master
Changes:
- Fix circular dependencies in codec utils
- Fix `api.at(<blockHash>)` to correctly decorate with the storage hash
- Adjustments for upcoming metadata v14 in type creation & `PortableRegistry`
## 5.2.1 Jul 26, 2021
Upgrade priority: Low. Recommended for users of `BTreeMap` and `BTreeSet`
+1 -1
View File
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, Registry } from '../types';
import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import { compareArray } from './utils';
import { compareArray } from './utils/compareArray';
/**
* @name AbstractArray
+1 -1
View File
@@ -310,7 +310,7 @@ export class Enum implements Codec {
} else if (this.#isBasic && isString(other)) {
return this.type === other;
} else if (isU8a(other)) {
return !this.toU8a().some((entry, index): boolean => entry !== other[index]);
return !this.toU8a().some((entry, index) => entry !== other[index]);
} else if (isHex(other)) {
return this.toHex() === other;
} else if (other instanceof Enum) {
+19 -15
View File
@@ -2,21 +2,25 @@
// SPDX-License-Identifier: Apache-2.0
import type { Codec } from '../../types';
import type { Enum } from '../Enum';
import { AbstractArray } from '../AbstractArray';
import { AbstractInt } from '../AbstractInt';
import { Enum, Struct } from '..';
import { isBn, isFunction, isNumber, stringify } from '@polkadot/util';
type SortArg = Codec | Codec[] | number[] | number | Uint8Array
/** @internal **/
function isArrayLike (arg: SortArg): arg is AbstractArray<Codec> | Uint8Array | Codec[] | number[] {
return arg instanceof AbstractArray || arg instanceof Uint8Array || Array.isArray(arg);
function isArrayLike (arg: SortArg): arg is Uint8Array | Codec[] | number[] {
return arg instanceof Uint8Array || Array.isArray(arg);
}
/** @internal **/
function isCodec (arg: SortArg): arg is Codec {
return !!(arg && typeof (arg as Codec).toU8a === 'function');
return isFunction(arg && (arg as Codec).toU8a);
}
/** @internal **/
function isEnum (arg: SortArg): arg is Enum {
return isCodec(arg) && isNumber((arg as Enum).index) && isCodec((arg as Enum).value);
}
/**
@@ -25,16 +29,16 @@ function isCodec (arg: SortArg): arg is Codec {
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html)
*/
export function sortAsc<V extends SortArg = Codec> (a: V, b: V): number {
if (typeof a === 'number' && typeof b === 'number') {
if (isNumber(a) && isNumber(b)) {
return a - b;
} else if (a instanceof Struct && b instanceof Struct) {
} else if (a instanceof Map && b instanceof Map) {
return sortAsc(Array.from(a.values()), Array.from(b.values()));
} else if (a instanceof Enum && b instanceof Enum) {
} else if (isEnum(a) && isEnum(b)) {
return sortAsc(a.index, b.index) || sortAsc(a.value, b.value);
} else if (isArrayLike(a) && isArrayLike(b)) {
// Vec, Tuple, Bytes etc.
let sortRes = 0; const lenA = a.length; const lenB = b.length;
const minLen = Math.min(lenA, lenB);
let sortRes = 0;
const minLen = Math.min(a.length, b.length);
for (let i = 0; i < minLen; ++i) {
sortRes = sortAsc(a[i], b[i]);
@@ -44,15 +48,15 @@ export function sortAsc<V extends SortArg = Codec> (a: V, b: V): number {
}
}
return lenA - lenB;
} else if (a instanceof AbstractInt && b instanceof AbstractInt) {
return a.length - b.length;
} else if (isBn(a) && isBn(b)) {
return a.cmp(b);
} else if (isCodec(a) && isCodec(b)) {
// Text, Bool etc.
return sortAsc(a.toU8a(true), b.toU8a(true));
} else {
throw new Error(`Attempting to sort unrecognized value: ${JSON.stringify(a)}`);
}
throw new Error(`Attempting to sort unrecognized value: ${stringify(a)}`);
}
export function sortSet<V extends Codec = Codec> (set: Set<V>): Set<V> {