diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..721d4cd --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,14 @@ +module.exports = { + extends: '@ethereumjs/eslint-config-defaults', + parserOptions: { + project: ['./tsconfig.json', './tsconfig.benchmarks.json'], + }, + overrides: [ + { + files: ['benchmarks/*.ts'], + rules: { + 'no-console': 'off', + }, + }, + ], +} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4633006..0179235 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/setup-node@v1 - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - run: npm install - run: npm run lint @@ -20,7 +20,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/setup-node@v1 - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - run: npm install - run: npm run coverage - name: Upload coverage to Coveralls @@ -38,6 +38,6 @@ jobs: uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - run: npm install - run: npm run test diff --git a/benchmarks/checkpointing.ts b/benchmarks/checkpointing.ts index ffad930..f47bc72 100644 --- a/benchmarks/checkpointing.ts +++ b/benchmarks/checkpointing.ts @@ -5,35 +5,33 @@ const iterations = 5000 const samples = 5 const iterTest = async (numOfIter: number): Promise> => { - return new Promise(async (resolve) => { - let vals = [] as any - let keys = [] as any + const vals = [] as any + const keys = [] as any - for (let i = 0; i <= numOfIter; i++) { - vals.push(pseudoRandomBytes(32)) - keys.push(pseudoRandomBytes(32)) - } + for (let i = 0; i <= numOfIter; i++) { + vals.push(pseudoRandomBytes(32)) + keys.push(pseudoRandomBytes(32)) + } - let hrstart = process.hrtime() - let numOfOps = 0 - let trie = new CheckpointTrie() - for (let i = 0; i < numOfIter; i++) { - trie.checkpoint() - await trie.put(vals[i], keys[i]) - await trie.get(Buffer.from('test')) - numOfOps++ - if (numOfOps === numOfIter) { - const hrend = process.hrtime(hrstart) - resolve(hrend) - } - trie.commit() + const hrstart = process.hrtime() + let numOfOps = 0 + const trie = new CheckpointTrie() + for (let i = 0; i < numOfIter; i++) { + trie.checkpoint() + await trie.put(vals[i], keys[i]) + await trie.get(Buffer.from('test')) + numOfOps++ + if (numOfOps === numOfIter) { + const hrend = process.hrtime(hrstart) + return hrend } - }) + await trie.commit() + } } const go = async () => { let i = 1 - let avg = [0, 0] + const avg = [0, 0] console.log(`Benchmark 'checkpointing' starting...`) while (i <= samples) { @@ -47,8 +45,8 @@ const go = async () => { console.log( 'benchmarks/checkpointing.ts | average execution time: %ds %dms', avg[0] / samples, - (avg[1] / 1000000 / samples).toFixed(3), + (avg[1] / 1000000 / samples).toFixed(3) ) } -go() +go().catch(console.error) diff --git a/benchmarks/random.ts b/benchmarks/random.ts index 40dcf28..824ead9 100644 --- a/benchmarks/random.ts +++ b/benchmarks/random.ts @@ -43,4 +43,4 @@ const go = async () => { console.timeEnd(testName) } -go() +go().catch(console.error) diff --git a/karma.conf.js b/karma.conf.js index d872533..aa906ad 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -7,6 +7,7 @@ module.exports = function (config) { }, plugins: ['karma-typescript', 'karma-tap', 'karma-chrome-launcher', 'karma-firefox-launcher'], karmaTypescriptConfig: { + tsconfig: './tsconfig.json', bundlerOptions: { entrypoints: /\.spec\.ts$/, }, diff --git a/package.json b/package.json index feecfa3..cfa3559 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,6 @@ "docs:build": "typedoc", "lint": "ethereumjs-config-lint", "lint:fix": "ethereumjs-config-lint-fix", - "format": "ethereumjs-config-format", - "format:fix": "ethereumjs-config-format-fix", - "formatTest": "node ./scripts/formatTest", "tsc": "ethereumjs-config-tsc", "test": "npm run test:node && npm run test:browser", "test:browser": "npm run build && karma start karma.conf.js", @@ -60,10 +57,9 @@ }, "devDependencies": { "0x": "^4.9.1", - "@ethereumjs/config-nyc": "^1.1.1", - "@ethereumjs/config-prettier": "^1.1.1", - "@ethereumjs/config-tsc": "^1.1.1", - "@ethereumjs/config-tslint": "^1.1.1", + "@ethereumjs/config-coverage": "^2.0.0", + "@ethereumjs/config-typescript": "^2.0.0", + "@ethereumjs/eslint-config-defaults": "^2.0.0", "@types/bn.js": "^4.11.6", "@types/tape": "^4.2.34", "husky": "^4.2.3", @@ -76,11 +72,9 @@ "prettier": "^2.0.2", "tape": "^4.13.2", "ts-node": "^8.8.1", - "tslint": "^5.18.0", "typedoc": "next", "typedoc-plugin-markdown": "^2.2.17", - "typescript": "^3.7.5", - "typestrict": "^1.0.2" + "typescript": "^3.7.5" }, "contributors": [ "Aaron Kumavis (https://github.com/kumavis)" diff --git a/prettier.config.js b/prettier.config.js index 0f2e5b7..7147272 100644 --- a/prettier.config.js +++ b/prettier.config.js @@ -1 +1 @@ -module.exports = require('@ethereumjs/config-prettier') +module.exports = require('@ethereumjs/eslint-config-defaults/prettier.config.js') diff --git a/src/baseTrie.ts b/src/baseTrie.ts index fbb660f..6523a06 100644 --- a/src/baseTrie.ts +++ b/src/baseTrie.ts @@ -30,7 +30,7 @@ type FoundNodeFunction = ( nodeRef: Buffer, node: TrieNode, key: Nibbles, - walkController: any, + walkController: any ) => void /** @@ -154,9 +154,10 @@ export class Trie { * @param key - the search key */ async findPath(key: Buffer): Promise { + // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve) => { - let stack: TrieNode[] = [] - let targetKey = bufferToNibbles(key) + const stack: TrieNode[] = [] + const targetKey = bufferToNibbles(key) const onFound: FoundNodeFunction = async (nodeRef, node, keyProgress, walkController) => { const keyRemainder = targetKey.slice(matchingNibbleLength(keyProgress, targetKey)) @@ -213,6 +214,7 @@ export class Trie { * @returns Resolves when finished walking trie. */ async _walkTrie(root: Buffer, onFound: FoundNodeFunction): Promise { + // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve) => { const self = this root = root || this.root @@ -230,7 +232,7 @@ export class Trie { const processNode = async ( nodeRef: Buffer, node: TrieNode, - key: Nibbles = [], + key: Nibbles = [] ): Promise => { const walkController = { next: async () => { @@ -259,7 +261,7 @@ export class Trie { const childNode = await self._lookupNode(childRef) taskCallback() if (childNode) { - processNode(childRef, childNode as TrieNode, childKey) + await processNode(childRef, childNode as TrieNode, childKey) } }) } @@ -344,7 +346,7 @@ export class Trie { k: Buffer, value: Buffer, keyRemainder: Nibbles, - stack: TrieNode[], + stack: TrieNode[] ): Promise { const toSave: BatchDBOp[] = [] const lastNode = stack.pop() @@ -353,7 +355,7 @@ export class Trie { } // add the new nodes - let key = bufferToNibbles(k) + const key = bufferToNibbles(k) // Check if the last node is a leaf and the key matches to this let matchLeaf = false @@ -449,7 +451,7 @@ export class Trie { branchKey: number, branchNode: TrieNode, parentNode: TrieNode, - stack: TrieNode[], + stack: TrieNode[] ) => { // branchNode is the node ON the branch node not THE branch node if (!parentNode || parentNode instanceof BranchNode) { @@ -549,7 +551,7 @@ export class Trie { branchNodeKey, foundNode as TrieNode, parentNode as TrieNode, - stack, + stack ) await this._saveStack(key, stack, opStack) } @@ -613,7 +615,7 @@ export class Trie { node: TrieNode, topLevel: boolean, opStack: BatchDBOp[], - remove: boolean = false, + remove: boolean = false ): Buffer | (EmbeddedNode | null)[] { const rlpNode = node.serialize() @@ -673,7 +675,7 @@ export class Trie { * @param trie */ static async fromProof(proof: Proof, trie?: Trie): Promise { - let opStack = proof.map((nodeValue) => { + const opStack = proof.map((nodeValue) => { return { type: 'put', key: keccak(nodeValue), diff --git a/src/checkpointTrie.ts b/src/checkpointTrie.ts index 26abb29..105ee9b 100644 --- a/src/checkpointTrie.ts +++ b/src/checkpointTrie.ts @@ -109,7 +109,7 @@ export class CheckpointTrie extends BaseTrie { * @private */ async _exitCpMode(commitState: boolean): Promise { - return new Promise(async (resolve) => { + return new Promise((resolve) => { const scratch = this._scratch as ScratchDB this._scratch = null this.db = this._mainDB @@ -130,7 +130,7 @@ export class CheckpointTrie extends BaseTrie { * @private */ _createScratchReadStream(scratchDb?: ScratchDB) { - let scratch = scratchDb || this._scratch + const scratch = scratchDb || this._scratch if (!scratch) { throw new Error('No scratch found to use') } diff --git a/src/db.ts b/src/db.ts index ad2c011..0735355 100644 --- a/src/db.ts +++ b/src/db.ts @@ -45,9 +45,8 @@ export class DB { } else { throw error } - } finally { - return value } + return value } /** diff --git a/src/trieNode.ts b/src/trieNode.ts index 103bca3..8f7d8e5 100644 --- a/src/trieNode.ts +++ b/src/trieNode.ts @@ -9,32 +9,6 @@ export type Nibbles = number[] // hash to next node, or embed it if its len < 32 export type EmbeddedNode = Buffer | Buffer[] -export function decodeNode(raw: Buffer): TrieNode { - const des = rlp.decode(raw) - if (!Array.isArray(des)) { - throw new Error('Invalid node') - } - return decodeRawNode(des) -} - -export function decodeRawNode(raw: Buffer[]): TrieNode { - if (raw.length === 17) { - return BranchNode.fromArray(raw) - } else if (raw.length === 2) { - const nibbles = bufferToNibbles(raw[0]) - if (isTerminator(nibbles)) { - return new LeafNode(LeafNode.decodeKey(nibbles), raw[1]) - } - return new ExtensionNode(ExtensionNode.decodeKey(nibbles), raw[1]) - } else { - throw new Error('Invalid node') - } -} - -export function isRawNode(n: any): boolean { - return Array.isArray(n) && !Buffer.isBuffer(n) -} - export class BranchNode { _branches: (EmbeddedNode | null)[] _value: Buffer | null @@ -87,7 +61,7 @@ export class BranchNode { getChildren(): [number, EmbeddedNode][] { const children: [number, EmbeddedNode][] = [] for (let i = 0; i < 16; i++) { - let b = this._branches[i] + const b = this._branches[i] if (b !== null && b.length > 0) { children.push([i, b]) } @@ -195,3 +169,29 @@ export class LeafNode { return keccak256(this.serialize()) } } + +export function decodeRawNode(raw: Buffer[]): TrieNode { + if (raw.length === 17) { + return BranchNode.fromArray(raw) + } else if (raw.length === 2) { + const nibbles = bufferToNibbles(raw[0]) + if (isTerminator(nibbles)) { + return new LeafNode(LeafNode.decodeKey(nibbles), raw[1]) + } + return new ExtensionNode(ExtensionNode.decodeKey(nibbles), raw[1]) + } else { + throw new Error('Invalid node') + } +} + +export function decodeNode(raw: Buffer): TrieNode { + const des = rlp.decode(raw) + if (!Array.isArray(des)) { + throw new Error('Invalid node') + } + return decodeRawNode(des) +} + +export function isRawNode(n: any): boolean { + return Array.isArray(n) && !Buffer.isBuffer(n) +} diff --git a/src/util/nibbles.ts b/src/util/nibbles.ts index a441dd2..89b91d7 100644 --- a/src/util/nibbles.ts +++ b/src/util/nibbles.ts @@ -7,7 +7,7 @@ import { Nibbles } from '../trieNode' */ export function bufferToNibbles(key: Buffer): Nibbles { const bkey = Buffer.from(key) - let nibbles = [] as any + const nibbles = [] as any for (let i = 0; i < bkey.length; i++) { let q = i * 2 @@ -25,7 +25,7 @@ export function bufferToNibbles(key: Buffer): Nibbles { * @param arr - Nibble array */ export function nibblesToBuffer(arr: Nibbles): Buffer { - let buf = Buffer.alloc(arr.length / 2) + const buf = Buffer.alloc(arr.length / 2) for (let i = 0; i < buf.length; i++) { let q = i * 2 buf[i] = (arr[q] << 4) + arr[++q] diff --git a/test/checkpoint.spec.ts b/test/checkpoint.spec.ts index 61dc410..1846650 100644 --- a/test/checkpoint.spec.ts +++ b/test/checkpoint.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { CheckpointTrie } from '../src' tape('testing checkpoints', function (tester) { @@ -94,9 +94,8 @@ tape('testing checkpoints', function (tester) { it('should commit a nested checkpoint', async function (t) { trie.checkpoint() - let root: Buffer await trie.put(Buffer.from('test'), Buffer.from('something else')) - root = trie.root + const { root } = trie trie.checkpoint() await trie.put(Buffer.from('the feels'), Buffer.from('emotion')) await trie.revert() diff --git a/test/db.spec.ts b/test/db.spec.ts index a340184..84e28f7 100644 --- a/test/db.spec.ts +++ b/test/db.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { DB, BatchDBOp } from '../src/db' tape('DB basic functionality', (t) => { diff --git a/test/encoding.spec.ts b/test/encoding.spec.ts index 46ffdba..b4f95a4 100644 --- a/test/encoding.spec.ts +++ b/test/encoding.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { toBuffer } from 'ethereumjs-util' import { CheckpointTrie } from '../src' diff --git a/test/index.spec.ts b/test/index.spec.ts index 5617210..d9b8ddc 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import * as rlp from 'rlp' import { KECCAK256_NULL } from 'ethereumjs-util' import { CheckpointTrie } from '../src' @@ -9,7 +9,7 @@ tape('simple save and retrieve', function (tester) { it('should not crash if given a non-existent root', async function (t) { const root = Buffer.from( '3f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d', - 'hex', + 'hex' ) const trie = new CheckpointTrie(null, root) const value = await trie.get(Buffer.from('test')) @@ -59,7 +59,7 @@ tape('simple save and retrieve', function (tester) { await trie.put(Buffer.from('doge'), Buffer.from('coin')) t.equal( 'de8a34a8c1d558682eae1528b47523a483dd8685d6db14b291451a66066bf0fc', - trie.root.toString('hex'), + trie.root.toString('hex') ) t.end() }) @@ -115,7 +115,7 @@ tape('simple save and retrieve', function (tester) { await trie.put(Buffer.from('do'), Buffer.from('verb')) t.equal( 'f803dfcb7e8f1afd45e88eedb4699a7138d6c07b71243d9ae9bff720c99925f9', - trie.root.toString('hex'), + trie.root.toString('hex') ) t.end() }) @@ -124,7 +124,7 @@ tape('simple save and retrieve', function (tester) { await trie.put(Buffer.from('done'), Buffer.from('finished')) t.equal( '409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', - trie.root.toString('hex'), + trie.root.toString('hex') ) t.end() }) @@ -148,7 +148,7 @@ tape('simple save and retrieve', function (tester) { await trie.put(Buffer.from('done'), Buffer.from('finished')) t.equal( '409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', - trie.root.toString('hex'), + trie.root.toString('hex') ) t.end() }) diff --git a/test/official.spec.ts b/test/official.spec.ts index de886fc..a33360f 100644 --- a/test/official.spec.ts +++ b/test/official.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { CheckpointTrie } from '../src' tape('official tests', async function (t) { diff --git a/test/prioritizedTaskExecutor.spec.ts b/test/prioritizedTaskExecutor.spec.ts index 36c9651..b78bf96 100644 --- a/test/prioritizedTaskExecutor.spec.ts +++ b/test/prioritizedTaskExecutor.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { PrioritizedTaskExecutor } from '../src/prioritizedTaskExecutor' const taskExecutor = new PrioritizedTaskExecutor(2) diff --git a/test/proof.spec.ts b/test/proof.spec.ts index ec11c99..b589786 100644 --- a/test/proof.spec.ts +++ b/test/proof.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { CheckpointTrie } from '../src' tape('simple merkle proofs generation and verification', function (tester) { @@ -75,11 +75,11 @@ tape('simple merkle proofs generation and verification', function (tester) { await trie.put( Buffer.from('key1aa'), - Buffer.from('0123456789012345678901234567890123456789xxx'), + Buffer.from('0123456789012345678901234567890123456789xxx') ) await trie.put( Buffer.from('key1'), - Buffer.from('0123456789012345678901234567890123456789Very_Long'), + Buffer.from('0123456789012345678901234567890123456789Very_Long') ) await trie.put(Buffer.from('key2bb'), Buffer.from('aval3')) await trie.put(Buffer.from('key2'), Buffer.from('short')) diff --git a/test/scratch.spec.ts b/test/scratch.spec.ts index 2ef8527..d277a18 100644 --- a/test/scratch.spec.ts +++ b/test/scratch.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { DB } from '../src/db' import { ScratchDB } from '../src/scratch' diff --git a/test/secure.spec.ts b/test/secure.spec.ts index 72b6d2f..4625ffe 100644 --- a/test/secure.spec.ts +++ b/test/secure.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { SecureTrie } from '../src' tape('SecureTrie', function (t) { @@ -72,17 +72,17 @@ tape('SecureTrie', function (t) { const trie = new SecureTrie() const a = Buffer.from( 'f8448080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', - 'hex', + 'hex' ) const ak = Buffer.from('095e7baea6a6c7c4c2dfeb977efac326af552d87', 'hex') const b = Buffer.from( 'f844802ea056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0db94dc4aab9b6a1a11956906ea34f3252f394576aece12199b23b269bb2738ab', - 'hex', + 'hex' ) const bk = Buffer.from('945304eb96065b2a98b57a48a06ae28d285a71b5', 'hex') const c = Buffer.from( 'f84c80880de0b6b3a7640000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', - 'hex', + 'hex' ) const ck = Buffer.from('a94f5374fce5edbc8e2a8697c15331677e6ebf0b', 'hex') // checkpoint @@ -90,23 +90,23 @@ const ck = Buffer.from('a94f5374fce5edbc8e2a8697c15331677e6ebf0b', 'hex') // commit const d = Buffer.from( 'f8488084535500b1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', - 'hex', + 'hex' ) const dk = Buffer.from('095e7baea6a6c7c4c2dfeb977efac326af552d87', 'hex') const e = Buffer.from( 'f8478083010851a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0db94dc4aab9b6a1a11956906ea34f3252f394576aece12199b23b269bb2738ab', - 'hex', + 'hex' ) const ek = Buffer.from('945304eb96065b2a98b57a48a06ae28d285a71b5', 'hex') const f = Buffer.from( 'f84c01880de0b6b3540df72ca056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', - 'hex', + 'hex' ) const fk = Buffer.from('a94f5374fce5edbc8e2a8697c15331677e6ebf0b', 'hex') // commit const g = Buffer.from( 'f8488084535500b1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', - 'hex', + 'hex' ) const gk = Buffer.from('095e7baea6a6c7c4c2dfeb977efac326af552d87', 'hex') diff --git a/test/stream.spec.ts b/test/stream.spec.ts index bdee5a3..cb28a8f 100644 --- a/test/stream.spec.ts +++ b/test/stream.spec.ts @@ -1,4 +1,4 @@ -import * as tape from 'tape' +import tape from 'tape' import { CheckpointTrie } from '../src' import { BatchDBOp } from '../src/db' diff --git a/tsconfig.benchmarks.json b/tsconfig.benchmarks.json new file mode 100644 index 0000000..a6c5d7b --- /dev/null +++ b/tsconfig.benchmarks.json @@ -0,0 +1,5 @@ +{ + "extends": "@ethereumjs/config-typescript", + "include": ["benchmarks/*.ts"] +} + \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index e88e376..288c2a8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "@ethereumjs/config-tsc", + "extends": "@ethereumjs/config-typescript", "include": ["src/**/*.ts", "test/*.spec.ts"] } \ No newline at end of file diff --git a/tsconfig.prod.json b/tsconfig.prod.json index 29b17bf..f59f9af 100644 --- a/tsconfig.prod.json +++ b/tsconfig.prod.json @@ -1,5 +1,5 @@ { - "extends": "@ethereumjs/config-tsc", + "extends": "@ethereumjs/config-typescript", "compilerOptions": { "outDir": "./dist", "target": "ES2017" diff --git a/tslint.json b/tslint.json deleted file mode 100644 index 2ba21c4..0000000 --- a/tslint.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "@ethereumjs/config-tslint" -}