Skip to content

Commit

Permalink
vm: stateManager: add getProof EIP-1186
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Mar 31, 2021
1 parent 80223fb commit 01d5fcb
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions packages/vm/lib/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
keccak256,
KECCAK256_NULL,
unpadBuffer,
PrefixedHexString
PrefixedHexString,
bnToHex,
bufferToHex,
} from 'ethereumjs-util'
import { encode, decode } from 'rlp'
import Common from '@ethereumjs/common'
Expand All @@ -23,19 +25,18 @@ const debug = createDebugLogger('vm:state')

type AddressHex = string


type StorageProof = {
key: PrefixedHexString,
proof: PrefixedHexString[],
key: PrefixedHexString
proof: PrefixedHexString[]
value: PrefixedHexString
}

type Proof = {
balance: PrefixedHexString,
codeHash: PrefixedHexString,
nonce: PrefixedHexString,
storageHash: PrefixedHexString,
accountProof: PrefixedHexString[],
balance: PrefixedHexString
codeHash: PrefixedHexString
nonce: PrefixedHexString
storageHash: PrefixedHexString
accountProof: PrefixedHexString[]
storageProof: StorageProof[]
}

Expand Down Expand Up @@ -445,8 +446,42 @@ export default class DefaultStateManager implements StateManager {
}
}

/**
* Get an EIP-1186 proof
* @param address - address to get proof of
* @param storageSlots - storage slots to get proof of
*/
async getProof(address: Address, storageSlots: Buffer[] = []): Promise<Proof> {
this._trie.walkTrie()
const account = await this.getAccount(address)
const accountProof: PrefixedHexString[] = (
await Trie.createProof(this._trie, address.buf)
).map((e: Buffer) => bufferToHex(e))
const storageProof: StorageProof[] = []
const storageTrie = await this._getStorageTrie(address)

await Promise.all(
storageSlots.map(async (storageKey: Buffer) => {
const proof = (await Trie.createProof(storageTrie, storageKey)).map((e: Buffer) =>
bufferToHex(e)
)
const proofItem: StorageProof = {
key: bufferToHex(storageKey),
value: bufferToHex(await this.getContractStorage(address, storageKey)),
proof,
}
storageProof.push(proofItem)
})
)

const returnValue: Proof = {
balance: bnToHex(account.balance),
codeHash: bufferToHex(account.codeHash),
nonce: bnToHex(account.nonce),
storageHash: bufferToHex(account.stateRoot),
accountProof,
storageProof,
}
return returnValue
}

/**
Expand Down

0 comments on commit 01d5fcb

Please sign in to comment.