Keey trie-db package as async library (#162)
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/trie-db authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the MPL-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BaseDb } from './types';
|
||||
|
||||
type StorageHash = {
|
||||
[index: string]: Uint8Array
|
||||
};
|
||||
|
||||
export default class HashDb implements BaseDb {
|
||||
private storage: StorageHash = {};
|
||||
|
||||
del (key: Uint8Array): void {
|
||||
delete this.storage[key.toString()];
|
||||
}
|
||||
|
||||
get (key: Uint8Array): Uint8Array | null {
|
||||
return this.storage[key.toString()] || null;
|
||||
}
|
||||
|
||||
put (key: Uint8Array, value: Uint8Array): void {
|
||||
this.storage[key.toString()] = value;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/trie-db authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the MPL-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { TrieDb } from './types';
|
||||
|
||||
import memdown from 'memdown';
|
||||
import syncify from '@polkadot/util/syncify';
|
||||
|
||||
import Trie from './Trie';
|
||||
|
||||
export default class MemoryDb implements TrieDb {
|
||||
trie: Trie;
|
||||
|
||||
constructor (root?: Uint8Array) {
|
||||
this.trie = new Trie(memdown(), root);
|
||||
}
|
||||
|
||||
checkpoint (): void {
|
||||
return syncify(
|
||||
this.trie.checkpoint()
|
||||
);
|
||||
}
|
||||
|
||||
commit (): void {
|
||||
syncify(
|
||||
this.trie.commit()
|
||||
);
|
||||
}
|
||||
|
||||
del (key: Uint8Array): void {
|
||||
syncify(
|
||||
this.trie.del(key)
|
||||
);
|
||||
}
|
||||
|
||||
get (key: Uint8Array): Uint8Array | null {
|
||||
return syncify(
|
||||
this.trie.get(key)
|
||||
);
|
||||
}
|
||||
|
||||
revert (): void {
|
||||
return syncify(
|
||||
this.trie.revert()
|
||||
);
|
||||
}
|
||||
|
||||
put (key: Uint8Array, value: Uint8Array): void {
|
||||
return syncify(
|
||||
this.trie.put(key, value)
|
||||
);
|
||||
}
|
||||
|
||||
trieRoot (): Uint8Array {
|
||||
return this.trie.root;
|
||||
}
|
||||
}
|
||||
Vendored
-13
@@ -2,19 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the MPL-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export interface BaseDb {
|
||||
del: (key: Uint8Array) => void,
|
||||
get: (key: Uint8Array) => Uint8Array | null,
|
||||
put: (key: Uint8Array, value: Uint8Array) => void
|
||||
}
|
||||
|
||||
export interface TrieDb extends BaseDb {
|
||||
checkpoint: () => void,
|
||||
commit: () => void,
|
||||
revert: () => void,
|
||||
trieRoot: () => Uint8Array
|
||||
}
|
||||
|
||||
export type Callback = (error?: Error | null, result?: any) => void;
|
||||
|
||||
// NOTE This is actually DecodedRlp = Array<Uint8Array | DecodedRlp>, but since the decoder is relatively simple, we get away with the definition below
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
|
||||
describe('testing checkpoints', () => {
|
||||
let trie, preRoot, postRoot;
|
||||
|
||||
@@ -8,7 +8,7 @@ import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
import keccakAsU8a from '@polkadot/util-crypto/keccak/asU8a';
|
||||
import encodeRlp from '@polkadot/util-rlp/encode';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
|
||||
describe('it should create the genesis state root from ethereum', () => {
|
||||
const trie4 = new Trie();
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
|
||||
describe('simple save and retrive', () => {
|
||||
it('should not crash if given a non-existant root', async () => {
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/trie-db authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the MPL-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
import MemoryDb from '../src/Memory';
|
||||
|
||||
const EMPTY_ROOT = toU8a('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421');
|
||||
const HELLO_ROOT = toU8a('0x0a915659b88f80bfa200570bd2767a6ab8cb0a4e44fd240cc5ed7a27728c4531');
|
||||
const FOO_ROOT = toU8a('0xed4e32371288ee83be74f78fb492f01261b7c3dc9c422581bb705c4376492dc6');
|
||||
|
||||
describe('MemoryDb', () => {
|
||||
let memory = new MemoryDb();
|
||||
|
||||
it('starts with the default root', () => {
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(EMPTY_ROOT);
|
||||
});
|
||||
|
||||
it('has the correct root after a single insertion', () => {
|
||||
memory.put(toU8a('hello'), toU8a('world'));
|
||||
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(
|
||||
HELLO_ROOT
|
||||
);
|
||||
});
|
||||
|
||||
it('has the correct root after a deletion', () => {
|
||||
memory.del(toU8a('hello'));
|
||||
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(
|
||||
EMPTY_ROOT
|
||||
);
|
||||
});
|
||||
|
||||
it('has the correct root after a re-insertion', () => {
|
||||
memory.put(toU8a('hello'), toU8a('world'));
|
||||
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(
|
||||
HELLO_ROOT
|
||||
);
|
||||
});
|
||||
|
||||
it('has the correct root after a second insertion', () => {
|
||||
memory.put(toU8a('foo'), toU8a('bar'));
|
||||
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(
|
||||
FOO_ROOT
|
||||
);
|
||||
});
|
||||
|
||||
it('has the correct root after a re-deletion', () => {
|
||||
memory.del(toU8a('hello'));
|
||||
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(
|
||||
toU8a('0x99650c730bbb99f6f58ce8b09bca2a8d90b36ac662e71bf81ec401ed23d199fb')
|
||||
);
|
||||
});
|
||||
|
||||
it('has the correct root after a re-re-insertion', () => {
|
||||
memory.put(toU8a('hello'), toU8a('world'));
|
||||
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(
|
||||
FOO_ROOT
|
||||
);
|
||||
});
|
||||
|
||||
it('has the correct root after an update', () => {
|
||||
memory.put(toU8a('hello'), toU8a('mars'));
|
||||
|
||||
expect(
|
||||
memory.trieRoot()
|
||||
).toEqual(
|
||||
toU8a('0x116267e482ab94eb8824af425ea7f20ee84d10a8fa735f26204d80000e2a907e')
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,7 @@
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
import { tests } from 'ethereumjs-testing';
|
||||
|
||||
const jsonTests = tests.trieTests;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
|
||||
const EMPTY_ROOT = '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421';
|
||||
const HELLO_ROOT = '0x0a915659b88f80bfa200570bd2767a6ab8cb0a4e44fd240cc5ed7a27728c4531';
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
import u8aToUtf8 from '@polkadot/util/u8a/toUtf8';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
|
||||
describe('simple merkle proofs generation and verification', () => {
|
||||
it('create a merkle proof and verify it with a single short key', async () => {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import randomAsU8a from '@polkadot/util-crypto/random/asU8a';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
|
||||
describe('put & get raw functions', () => {
|
||||
var trie = new Trie();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
import Trie from '../src/Trie';
|
||||
import Trie from '../src/index';
|
||||
|
||||
describe('kv stream test', () => {
|
||||
var trie = new Trie();
|
||||
|
||||
Reference in New Issue
Block a user