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

util: Replace account static constructors #3524

Merged
merged 6 commits into from
Jul 23, 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
6 changes: 3 additions & 3 deletions packages/client/src/sync/fetcher/trienodefetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
pathToHexKey,
} from '@ethereumjs/trie'
import {
Account,
BIGINT_0,
KECCAK256_NULL,
KECCAK256_RLP,
createAccountFromRLP,
unprefixedHexToBytes,
} from '@ethereumjs/util'
import { OrderedMap } from '@js-sdsl/ordered-map'
Expand Down Expand Up @@ -253,7 +253,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
this.debug('leaf node found')
if (storagePath === undefined) {
this.debug('account leaf node found')
const account = Account.fromRlpSerializedAccount(node.value())
const account = createAccountFromRLP(node.value())
const storageRoot: Uint8Array = account.storageRoot
if (equalsBytes(storageRoot, KECCAK256_RLP) === false) {
this.debug('storage component found')
Expand Down Expand Up @@ -366,7 +366,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
}
await storageTrie.batch(storageTrieOps, true)
await storageTrie.persistRoot()
const a = Account.fromRlpSerializedAccount(node.value())
const a = createAccountFromRLP(node.value())
this.debug(
`Stored storageTrie with root actual=${bytesToHex(
storageTrie.root()
Expand Down
10 changes: 5 additions & 5 deletions packages/statemanager/src/rpcStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Account,
bigIntToHex,
bytesToHex,
createAccount,
createAccountFromRLP,
equalsBytes,
fetchFromProvider,
hexToBytes,
Expand Down Expand Up @@ -257,17 +259,15 @@ export class RPCStateManager implements EVMStateManagerInterface {
async getAccount(address: Address): Promise<Account | undefined> {
const elem = this._accountCache?.get(address)
if (elem !== undefined) {
return elem.accountRLP !== undefined
? Account.fromRlpSerializedAccount(elem.accountRLP)
: undefined
return elem.accountRLP !== undefined ? createAccountFromRLP(elem.accountRLP) : undefined
}

const accountFromProvider = await this.getAccountFromProvider(address)
const account =
equalsBytes(accountFromProvider.codeHash, new Uint8Array(32).fill(0)) ||
equalsBytes(accountFromProvider.serialize(), KECCAK256_RLP_EMPTY_ACCOUNT)
? undefined
: Account.fromRlpSerializedAccount(accountFromProvider.serialize())
: createAccountFromRLP(accountFromProvider.serialize())

this._accountCache?.put(address, account)

Expand All @@ -285,7 +285,7 @@ export class RPCStateManager implements EVMStateManagerInterface {
method: 'eth_getProof',
params: [address.toString(), [] as any, this._blockTag],
})
const account = Account.fromAccountData({
const account = createAccount({
balance: BigInt(accountData.balance),
nonce: BigInt(accountData.nonce),
codeHash: toBytes(accountData.codeHash),
Expand Down
14 changes: 7 additions & 7 deletions packages/statemanager/src/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
bytesToHex,
bytesToUnprefixedHex,
concatBytes,
createAccount,
createAccountFromRLP,
equalsBytes,
hexToBytes,
setLengthLeft,
Expand Down Expand Up @@ -274,14 +276,12 @@ export class DefaultStateManager implements EVMStateManagerInterface {
if (!this._accountCacheSettings.deactivate) {
const elem = this._accountCache!.get(address)
if (elem !== undefined) {
return elem.accountRLP !== undefined
? Account.fromRlpSerializedAccount(elem.accountRLP)
: undefined
return elem.accountRLP !== undefined ? createAccountFromRLP(elem.accountRLP) : undefined
}
}

const rlp = await this._trie.get(address.bytes)
const account = rlp !== null ? Account.fromRlpSerializedAccount(rlp) : undefined
const account = rlp !== null ? createAccountFromRLP(rlp) : undefined
if (this.DEBUG) {
this._debug(`Get account ${address} from DB (${account ? 'exists' : 'non-existent'})`)
}
Expand Down Expand Up @@ -896,7 +896,7 @@ export class DefaultStateManager implements EVMStateManagerInterface {
throw new Error(`${notEmptyErrorMsg} (codeHash does not equal KECCAK256_NULL)`)
}
} else {
const account = Account.fromRlpSerializedAccount(value)
const account = createAccountFromRLP(value)
const { nonce, balance, storageRoot, codeHash } = account
const invalidErrorMsg = 'Invalid proof provided:'
if (nonce !== BigInt(proof.nonce)) {
Expand Down Expand Up @@ -1085,12 +1085,12 @@ export class DefaultStateManager implements EVMStateManagerInterface {
const state = initState[address]
if (!Array.isArray(state)) {
// Prior format: address -> balance
const account = Account.fromAccountData({ balance: state })
const account = createAccount({ balance: state })
await this.putAccount(addr, account)
} else {
// New format: address -> [balance, code, storage]
const [balance, code, storage, nonce] = state
const account = Account.fromAccountData({ balance, nonce })
const account = createAccount({ balance, nonce })
await this.putAccount(addr, account)
if (code !== undefined) {
await this.putContractCode(addr, toBytes(code))
Expand Down
18 changes: 9 additions & 9 deletions packages/statemanager/src/statelessVerkleStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
bytesToBigInt,
bytesToHex,
bytesToInt32,
createPartialAccount,
createPartialAccountFromRLP,
getVerkleKey,
getVerkleStem,
getVerkleTreeKeyForCodeChunk,
Expand Down Expand Up @@ -423,9 +425,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
const elem = this._accountCache!.get(address)
if (elem !== undefined) {
const account =
elem.accountRLP !== undefined
? Account.fromRlpSerializedPartialAccount(elem.accountRLP)
: undefined
elem.accountRLP !== undefined ? createPartialAccountFromRLP(elem.accountRLP) : undefined
if (account === undefined) {
const errorMsg = `account=${account} in cache`
debug(errorMsg)
Expand Down Expand Up @@ -514,7 +514,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
const elem = this._accountCache!.get(address)
if (elem !== undefined) {
return elem.accountRLP !== undefined
? Account.fromRlpSerializedPartialAccount(elem.accountRLP)
? createPartialAccountFromRLP(elem.accountRLP)
: undefined
}
}
Expand Down Expand Up @@ -572,7 +572,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
throw Error(errorMsg)
}

const account = Account.fromPartialAccountData({
const account = createPartialAccount({
version: typeof versionRaw === 'string' ? bytesToInt32(hexToBytes(versionRaw), true) : null,
balance: typeof balanceRaw === 'string' ? bytesToBigInt(hexToBytes(balanceRaw), true) : null,
nonce: typeof nonceRaw === 'string' ? bytesToBigInt(hexToBytes(nonceRaw), true) : null,
Expand Down Expand Up @@ -777,7 +777,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
return null
}

const balanceBigint = Account.fromRlpSerializedPartialAccount(encodedAccount).balance
const balanceBigint = createPartialAccountFromRLP(encodedAccount).balance
return bytesToHex(setLengthRight(bigIntToBytes(balanceBigint, true), 32))
}

Expand All @@ -786,7 +786,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
if (encodedAccount === undefined) {
return null
}
const nonceBigint = Account.fromRlpSerializedPartialAccount(encodedAccount).nonce
const nonceBigint = createPartialAccountFromRLP(encodedAccount).nonce
return bytesToHex(setLengthRight(bigIntToBytes(nonceBigint, true), 32))
}

Expand All @@ -795,7 +795,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
if (encodedAccount === undefined) {
return null
}
return bytesToHex(Account.fromRlpSerializedPartialAccount(encodedAccount).codeHash)
return bytesToHex(createPartialAccountFromRLP(encodedAccount).codeHash)
}

case AccessedStateType.CodeSize: {
Expand All @@ -807,7 +807,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface {
return null
}

const account = Account.fromRlpSerializedPartialAccount(encodedAccount)
const account = createPartialAccountFromRLP(encodedAccount)
if (account.isContract()) {
const errorMsg = `Code cache not found for address=${address.toString()}`
debug(errorMsg)
Expand Down
8 changes: 4 additions & 4 deletions packages/statemanager/test/cache/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Account, Address, equalsBytes, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { AccountCache, CacheType } from '../../src/cache/index.js'
import { createAccount } from '../util.js'
import { createAccountWithDefaults } from '../util.js'

describe('Account Cache: initialization', () => {
for (const type of [CacheType.LRU, CacheType.ORDERED_MAP]) {
Expand All @@ -19,7 +19,7 @@ describe('Account Cache: put and get account', () => {
const cache = new AccountCache({ size: 100, type })

const addr = new Address(hexToBytes(`0x${'10'.repeat(20)}`))
const acc: Account = createAccount(BigInt(1), BigInt(0xff11))
const acc: Account = createAccountWithDefaults(BigInt(1), BigInt(0xff11))
const accRLP = acc.serialize()

it('should return undefined for CacheElement if account not present in the cache', async () => {
Expand Down Expand Up @@ -52,10 +52,10 @@ describe('Account Cache: checkpointing', () => {
const cache = new AccountCache({ size: 100, type })

const addr = new Address(hexToBytes(`0x${'10'.repeat(20)}`))
const acc = createAccount(BigInt(1), BigInt(0xff11))
const acc = createAccountWithDefaults(BigInt(1), BigInt(0xff11))
const accRLP = acc.serialize()

const updatedAcc = createAccount(BigInt(0x00), BigInt(0xff00))
const updatedAcc = createAccountWithDefaults(BigInt(0x00), BigInt(0xff00))
const updatedAccRLP = updatedAcc.serialize()

it(`should revert to correct state`, async () => {
Expand Down
13 changes: 7 additions & 6 deletions packages/statemanager/test/checkpointing.account.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Account, Address, hexToBytes } from '@ethereumjs/util'
import { Address, createAccount, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { DefaultStateManager, SimpleStateManager } from '../src/index.js'

import type { StateManagerInterface } from '@ethereumjs/common'
import type { Account } from '@ethereumjs/util'

/**
* Compares account read to none or undefined
Expand Down Expand Up @@ -33,31 +34,31 @@ describe('StateManager -> Account Checkpointing', () => {
const stateManagers = [DefaultStateManager, SimpleStateManager]

const accountN1: CompareList = [
Account.fromAccountData({
createAccount({
nonce: 1,
}),
1n,
]
const accountN2: CompareList = [
Account.fromAccountData({
createAccount({
nonce: 2,
}),
2n,
]
const accountN3: CompareList = [
Account.fromAccountData({
createAccount({
nonce: 3,
}),
3n,
]
const accountN4: CompareList = [
Account.fromAccountData({
createAccount({
nonce: 4,
}),
4n,
]
const accountN5: CompareList = [
Account.fromAccountData({
createAccount({
nonce: 5,
}),
5n,
Expand Down
4 changes: 2 additions & 2 deletions packages/statemanager/test/rpcStateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { type EVMRunCallOpts, createEVM } from '@ethereumjs/evm'
import { FeeMarketEIP1559Transaction, createTxFromRPC } from '@ethereumjs/tx'
import {
Account,
Address,
bigIntToBytes,
bytesToHex,
bytesToUnprefixedHex,
createAccountFromRLP,
equalsBytes,
hexToBytes,
setLengthLeft,
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('RPC State Manager API tests', () => {

await state.putAccount(vitalikDotEth, account!)

const retrievedVitalikAccount = Account.fromRlpSerializedAccount(
const retrievedVitalikAccount = createAccountFromRLP(
(state as any)._accountCache.get(vitalikDotEth)!.accountRLP
)

Expand Down
12 changes: 6 additions & 6 deletions packages/statemanager/test/stateManager.account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assert, describe, it } from 'vitest'

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

import { createAccount } from './util.js'
import { createAccountWithDefaults } from './util.js'

describe('StateManager -> General/Account', () => {
for (const accountCacheOpts of [
Expand All @@ -17,7 +17,7 @@ describe('StateManager -> General/Account', () => {

// commit some data to the trie
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccount(BigInt(0), BigInt(1000))
const account = createAccountWithDefaults(BigInt(0), BigInt(1000))
await stateManager.checkpoint()
await stateManager.putAccount(address, account)
await stateManager.commit()
Expand All @@ -34,7 +34,7 @@ describe('StateManager -> General/Account', () => {
it(`should clear the cache when the state root is set`, async () => {
const stateManager = new DefaultStateManager({ accountCacheOpts })
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccount()
const account = createAccountWithDefaults()

// test account storage cache
const initialStateRoot = await stateManager.getStateRoot()
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('StateManager -> General/Account', () => {

it('should put and get account, and add to the underlying cache if the account is not found', async () => {
const stateManager = new DefaultStateManager({ accountCacheOpts })
const account = createAccount()
const account = createAccountWithDefaults()
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))

await stateManager.putAccount(address, account)
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('StateManager -> General/Account', () => {

it(`should return undefined for an existent account`, async () => {
const stateManager = new DefaultStateManager({ accountCacheOpts })
const account = createAccount(BigInt(0x1), BigInt(0x1))
const account = createAccountWithDefaults(BigInt(0x1), BigInt(0x1))
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))

await stateManager.putAccount(address, account)
Expand All @@ -116,7 +116,7 @@ describe('StateManager -> General/Account', () => {

it(`should modify account fields correctly`, async () => {
const stateManager = new DefaultStateManager({ accountCacheOpts })
const account = createAccount()
const account = createAccountWithDefaults()
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
await stateManager.putAccount(address, account)

Expand Down
12 changes: 6 additions & 6 deletions packages/statemanager/test/stateManager.code.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Account, Address, equalsBytes, hexToBytes } from '@ethereumjs/util'
import { Address, createAccount, equalsBytes, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

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

import { createAccount } from './util.js'
import { createAccountWithDefaults } from './util.js'

import type { AccountData } from '@ethereumjs/util'

Expand All @@ -28,7 +28,7 @@ describe('StateManager -> Code', () => {
const stateManager = new DefaultStateManager({ accountCacheOpts })
const codeStateManager = new DefaultStateManager({ accountCacheOpts })
const address1 = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
const account = createAccount()
const account = createAccountWithDefaults()
const key1 = hexToBytes(`0x${'00'.repeat(32)}`)
const key2 = hexToBytes(`0x${'00'.repeat(31)}01`)

Expand Down Expand Up @@ -91,7 +91,7 @@ describe('StateManager -> Code', () => {
balance: '0x03e7',
codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4',
}
const account = Account.fromAccountData(raw)
const account = createAccount(raw)
await stateManager.putAccount(address, account)
await stateManager.putContractCode(address, code)
const codeRetrieved = await stateManager.getContractCode(address)
Expand All @@ -105,7 +105,7 @@ describe('StateManager -> Code', () => {
nonce: '0x0',
balance: '0x03e7',
}
const account = Account.fromAccountData(raw)
const account = createAccount(raw)
await stateManager.putAccount(address, account)
const code = await stateManager.getContractCode(address)
assert.ok(equalsBytes(code, new Uint8Array(0)))
Expand All @@ -118,7 +118,7 @@ describe('StateManager -> Code', () => {
nonce: '0x0',
balance: '0x03e7',
}
const account = Account.fromAccountData(raw)
const account = createAccount(raw)
const code = new Uint8Array(0)
await stateManager.putAccount(address, account)
await stateManager.putContractCode(address, code)
Expand Down
Loading
Loading