Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Upgrade to ethereumjs-config 2.0 libs #133

Merged
merged 2 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
],
}
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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

coverage:
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
Expand All @@ -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
46 changes: 22 additions & 24 deletions benchmarks/checkpointing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,33 @@ const iterations = 5000
const samples = 5

const iterTest = async (numOfIter: number): Promise<Array<number>> => {
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) {
Expand All @@ -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)
2 changes: 1 addition & 1 deletion benchmarks/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ const go = async () => {
console.timeEnd(testName)
}

go()
go().catch(console.error)
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/,
},
Expand Down
14 changes: 4 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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 <http://aaron.kumavis.me/> (https://github.com/kumavis)"
Expand Down
2 changes: 1 addition & 1 deletion prettier.config.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('@ethereumjs/config-prettier')
module.exports = require('@ethereumjs/eslint-config-defaults/prettier.config.js')
24 changes: 13 additions & 11 deletions src/baseTrie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type FoundNodeFunction = (
nodeRef: Buffer,
node: TrieNode,
key: Nibbles,
walkController: any,
walkController: any
) => void

/**
Expand Down Expand Up @@ -154,9 +154,10 @@ export class Trie {
* @param key - the search key
*/
async findPath(key: Buffer): Promise<Path> {
// 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))
Expand Down Expand Up @@ -213,6 +214,7 @@ export class Trie {
* @returns Resolves when finished walking trie.
*/
async _walkTrie(root: Buffer, onFound: FoundNodeFunction): Promise<void> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
const self = this
root = root || this.root
Expand All @@ -230,7 +232,7 @@ export class Trie {
const processNode = async (
nodeRef: Buffer,
node: TrieNode,
key: Nibbles = [],
key: Nibbles = []
): Promise<void> => {
const walkController = {
next: async () => {
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -344,7 +346,7 @@ export class Trie {
k: Buffer,
value: Buffer,
keyRemainder: Nibbles,
stack: TrieNode[],
stack: TrieNode[]
): Promise<void> {
const toSave: BatchDBOp[] = []
const lastNode = stack.pop()
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -549,7 +551,7 @@ export class Trie {
branchNodeKey,
foundNode as TrieNode,
parentNode as TrieNode,
stack,
stack
)
await this._saveStack(key, stack, opStack)
}
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -673,7 +675,7 @@ export class Trie {
* @param trie
*/
static async fromProof(proof: Proof, trie?: Trie): Promise<Trie> {
let opStack = proof.map((nodeValue) => {
const opStack = proof.map((nodeValue) => {
return {
type: 'put',
key: keccak(nodeValue),
Expand Down
4 changes: 2 additions & 2 deletions src/checkpointTrie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class CheckpointTrie extends BaseTrie {
* @private
*/
async _exitCpMode(commitState: boolean): Promise<void> {
return new Promise(async (resolve) => {
return new Promise((resolve) => {
const scratch = this._scratch as ScratchDB
this._scratch = null
this.db = this._mainDB
Expand All @@ -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')
}
Expand Down
3 changes: 1 addition & 2 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ export class DB {
} else {
throw error
}
} finally {
return value
}
return value
}

/**
Expand Down
54 changes: 27 additions & 27 deletions src/trieNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions src/util/nibbles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions test/checkpoint.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as tape from 'tape'
import tape from 'tape'
import { CheckpointTrie } from '../src'

tape('testing checkpoints', function (tester) {
Expand Down Expand Up @@ -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()
Expand Down
Loading