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

Common: Optional Param Dict Name to avoid Parameter Re-Addition #3696

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/block/src/header/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class BlockHeader {
chain: Mainnet, // default
})
}
this.common.updateParams(opts.params ?? paramsBlock)
this.common.updateParams(opts.params ?? paramsBlock, '@ethereumjs/block')

this.keccakFunction = this.common.customCrypto.keccak256 ?? keccak256

Expand Down
37 changes: 16 additions & 21 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class Common {
protected _hardfork: string | Hardfork
protected _eips: number[] = []
protected _params: ParamsDict
protected _includedParams: string[] = []

public readonly customCrypto: CustomCrypto

Expand Down Expand Up @@ -101,8 +102,15 @@ export class Common {
* ```
*
* @param params
* @param name Provide a self-chosen name for a param set to not unnecessarily re-include parameters
*/
updateParams(params: ParamsDict) {
updateParams(params: ParamsDict, name?: string) {
if (name !== undefined) {
if (this._includedParams.includes(name)) {
return
}
this._includedParams.push(name)
}
for (const [eip, paramsConfig] of Object.entries(params)) {
if (!(eip in this._params)) {
this._params[eip] = JSON.parse(JSON.stringify(paramsConfig)) // copy
Expand All @@ -114,26 +122,6 @@ export class Common {
this._buildParamsCache()
}

/**
* Fully resets the internal Common EIP params set with the values provided.
*
* Example Format:
*
* ```ts
* {
* 1559: {
* initialBaseFee: 1000000000,
* }
* }
* ```
*
* @param params
*/
resetParams(params: ParamsDict) {
this._params = JSON.parse(JSON.stringify(params)) // copy
this._buildParamsCache()
}

/**
* Sets the hardfork to get params for
* @param hardfork String identifier (e.g. 'byzantium') or {@link Hardfork} enum
Expand Down Expand Up @@ -842,6 +830,13 @@ export class Common {
*/
copy(): Common {
const copy = Object.assign(Object.create(Object.getPrototypeOf(this)), this)
// TODO: hotfix since copy() method is still not working properly in Common,
// discovered along double association of this.common in the tx library
// (BaseTransaction + Subclass), where not all properties made it to the
// copied object. So this copy() should be replaced with a more robust/correct
// method (simple re-instantiation likely, needs various tests though to secure)
copy._buildParamsCache()
copy._buildActivatedEIPsCache()
copy.events = new EventEmitter()
return copy
}
Expand Down
12 changes: 5 additions & 7 deletions packages/common/test/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ describe('[Common]: Parameter instantiation / params option / Updates', () => {
c.updateParams(params)
msg = 'Should update parameter on updateParams() and properly rebuild cache'
assert.equal(c.param('bn254AddGas'), BigInt(250), msg)

c.resetParams(params)
msg = 'Should reset all parameters on resetParams() and properly rebuild cache'
assert.equal(c.param('bn254AddGas'), BigInt(250), msg)
assert.throws(() => {
c.param('bn254MulGas'), BigInt(250)
})
assert.equal(c['_includedParams'].length, 0, msg)

msg = 'Should not side-manipulate the original params file during updating internally'
assert.equal(paramsTest['1679']['bn254AddGas'], 150)

msg = 'Should add optional parameter set name to parameter set names array'
c.updateParams(params, 'myparams')
assert.equal(c['_includedParams'].length, 1, msg)
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class EVM implements EVMInterface {
)
}

this.common.updateParams(opts.params ?? paramsEVM)
this.common.updateParams(opts.params ?? paramsEVM, '@ethereumjs/evm')

this.allowUnlimitedContractSize = opts.allowUnlimitedContractSize ?? false
this.allowUnlimitedInitCodeSize = opts.allowUnlimitedInitCodeSize ?? false
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/1559/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class FeeMarket1559Tx extends BaseTransaction<TransactionType.FeeMarketEI
`Common chain ID ${this.common.chainId} not matching the derived chain ID ${chainId}`,
)
}
this.common.updateParams(opts.params ?? paramsTx)
this.common.updateParams(opts.params ?? paramsTx, '@ethereumjs/tx')
this.chainId = this.common.chainId()

if (!this.common.isActivatedEIP(1559)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/2930/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class AccessList2930Transaction extends BaseTransaction<TransactionType.A
`Common chain ID ${this.common.chainId} not matching the derived chain ID ${chainId}`,
)
}
this.common.updateParams(opts.params ?? paramsTx)
this.common.updateParams(opts.params ?? paramsTx, '@ethereumjs/tx')
this.chainId = this.common.chainId()

// EIP-2718 check is done in Common
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/4844/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export function createBlob4844TxFromSerializedNetworkWrapper(
}

const commonCopy = opts.common.copy()
commonCopy.updateParams(opts.params ?? paramsTx)
commonCopy.updateParams(opts.params ?? paramsTx, '@ethereumjs/tx')

const version = Number(commonCopy.param('blobCommitmentVersionKzg'))
const blobsHex = blobs.map((blob) => bytesToHex(blob))
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/4844/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Blob4844Tx extends BaseTransaction<TransactionType.BlobEIP4844> {
`Common chain ID ${this.common.chainId} not matching the derived chain ID ${chainId}`,
)
}
this.common.updateParams(opts.params ?? paramsTx)
this.common.updateParams(opts.params ?? paramsTx, '@ethereumjs/tx')
this.chainId = this.common.chainId()

if (!this.common.isActivatedEIP(1559)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/7702/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class EOACode7702Transaction extends BaseTransaction<TransactionType.EOAC
`Common chain ID ${this.common.chainId} not matching the derived chain ID ${chainId}`,
)
}
this.common.updateParams(opts.params ?? paramsTx)
this.common.updateParams(opts.params ?? paramsTx, '@ethereumjs/tx')
this.chainId = this.common.chainId()

if (!this.common.isActivatedEIP(7702)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/baseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export abstract class BaseTransaction<T extends TransactionType>
const createContract = this.to === undefined || this.to === null
const allowUnlimitedInitCodeSize = opts.allowUnlimitedInitCodeSize ?? false

this.common.updateParams(opts.params ?? paramsTx)
this.common.updateParams(opts.params ?? paramsTx, '@ethereumjs/tx')
if (
createContract &&
this.common.isActivatedEIP(3860) &&
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/legacy/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class LegacyTx extends BaseTransaction<TransactionType.Legacy> {
)
}

this.common.updateParams(opts.params ?? paramsTx)
this.common.updateParams(opts.params ?? paramsTx, '@ethereumjs/tx')
this.keccakFunction = this.common.customCrypto.keccak256 ?? keccak256
this.gasPrice = bytesToBigInt(toBytes(txData.gasPrice))

Expand Down
3 changes: 2 additions & 1 deletion packages/tx/test/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type { AccessList2930TxData, FeeMarketEIP1559TxData, LegacyTxData } from

describe('[BaseTransaction]', () => {
// EIP-2930 is not enabled in Common by default (2021-03-06)
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
let common = new Common({ chain: Mainnet, hardfork: Hardfork.London })

const legacyTxs: BaseTransaction<TransactionType.Legacy>[] = []
for (const tx of txsData.slice(0, 4)) {
Expand Down Expand Up @@ -148,6 +148,7 @@ describe('[BaseTransaction]', () => {
`${txType.name}: tx should not be frozen when freeze deactivated in options`,
)

common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const params = JSON.parse(JSON.stringify(paramsTx))
params['1']['txGas'] = 30000 // 21000
tx = txType.create.txData({}, { common, params })
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class VM {
*/
constructor(opts: VMOpts = {}) {
this.common = opts.common!
this.common.updateParams(opts.params ?? paramsVM)
this.common.updateParams(opts.params ?? paramsVM, '@ethereumjs/vm')
this.stateManager = opts.stateManager!
this.blockchain = opts.blockchain!
this.evm = opts.evm!
Expand Down
Loading