Re-implementation of Patricia-Merkle Trie (#203)
* Start of trie-db re-implementation * WIP commit * More WIP * WIP, seems complete, untested * Ported trie-db operational * Add Ethereum trie tests * Use u8aConcat to construct u8a * Basic RocksDb interface * Combine engines into DB, expand tests * Update Trie with transaction wrapper * LevelDb (sync) interface * Remove unused (for now) package (compatibility) * trie-d -> trie-db-old, trie-db-ext -> trie-db * Version bump * Skip old trie tests (package to be removed) * Skip tests (fixed) * Remove non-used RocksDb spec * Skip FileFlat tests (deprecated?)
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
/**
|
||||
* @name asNibbles
|
||||
* @signature asNibbles (pairs: Trie$Pairs): Uint8Array
|
||||
* @signature asNibbles (bytes: Uint8Array | Array<number>): Uint8Array
|
||||
* @summary Converts the input to Nibbles.
|
||||
* @description
|
||||
* From an `Uint8Array` input, calculate and return a list of nibbles that makes up the input.
|
||||
@@ -13,14 +13,18 @@
|
||||
*
|
||||
* asNibbles(new Uint8Array([0x41, 0x20]) // => Uint8Array([4, 1, 2, 0])
|
||||
*/
|
||||
export default function asNibbles (bytes: Uint8Array | Array<number>): Uint8Array {
|
||||
export default function asNibbles (bytes: Uint8Array | Array<number> | null): Uint8Array {
|
||||
if (bytes === null) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
// HACK TypeScript gets a little bit confused as to what to apply, hence casting here although the reduces function for both types does exactly the same
|
||||
return (bytes as number[]).reduce((result, byte, index) => {
|
||||
result.set(
|
||||
return (bytes as number[]).reduce((nibbles, byte, index) => {
|
||||
nibbles.set(
|
||||
[byte >> 4, byte & 0b1111],
|
||||
index * 2
|
||||
);
|
||||
|
||||
return result;
|
||||
return nibbles;
|
||||
}, new Uint8Array(bytes.length * 2));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2018 @polkadot/trie-hash authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import u8aFromString from '@polkadot/util/u8a/fromString';
|
||||
|
||||
import fromNibbles from './fromNibbles';
|
||||
|
||||
describe('fromNibbles', () => {
|
||||
it('converts a single', () => {
|
||||
expect(
|
||||
fromNibbles(
|
||||
new Uint8Array([4, 1])
|
||||
)
|
||||
).toEqual(
|
||||
u8aFromString('A')
|
||||
);
|
||||
});
|
||||
|
||||
it('converts an number of values', () => {
|
||||
expect(
|
||||
fromNibbles(
|
||||
new Uint8Array([3, 1, 2, 3, 4, 5])
|
||||
)
|
||||
).toEqual(
|
||||
new Uint8Array([0x31, 0x23, 0x45])
|
||||
);
|
||||
});
|
||||
|
||||
it('converts an number of values (array)', () => {
|
||||
expect(
|
||||
fromNibbles(
|
||||
[3, 1, 2, 3, 4, 5, 7, 8]
|
||||
)
|
||||
).toEqual(
|
||||
new Uint8Array([0x31, 0x23, 0x45, 0x78])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2018 @polkadot/trie-hash authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
/**
|
||||
* @name fromNibbles
|
||||
* @signature fromNibbles (nibbles: Uint8Array): Uint8Array
|
||||
* @summary Converts the input to Nibbles.
|
||||
* @description
|
||||
* From an `Uint8Array` input, calculate and return a list of nibbles that makes up the input.
|
||||
* @example
|
||||
* import { fromNibbles } from '@polkadot/trie-hash/util';
|
||||
*
|
||||
* asNibbles(new Uint8Array([4, 1, 2, 0])) // => Uint8Array([0x41, 0x20]
|
||||
*/
|
||||
export default function fromNibbles (nibbles: Uint8Array | Array<number>): Uint8Array {
|
||||
const u8a = new Uint8Array(nibbles.length / 2);
|
||||
|
||||
for (let index = 0; index < u8a.length; index++) {
|
||||
const nibIndex = index * 2;
|
||||
|
||||
u8a[index] = (nibbles[nibIndex] << 4) + nibbles[nibIndex + 1];
|
||||
}
|
||||
|
||||
return u8a;
|
||||
}
|
||||
Reference in New Issue
Block a user