Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Copyright 2017-2018 @polkadot/client-db authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import toU8a from '@polkadot/util/u8a/toU8a';
import TrieDb from './Trie/Memory';
const EMPTY_ROOT = toU8a('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421');
const HELLO_ROOT = toU8a('0x0a915659b88f80bfa200570bd2767a6ab8cb0a4e44fd240cc5ed7a27728c4531');
const FOO_ROOT = toU8a('0xed4e32371288ee83be74f78fb492f01261b7c3dc9c422581bb705c4376492dc6');
describe('TrieDb', () => {
let memory = new TrieDb();
it('starts with the default root', () => {
expect(
memory.getRoot()
).toEqual(EMPTY_ROOT);
});
it('has the correct root after a single insertion', () => {
memory.put(toU8a('hello'), toU8a('world'));
expect(
export default function decode (type: Param$Types, _input: Uint8Array | string | null | undefined, version: EncodingVersions, isStorage: boolean = false): Param$Decoded {
const input: Uint8Array | null | undefined = isUndefined(_input) || isNull(_input)
? _input
: toU8a(_input);
if (Array.isArray(type)) {
// Arrays have single entries, Tuples will have multiple types
if (type.length === 1) {
return decodeArray(type, input, version, isStorage);
} else {
return decodeTuple(type, input, version, isStorage);
}
}
return decodeValue(decode, type, input, version, isStorage);
}
it('should delete from a branch', (): void => {
trie.del(toU8a('doge'));
expect(
trie.get(toU8a('doge'))
).toEqual(null);
expect(
trie.getRoot()
).toEqual(
new Uint8Array([
43, 119, 232, 84, 123, 197, 94, 42, 149, 34, 124, 147, 159, 159, 157, 103, 149, 45, 225, 233, 112, 160, 23, 224, 145, 11, 229, 16, 176, 144, 175, 243
])
);
});
});
it('should update a value', (): void => {
trie.put(toU8a('test'), toU8a('two'));
expect(
trie.get(toU8a('test'))
).toEqual(toU8a('two'));
expect(
trie.getRoot()
).toEqual(
new Uint8Array([
127, 237, 74, 184, 190, 46, 103, 129, 65, 141, 13, 96, 61, 232, 146, 13, 56, 32, 59, 29, 246, 58, 175, 140, 72, 182, 196, 103, 230, 245, 229, 148
])
);
});
it('should get a value', (): void => {
expect(
trie.get(toU8a('test'))
).toEqual(toU8a('one'));
});
it('encodes KeyValue -> Uint8Array properly', () => {
expect(
encode('KeyValue', {
key: toU8a('0x11'),
value: toU8a('0x9988')
})
).toEqual(
new Uint8Array([
1, 0, 0, 0,
0x11,
2, 0, 0, 0,
0x99, 0x88
])
);
});
});
it('has the correct root after a second insertion', () => {
memory.put(toU8a('foo'), toU8a('bar'));
expect(
memory.getRoot()
).toEqual(
FOO_ROOT
);
});
it('creates a snapshot of the (relevant) trie data', () => {
const root = new Uint8Array([43, 119, 232, 84, 123, 197, 94, 42, 149, 34, 124, 147, 159, 159, 157, 103, 149, 45, 225, 233, 112, 160, 23, 224, 145, 11, 229, 16, 176, 144, 175, 243]);
const trie = new Trie();
const back = new Trie();
trie.put(toU8a('test'), toU8a('one'));
trie.put(toU8a('test'), toU8a('two'));
trie.del(toU8a('test'));
trie.put(toU8a('test'), toU8a('one'));
trie.put(toU8a('doge'), toU8a('coin'));
trie.del(toU8a('doge'));
trie.snapshot(back, () => {});
expect(back.getRoot()).toEqual(root);
expect(trie.get(toU8a('test'))).toEqual(toU8a('one'));
});
decode(
nullDecoder,
'KeyValue',
new Uint8Array([
4, 0, 0, 0,
0x11, 0x22, 0x33, 0x44,
9, 0, 0, 0,
0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11
]),
'poc-1'
)
).toEqual({
length: (4 + 4 + 4 + 9),
value: {
key: toU8a('0x11223344'),
value: toU8a('0x998877665544332211')
}
});
});