Trie codec cleanups (new objects, loops) (#426)
* Trie codec cleanups (new objects, loops) * Enum -> const
This commit is contained in:
@@ -4,36 +4,17 @@
|
||||
|
||||
import { decodeNibbles, isNibblesTerminated } from '@polkadot/trie-codec/nibbles';
|
||||
|
||||
import { BRANCH_NODE_NO_VALUE, BRANCH_NODE_WITH_VALUE, EMPTY_TRIE, EXTENSION_NODE_BIG, EXTENSION_NODE_OFFSET, EXTENSION_NODE_SMALL_MAX, EXTENSION_NODE_THRESHOLD, LEAF_NODE_BIG, LEAF_NODE_OFFSET, LEAF_NODE_SMALL_MAX, LEAF_NODE_THRESHOLD, NODE_TYPE_NULL, NODE_TYPE_BRANCH, NODE_TYPE_EXT, NODE_TYPE_LEAF } from './constants';
|
||||
import { BRANCH_NODE_NO_VALUE, BRANCH_NODE_WITH_VALUE, EMPTY_TRIE, EXTENSION_NODE_BIG, EXTENSION_NODE_OFFSET, EXTENSION_NODE_SMALL_MAX, EXTENSION_NODE_THRESHOLD, LEAF_NODE_BIG, LEAF_NODE_OFFSET, LEAF_NODE_SMALL_MAX, LEAF_NODE_THRESHOLD, NODE_TYPE_BRANCH, NODE_TYPE_EXT, NODE_TYPE_LEAF, NODE_TYPE_NULL } from './constants';
|
||||
|
||||
// FIXME These are not actually extending from types, we may want to bring
|
||||
// that in if the dependency imports can be sorted out
|
||||
|
||||
type NodeType = 0 | 1 | 2 | 3;
|
||||
|
||||
export class Null {
|
||||
}
|
||||
|
||||
export class BranchHeader extends Boolean {
|
||||
}
|
||||
|
||||
export class NibbleHeader {
|
||||
private _value: number;
|
||||
|
||||
constructor (input: number) {
|
||||
this._value = input;
|
||||
}
|
||||
|
||||
toNumber (): number {
|
||||
return this._value;
|
||||
}
|
||||
}
|
||||
|
||||
export class ExtensionHeader extends NibbleHeader {
|
||||
}
|
||||
|
||||
export class LeafHeader extends NibbleHeader {
|
||||
}
|
||||
export type Null = null;
|
||||
export type BranchHeader = boolean;
|
||||
export type NibbleHeader = number;
|
||||
export type ExtensionHeader = NibbleHeader;
|
||||
export type LeafHeader = NibbleHeader;
|
||||
|
||||
export default class NodeHeader {
|
||||
private _nodeType: NodeType;
|
||||
@@ -50,16 +31,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, null];
|
||||
} else if (input.length === 2) {
|
||||
const nibbles = decodeNibbles(input[0]);
|
||||
const isTerminated = isNibblesTerminated(nibbles);
|
||||
|
||||
return isTerminated
|
||||
? [NODE_TYPE_LEAF, new LeafHeader(nibbles.length - 1)]
|
||||
: [NODE_TYPE_EXT, new ExtensionHeader(nibbles.length)];
|
||||
? [NODE_TYPE_LEAF, nibbles.length - 1]
|
||||
: [NODE_TYPE_EXT, nibbles.length];
|
||||
} else if (input.length === 17) {
|
||||
return [NODE_TYPE_BRANCH, new BranchHeader(!!input[16])];
|
||||
return [NODE_TYPE_BRANCH, !!input[16]];
|
||||
}
|
||||
|
||||
throw new Error('Unreachable');
|
||||
@@ -71,44 +52,43 @@ export default class NodeHeader {
|
||||
: EMPTY_TRIE;
|
||||
|
||||
if (!input || firstByte === EMPTY_TRIE) {
|
||||
return [NODE_TYPE_NULL, new Null()];
|
||||
return [NODE_TYPE_NULL, null];
|
||||
} else if (firstByte === BRANCH_NODE_NO_VALUE) {
|
||||
return [NODE_TYPE_BRANCH, new BranchHeader(false)];
|
||||
return [NODE_TYPE_BRANCH, false];
|
||||
} else if (firstByte === BRANCH_NODE_WITH_VALUE) {
|
||||
return [NODE_TYPE_BRANCH, new BranchHeader(true)];
|
||||
return [NODE_TYPE_BRANCH, true];
|
||||
} else if (firstByte >= EXTENSION_NODE_OFFSET && firstByte <= EXTENSION_NODE_SMALL_MAX) {
|
||||
return [NODE_TYPE_EXT, new ExtensionHeader(firstByte - EXTENSION_NODE_OFFSET)];
|
||||
return [NODE_TYPE_EXT, firstByte - EXTENSION_NODE_OFFSET];
|
||||
} else if (firstByte === EXTENSION_NODE_BIG) {
|
||||
return [NODE_TYPE_EXT, new ExtensionHeader(input[1] + EXTENSION_NODE_THRESHOLD)];
|
||||
return [NODE_TYPE_EXT, input[1] + EXTENSION_NODE_THRESHOLD];
|
||||
} else if (firstByte >= LEAF_NODE_OFFSET && firstByte <= LEAF_NODE_SMALL_MAX) {
|
||||
return [NODE_TYPE_LEAF, new LeafHeader(firstByte - LEAF_NODE_OFFSET)];
|
||||
return [NODE_TYPE_LEAF, firstByte - LEAF_NODE_OFFSET];
|
||||
} else if (firstByte === LEAF_NODE_BIG) {
|
||||
return [NODE_TYPE_LEAF, new LeafHeader(input[1] + LEAF_NODE_THRESHOLD)];
|
||||
return [NODE_TYPE_LEAF, input[1] + LEAF_NODE_THRESHOLD];
|
||||
}
|
||||
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
const nodeType = this.nodeType;
|
||||
switch (this.nodeType) {
|
||||
case NODE_TYPE_NULL:
|
||||
case NODE_TYPE_BRANCH:
|
||||
return 1;
|
||||
|
||||
if (nodeType === NODE_TYPE_NULL || nodeType === NODE_TYPE_BRANCH) {
|
||||
return 1;
|
||||
} else if (nodeType === NODE_TYPE_EXT) {
|
||||
const nibbleCount = (this.value as ExtensionHeader).toNumber();
|
||||
case NODE_TYPE_EXT:
|
||||
return (this.value as ExtensionHeader) < EXTENSION_NODE_THRESHOLD
|
||||
? 1
|
||||
: 2;
|
||||
|
||||
return nibbleCount < EXTENSION_NODE_THRESHOLD
|
||||
? 1
|
||||
: 2;
|
||||
} else if (nodeType === NODE_TYPE_LEAF) {
|
||||
const nibbleCount = (this.value as LeafHeader).toNumber();
|
||||
case NODE_TYPE_LEAF:
|
||||
return (this.value as LeafHeader) < LEAF_NODE_THRESHOLD
|
||||
? 1
|
||||
: 2;
|
||||
|
||||
return nibbleCount < LEAF_NODE_THRESHOLD
|
||||
? 1
|
||||
: 2;
|
||||
default:
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
|
||||
get nodeType (): number {
|
||||
@@ -120,30 +100,35 @@ export default class NodeHeader {
|
||||
}
|
||||
|
||||
toU8a (): Uint8Array {
|
||||
const nodeType = this.nodeType;
|
||||
switch (this.nodeType) {
|
||||
case NODE_TYPE_NULL:
|
||||
return new Uint8Array([EMPTY_TRIE]);
|
||||
|
||||
if (nodeType === NODE_TYPE_NULL) {
|
||||
return new Uint8Array([EMPTY_TRIE]);
|
||||
} else if (nodeType === NODE_TYPE_BRANCH) {
|
||||
return new Uint8Array([
|
||||
(this.value as BranchHeader).valueOf() === true
|
||||
? BRANCH_NODE_WITH_VALUE
|
||||
: BRANCH_NODE_NO_VALUE
|
||||
]);
|
||||
} else if (nodeType === NODE_TYPE_EXT) {
|
||||
const nibbleCount = (this.value as ExtensionHeader).toNumber();
|
||||
case NODE_TYPE_BRANCH:
|
||||
return new Uint8Array([
|
||||
this.value as BranchHeader
|
||||
? BRANCH_NODE_WITH_VALUE
|
||||
: BRANCH_NODE_NO_VALUE
|
||||
]);
|
||||
|
||||
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();
|
||||
case NODE_TYPE_EXT: {
|
||||
const nibbleCount = this.value as ExtensionHeader;
|
||||
|
||||
return (nibbleCount < LEAF_NODE_THRESHOLD)
|
||||
? new Uint8Array([LEAF_NODE_OFFSET + nibbleCount])
|
||||
: new Uint8Array([LEAF_NODE_BIG, nibbleCount - LEAF_NODE_THRESHOLD]);
|
||||
return (nibbleCount < EXTENSION_NODE_THRESHOLD)
|
||||
? new Uint8Array([EXTENSION_NODE_OFFSET + nibbleCount])
|
||||
: new Uint8Array([EXTENSION_NODE_BIG, nibbleCount - EXTENSION_NODE_THRESHOLD]);
|
||||
}
|
||||
|
||||
case NODE_TYPE_LEAF: {
|
||||
const nibbleCount = this.value as LeafHeader;
|
||||
|
||||
return (nibbleCount < LEAF_NODE_THRESHOLD)
|
||||
? new Uint8Array([LEAF_NODE_OFFSET + nibbleCount])
|
||||
: new Uint8Array([LEAF_NODE_BIG, nibbleCount - LEAF_NODE_THRESHOLD]);
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,25 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export const BITMAP = [
|
||||
1 << 0,
|
||||
1 << 1,
|
||||
1 << 2,
|
||||
1 << 3,
|
||||
1 << 4,
|
||||
1 << 5,
|
||||
1 << 6,
|
||||
1 << 7,
|
||||
1 << 8,
|
||||
1 << 9,
|
||||
1 << 10,
|
||||
1 << 11,
|
||||
1 << 12,
|
||||
1 << 13,
|
||||
1 << 14,
|
||||
1 << 15
|
||||
];
|
||||
|
||||
export const EMPTY_TRIE = 0;
|
||||
export const LEAF_NODE_OFFSET = 1;
|
||||
export const LEAF_NODE_BIG = 127;
|
||||
@@ -13,6 +32,7 @@ export const LEAF_NODE_THRESHOLD = LEAF_NODE_BIG - LEAF_NODE_OFFSET;
|
||||
export const EXTENSION_NODE_THRESHOLD = EXTENSION_NODE_BIG - EXTENSION_NODE_OFFSET; // 125
|
||||
export const LEAF_NODE_SMALL_MAX = LEAF_NODE_BIG - 1;
|
||||
export const EXTENSION_NODE_SMALL_MAX = EXTENSION_NODE_BIG - 1;
|
||||
|
||||
export const NODE_TYPE_NULL = 0;
|
||||
export const NODE_TYPE_BRANCH = 1;
|
||||
export const NODE_TYPE_EXT = 2;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import { compactStripLength, logger } from '@polkadot/util';
|
||||
|
||||
import NodeHeader, { BranchHeader, NibbleHeader } from './NodeHeader';
|
||||
import { NODE_TYPE_NULL, NODE_TYPE_BRANCH, NODE_TYPE_EXT, NODE_TYPE_LEAF } from './constants';
|
||||
import { BITMAP, NODE_TYPE_BRANCH, NODE_TYPE_EXT, NODE_TYPE_LEAF, NODE_TYPE_NULL } from './constants';
|
||||
import { addNibblesTerminator, encodeNibbles } from './nibbles';
|
||||
import { toNibbles } from './util';
|
||||
|
||||
@@ -27,19 +27,17 @@ function _decodeBranch (header: NodeHeader, input: Uint8Array): Array<null | Uin
|
||||
|
||||
offset += 2;
|
||||
|
||||
if (branch.valueOf() === true) {
|
||||
if (branch === true) {
|
||||
const [length, bytes] = compactStripLength(input.subarray(offset));
|
||||
|
||||
value = bytes;
|
||||
offset += length;
|
||||
}
|
||||
|
||||
let cursor = 1;
|
||||
|
||||
return EMPTY_BRANCH.concat(value).map((value, index) => {
|
||||
let result: null | Uint8Array | [Uint8Array, Uint8Array] = value;
|
||||
|
||||
if ((index < 16) && (bitmap & cursor)) {
|
||||
if ((index < 16) && (bitmap & BITMAP[index])) {
|
||||
const [length, bytes] = compactStripLength(input.subarray(offset));
|
||||
|
||||
result = bytes.length === 32
|
||||
@@ -48,15 +46,13 @@ function _decodeBranch (header: NodeHeader, input: Uint8Array): Array<null | Uin
|
||||
offset += length;
|
||||
}
|
||||
|
||||
cursor = cursor << 1;
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
function _decodeKv (header: NodeHeader, input: Uint8Array): Array<null | Uint8Array | [Uint8Array, Uint8Array]> {
|
||||
let offset = header.encodedLength;
|
||||
const nibbleCount = (header.value as NibbleHeader).toNumber();
|
||||
const nibbleCount = (header.value as NibbleHeader);
|
||||
const nibbleLength = Math.floor((nibbleCount + 1) / 2);
|
||||
const nibbleData = input.subarray(offset, offset + nibbleLength);
|
||||
|
||||
@@ -77,7 +73,7 @@ function _decodeKv (header: NodeHeader, input: Uint8Array): Array<null | Uint8Ar
|
||||
];
|
||||
}
|
||||
|
||||
function _decode (input: null | Uint8Array): Uint8Array | null | Array<null | Uint8Array | [Uint8Array, Uint8Array]> {
|
||||
export default function decode (input: null | Uint8Array): Uint8Array | null | Array<null | Uint8Array | [Uint8Array, Uint8Array]> {
|
||||
const header = new NodeHeader(input);
|
||||
const nodeType = header.nodeType;
|
||||
|
||||
@@ -92,10 +88,10 @@ function _decode (input: null | Uint8Array): Uint8Array | null | Array<null | Ui
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
|
||||
export default function decode (input: null | Uint8Array): Uint8Array | null | Array<null | Uint8Array | Array<null | Uint8Array>> {
|
||||
const decoded = _decode(input);
|
||||
// export default function decode (input: null | Uint8Array): Uint8Array | null | Array<null | Uint8Array | Array<null | Uint8Array>> {
|
||||
// const decoded = _decode(input);
|
||||
|
||||
// l.debug(() => ['decode', { input, decoded }]);
|
||||
// // l.debug(() => ['decode', { input, decoded }]);
|
||||
|
||||
return decoded;
|
||||
}
|
||||
// return decoded;
|
||||
// }
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
import { compactAddLength, u8aConcat, isU8a, logger } from '@polkadot/util';
|
||||
|
||||
import NodeHeader from './NodeHeader';
|
||||
import { NODE_TYPE_BRANCH, NODE_TYPE_EXT, NODE_TYPE_LEAF, NODE_TYPE_NULL } from './constants';
|
||||
import { BITMAP, NODE_TYPE_BRANCH, NODE_TYPE_EXT, NODE_TYPE_LEAF, NODE_TYPE_NULL } from './constants';
|
||||
import { extractKey } from './nibbles';
|
||||
import { fromNibbles } from './util';
|
||||
|
||||
const EMPTY = new Uint8Array();
|
||||
const BRANCH_VALUE_INDEX = 16;
|
||||
const EMPTY = new Uint8Array();
|
||||
|
||||
const l = logger('trie/codec');
|
||||
|
||||
@@ -25,10 +25,7 @@ function encodeKey (input: null | Uint8Array): Uint8Array {
|
||||
// l.debug(() => ['encodeKey', { input, nibbles }]);
|
||||
|
||||
return nibbles.length % 2
|
||||
? u8aConcat(
|
||||
Uint8Array.from([nibbles[0]]),
|
||||
fromNibbles(nibbles.subarray(1))
|
||||
)
|
||||
? u8aConcat(Uint8Array.from([nibbles[0]]), fromNibbles(nibbles.subarray(1)))
|
||||
: fromNibbles(nibbles);
|
||||
}
|
||||
|
||||
@@ -37,29 +34,24 @@ function encodeValue (input: null | Uint8Array | Array<null | Uint8Array>): Uint
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
const encoded = encode(input);
|
||||
|
||||
// l.debug(() => ['encodeValue', { input, encoded }]);
|
||||
|
||||
return compactAddLength(encoded);
|
||||
return compactAddLength(encode(input));
|
||||
}
|
||||
|
||||
function _encodeBranch (header: NodeHeader, input: Array<null | Uint8Array>): Uint8Array {
|
||||
let valuesU8a = EMPTY;
|
||||
let bitmap = 0;
|
||||
|
||||
input.reduce((cursor, value, index) => {
|
||||
if ((index < BRANCH_VALUE_INDEX) && value) {
|
||||
bitmap = bitmap | cursor;
|
||||
for (let index = 0; index < BRANCH_VALUE_INDEX; index++) {
|
||||
const value = input[index];
|
||||
|
||||
valuesU8a = u8aConcat(
|
||||
valuesU8a,
|
||||
encodeValue(value)
|
||||
);
|
||||
if (value) {
|
||||
bitmap = bitmap | BITMAP[index];
|
||||
|
||||
valuesU8a = u8aConcat(valuesU8a, encodeValue(value));
|
||||
}
|
||||
|
||||
return cursor << 1;
|
||||
}, 1);
|
||||
}
|
||||
|
||||
return u8aConcat(
|
||||
header.toU8a(),
|
||||
@@ -79,7 +71,7 @@ function _encodeKv (header: NodeHeader, input: Array<null | Uint8Array>): Uint8A
|
||||
);
|
||||
}
|
||||
|
||||
function _encode (input?: null | Uint8Array | Array<null | Uint8Array>): Uint8Array {
|
||||
export default function encode (input?: null | Uint8Array | Array<null | Uint8Array>): Uint8Array {
|
||||
if (isU8a(input)) {
|
||||
return input;
|
||||
}
|
||||
@@ -98,10 +90,10 @@ function _encode (input?: null | Uint8Array | Array<null | Uint8Array>): Uint8Ar
|
||||
throw new Error('Unreachable');
|
||||
}
|
||||
|
||||
export default function encode (input?: null | Uint8Array | Array<null | Uint8Array>): Uint8Array {
|
||||
const encoded = _encode(input);
|
||||
// export default function encode (input?: null | Uint8Array | Array<null | Uint8Array>): Uint8Array {
|
||||
// const encoded = _encode(input);
|
||||
|
||||
// l.debug(() => ['encode', { input, encoded }]);
|
||||
// // l.debug(() => ['encode', { input, encoded }]);
|
||||
|
||||
return encoded;
|
||||
}
|
||||
// return encoded;
|
||||
// }
|
||||
|
||||
@@ -20,9 +20,9 @@ export default function fromNibbles (input: Uint8Array): Uint8Array {
|
||||
const u8a = new Uint8Array(input.length / 2);
|
||||
|
||||
for (let index = 0; index < u8a.length; index++) {
|
||||
const nibIndex = index * 2;
|
||||
const nibIndex = index << 1;
|
||||
|
||||
u8a.set([(input[nibIndex] << 4) + input[nibIndex + 1]], index);
|
||||
u8a[index] = (input[nibIndex] << 4) + input[nibIndex + 1];
|
||||
}
|
||||
|
||||
return u8a;
|
||||
|
||||
@@ -21,12 +21,15 @@ export default function toNibbles (input?: Uint8Array | null): Uint8Array {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
return input.reduce((nibbles, byte, index) => {
|
||||
nibbles.set(
|
||||
[byte >> 4, byte & 0b1111],
|
||||
index * 2
|
||||
);
|
||||
const result = new Uint8Array(input.length * 2);
|
||||
|
||||
return nibbles;
|
||||
}, new Uint8Array(input.length * 2));
|
||||
for (let index = 0; index < input.length; index++) {
|
||||
const nibIndex = index << 1;
|
||||
const byte = input[index];
|
||||
|
||||
result[nibIndex] = byte >> 4;
|
||||
result[nibIndex + 1] = byte & 0b1111;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user