Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof function renaming #3557

Merged
merged 8 commits into from
Aug 5, 2024
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
20 changes: 11 additions & 9 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { CacheType, Common, Mainnet } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import {
Trie,
createProof,
createMerkleProof,
createTrieFromProof,
updateFromProof,
updateTrieFromMerkleProof,
verifyTrieProof,
} from '@ethereumjs/trie'
import {
Expand Down Expand Up @@ -579,19 +579,21 @@ export class DefaultStateManager implements StateManagerInterface {
codeHash: KECCAK256_NULL_S,
nonce: '0x0',
storageHash: KECCAK256_RLP_S,
accountProof: (await createProof(this._trie, address.bytes)).map((p) => bytesToHex(p)),
accountProof: (await createMerkleProof(this._trie, address.bytes)).map((p) =>
bytesToHex(p),
),
storageProof: [],
}
return returnValue
}
const accountProof: PrefixedHexString[] = (await createProof(this._trie, address.bytes)).map(
(p) => bytesToHex(p),
)
const accountProof: PrefixedHexString[] = (
await createMerkleProof(this._trie, address.bytes)
).map((p) => bytesToHex(p))
const storageProof: StorageProof[] = []
const storageTrie = this._getStorageTrie(address, account)

for (const storageKey of storageSlots) {
const proof = (await createProof(storageTrie, storageKey)).map((p) => bytesToHex(p))
const proof = (await createMerkleProof(storageTrie, storageKey)).map((p) => bytesToHex(p))
const value = bytesToHex(await this.getStorage(address, storageKey))
const proofItem: StorageProof = {
key: bytesToHex(storageKey),
Expand Down Expand Up @@ -667,7 +669,7 @@ export class DefaultStateManager implements StateManagerInterface {
const trie = this._getStorageTrie(address)
trie.root(hexToBytes(storageHash))
for (let i = 0; i < storageProof.length; i++) {
await updateFromProof(
await updateTrieFromMerkleProof(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, especially this one I can understand a lot better just from reading it, now seeing it in the code base! 👍

trie,
storageProof[i].proof.map((e) => hexToBytes(e)),
safe,
Expand All @@ -684,7 +686,7 @@ export class DefaultStateManager implements StateManagerInterface {
async addProofData(proof: Proof | Proof[], safe: boolean = false) {
if (Array.isArray(proof)) {
for (let i = 0; i < proof.length; i++) {
await updateFromProof(
await updateTrieFromMerkleProof(
this._trie,
proof[i].accountProof.map((e) => hexToBytes(e)),
safe,
Expand Down
15 changes: 10 additions & 5 deletions packages/trie/examples/createFromProof.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Trie, createProof, createTrieFromProof, updateFromProof } from '@ethereumjs/trie'
import {
Trie,
createMerkleProof,
createTrieFromProof,
updateTrieFromMerkleProof,
} from '@ethereumjs/trie'
import { bytesToUtf8, utf8ToBytes } from '@ethereumjs/util'

async function main() {
Expand All @@ -9,12 +14,12 @@ async function main() {
await someOtherTrie.put(k1, utf8ToBytes('valueOne'))
await someOtherTrie.put(k2, utf8ToBytes('valueTwo'))

const proof = await createProof(someOtherTrie, k1)
const proof = await createMerkleProof(someOtherTrie, k1)
const trie = await createTrieFromProof(proof, { useKeyHashing: true })
const otherProof = await createProof(someOtherTrie, k2)
const otherProof = await createMerkleProof(someOtherTrie, k2)

// To add more proofs to the trie, use `updateFromProof`
await updateFromProof(trie, otherProof)
// To add more proofs to the trie, use `updateTrieFromMerkleProof`
await updateTrieFromMerkleProof(trie, otherProof)

const value = await trie.get(k1)
console.log(bytesToUtf8(value!)) // valueOne
Expand Down
6 changes: 3 additions & 3 deletions packages/trie/examples/logDemo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Run with DEBUG=ethjs,trie:* to see debug log ouput
*/
import { Trie, createProof, verifyProof } from '@ethereumjs/trie'
import { Trie, createMerkleProof, verifyMerkleProof } from '@ethereumjs/trie'
import { utf8ToBytes } from '@ethereumjs/util'

const trie_entries: [string, string | null][] = [
Expand All @@ -22,8 +22,8 @@ const main = async () => {
for (const [key, value] of trie_entries) {
await trie.put(utf8ToBytes(key), value === null ? Uint8Array.from([]) : utf8ToBytes(value))
}
const proof = await createProof(trie, utf8ToBytes('doge'))
const valid = await verifyProof(trie, trie.root(), utf8ToBytes('doge'), proof)
const proof = await createMerkleProof(trie, utf8ToBytes('doge'))
const valid = await verifyMerkleProof(trie, trie.root(), utf8ToBytes('doge'), proof)
console.log('valid', valid)
}

Expand Down
14 changes: 7 additions & 7 deletions packages/trie/examples/proofs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Trie, createProof, verifyProof } from '@ethereumjs/trie'
import { Trie, createMerkleProof, verifyMerkleProof } from '@ethereumjs/trie'
import { bytesToUtf8, utf8ToBytes } from '@ethereumjs/util'

const trie = new Trie()
Expand All @@ -11,24 +11,24 @@ async function main() {

// proof-of-inclusion
await trie.put(k1, v1)
let proof = await createProof(trie, k1)
let value = await verifyProof(trie, trie.root(), k1, proof)
let proof = await createMerkleProof(trie, k1)
let value = await verifyMerkleProof(trie, trie.root(), k1, proof)
console.log(value ? bytesToUtf8(value) : 'not found') // 'one'

// proof-of-exclusion
await trie.put(k1, v1)
await trie.put(k2, v2)
proof = await createProof(trie, utf8ToBytes('key3'))
value = await verifyProof(trie, trie.root(), utf8ToBytes('key3'), proof)
proof = await createMerkleProof(trie, utf8ToBytes('key3'))
value = await verifyMerkleProof(trie, trie.root(), utf8ToBytes('key3'), proof)
console.log(value ? bytesToUtf8(value) : 'null') // null

// invalid proof
await trie.put(k1, v1)
await trie.put(k2, v2)
proof = await createProof(trie, k2)
proof = await createMerkleProof(trie, k2)
proof[0].reverse()
try {
const _value = await verifyProof(trie, trie.root(), k2, proof) // results in error
const _value = await verifyMerkleProof(trie, trie.root(), k2, proof) // results in error
} catch (err) {
console.log(err)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/trie/src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { keccak256 } from 'ethereum-cryptography/keccak'
import { concatBytes } from 'ethereum-cryptography/utils'

import { ROOT_DB_KEY, Trie, updateFromProof } from './index.js'
import { ROOT_DB_KEY, Trie, updateTrieFromMerkleProof } from './index.js'

import type { Proof, TrieOpts } from './index.js'

Expand Down Expand Up @@ -63,7 +63,7 @@ export async function createTrie(opts?: TrieOpts) {
export async function createTrieFromProof(proof: Proof, trieOpts?: TrieOpts) {
const shouldVerifyRoot = trieOpts?.root !== undefined
const trie = new Trie(trieOpts)
const root = await updateFromProof(trie, proof, shouldVerifyRoot)
const root = await updateTrieFromMerkleProof(trie, proof, shouldVerifyRoot)
trie.root(root)
await trie.persistRoot()
return trie
Expand Down
12 changes: 8 additions & 4 deletions packages/trie/src/proof/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function verifyTrieRangeProof(
* serialized branch, extension, and/or leaf nodes.
* @param key key to create a proof for
*/
export async function createProof(trie: Trie, key: Uint8Array): Promise<Proof> {
export async function createMerkleProof(trie: Trie, key: Uint8Array): Promise<Proof> {
trie['DEBUG'] && trie['debug'](`Creating Proof for Key: ${bytesToHex(key)}`, ['CREATE_PROOF'])
const { stack } = await trie.findPath(trie['appliedKey'](key))
const p = stack.map((stackElem) => {
Expand All @@ -89,7 +89,11 @@ export async function createProof(trie: Trie, key: Uint8Array): Promise<Proof> {
* @param shouldVerifyRoot - defaults to false. If `true`, verifies that the root key of the proof matches the trie root and throws if not (i.e invalid proof).
* @returns The root of the proof
*/
export async function updateFromProof(trie: Trie, proof: Proof, shouldVerifyRoot: boolean = false) {
export async function updateTrieFromMerkleProof(
trie: Trie,
proof: Proof,
shouldVerifyRoot: boolean = false,
) {
trie['DEBUG'] && trie['debug'](`Saving (${proof.length}) proof nodes in DB`, ['FROM_PROOF'])
const opStack = proof.map((nodeValue) => {
let key = Uint8Array.from(trie['hash'](nodeValue))
Expand Down Expand Up @@ -124,7 +128,7 @@ export async function updateFromProof(trie: Trie, proof: Proof, shouldVerifyRoot
* @throws If proof is found to be invalid.
* @returns The value from the key, or null if valid proof of non-existence.
*/
export async function verifyProof(
export async function verifyMerkleProof(
trie: Trie,
rootHash: Uint8Array,
key: Uint8Array,
Expand All @@ -144,7 +148,7 @@ export async function verifyProof(
common: trie['_opts'].common,
})
try {
await updateFromProof(proofTrie, proof, true)
await updateTrieFromMerkleProof(proofTrie, proof, true)
} catch (e: any) {
throw new Error('Invalid proof nodes given')
}
Expand Down
6 changes: 3 additions & 3 deletions packages/trie/src/proof/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ async function unsetInternal(trie: Trie, left: Nibbles, right: Nibbles): Promise
* @throws If proof is found to be invalid.
* @returns The value from the key, or null if valid proof of non-existence.
*/
async function verifyProof(
async function verifyMerkleProof(
rootHash: Uint8Array,
key: Uint8Array,
proof: Uint8Array[],
Expand Down Expand Up @@ -450,7 +450,7 @@ export async function verifyRangeProof(
if (proof !== null && firstKey !== null && lastKey === null) {
// Zero element proof
if (keys.length === 0) {
const { trie, value } = await verifyProof(
const { trie, value } = await verifyMerkleProof(
rootHash,
nibblesTypeToPackedBytes(firstKey),
proof,
Expand All @@ -473,7 +473,7 @@ export async function verifyRangeProof(

// One element proof
if (keys.length === 1 && nibblesCompare(firstKey, lastKey) === 0) {
const { trie, value } = await verifyProof(
const { trie, value } = await verifyMerkleProof(
rootHash,
nibblesTypeToPackedBytes(firstKey),
proof,
Expand Down
2 changes: 1 addition & 1 deletion packages/trie/src/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ export class Trie {
const value = (await this._db.get(key)) ?? null

if (value === null) {
// Dev note: this error message text is used for error checking in `checkRoot`, `verifyProof`, and `findPath`
// Dev note: this error message text is used for error checking in `checkRoot`, `verifyMerkleProof`, and `findPath`
throw new Error('Missing node in DB')
}

Expand Down
Loading
Loading