Cleanup more Codeclimate issues (#267)
* Cleanup more Codeclimate issues * Cleanup unused imports
This commit is contained in:
@@ -105,7 +105,7 @@ export default class Impl extends Cache {
|
||||
}
|
||||
|
||||
protected _writeValue (keyAt: number, keyValue: Buffer, value: Buffer): Value {
|
||||
l.debug(() => ['writeValue', { keyAt, keyValue, value }]);
|
||||
// l.debug(() => ['writeValue', { keyAt, keyValue, value }]);
|
||||
|
||||
const current = this._extractValueInfo(keyValue);
|
||||
const valueAt = value.length > current.valueLength
|
||||
@@ -123,7 +123,7 @@ export default class Impl extends Cache {
|
||||
}
|
||||
|
||||
protected _writeNewKey (key: NibbleBuffer): Key {
|
||||
l.debug(() => ['writeNewKey', { key }]);
|
||||
// l.debug(() => ['writeNewKey', { key }]);
|
||||
|
||||
const keyValue = Buffer.alloc(defaults.KEY_TOTAL_SIZE);
|
||||
|
||||
@@ -147,7 +147,7 @@ export default class Impl extends Cache {
|
||||
}
|
||||
|
||||
protected _writeNewBranch (branch: Buffer, branchAt: number, entryIndex: number, key: NibbleBuffer, prevAt: number, prevKey: NibbleBuffer, matchIndex: number, depth: number): Key {
|
||||
l.debug(() => ['writeNewBranch', { branch, branchAt, entryIndex, key, prevAt, prevKey, matchIndex, depth }]);
|
||||
// l.debug(() => ['writeNewBranch', { branch, branchAt, entryIndex, key, prevAt, prevKey, matchIndex, depth }]);
|
||||
|
||||
const { keyAt, keyValue } = this.writeNewKey(key);
|
||||
const keyIndex = key.nibbles[matchIndex] * defaults.ENTRY_SIZE;
|
||||
@@ -178,11 +178,7 @@ export default class Impl extends Cache {
|
||||
branch.writeUIntBE(newBranchAt, entryIndex + 1, defaults.UINT_SIZE);
|
||||
fs.writeSync(this._fd, branch, entryIndex, defaults.ENTRY_SIZE, branchAt + entryIndex);
|
||||
|
||||
return {
|
||||
key,
|
||||
keyAt,
|
||||
keyValue
|
||||
};
|
||||
return { key, keyAt, keyValue };
|
||||
}
|
||||
|
||||
protected writeNewBranch (branch: Buffer, branchAt: number, entryIndex: number, key: NibbleBuffer, prevAt: number, prevValue: NibbleBuffer, matchIndex: number, depth: number): Key {
|
||||
|
||||
@@ -50,30 +50,16 @@ export default class NodeHeader {
|
||||
|
||||
private static decodeNodeHeaderArray (input: Array<null | Uint8Array>): [NodeType, Null | BranchHeader | ExtensionHeader | LeafHeader] {
|
||||
if (input.length === 0) {
|
||||
return [
|
||||
NODE_TYPE_NULL,
|
||||
new Null()
|
||||
];
|
||||
return [NODE_TYPE_NULL, new Null()];
|
||||
} else if (input.length === 2) {
|
||||
const nibbles = decodeNibbles(input[0]);
|
||||
const isTerminated = isNibblesTerminated(nibbles);
|
||||
|
||||
if (isTerminated) {
|
||||
return [
|
||||
NODE_TYPE_LEAF,
|
||||
new LeafHeader(nibbles.length - 1)
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
NODE_TYPE_EXT,
|
||||
new ExtensionHeader(nibbles.length)
|
||||
];
|
||||
}
|
||||
return isTerminated
|
||||
? [NODE_TYPE_LEAF, new LeafHeader(nibbles.length - 1)]
|
||||
: [NODE_TYPE_EXT, new ExtensionHeader(nibbles.length)];
|
||||
} else if (input.length === 17) {
|
||||
return [
|
||||
NODE_TYPE_BRANCH,
|
||||
new BranchHeader(!!input[16])
|
||||
];
|
||||
return [NODE_TYPE_BRANCH, new BranchHeader(!!input[16])];
|
||||
}
|
||||
|
||||
throw new Error('Unreachable');
|
||||
@@ -147,19 +133,15 @@ export default class NodeHeader {
|
||||
} else if (nodeType === NODE_TYPE_EXT) {
|
||||
const nibbleCount = (this.value as ExtensionHeader).toNumber();
|
||||
|
||||
if (nibbleCount < EXTENSION_NODE_THRESHOLD) {
|
||||
return new Uint8Array([EXTENSION_NODE_OFFSET + nibbleCount]);
|
||||
}
|
||||
|
||||
return new Uint8Array([EXTENSION_NODE_BIG, nibbleCount - EXTENSION_NODE_THRESHOLD]);
|
||||
return (nibbleCount < EXTENSION_NODE_THRESHOLD)
|
||||
? new Uint8Array([EXTENSION_NODE_OFFSET + nibbleCount])
|
||||
: new Uint8Array([EXTENSION_NODE_BIG, nibbleCount - EXTENSION_NODE_THRESHOLD]);
|
||||
} else if (nodeType === NODE_TYPE_LEAF) {
|
||||
const nibbleCount = (this.value as LeafHeader).toNumber();
|
||||
|
||||
if (nibbleCount < LEAF_NODE_THRESHOLD) {
|
||||
return new Uint8Array([LEAF_NODE_OFFSET + nibbleCount]);
|
||||
}
|
||||
|
||||
return new Uint8Array([LEAF_NODE_BIG, nibbleCount - LEAF_NODE_THRESHOLD]);
|
||||
return (nibbleCount < LEAF_NODE_THRESHOLD)
|
||||
? new Uint8Array([LEAF_NODE_OFFSET + nibbleCount])
|
||||
: new Uint8Array([LEAF_NODE_BIG, nibbleCount - LEAF_NODE_THRESHOLD]);
|
||||
}
|
||||
|
||||
throw new Error('Unreachable');
|
||||
|
||||
@@ -9,6 +9,12 @@ import { NODE_TYPE_NULL, NODE_TYPE_BRANCH, NODE_TYPE_EXT, NODE_TYPE_LEAF } from
|
||||
import { addNibblesTerminator, encodeNibbles } from './nibbles';
|
||||
import { toNibbles } from './util';
|
||||
|
||||
const EMPTY_BRANCH: Array<Uint8Array | null> = [
|
||||
null, null, null, null,
|
||||
null, null, null, null,
|
||||
null, null, null, null,
|
||||
null, null, null, null
|
||||
];
|
||||
const l = logger('trie/codec');
|
||||
|
||||
l.noop();
|
||||
@@ -30,13 +36,7 @@ function _decodeBranch (header: NodeHeader, input: Uint8Array): Array<null | Uin
|
||||
|
||||
let cursor = 1;
|
||||
|
||||
return [
|
||||
null, null, null, null,
|
||||
null, null, null, null,
|
||||
null, null, null, null,
|
||||
null, null, null, null,
|
||||
value
|
||||
].map((value, index) => {
|
||||
return EMPTY_BRANCH.concat(value).map((value, index) => {
|
||||
let result: null | Uint8Array | [Uint8Array, Uint8Array] = value;
|
||||
|
||||
if ((index < 16) && (bitmap & cursor)) {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 @polkadot/trie-db authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export default class Checkpoint {
|
||||
protected txRoot: Uint8Array;
|
||||
protected rootHash: Uint8Array;
|
||||
|
||||
constructor (rootHash: Uint8Array) {
|
||||
this.rootHash = rootHash;
|
||||
this.txRoot = rootHash;
|
||||
}
|
||||
|
||||
protected createCheckpoint (): Uint8Array {
|
||||
this.txRoot = this.rootHash;
|
||||
|
||||
return this.txRoot;
|
||||
}
|
||||
|
||||
protected commitCheckpoint (): Uint8Array {
|
||||
return this.rootHash;
|
||||
}
|
||||
|
||||
protected revertCheckpoint (): Uint8Array {
|
||||
this.rootHash = this.txRoot;
|
||||
|
||||
return this.rootHash;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import { isNull, logger , u8aConcat, u8aToHex } from '@polkadot/util/index';
|
||||
import { isBranchNode, isEmptyNode, isExtensionNode, isKvNode, isLeafNode } from './util/is';
|
||||
import { keyEquals, keyStartsWith, computeExtensionKey, computeLeafKey, consumeCommonPrefix } from './util/key';
|
||||
import { getNodeType, decodeNode, encodeNode } from './util/node';
|
||||
import Checkpoint from './Checkpoint';
|
||||
import constants, { Constants } from './constants';
|
||||
|
||||
const l = logger('trie/db');
|
||||
@@ -26,39 +27,23 @@ const l = logger('trie/db');
|
||||
* @summary Re-implementation of a Patricia Trie
|
||||
* @example See [Polkadot-JS Common Trie-DB Examples](https://polkadot.js.org/api/common/examples/trie-db/)
|
||||
*/
|
||||
export default class Impl {
|
||||
export default class Impl extends Checkpoint {
|
||||
readonly db: TxDb;
|
||||
protected codec: Codec;
|
||||
protected constants: Constants;
|
||||
protected txRoot: Uint8Array;
|
||||
protected rootHash: Uint8Array;
|
||||
|
||||
constructor (db: TxDb, rootHash?: Uint8Array, codec: Codec = substrateCodec) {
|
||||
const _constants = constants(codec);
|
||||
|
||||
super(rootHash || _constants.EMPTY_HASH);
|
||||
|
||||
this.db = db;
|
||||
this.codec = codec;
|
||||
this.constants = constants(codec);
|
||||
this.rootHash = rootHash || this.constants.EMPTY_HASH;
|
||||
this.txRoot = this.rootHash;
|
||||
this.constants = _constants;
|
||||
|
||||
l.log(`Created with ${codec.type} codec, root ${u8aToHex(this.rootHash, 64)}`);
|
||||
}
|
||||
|
||||
protected createCheckpoint (): Uint8Array {
|
||||
this.txRoot = this.rootHash;
|
||||
|
||||
return this.txRoot;
|
||||
}
|
||||
|
||||
protected commitCheckpoint (): Uint8Array {
|
||||
return this.rootHash;
|
||||
}
|
||||
|
||||
protected revertCheckpoint (): Uint8Array {
|
||||
this.rootHash = this.txRoot;
|
||||
|
||||
return this.rootHash;
|
||||
}
|
||||
|
||||
protected _snapshot (dest: TrieDb, fn: ProgressCb, root: Uint8Array, keys: number, percent: number, depth: number): number {
|
||||
// l.debug(() => ['snapshot', { root }]);
|
||||
|
||||
@@ -291,10 +276,7 @@ export default class Impl {
|
||||
// l.debug(() => ['_put', { node, trieKey, value }]);
|
||||
|
||||
if (isEmptyNode(node)) {
|
||||
return [
|
||||
computeLeafKey(trieKey),
|
||||
value
|
||||
];
|
||||
return [computeLeafKey(trieKey), value];
|
||||
} else if (isKvNode(node)) {
|
||||
return this._putKvNode(node, trieKey, value);
|
||||
} else if (isBranchNode(node)) {
|
||||
@@ -332,10 +314,7 @@ export default class Impl {
|
||||
|
||||
if (currentRemainder.length === 0 && trieRemainder.length === 0) {
|
||||
if (isLeaf) {
|
||||
return [
|
||||
node[0],
|
||||
value
|
||||
];
|
||||
return [node[0], value];
|
||||
}
|
||||
|
||||
const subNode = this._getNode(node[1]);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { logger } from './index';
|
||||
import { format } from './logger';
|
||||
|
||||
describe('logger', () => {
|
||||
let dateMatch;
|
||||
@@ -39,6 +40,16 @@ describe('logger', () => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('format', () => {
|
||||
it('returns null as-is', () => {
|
||||
expect(format(null)).toEqual(null);
|
||||
});
|
||||
|
||||
it('returns undefined as-is', () => {
|
||||
expect(format(undefined)).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
it('logs to console.log with .log', () => {
|
||||
l.log('console.log test');
|
||||
|
||||
|
||||
+10
-14
@@ -11,9 +11,7 @@ import isBn from './is/bn';
|
||||
import isBuffer from './is/buffer';
|
||||
import isFunction from './is/function';
|
||||
import isObject from './is/object';
|
||||
import isNull from './is/null';
|
||||
import isU8a from './is/u8a';
|
||||
import isUndefined from './is/undefined';
|
||||
import u8aToHex from './u8a/toHex';
|
||||
|
||||
type ConsoleType = 'error' | 'log' | 'warn';
|
||||
@@ -33,13 +31,15 @@ const chalked = {
|
||||
warn: chalk.yellow
|
||||
};
|
||||
|
||||
export function format (value: any): any {
|
||||
if (isNull(value)) {
|
||||
return null;
|
||||
} else if (isUndefined(value)) {
|
||||
return undefined;
|
||||
}
|
||||
function formatObject (value: { [index: string]: any }): any {
|
||||
return Object.keys(value).reduce((result, key) => {
|
||||
result[key] = format(value[key]);
|
||||
|
||||
return result;
|
||||
}, {} as { [index: string]: any });
|
||||
}
|
||||
|
||||
export function format (value: any): any {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(format);
|
||||
}
|
||||
@@ -56,12 +56,8 @@ export function format (value: any): any {
|
||||
return u8aToHex(value);
|
||||
}
|
||||
|
||||
if (isObject(value) && value.constructor === Object) {
|
||||
return Object.keys(value).reduce((result, key) => {
|
||||
result[key] = format(value[key]);
|
||||
|
||||
return result;
|
||||
}, {} as { [index: string]: any });
|
||||
if (value && isObject(value) && value.constructor === Object) {
|
||||
return formatObject(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
@@ -35,11 +35,9 @@ export default function u8aToU8a (value?: Array<number> | Buffer | Uint8Array |
|
||||
}
|
||||
|
||||
if (isString(value)) {
|
||||
if (isHex(value)) {
|
||||
return hexToU8a(value);
|
||||
}
|
||||
|
||||
return stringToU8a(value);
|
||||
return isHex(value)
|
||||
? hexToU8a(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
|
||||
Reference in New Issue
Block a user