Skip to content

Commit

Permalink
Merge branch 'master' into hive-reorg-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer authored Nov 13, 2023
2 parents 8384cd4 + 3b257e9 commit 3b2e972
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 83 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/client/src/service/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ export class TxPool {
* @param peerPool Reference to the peer pool
*/
async handleAnnouncedTxHashes(txHashes: Uint8Array[], peer: Peer, peerPool: PeerPool) {
if (!this.running || txHashes.length === 0) return
if (!this.running || txHashes === undefined || txHashes.length === 0) return
this.addToKnownByPeer(txHashes, peer)

const reqHashes = []
Expand Down
22 changes: 22 additions & 0 deletions packages/verkle/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

26 changes: 10 additions & 16 deletions packages/verkle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@ npm install @ethereumjs/verkle

## Usage

This class implements the basic [Modified Merkle Patricia Trie](https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/) in the `Trie` base class, which you can use with the `useKeyHashing` option set to `true` to create a trie which stores values under the `keccak256` hash of its keys (this is the Trie flavor which is used in Ethereum production systems).

Checkpointing functionality to `Trie` through the methods `checkpoint`, `commit` and `revert`.

It is best to select the variant that is most appropriate for your unique use case.

### Initialization and Basic Usage

```typescript
import { VerkleTrie } from '@ethereumjs/verkle'
import { bytesToUtf8, utf8ToBytes } from 'ethereumjs/util'
import { VerkleTree } from '@ethereumjs/verkle'
import { bytesToUtf8, utf8ToBytes } from '@ethereumjs/util'

const trie = new VerkleTrie()
const tree = new VerkleTree()

async function test() {
await trie.put(utf8ToBytes('test'), utf8ToBytes('one'))
const value = await trie.get(utf8ToBytes('test'))
await tree.put(utf8ToBytes('test'), utf8ToBytes('one'))
const value = await tree.get(utf8ToBytes('test'))
console.log(value ? bytesToUtf8(value) : 'not found') // 'one'
}

Expand Down Expand Up @@ -89,24 +83,24 @@ Using ESM will give you additional advantages over CJS beyond browser usage like
## References

- Wiki
- [Overview of verkle tries](https://ethereum.org/en/roadmap/verkle-trees/)
- [Verkle tries General Resource](https://verkle.info/)
- [Overview of verkle trees](https://ethereum.org/en/roadmap/verkle-trees/)
- [Verkle trees general resource](https://verkle.info/)

## EthereumJS

See our organizational [documentation](https://ethereumjs.readthedocs.io) for an introduction to `EthereumJS` as well as information on current standards and best practices. If you want to join for work or carry out improvements on the libraries, please review our [contribution guidelines](https://ethereumjs.readthedocs.io/en/latest/contributing.html) first.

## License

[MPL-2.0](<https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)>)
[MIT](https://opensource.org/licenses/MIT)

[discord-badge]: https://img.shields.io/static/v1?logo=discord&label=discord&message=Join&color=blue
[discord-link]: https://discord.gg/TNwARpR
[verkle-npm-badge]: https://img.shields.io/npm/v/@ethereumjs/verkle.svg
[verkle-npm-link]: https://www.npmjs.com/package/@ethereumjs/verkle
[verkle-issues-badge]: https://img.shields.io/github/issues/ethereumjs/ethereumjs-monorepo/package:%20verkle?label=issues
[verkle-issues-link]: https://github.com/ethereumjs/ethereumjs-monorepo/issues?q=is%3Aopen+is%3Aissue+label%3A"package%3A+verkle"
[verkle-actions-badge]: https://github.com/ethereumjs/ethereumjs-monorepo/workflows/Trie/badge.svg
[verkle-actions-link]: https://github.com/ethereumjs/ethereumjs-monorepo/actions?query=workflow%3A%22Trie%22
[verkle-actions-badge]: https://github.com/ethereumjs/ethereumjs-monorepo/workflows/Verkle/badge.svg
[verkle-actions-link]: https://github.com/ethereumjs/ethereumjs-monorepo/actions?query=workflow%3A%22Verkle%22
[verkle-coverage-badge]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/branch/master/graph/badge.svg?flag=verkle
[verkle-coverage-link]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/tree/master/packages/verkle
5 changes: 3 additions & 2 deletions packages/verkle/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "@ethereumjs/verkle",
"version": "0.0.1",
"description": "Implementation of verkle tries as used in Ethereum.",
"description": "Implementation of verkle trees as used in Ethereum.",
"keywords": [
"verkle",
"tree",
"trie",
"ethereum"
],
Expand All @@ -15,7 +16,7 @@
"type": "git",
"url": "https://github.com/ethereumjs/ethereumjs-monorepo.git"
},
"license": "MPL-2.0",
"license": "MIT",
"author": "EthereumJS Team",
"contributors": [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/verkle/src/db/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CheckpointDB implements DB {
// Starting with lru-cache v8 undefined and null are not allowed any more
// as cache values. At the same time our design works well, since undefined
// indicates for us that we know that the value is not present in the
// underlying trie database as well (so it carries real value).
// underlying tree database as well (so it carries real value).
//
// Solution here seems therefore adequate, other solutions would rather
// be some not so clean workaround.
Expand All @@ -43,7 +43,7 @@ export class CheckpointDB implements DB {
constructor(opts: CheckpointDBOpts) {
this.db = opts.db
this.cacheSize = opts.cacheSize ?? 0
// Roots of trie at the moment of checkpoint
// Roots of tree at the moment of checkpoint
this.checkpoints = []

if (this.cacheSize > 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/verkle/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './db/index.js'
export * from './node/index.js'
export * from './types.js'
export * from './util/index.js'
export * from './verkleTrie.js'
export * from './verkleTree.js'
2 changes: 1 addition & 1 deletion packages/verkle/src/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface VerkleInternalNodeOptions extends BaseVerkleNodeOptions {
// Children nodes of this internal node.
children?: VerkleNode[]

// Values of the child commitments before the trie is modified by inserts.
// Values of the child commitments before the tree is modified by inserts.
// This is useful because the delta of the child commitments can be used to efficiently update the node's commitment
copyOnWrite?: Record<string, Point>
}
Expand Down
8 changes: 4 additions & 4 deletions packages/verkle/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export interface Point {

export type Proof = Uint8Array[]

export interface VerkleTrieOpts {
export interface VerkleTreeOpts {
/**
* A database instance.
*/
db?: DB<Uint8Array, Uint8Array>

/**
* A `Uint8Array` for the root of a previously stored trie
* A `Uint8Array` for the root of a previously stored tree
*/
root?: Uint8Array

Expand All @@ -77,14 +77,14 @@ export interface VerkleTrieOpts {
useRootPersistence?: boolean

/**
* LRU cache for trie nodes to allow for faster node retrieval.
* LRU cache for tree nodes to allow for faster node retrieval.
*
* Default: 0 (deactivated)
*/
cacheSize?: number
}

export type VerkleTrieOptsWithDefaults = VerkleTrieOpts & {
export type VerkleTreeOptsWithDefaults = VerkleTreeOpts & {
useRootPersistence: boolean
cacheSize: number
}
Expand Down
24 changes: 12 additions & 12 deletions packages/verkle/src/util/walkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@ import { PrioritizedTaskExecutor } from './tasks.js'

import type { VerkleNode } from '../node/types.js'
import type { FoundNodeFunction } from '../types.js'
import type { VerkleTrie } from '../verkleTrie.js'
import type { VerkleTree } from '../verkleTree.js'

/**
* WalkController is an interface to control how the trie is being traversed.
* WalkController is an interface to control how the tree is being traversed.
*/
export class WalkController {
readonly onNode: FoundNodeFunction
readonly taskExecutor: PrioritizedTaskExecutor
readonly trie: VerkleTrie
readonly tree: VerkleTree
private resolve: Function
private reject: Function

/**
* Creates a new WalkController
* @param onNode - The `FoundNodeFunction` to call if a node is found.
* @param trie - The `VerkleTrie` to walk on.
* @param tree - The `VerkleTree` to walk on.
* @param poolSize - The size of the task queue.
*/
private constructor(onNode: FoundNodeFunction, trie: VerkleTrie, poolSize: number) {
private constructor(onNode: FoundNodeFunction, tree: VerkleTree, poolSize: number) {
this.onNode = onNode
this.taskExecutor = new PrioritizedTaskExecutor(poolSize)
this.trie = trie
this.tree = tree
this.resolve = () => {}
this.reject = () => {}
}

/**
* Async function to create and start a new walk over a trie.
* Async function to create and start a new walk over a tree.
* @param onNode - The `FoundNodeFunction to call if a node is found.
* @param trie - The trie to walk on.
* @param tree - The tree to walk on.
* @param root - The root key to walk on.
* @param poolSize - Task execution pool size to prevent OOM errors. Defaults to 500.
*/
static async newWalk(
onNode: FoundNodeFunction,
trie: VerkleTrie,
tree: VerkleTree,
root: Uint8Array,
poolSize?: number
): Promise<void> {
const strategy = new WalkController(onNode, trie, poolSize ?? 500)
const strategy = new WalkController(onNode, tree, poolSize ?? 500)
await strategy.startWalk(root)
}

Expand All @@ -54,7 +54,7 @@ export class WalkController {
this.reject = reject
let node
try {
node = await this.trie.lookupNode(root)
node = await this.tree.lookupNode(root)
} catch (error) {
return this.reject(error)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export class WalkController {
async (taskFinishedCallback: Function) => {
let childNode
try {
childNode = await this.trie.lookupNode(nodeRef)
childNode = await this.tree.lookupNode(nodeRef)
} catch (error: any) {
return this.reject(error)
}
Expand Down
Loading

0 comments on commit 3b2e972

Please sign in to comment.