diff --git a/packages/trie-db/src/Impl.ts b/packages/trie-db/src/Impl.ts index 8bddd5dea..21666b6b7 100644 --- a/packages/trie-db/src/Impl.ts +++ b/packages/trie-db/src/Impl.ts @@ -4,7 +4,7 @@ import { TxDb, ProgressCb } from '@polkadot/db/types'; import { Codec } from '@polkadot/trie-codec/types'; -import { TrieDb, Node, NodeBranch, NodeEncodedOrEmpty, NodeKv, NodeNotEmpty, NodeType } from './types'; +import { EncodedPath, TrieDb, Node, NodeBranch, NodeEncodedOrEmpty, NodeKv, NodeNotEmpty, NodeType } from './types'; import substrateCodec from '@polkadot/trie-codec/index'; import { decodeNibbles, encodeNibbles, extractNodeKey } from '@polkadot/trie-codec/nibbles'; @@ -16,6 +16,11 @@ import { getNodeType, decodeNode, encodeNode } from './util/node'; import Checkpoint from './Checkpoint'; import constants, { Constants } from './constants'; +const BLANK_BRANCH: Array = [ + null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null +]; + const l = logger('trie/db'); /** @@ -113,11 +118,9 @@ export default class Impl extends Checkpoint { node[trieKey[0]] = encodedSubNode; - if (isNull(encodedSubNode)) { - return this._normaliseBranchNode(node); - } - - return node; + return isNull(encodedSubNode) + ? this._normaliseBranchNode(node) + : node; } protected _delKvNode (node: NodeNotEmpty, trieKey: Uint8Array): Node { @@ -128,9 +131,7 @@ export default class Impl extends Checkpoint { if (!keyStartsWith(trieKey, currentKey)) { return node; - } - - if (nodeType === NodeType.LEAF) { + } else if (nodeType === NodeType.LEAF) { return keyEquals(trieKey, currentKey) ? null : node; @@ -194,11 +195,9 @@ export default class Impl extends Checkpoint { // l.debug(() => [{ currentKey, trieKey, nodeType }]); if (nodeType === NodeType.LEAF) { - if (keyEquals(trieKey, currentKey)) { - return node[1]; - } - - return null; + return keyEquals(trieKey, currentKey) + ? node[1] + : null; } else if (nodeType === NodeType.EXTENSION) { if (keyStartsWith(trieKey, currentKey)) { const subNode = this._getNode(node[1]); @@ -221,13 +220,9 @@ export default class Impl extends Checkpoint { const encoded = encodeNode(this.codec, node); - if (encoded.length < 32) { - // FIXME typings... - // @ts-ignore Ok, this is not correct - this comes back as an embedded node, which mean that our definitions here are completely wrong (or what we think it is, does not align with what it should be as per the spec and implementation) - return [node, null]; - } - - return [this.codec.hashing(encoded), encoded]; + return (encoded.length < 32) + ? [node as any, null] + : [this.codec.hashing(encoded), encoded]; } protected _normaliseBranchNode (node: NodeNotEmpty): Node { @@ -239,9 +234,7 @@ export default class Impl extends Checkpoint { if (mapped.length >= 2) { return node; - } - - if (node[16]) { + } else if (node[16]) { return [computeLeafKey(new Uint8Array()), node[16]]; } @@ -330,19 +323,11 @@ export default class Impl extends Checkpoint { const subKey = computeLeafKey(trieRemainder.subarray(1)); const subNode: NodeKv = [subKey, value]; - newNode = [ - null, null, null, null, null, null, null, null, - null, null, null, null, null, null, null, null, - node[1] - ]; + newNode = BLANK_BRANCH.concat(node[1]) as NodeNotEmpty; newNode[subPosition] = this._persistNode(subNode); } } else { - newNode = [ - null, null, null, null, null, null, null, null, - null, null, null, null, null, null, null, null, - null - ]; + newNode = BLANK_BRANCH.concat(null) as NodeNotEmpty; if (currentRemainder.length === 1 && isExtension) { newNode[currentRemainder[0]] = node[1]; @@ -351,17 +336,11 @@ export default class Impl extends Checkpoint { ? computeExtensionKey(currentRemainder.subarray(1)) : computeLeafKey(currentRemainder.subarray(1)); - newNode[currentRemainder[0]] = this._persistNode([ - computedKey, - node[1] - ]); + newNode[currentRemainder[0]] = this._persistNode([computedKey, node[1]]); } if (trieRemainder.length) { - newNode[trieRemainder[0]] = this._persistNode([ - computeLeafKey(trieRemainder.subarray(1)), - value - ]); + newNode[trieRemainder[0]] = this._persistNode([computeLeafKey(trieRemainder.subarray(1)), value]); } else { newNode[16] = value; } @@ -370,10 +349,7 @@ export default class Impl extends Checkpoint { // l.debug(() => ['newNode', newNode]); if (commonPrefix.length) { - return [ - computeExtensionKey(commonPrefix), - this._persistNode(newNode) - ]; + return [computeExtensionKey(commonPrefix), this._persistNode(newNode)]; } return newNode; diff --git a/packages/trie-hash/src/index.ts b/packages/trie-hash/src/index.ts index 593f56c6c..57b85a008 100644 --- a/packages/trie-hash/src/index.ts +++ b/packages/trie-hash/src/index.ts @@ -2,13 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import trieRoot from './trieRoot'; -import trieRootOrdered from './trieRootOrdered'; - /** * @summary Utility methods to create [Trie hashes](https://github.com/ethereum/wiki/wiki/Patricia-Tree) */ -export { - trieRoot, - trieRootOrdered -}; + +export { default as trieRoot } from './trieRoot'; +export { default as trieRootOrdered } from './trieRootOrdered'; diff --git a/packages/util-crypto/src/blake2/index.ts b/packages/util-crypto/src/blake2/index.ts index 5e1b99dde..80609b8d7 100644 --- a/packages/util-crypto/src/blake2/index.ts +++ b/packages/util-crypto/src/blake2/index.ts @@ -2,13 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import blake2AsHex from './asHex'; -import blake2AsU8a from './asU8a'; - /** * @summary Create blake2b values with specified bitlengths */ -export { - blake2AsHex, - blake2AsU8a -}; + +export { default as blake2AsHex } from './asHex'; +export { default as blake2AsU8a } from './asU8a'; diff --git a/packages/util-crypto/src/keccak/index.ts b/packages/util-crypto/src/keccak/index.ts index 5e12b8a78..40562263c 100644 --- a/packages/util-crypto/src/keccak/index.ts +++ b/packages/util-crypto/src/keccak/index.ts @@ -2,13 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import keccakAsHex from './asHex'; -import keccakAsU8a from './asU8a'; - /** * @summary Create Keccak256 values as hex & Uint8Array output */ -export { - keccakAsHex, - keccakAsU8a -}; + +export { default as keccakAsHex } from './asHex'; +export { default as keccakAsU8a } from './asU8a'; diff --git a/packages/util-crypto/src/mnemonic/index.ts b/packages/util-crypto/src/mnemonic/index.ts index 04980f784..d3aa7b4da 100644 --- a/packages/util-crypto/src/mnemonic/index.ts +++ b/packages/util-crypto/src/mnemonic/index.ts @@ -2,15 +2,10 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import mnemonicGenerate from './generate'; -import mnemonicToSeed from './toSeed'; -import mnemonicValidate from './validate'; - /** * @summary Create valid mnemonic strings, validate them using BIP39, and convert them to valid seeds */ -export { - mnemonicGenerate, - mnemonicToSeed, - mnemonicValidate -}; + +export { default as mnemonicGenerate } from './generate'; +export { default as mnemonicToSeed } from './toSeed'; +export { default as mnemonicValidate } from './validate'; diff --git a/packages/util-crypto/src/nacl/index.ts b/packages/util-crypto/src/nacl/index.ts index 48902c683..798cced9e 100644 --- a/packages/util-crypto/src/nacl/index.ts +++ b/packages/util-crypto/src/nacl/index.ts @@ -2,25 +2,15 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import naclDecrypt from './decrypt'; -import naclEncrypt from './encrypt'; -import naclKeypairFromRandom from './keypair/fromRandom'; -import naclKeypairFromSecret from './keypair/fromSecret'; -import naclKeypairFromSeed from './keypair/fromSeed'; -import naclKeypairFromString from './keypair/fromString'; -import naclSign from './sign'; -import naclVerify from './verify'; - /** * @summary Implements [NaCl](http://nacl.cr.yp.to/) secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures */ -export { - naclDecrypt, - naclEncrypt, - naclKeypairFromRandom, - naclKeypairFromSecret, - naclKeypairFromSeed, - naclKeypairFromString, - naclSign, - naclVerify -}; + +export { default as naclDecrypt } from './decrypt'; +export { default as naclEncrypt } from './encrypt'; +export { default as naclKeypairFromRandom } from './keypair/fromRandom'; +export { default as naclKeypairFromSecret } from './keypair/fromSecret'; +export { default as naclKeypairFromSeed } from './keypair/fromSeed'; +export { default as naclKeypairFromString } from './keypair/fromString'; +export { default as naclSign } from './sign'; +export { default as naclVerify } from './verify'; diff --git a/packages/util-crypto/src/random/index.ts b/packages/util-crypto/src/random/index.ts index 9e5150f7f..919633bb0 100644 --- a/packages/util-crypto/src/random/index.ts +++ b/packages/util-crypto/src/random/index.ts @@ -2,15 +2,10 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import randomAsHex from './asHex'; -import randomAsNumber from './asNumber'; -import randomAsU8a from './asU8a'; - /** * @summary Returns a sequence of secure random bytes in a variety of formats */ -export { - randomAsHex, - randomAsNumber, - randomAsU8a -}; + +export { default as randomAsHex } from './asHex'; +export { default as randomAsNumber } from './asNumber'; +export { default as randomAsU8a } from './asU8a'; diff --git a/packages/util-crypto/src/sha512/index.ts b/packages/util-crypto/src/sha512/index.ts index 4d9451a72..e5d0ba7e9 100644 --- a/packages/util-crypto/src/sha512/index.ts +++ b/packages/util-crypto/src/sha512/index.ts @@ -2,11 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import sha512AsU8a from './asU8a'; - /** * @summary Implements Sha-512 hashing functions for a variety of input and outputs */ -export { - sha512AsU8a -}; + +export { default as sha512AsU8a } from './asU8a'; diff --git a/packages/util-crypto/src/xxhash/index.ts b/packages/util-crypto/src/xxhash/index.ts index 14028815f..5324ebcf4 100644 --- a/packages/util-crypto/src/xxhash/index.ts +++ b/packages/util-crypto/src/xxhash/index.ts @@ -2,13 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import xxhashAsHex from './asHex'; -import xxhashAsU8a from './asU8a'; - /** * @summary Create xxhash64 values with specified bitlengths */ -export { - xxhashAsHex, - xxhashAsU8a -}; + +export { default as xxhashAsHex } from './asHex'; +export { default as xxhashAsU8a } from './asU8a'; diff --git a/packages/util-rlp/src/index.ts b/packages/util-rlp/src/index.ts index 2ac92ca10..3ee61dc26 100644 --- a/packages/util-rlp/src/index.ts +++ b/packages/util-rlp/src/index.ts @@ -2,13 +2,9 @@ // This software may be modified and distributed under the terms // of the MPL-2.0 license. See the LICENSE file for details. -import decode from './decode'; -import encode from './encode'; - /** * @summary Utility methods to encode and decode [Ethereum RLP](https://github.com/ethereum/wiki/wiki/RLP). Adapted from the [EthereumJS implementation](https://github.com/ethereumjs/rlp/blob/0ce09db81fc303fcee593f7cc094ba44015f9b92/index.js) */ -export { - decode, - encode -}; + +export { default as decode } from './decode'; +export { default as encode } from './encode'; diff --git a/packages/util/src/array/index.ts b/packages/util/src/array/index.ts index 8213f1e5b..a33a66a7e 100644 --- a/packages/util/src/array/index.ts +++ b/packages/util/src/array/index.ts @@ -2,11 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import arrayFilter from './filter'; - /** * @summary Utility methods that operates on arrays */ -export { - arrayFilter -}; + +export { default as arrayFilter } from './filter'; diff --git a/packages/util/src/bn/index.ts b/packages/util/src/bn/index.ts index b92a377ea..420a60724 100644 --- a/packages/util/src/bn/index.ts +++ b/packages/util/src/bn/index.ts @@ -2,17 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import bnFromHex from './fromHex'; -import bnToBn from './toBn'; -import bnToHex from './toHex'; -import bnToU8a from './toU8a'; - /** * @summary Utility methods to convert to and from `BN` objects */ -export { - bnFromHex, - bnToBn, - bnToHex, - bnToU8a -}; + +export { default as bnFromHex } from './fromHex'; +export { default as bnToBn } from './toBn'; +export { default as bnToHex } from './toHex'; +export { default as bnToU8a } from './toU8a'; diff --git a/packages/util/src/buffer/index.ts b/packages/util/src/buffer/index.ts index 906683338..12c34d68a 100644 --- a/packages/util/src/buffer/index.ts +++ b/packages/util/src/buffer/index.ts @@ -2,11 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import bufferToU8a from './toU8a'; - /** * @summary Utility methods to convert to and from `Buffer` objects */ -export { - bufferToU8a -}; + +export { default as bufferToU8a } from './toU8a'; diff --git a/packages/util/src/compact/index.ts b/packages/util/src/compact/index.ts index 84a595764..cfc325a4d 100644 --- a/packages/util/src/compact/index.ts +++ b/packages/util/src/compact/index.ts @@ -2,11 +2,6 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import compactAddLength from './addLength'; -import compactStripLength from './stripLength'; -import compactFromU8a from './fromU8a'; -import compactToU8a from './toU8a'; - /** * @description * Encoding and decoding of parity-codec compact numbers. The codec is created @@ -27,9 +22,8 @@ import compactToU8a from './toU8a'; * * Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key. */ -export { - compactAddLength, - compactFromU8a, - compactStripLength, - compactToU8a -}; + +export { default as compactAddLength } from './addLength'; +export { default as compactStripLength } from './stripLength'; +export { default as compactFromU8a } from './fromU8a'; +export { default as compactToU8a } from './toU8a'; diff --git a/packages/util/src/ext/index.ts b/packages/util/src/ext/index.ts index 25e8c076b..3e0736c62 100644 --- a/packages/util/src/ext/index.ts +++ b/packages/util/src/ext/index.ts @@ -2,11 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import ExtError from './error'; - /** * @summary Extensions to basic classes */ -export { - ExtError -}; + +export { default as ExtError } from './error'; diff --git a/packages/util/src/hex/index.ts b/packages/util/src/hex/index.ts index a5e4853de..2a5482980 100644 --- a/packages/util/src/hex/index.ts +++ b/packages/util/src/hex/index.ts @@ -2,23 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import hexAddPrefix from './addPrefix'; -import hexFixLength from './fixLength'; -import hexHasPrefix from './hasPrefix'; -import hexStripPrefix from './stripPrefix'; -import hexToBn from './toBn'; -import hexToNumber from './toNumber'; -import hexToU8a from './toU8a'; - /** * @summary Internal utilities to create and test for hex values */ -export { - hexAddPrefix, - hexFixLength, - hexHasPrefix, - hexStripPrefix, - hexToBn, - hexToNumber, - hexToU8a -}; + +export { default as hexAddPrefix } from './addPrefix'; +export { default as hexFixLength } from './fixLength'; +export { default as hexHasPrefix } from './hasPrefix'; +export { default as hexStripPrefix } from './stripPrefix'; +export { default as hexToBn } from './toBn'; +export { default as hexToNumber } from './toNumber'; +export { default as hexToU8a } from './toU8a'; diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index ce58d2d8f..ef16fcb66 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -2,9 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import assert from './assert'; -import logger from './logger'; -import promisify from './promisify'; +/** + * @summary Utility methods for this package are split into groups + */ + +export { default as assert } from './assert'; +export { default as logger } from './logger'; +export { default as promisify } from './promisify'; export * from './array'; export * from './bn'; @@ -16,12 +20,3 @@ export * from './is'; export * from './number'; export * from './string'; export * from './u8a'; - -/** - * @summary Utility methods for this package are split into groups - */ -export { - assert, - logger, - promisify -}; diff --git a/packages/util/src/is/index.ts b/packages/util/src/is/index.ts index 36fa3950c..14c8b009c 100644 --- a/packages/util/src/is/index.ts +++ b/packages/util/src/is/index.ts @@ -2,39 +2,22 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import isBn from './bn'; -import isBuffer from './buffer'; -import isBoolean from './boolean'; -import isError from './error'; -import isFunction from './function'; -import isHex from './hex'; -import isInstanceOf from './instanceOf'; -import isIp from './ip'; -import isNull from './null'; -import isNumber from './number'; -import isObject from './object'; -import isObservable from './observable'; -import isString from './string'; -import isU8a from './u8a'; -import isUndefined from './undefined'; - /** * @summary Type checking utilities */ -export { - isBn, - isBoolean, - isBuffer, - isError, - isFunction, - isHex, - isInstanceOf, - isIp, - isObject, - isObservable, - isNull, - isNumber, - isString, - isU8a, - isUndefined -}; + +export { default as isBn } from './bn'; +export { default as isBuffer } from './buffer'; +export { default as isBoolean } from './boolean'; +export { default as isError } from './error'; +export { default as isFunction } from './function'; +export { default as isHex } from './hex'; +export { default as isInstanceOf } from './instanceOf'; +export { default as isIp } from './ip'; +export { default as isNull } from './null'; +export { default as isNumber } from './number'; +export { default as isObject } from './object'; +export { default as isObservable } from './observable'; +export { default as isString } from './string'; +export { default as isU8a } from './u8a'; +export { default as isUndefined } from './undefined'; diff --git a/packages/util/src/number/index.ts b/packages/util/src/number/index.ts index 773f07c44..9191c3838 100644 --- a/packages/util/src/number/index.ts +++ b/packages/util/src/number/index.ts @@ -2,13 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import numberToHex from './toHex'; -import numberToU8a from './toU8a'; - /** * @summary Utility methods to convert to and from `number` values */ -export { - numberToHex, - numberToU8a -}; + +export { default as numberToHex } from './toHex'; +export { default as numberToU8a } from './toU8a'; diff --git a/packages/util/src/string/index.ts b/packages/util/src/string/index.ts index acf468a62..1e592099b 100644 --- a/packages/util/src/string/index.ts +++ b/packages/util/src/string/index.ts @@ -2,19 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import stringCamelCase from './camelCase'; -import stringLowerFirst from './lowerFirst'; -import stringShorten from './shorten'; -import stringToU8a from './toU8a'; -import stringUpperFirst from './upperFirst'; - /** * @summary Utility methods to convert to work with `string` values */ -export { - stringCamelCase, - stringLowerFirst, - stringShorten, - stringToU8a, - stringUpperFirst -}; + +export { default as stringCamelCase } from './camelCase'; +export { default as stringLowerFirst } from './lowerFirst'; +export { default as stringShorten } from './shorten'; +export { default as stringToU8a } from './toU8a'; +export { default as stringUpperFirst } from './upperFirst'; diff --git a/packages/util/src/u8a/index.ts b/packages/util/src/u8a/index.ts index 479cfbd31..604f750c9 100644 --- a/packages/util/src/u8a/index.ts +++ b/packages/util/src/u8a/index.ts @@ -2,23 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import u8aConcat from './concat'; -import u8aFixLength from './fixLength'; -import u8aToBn from './toBn'; -import u8aToBuffer from './toBuffer'; -import u8aToHex from './toHex'; -import u8aToString from './toString'; -import u8aToU8a from './toU8a'; - /** * @summary Utility methods to convert to and from `Uint8Array` objects */ -export { - u8aConcat, - u8aFixLength, - u8aToBn, - u8aToBuffer, - u8aToHex, - u8aToString, - u8aToU8a -}; + +export { default as u8aConcat } from './concat'; +export { default as u8aFixLength } from './fixLength'; +export { default as u8aToBn } from './toBn'; +export { default as u8aToBuffer } from './toBuffer'; +export { default as u8aToHex } from './toHex'; +export { default as u8aToString } from './toString'; +export { default as u8aToU8a } from './toU8a';