Skip to content

Commit

Permalink
utils: refactor trie and verkle utils (#3600)
Browse files Browse the repository at this point in the history
* verkle: remove bytes utils

* util: add matching bytes length util from verkle

* util: refactor PrioritizedTaskExecutor

* verkle: remove extra import
  • Loading branch information
gabrocheleau committed Aug 17, 2024
1 parent 2563fb8 commit 0aa7445
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 178 deletions.
40 changes: 0 additions & 40 deletions package-lock.json

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

1 change: 0 additions & 1 deletion packages/trie/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './encoding.js'
export * from './genesisState.js'
export * from './tasks.js'
export * from './walkController.js'
4 changes: 2 additions & 2 deletions packages/trie/src/util/walkController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BranchNode, ExtensionNode, LeafNode } from '../node/index.js'
import { PrioritizedTaskExecutor } from '@ethereumjs/util'

import { PrioritizedTaskExecutor } from './tasks.js'
import { BranchNode, ExtensionNode, LeafNode } from '../node/index.js'

import type { Trie } from '../trie.js'
import type { FoundNodeFunction, Nibbles, TrieNode } from '../types.js'
Expand Down
23 changes: 23 additions & 0 deletions packages/util/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,26 @@ export { bytesToUtf8, equalsBytes, utf8ToBytes } from 'ethereum-cryptography/uti
export function hexToBigInt(input: PrefixedHexString): bigint {
return bytesToBigInt(hexToBytes(isHexString(input) ? input : `0x${input}`))
}

/**
* Compares two byte arrays and returns the count of consecutively matching items from the start.
*
* @function
* @param {Uint8Array} bytes1 - The first Uint8Array to compare.
* @param {Uint8Array} bytes2 - The second Uint8Array to compare.
* @returns {number} The count of consecutively matching items from the start.
*/
export function matchingBytesLength(bytes1: Uint8Array, bytes2: Uint8Array): number {
let count = 0
const minLength = Math.min(bytes1.length, bytes2.length)

for (let i = 0; i < minLength; i++) {
if (bytes1[i] === bytes2[i]) {
count++
} else {
// Break early if a mismatch is found
break
}
}
return count
}
1 change: 1 addition & 0 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ export * from './lock.js'
export * from './mapDB.js'
export * from './provider.js'
export * from './requests.js'
export * from './tasks.js'
export * from './verkle.js'
File renamed without changes.
45 changes: 45 additions & 0 deletions packages/util/test/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
intToHex,
intToUnpaddedBytes,
isZeroAddress,
matchingBytesLength,
setLengthLeft,
setLengthRight,
short,
Expand Down Expand Up @@ -482,3 +483,47 @@ describe('unprefixedHexToBytes', () => {
assert.deepEqual(converted, new Uint8Array([17]))
})
})

describe('matchingBytesLength', () => {
it('should return 0 when both arrays are empty', () => {
const bytes1 = new Uint8Array([])
const bytes2 = new Uint8Array([])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should return 0 when one of the arrays is empty', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should return 0 when arrays have no matching elements', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([4, 5, 6])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should handle arrays with same elements but different lengths', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([1, 2, 3, 4])
assert.equal(matchingBytesLength(bytes1, bytes2), 3)
})

it('should handle arrays with matching elements at end', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([0, 1, 2, 3])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should handle arrays with matching elements at start', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([1, 2, 3, 4, 5])
assert.equal(matchingBytesLength(bytes1, bytes2), 3)
})

it('should handle arrays with large number of elements', () => {
const bytes1 = new Uint8Array(Array.from({ length: 1000000 }, (_, i) => i))
const bytes2 = new Uint8Array(Array.from({ length: 1000000 }, (_, i) => i))
assert.equal(matchingBytesLength(bytes1, bytes2), 1000000)
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, describe, it } from 'vitest'

import { PrioritizedTaskExecutor } from '../../src/index.js'
import { PrioritizedTaskExecutor } from '../src/index.js'

const taskExecutor = new PrioritizedTaskExecutor(2)

Expand Down
1 change: 0 additions & 1 deletion packages/verkle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './db/index.js'
export * from './node/index.js'
export * from './types.js'
export * from './util/index.js'
export * from './verkleTree.js'
22 changes: 0 additions & 22 deletions packages/verkle/src/util/bytes.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/verkle/src/util/index.ts

This file was deleted.

59 changes: 0 additions & 59 deletions packages/verkle/src/util/tasks.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/verkle/src/verkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
bytesToHex,
equalsBytes,
intToHex,
matchingBytesLength,
zeros,
} from '@ethereumjs/util'
import debug from 'debug'
Expand All @@ -22,7 +23,6 @@ import {
type VerkleTreeOpts,
type VerkleTreeOptsWithDefaults,
} from './types.js'
import { matchingBytesLength } from './util/index.js'

import type { DB, PutBatch, VerkleCrypto } from '@ethereumjs/util'
import type { Debugger } from 'debug'
Expand Down
47 changes: 0 additions & 47 deletions packages/verkle/test/util/bytes.spec.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/verkle/test/verkle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MapDB, equalsBytes, hexToBytes } from '@ethereumjs/util'
import { MapDB, equalsBytes, hexToBytes, matchingBytesLength } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import { assert, beforeAll, describe, it } from 'vitest'

Expand All @@ -8,7 +8,6 @@ import {
VerkleLeafNodeValue,
VerkleNodeType,
decodeNode,
matchingBytesLength,
} from '../src/index.js'
import { VerkleTree } from '../src/verkleTree.js'

Expand Down

0 comments on commit 0aa7445

Please sign in to comment.