Skip to content

Commit

Permalink
develop: fix client parse, move modifyAccountFields to baseStateManag…
Browse files Browse the repository at this point in the history
…er (#1765)

* client fix parse: now with common default being london, explicitly setHardforkByBlockNumber(0) when creating genesis header
* vm: move new modifyAccountFields to baseStateManager
* fix lint
  • Loading branch information
ryanio authored and g11tech committed Jun 2, 2022
1 parent 5e09ca5 commit e64d3ed
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
6 changes: 3 additions & 3 deletions packages/client/lib/util/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ async function createGethGenesisBlockHeader(json: any) {
stateRoot,
baseFeePerGas,
}
let common
let common = new Common({ chain: 1 })
if (json.config.londonBlock === 0) {
// chainId is not important here, we just want to set
// hardfork to London for baseFeePerGas support
const hardforks = new Common({ chain: 1 })
const hardforks = common
.hardforks()
.map((h) => (h.name === Hardfork.London ? { ...h, block: 0 } : h))
common = Common.custom({ chainId: 1, hardforks })
common.setHardforkByBlockNumber(0)
}
common.setHardforkByBlockNumber(0)
return BlockHeader.fromHeaderData(headerData, { common })
}

Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/miner/miner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Common, { Chain as CommonChain, Hardfork } from '@ethereumjs/common'
import { FeeMarketEIP1559Transaction, Transaction } from '@ethereumjs/tx'
import { Block, BlockHeader } from '@ethereumjs/block'
import { DefaultStateManager, StateManager } from '@ethereumjs/vm/dist/state'
import { Account, Address, BN, keccak256 } from 'ethereumjs-util'
import { Address, BN, keccak256 } from 'ethereumjs-util'
import { Config } from '../../lib/config'
import { FullEthereumService } from '../../lib/service'
import { Chain } from '../../lib/blockchain'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/miner/pendingBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Transaction } from '@ethereumjs/tx'
import { BlockHeader } from '@ethereumjs/block'
import VM from '@ethereumjs/vm'
import { DefaultStateManager, StateManager } from '@ethereumjs/vm/dist/state'
import { Account, Address, BN } from 'ethereumjs-util'
import { Address, BN } from 'ethereumjs-util'
import { Config } from '../../lib/config'
import { TxPool } from '../../lib/service/txpool'
import { PendingBlock } from '../../lib/miner'
Expand Down
17 changes: 17 additions & 0 deletions packages/vm/src/state/baseStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { debug as createDebugLogger, Debugger } from 'debug'
import { Account, Address, toBuffer } from 'ethereumjs-util'
import { ripemdPrecompileAddress } from '../evm/precompiles'
import Cache from './cache'
import { AccountFields } from './interface'
import { DefaultStateManagerOpts } from './stateManager'

type AddressHex = string
Expand Down Expand Up @@ -108,6 +109,22 @@ export abstract class BaseStateManager {
this.touchAccount(address)
}

/**
* Gets the account associated with `address`, modifies the given account
* fields, then saves the account into state. Account fields can include
* `nonce`, `balance`, `stateRoot`, and `codeHash`.
* @param address - Address of the account to modify
* @param accountFields - Object containing account fields and values to modify
*/
async modifyAccountFields(address: Address, accountFields: AccountFields): Promise<void> {
const account = await this.getAccount(address)
account.nonce = accountFields.nonce ?? account.nonce
account.balance = accountFields.balance ?? account.balance
account.stateRoot = accountFields.stateRoot ?? account.stateRoot
account.codeHash = accountFields.codeHash ?? account.codeHash
await this.putAccount(address, account)
}

/**
* Deletes an account from state under the provided `address`. The account will also be removed from the state trie.
* @param address - Address of the account which should be deleted
Expand Down
18 changes: 1 addition & 17 deletions packages/vm/src/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
setLengthLeft,
} from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { StateManager, StorageDump, AccountFields } from './interface'
import { StateManager, StorageDump } from './interface'
import Cache, { getCb, putCb } from './cache'
import { BaseStateManager } from './'
import { short } from '../evm/opcodes'
Expand Down Expand Up @@ -518,20 +518,4 @@ export default class DefaultStateManager extends BaseStateManager implements Sta
}
return false
}

/**
* Gets the account associated with `address`, modifies the given account
* fields, then saves the account into state. Account fields can include
* `nonce`, `balance`, `stateRoot`, and `codeHash`.
* @param address - Address of the account to modify
* @param accountFields - Object containing account fields and values to modify
*/
async modifyAccountFields(address: Address, accountFields: AccountFields): Promise<void> {
const account = await this.getAccount(address)
account.nonce = accountFields.nonce ?? account.nonce
account.balance = accountFields.balance ?? account.balance
account.stateRoot = accountFields.stateRoot ?? account.stateRoot
account.codeHash = accountFields.codeHash ?? account.codeHash
await this.putAccount(address, account)
}
}

0 comments on commit e64d3ed

Please sign in to comment.