Remove temp db interface, cleanup logging (#161)
This commit is contained in:
@@ -99,7 +99,7 @@ export default class BaseTrie {
|
||||
* @returns {Function} Promise<void>
|
||||
*/
|
||||
async put (key: Uint8Array, value: Uint8Array) {
|
||||
l.debug(() => ['put', 'key ->', u8aToHex(key), 'value ->', u8aToHex(value)]);
|
||||
l.debug(() => ['put', 'key ->', u8aToHex(key), 'value ->', u8aToHex(value, 256)]);
|
||||
|
||||
if (!value || value.toString() === '') {
|
||||
return this.del(key);
|
||||
@@ -244,7 +244,7 @@ export default class BaseTrie {
|
||||
const stack: any[] = []; // FIXME
|
||||
const targetKey = nibblesFromU8a(_targetKey);
|
||||
|
||||
l.debug(() => ['findPath', targetKey.join(', ')]);
|
||||
// l.debug(() => ['findPath', targetKey.join(', ')]);
|
||||
|
||||
// @ts-ignore FIXME, we need to properly check the full file
|
||||
const processNode = (nodeRef, node, keyProgress, walkController) => {
|
||||
|
||||
@@ -7,10 +7,10 @@ import { Logger } from '@polkadot/util/types';
|
||||
import { HashFn, DecodedRlp, NodeFactory, Trie$Node, Trie$Node$Type } from './types';
|
||||
|
||||
import logger from '@polkadot/util/logger';
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
// import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
|
||||
import nibblesFromU8a from './nibbles/fromU8a';
|
||||
import rlpToString from './util/rlpToString';
|
||||
// import rlpToString from './util/rlpToString';
|
||||
|
||||
import TrieNode from './Node';
|
||||
|
||||
@@ -43,7 +43,7 @@ function getNodeType (node: DecodedRlp): Trie$Node$Type {
|
||||
|
||||
function fromBranch (l: Logger, hashing: HashFn) {
|
||||
return (rlp: DecodedRlp): Trie$Node => {
|
||||
l.debug(() => ['fromBranch']);
|
||||
// l.debug(() => ['fromBranch']);
|
||||
|
||||
const node = new TrieNode(hashing, 'branch', Array.apply(null, Array(17)));
|
||||
|
||||
@@ -62,7 +62,7 @@ function fromBranch (l: Logger, hashing: HashFn) {
|
||||
|
||||
function fromRaw (l: Logger, hashing: HashFn) {
|
||||
return (rlp: DecodedRlp): Trie$Node => {
|
||||
l.debug(() => ['fromRaw ->', rlpToString(rlp)]);
|
||||
// l.debug(() => ['fromRaw ->', rlpToString(rlp)]);
|
||||
|
||||
const node = new TrieNode(hashing, getNodeType(rlp), rlp);
|
||||
|
||||
@@ -72,7 +72,7 @@ function fromRaw (l: Logger, hashing: HashFn) {
|
||||
|
||||
function fromType (l: Logger, hashing: HashFn, type: Trie$Node$Type) {
|
||||
return (key: Uint8Array, value: Uint8Array): Trie$Node => {
|
||||
l.debug(() => ['fromType', 'type ->', type, 'key ->', u8aToHex(key), 'value ->', u8aToHex(value)]);
|
||||
// l.debug(() => ['fromType', 'type ->', type, 'key ->', u8aToHex(key), 'value ->', u8aToHex(value, 256)]);
|
||||
|
||||
const node = new TrieNode(hashing, type, Array(2));
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ import { Trie$Base } from '../types';
|
||||
// @ts-ignore FIXME, we need to properly check the full file
|
||||
import { Readable } from 'readable-stream';
|
||||
import logger from '@polkadot/util/logger';
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
// import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
|
||||
import rlpToString from '../util/rlpToString';
|
||||
// import rlpToString from '../util/rlpToString';
|
||||
|
||||
const l = logger('trie/scratch');
|
||||
|
||||
@@ -39,7 +39,7 @@ export default class ScratchReadStream extends Readable {
|
||||
};
|
||||
// @ts-ignore FIXME, we need to properly check the full file
|
||||
const onFound = (nodeRef, node, key, next) => {
|
||||
l.debug(() => ['onFound', u8aToHex(nodeRef), node.type, rlpToString(node.raw)]);
|
||||
// l.debug(() => ['onFound', u8aToHex(nodeRef), node.type, rlpToString(node.raw)]);
|
||||
|
||||
// @ts-ignore FIXME, we need to properly check the full file
|
||||
this.push({
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Trie$Base } from '../types';
|
||||
// @ts-ignore FIXME, we need to properly check the full file
|
||||
import { Readable } from 'readable-stream';
|
||||
import logger from '@polkadot/util/logger';
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
// import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
|
||||
import { nibblesToU8a } from '../nibbles';
|
||||
|
||||
@@ -40,7 +40,7 @@ export default class TrieReadStream extends Readable {
|
||||
};
|
||||
// @ts-ignore FIXME, we need to properly check the full file
|
||||
const onFound = (nodeRef, node, nibbles, next) => {
|
||||
l.debug(() => ['onFound', nibbles, u8aToHex(node.value)]);
|
||||
// l.debug(() => ['onFound', nibbles, u8aToHex(node.value)]);
|
||||
|
||||
// @ts-ignore FIXME, we need to properly check the full file
|
||||
this.push({
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
ISC License (ISC)
|
||||
|
||||
Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
@@ -1,13 +0,0 @@
|
||||
[](https://polkadot.js.org)
|
||||

|
||||
[](https://github.com/Flet/semistandard)
|
||||
[](https://www.npmjs.com/package/@polkadot/util-triedb)
|
||||
[](https://travis-ci.org/polkadot-js/common)
|
||||
[](https://codeclimate.com/github/polkadot-js/common/maintainability)
|
||||
[](https://coveralls.io/github/polkadot-js/common?branch=master)
|
||||
[](https://david-dm.org/polkadot-js/common?path=packages/util-triedb)
|
||||
[](https://david-dm.org/polkadot-js/common?path=packages/util-triedb#info=devDependencies)
|
||||
|
||||
# @polkadot/util-triedb
|
||||
|
||||
DEPRECTATED, but still used until converted
|
||||
@@ -1,7 +0,0 @@
|
||||
# Available interfaces
|
||||
|
||||
|
||||
|
||||
- [temp](temp.md)
|
||||
|
||||
# Available methods
|
||||
@@ -1,3 +0,0 @@
|
||||
# temp
|
||||
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
{
|
||||
"name": "@polkadot/util-triedb",
|
||||
"version": "0.26.11",
|
||||
"description": "Creation of triehash values",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"Polkadot"
|
||||
],
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"maintainers": [
|
||||
"Jaco Greeff <jacogr@gmail.com>"
|
||||
],
|
||||
"contributors": [],
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=6.4"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/polkadot-js/common.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/polkadot-js/common/issues"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme",
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-babel && polkadot-dev-build-docs",
|
||||
"check": "eslint src && flow check",
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.0.0-beta.51",
|
||||
"@polkadot/trie-hash": "^0.26.11",
|
||||
"@polkadot/util": "^0.26.11",
|
||||
"@polkadot/util-crypto": "^0.26.11",
|
||||
"@polkadot/util-rlp": "^0.26.11",
|
||||
"leveldown": "^4.0.1",
|
||||
"levelup": "^3.0.1",
|
||||
"memdown": "^3.0.0"
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { TrieDbState } from './types';
|
||||
|
||||
export default function clear (self: TrieDbState): void {
|
||||
// TODO
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { TrieDbState } from './types';
|
||||
|
||||
export default function commit (self: TrieDbState): void {
|
||||
// TODO
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
// import { LevelUp$AbstractStorage } from 'levelup';
|
||||
// import { TrieDb } from './types';
|
||||
|
||||
// const clear from './clear');
|
||||
// const commit from './commit');
|
||||
// const state from './state');
|
||||
|
||||
// export default function triedb (db: LevelUp$AbstractStorage): TrieDb {
|
||||
// const self = state(db);
|
||||
|
||||
// return {
|
||||
// clear: (): void =>
|
||||
// clear(self),
|
||||
// commit: (): void =>
|
||||
// commit(self)
|
||||
// };
|
||||
// };
|
||||
@@ -1,19 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { TrieDbState } from './types';
|
||||
|
||||
import logger from '@polkadot/util/logger';
|
||||
|
||||
import createTemp from './temp';
|
||||
|
||||
export default function state (db: {}): TrieDbState {
|
||||
const storage = createTemp();
|
||||
|
||||
return {
|
||||
db,
|
||||
l: logger('triedb'),
|
||||
storage
|
||||
};
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import factory from './index';
|
||||
|
||||
describe('clear', () => {
|
||||
let memory;
|
||||
|
||||
beforeEach(() => {
|
||||
memory = factory();
|
||||
});
|
||||
|
||||
it('clears all values', () => {
|
||||
memory.set(new Uint8Array([1]), new Uint8Array([2]));
|
||||
memory.clear();
|
||||
|
||||
expect(
|
||||
memory.isEmpty()
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -1,63 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import factory from './index';
|
||||
|
||||
describe('commit', () => {
|
||||
let memory;
|
||||
const k1 = new Uint8Array([1]);
|
||||
const k2 = new Uint8Array([2]);
|
||||
const v1 = new Uint8Array([9]);
|
||||
const v2 = new Uint8Array([8]);
|
||||
|
||||
beforeEach(() => {
|
||||
memory = factory();
|
||||
memory.commit([
|
||||
{ k: k1, v: v1 }
|
||||
]);
|
||||
});
|
||||
|
||||
it('sets values to back-end', () => {
|
||||
expect(
|
||||
memory.get(k1)
|
||||
).toEqual(v1);
|
||||
});
|
||||
|
||||
it('removes empty values from storage', () => {
|
||||
memory.commit([
|
||||
{ k: k1, v: null },
|
||||
{ k: k2, v: v2 }
|
||||
]);
|
||||
|
||||
expect(
|
||||
memory.get(k1)
|
||||
).toEqual(null);
|
||||
expect(
|
||||
memory.get(k2)
|
||||
).toEqual(v2);
|
||||
expect(
|
||||
memory.pairs()
|
||||
).toEqual([
|
||||
{ k: k2, v: v2 }
|
||||
]);
|
||||
});
|
||||
|
||||
it('ignores empty values, not in storage', () => {
|
||||
memory.commit([
|
||||
{ k: k2, v: null }
|
||||
]);
|
||||
|
||||
expect(
|
||||
memory.get(k2)
|
||||
).toEqual(null);
|
||||
});
|
||||
|
||||
it('ignores empty array', () => {
|
||||
memory.commit();
|
||||
|
||||
expect(
|
||||
memory.get(k1)
|
||||
).toEqual(v1);
|
||||
});
|
||||
});
|
||||
@@ -1,19 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Trie$Pairs } from '@polkadot/trie-hash/types';
|
||||
import { Temp$Storage } from './types';
|
||||
|
||||
import del from './del';
|
||||
import set from './set';
|
||||
|
||||
export default function commit (storage: Temp$Storage, values: Trie$Pairs): void {
|
||||
values.forEach(({ k, v }) => {
|
||||
if (v && v.length) {
|
||||
set(storage, k, v);
|
||||
} else {
|
||||
del(storage, k);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import factory from './index';
|
||||
|
||||
describe('del', () => {
|
||||
let memory;
|
||||
|
||||
beforeEach(() => {
|
||||
memory = factory();
|
||||
});
|
||||
|
||||
it('removes a value', () => {
|
||||
memory.set(new Uint8Array([1]), new Uint8Array([2]));
|
||||
memory.del(new Uint8Array([1]));
|
||||
|
||||
expect(
|
||||
memory.isEmpty()
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Temp$Storage } from './types';
|
||||
|
||||
export default function del (storage: Temp$Storage, k: Uint8Array): void {
|
||||
// @ts-ignore yes, U8a index
|
||||
delete storage[k];
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Temp$Storage } from './types';
|
||||
|
||||
export default function get (storage: Temp$Storage, k: Uint8Array): Uint8Array | null {
|
||||
// @ts-ignore yes, U8a index
|
||||
const value = storage[k];
|
||||
|
||||
return value && value.v
|
||||
? value.v.slice()
|
||||
: null;
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Trie$Pairs } from '@polkadot/trie-hash/types';
|
||||
import { TrieDb } from '../types';
|
||||
import { Temp$Storage } from './types';
|
||||
|
||||
import trieRoot from '@polkadot/trie-hash/root';
|
||||
|
||||
import commit from './commit';
|
||||
import del from './del';
|
||||
import get from './get';
|
||||
import pairs from './pairs';
|
||||
import set from './set';
|
||||
|
||||
export default function temp (): TrieDb {
|
||||
let storage: Temp$Storage = {};
|
||||
|
||||
return {
|
||||
clear: (): void => {
|
||||
storage = {};
|
||||
},
|
||||
commit: (values: Trie$Pairs = []): void =>
|
||||
commit(storage, values),
|
||||
del: (k: Uint8Array): void =>
|
||||
del(storage, k),
|
||||
isEmpty: (): boolean =>
|
||||
Object.keys(storage).length === 0,
|
||||
get: (k: Uint8Array): Uint8Array | null =>
|
||||
get(storage, k),
|
||||
pairs: (): Trie$Pairs =>
|
||||
pairs(storage),
|
||||
set: (k: Uint8Array, v: Uint8Array): void =>
|
||||
set(storage, k, v),
|
||||
trieRoot: (): Uint8Array =>
|
||||
trieRoot(pairs(storage))
|
||||
};
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Trie$Pairs } from '@polkadot/trie-hash/types';
|
||||
import { Temp$Storage } from './types';
|
||||
|
||||
export default function pairs (storage: Temp$Storage): Trie$Pairs {
|
||||
return Object
|
||||
.keys(storage)
|
||||
.map((k) => storage[k]);
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Temp$Storage } from './types';
|
||||
|
||||
export default function set (storage: Temp$Storage, k: Uint8Array, v: Uint8Array): void {
|
||||
// @ts-ignore we coax into string here, bad I know
|
||||
storage[k] = {
|
||||
k: k.slice(),
|
||||
v: v.slice()
|
||||
};
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb 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 hexToU8a from '@polkadot/util/hex/toU8a';
|
||||
|
||||
import factory from './index';
|
||||
|
||||
describe('trieRoot', () => {
|
||||
let temp;
|
||||
|
||||
beforeEach(() => {
|
||||
temp = factory();
|
||||
});
|
||||
|
||||
it('calculates the correct trieRoot', () => {
|
||||
temp.set(u8aFromString('doe'), u8aFromString('reindeer'));
|
||||
temp.set(u8aFromString('dog'), u8aFromString('puppy'));
|
||||
temp.set(u8aFromString('dogglesworth'), u8aFromString('cat'));
|
||||
|
||||
expect(
|
||||
temp.trieRoot()
|
||||
).toEqual(
|
||||
hexToU8a(
|
||||
'0x8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3'
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Trie$Pair } from '@polkadot/trie-hash/types';
|
||||
|
||||
export type Temp$Storage = {
|
||||
[index: string]: Trie$Pair
|
||||
};
|
||||
Vendored
-23
@@ -1,23 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/util-triedb authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { Logger } from '@polkadot/util/types';
|
||||
import { Trie$Pairs } from '@polkadot/trie-hash/types';
|
||||
|
||||
export type TrieDb = {
|
||||
clear: () => void,
|
||||
commit: (values?: Trie$Pairs) => void,
|
||||
del: (key: Uint8Array) => void,
|
||||
isEmpty: () => boolean,
|
||||
get: (key: Uint8Array) => Uint8Array | null,
|
||||
pairs: () => Trie$Pairs,
|
||||
set: (key: Uint8Array, value: Uint8Array) => void,
|
||||
trieRoot: () => Uint8Array
|
||||
}
|
||||
|
||||
export type TrieDbState = {
|
||||
db: {},
|
||||
l: Logger,
|
||||
storage: TrieDb
|
||||
};
|
||||
Reference in New Issue
Block a user