-
Notifications
You must be signed in to change notification settings - Fork 846
Expand file tree
/
Copy pathsuite.ts
More file actions
64 lines (54 loc) · 1.86 KB
/
suite.ts
File metadata and controls
64 lines (54 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { keccak_256 } from '@noble/hashes/sha3.js'
//@ts-expect-error - package has no types...
import { mark, utils } from 'micro-bmark' // cspell:disable-line
import { MerklePatriciaTrie } from '../dist/cjs/index.js'
import { keys } from './keys'
import type { DB } from '@ethereumjs/util'
export function createSuite(
db: DB<string, string>,
hashFn: (msg: Uint8Array) => Uint8Array = keccak_256,
label: string = '',
) {
const trie = new MerklePatriciaTrie({ db, useKeyHashingFunction: hashFn })
const checkpointTrie = new MerklePatriciaTrie({ db, useKeyHashingFunction: hashFn })
const ROUNDS = 1000
const KEY_SIZE = 32
;(async () => {
// random.ts
// Test ID is defined as: `pair_count`-`era_size`-`key_size`-`value_type`
// where value_type = symmetric ? 'mir' : 'ran'
// The standard secure-trie test is `1k-9-32-ran`
for (const [title, eraSize, symmetric] of [
['1k-3-32-ran', 3, false],
['1k-5-32-ran', 5, false],
['1k-9-32-ran', 9, false],
['1k-1k-32-ran', 1000, false],
['1k-1k-32-mir', 1000, true],
]) {
await mark(`${label}${title}`, async () => {
let key = new Uint8Array(KEY_SIZE)
for (let i = 0; i <= ROUNDS; i++) {
key = hashFn(key)
if (symmetric) {
await trie.put(key, key)
} else {
await trie.put(key, key)
}
if (i % (eraSize as number) === 0) {
key = trie.root()
}
}
})
}
// References:
// https://gist.github.com/heikoheiko/0fa2b322560ba7794f22/
for (const samples of [100, 500, 1000, 5000]) {
await mark(`${label}Checkpointing: ${samples} iterations`, samples, async (i: number) => {
checkpointTrie.checkpoint()
await checkpointTrie.put(keys[i], keys[i])
await checkpointTrie.commit()
})
}
utils.logMem()
})()
}