Skip to content

Commit

Permalink
Update statemanager to use state.getBytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Nov 9, 2023
1 parent b9eb02d commit ef61d09
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions packages/portalnetwork/src/networks/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EVMStateManagerInterface, Proof, StorageDump, StorageRange } from '@eth
import { Address, Account, bytesToBigInt, bytesToHex } from '@ethereumjs/util'

import { StateNetwork } from './state.js'
import { toHexString } from '@chainsafe/ssz'

export class UltralightStateManager implements EVMStateManagerInterface {
originalStorageCache: {
Expand Down Expand Up @@ -34,7 +35,7 @@ export class UltralightStateManager implements EVMStateManagerInterface {
return new UltralightStateManager(this.state)
}
getAccount(address: Address): Promise<Account | undefined> {
return this.state.stateDB.getAccount(address.toString(), this.stateRoot)
return this.state.getAccount(address.toString(), this.stateRoot)
}
putAccount = async (address: Address, account?: Account | undefined): Promise<void> => {
return undefined
Expand All @@ -52,7 +53,9 @@ export class UltralightStateManager implements EVMStateManagerInterface {
throw new Error('Method not implemented.')
}
getContractCode = async (address: Address): Promise<Uint8Array> => {
const code = await this.state.stateDB.getCode(address.toString(), this.stateRoot)
const account = await this.state.getAccount(address.toString(), this.stateRoot)
const code =
account && (await this.state.getBytecode(toHexString(account.codeHash), address.toString()))
return code ?? new Uint8Array()
}
getContractStorage = async (address: Address, key: Uint8Array): Promise<Uint8Array> => {
Expand Down
8 changes: 4 additions & 4 deletions packages/portalnetwork/src/networks/state/statedb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ export class StateDB {

/**
* Get account data by address and state root
* @param address account address
* @param stateRoot state root
* @param account address as hex prefixed string
* @param stateRoot state root as hex prefixed string
* @returns account data
*/
async getAccount(address: Address, stateRoot: StateRoot) {
async getAccount(address: string, stateRoot: StateRoot) {
const trie = this.getAccountTrie(stateRoot)
const key = fromHexString(address.toString())
const key = fromHexString(address)
const accountRLP = await trie.get(key)
if (accountRLP === null) {
return undefined
Expand Down
13 changes: 12 additions & 1 deletion packages/portalnetwork/test/networks/state/stateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,30 @@ describe('UltralightStateManager', () => {
const codehash = keccak256(byteCode)
const account = Account.fromAccountData({ balance: 0n, nonce: 1n, codeHash: codehash })

const zero = Address.zero()
const zeroAccount = new Account()
const trie = new Trie({ useKeyHashing: true })
await trie.put(address.toBytes(), account.serialize())
await trie.put(zero.bytes, zeroAccount.serialize())

const proof = await trie.createProof(address.toBytes())
const zeroProof = await trie.createProof(zero.bytes)
const content = AccountTrieProofType.serialize({
balance: account!.balance,
nonce: account!.nonce,
codeHash: account!.codeHash,
storageRoot: account!.storageRoot,
witnesses: proof,
})
const zeroContent = AccountTrieProofType.serialize({
balance: zeroAccount!.balance,
nonce: zeroAccount!.nonce,
codeHash: zeroAccount!.codeHash,
storageRoot: zeroAccount!.storageRoot,
witnesses: zeroProof,
})
await network.stateDB.inputAccountTrieProof(address.toBytes(), trie.root(), content)

await network.stateDB.inputAccountTrieProof(zero.bytes, trie.root(), zeroContent)
const byteCodeContent = ContractByteCodeType.serialize(byteCode)
await network.stateDB.inputContractByteCode(address.toBytes(), codehash, byteCodeContent)

Expand Down

0 comments on commit ef61d09

Please sign in to comment.