Skip to content

Commit

Permalink
tx: clarify some method names (#3535)
Browse files Browse the repository at this point in the history
Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
  • Loading branch information
jochem-brouwer and acolytec3 authored Jul 24, 2024
1 parent da22a38 commit 759dcd2
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 59 deletions.
4 changes: 2 additions & 2 deletions packages/tx/src/1559/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export class FeeMarketEIP1559Transaction extends BaseTransaction<TransactionType
/**
* The amount of gas paid for the data in this tx
*/
getDataFee(): bigint {
return EIP2930.getDataFee(this)
getDataGas(): bigint {
return EIP2930.getDataGas(this)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/2930/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export class AccessListEIP2930Transaction extends BaseTransaction<TransactionTyp
/**
* The amount of gas paid for the data in this tx
*/
getDataFee(): bigint {
return EIP2930.getDataFee(this)
getDataGas(): bigint {
return EIP2930.getDataGas(this)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/4844/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ export class BlobEIP4844Transaction extends BaseTransaction<TransactionType.Blob
/**
* The amount of gas paid for the data in this tx
*/
getDataFee(): bigint {
return EIP2930.getDataFee(this)
getDataGas(): bigint {
return EIP2930.getDataGas(this)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/7702/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class EOACodeEIP7702Transaction extends BaseTransaction<TransactionType.E
/**
* The amount of gas paid for the data in this tx
*/
getDataFee(): bigint {
return EIP7702.getDataFee(this)
getDataGas(): bigint {
return EIP7702.getDataGas(this)
}

/**
Expand Down
21 changes: 13 additions & 8 deletions packages/tx/src/baseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ export abstract class BaseTransaction<T extends TransactionType>
errors.push('Invalid Signature')
}

if (this.getBaseFee() > this.gasLimit) {
errors.push(`gasLimit is too low. given ${this.gasLimit}, need at least ${this.getBaseFee()}`)
if (this.getIntrinsicGas() > this.gasLimit) {
errors.push(
`gasLimit is too low. given ${this.gasLimit}, need at least ${this.getIntrinsicGas()}`
)
}

return errors
Expand All @@ -171,11 +173,14 @@ export abstract class BaseTransaction<T extends TransactionType>
}

/**
* The minimum amount of gas the tx must have (DataFee + TxFee + Creation Fee)
* The minimum gas limit which the tx to have to be valid.
* This covers costs as the standard fee (21000 gas), the data fee (paid for each calldata byte),
* the optional creation fee (if the transaction creates a contract), and if relevant the gas
* to be paid for access lists (EIP-2930) and authority lists (EIP-7702).
*/
getBaseFee(): bigint {
getIntrinsicGas(): bigint {
const txFee = this.common.param('txGas')
let fee = this.getDataFee()
let fee = this.getDataGas()
if (txFee) fee += txFee
if (this.common.gteHardfork('homestead') && this.toCreationAddress()) {
const txCreationFee = this.common.param('txCreationGas')
Expand All @@ -185,9 +190,9 @@ export abstract class BaseTransaction<T extends TransactionType>
}

/**
* The amount of gas paid for the data in this tx
* The amount of gas paid for the calldata in this tx
*/
getDataFee(): bigint {
getDataGas(): bigint {
const txDataZero = this.common.param('txDataZeroGas')
const txDataNonZero = this.common.param('txDataNonZeroGas')

Expand All @@ -213,7 +218,7 @@ export abstract class BaseTransaction<T extends TransactionType>
abstract getEffectivePriorityFee(baseFee: bigint | undefined): bigint

/**
* The up front amount that an account must have for this transaction to be valid
* The upfront amount of wei to be paid in order for this tx to be valid and included in a block
*/
abstract getUpfrontCost(): bigint

Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/capabilities/eip2930.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import type { EIP2930CompatibleTx } from '../types.js'
/**
* The amount of gas paid for the data in this tx
*/
export function getDataFee(tx: EIP2930CompatibleTx): bigint {
return Legacy.getDataFee(tx, BigInt(AccessLists.getDataFeeEIP2930(tx.accessList, tx.common)))
export function getDataGas(tx: EIP2930CompatibleTx): bigint {
return Legacy.getDataGas(tx, BigInt(AccessLists.getDataGasEIP2930(tx.accessList, tx.common)))
}
6 changes: 3 additions & 3 deletions packages/tx/src/capabilities/eip7702.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import type { EIP7702CompatibleTx } from '../types.js'
/**
* The amount of gas paid for the data in this tx
*/
export function getDataFee(tx: EIP7702CompatibleTx): bigint {
const eip2930Cost = BigInt(AccessLists.getDataFeeEIP2930(tx.accessList, tx.common))
export function getDataGas(tx: EIP7702CompatibleTx): bigint {
const eip2930Cost = BigInt(AccessLists.getDataGasEIP2930(tx.accessList, tx.common))
const eip7702Cost = BigInt(
tx.authorizationList.length * Number(tx.common.param('perAuthBaseGas'))
)
return Legacy.getDataFee(tx, eip2930Cost + eip7702Cost)
return Legacy.getDataGas(tx, eip2930Cost + eip7702Cost)
}
4 changes: 2 additions & 2 deletions packages/tx/src/capabilities/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export function isSigned(tx: LegacyTxInterface): boolean {
/**
* The amount of gas paid for the data in this tx
*/
export function getDataFee(tx: LegacyTxInterface, extraCost?: bigint): bigint {
export function getDataGas(tx: LegacyTxInterface, extraCost?: bigint): bigint {
if (tx.cache.dataFee && tx.cache.dataFee.hardfork === tx.common.hardfork()) {
return tx.cache.dataFee.value
}

const cost = BaseTransaction.prototype.getDataFee.bind(tx)() + (extraCost ?? 0n)
const cost = BaseTransaction.prototype.getDataGas.bind(tx)() + (extraCost ?? 0n)

if (Object.isFrozen(tx)) {
tx.cache.dataFee = {
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/legacy/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ export class LegacyTransaction extends BaseTransaction<TransactionType.Legacy> {
/**
* The amount of gas paid for the data in this tx
*/
getDataFee(): bigint {
return Legacy.getDataFee(this)
getDataGas(): bigint {
return Legacy.getDataGas(this)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export interface TransactionInterface<T extends TransactionType = TransactionTyp
readonly cache: TransactionCache
supports(capability: Capability): boolean
type: TransactionType
getBaseFee(): bigint
getDataFee(): bigint
getIntrinsicGas(): bigint
getDataGas(): bigint
getUpfrontCost(): bigint
toCreationAddress(): boolean
raw(): TxValuesArray[T]
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class AccessLists {
return accessListJSON
}

public static getDataFeeEIP2930(accessList: AccessListBytes, common: Common): number {
public static getDataGasEIP2930(accessList: AccessListBytes, common: Common): number {
const accessListStorageKeyCost = common.param('accessListStorageKeyGas')
const accessListAddressCost = common.param('accessListAddressGas')

Expand Down Expand Up @@ -214,7 +214,7 @@ export class AuthorizationLists {
}
}

public static getDataFeeEIP7702(authorityList: AuthorizationListBytes, common: Common): number {
public static getDataGasEIP7702(authorityList: AuthorizationListBytes, common: Common): number {
const perAuthBaseCost = common.param('perAuthBaseGas')
return authorityList.length * Number(perAuthBaseCost)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/test/eip3860.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('[EIP3860 tests]', () => {
{ common, allowUnlimitedInitCodeSize: false }
)
assert.ok(
eip3860ActiveTx.getDataFee() === eip3860DeactivedTx.getDataFee(),
eip3860ActiveTx.getDataGas() === eip3860DeactivedTx.getDataGas(),
'charged initcode analysis gas'
)
}
Expand Down
24 changes: 12 additions & 12 deletions packages/tx/test/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,52 +181,52 @@ describe('[Transaction]', () => {
}
})

it('getBaseFee() -> should return base fee', () => {
it('getIntrinsicGas() -> should return base fee', () => {
const tx = createLegacyTx({})
assert.equal(tx.getBaseFee(), BigInt(53000))
assert.equal(tx.getIntrinsicGas(), BigInt(53000))
})

it('getDataFee() -> should return data fee', () => {
it('getDataGas() -> should return data fee', () => {
let tx = createLegacyTx({})
assert.equal(tx.getDataFee(), BigInt(0))
assert.equal(tx.getDataGas(), BigInt(0))

tx = createLegacyTxFromBytesArray(
txFixtures[3].raw.map((rawTxData) => hexToBytes(rawTxData as PrefixedHexString))
)
assert.equal(tx.getDataFee(), BigInt(1716))
assert.equal(tx.getDataGas(), BigInt(1716))

tx = createLegacyTxFromBytesArray(
txFixtures[3].raw.map((rawTxData) => hexToBytes(rawTxData as PrefixedHexString)),
{ freeze: false }
)
assert.equal(tx.getDataFee(), BigInt(1716))
assert.equal(tx.getDataGas(), BigInt(1716))
})

it('getDataFee() -> should return correct data fee for istanbul', () => {
it('getDataGas() -> should return correct data fee for istanbul', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
let tx = createLegacyTx({}, { common })
assert.equal(tx.getDataFee(), BigInt(0))
assert.equal(tx.getDataGas(), BigInt(0))

tx = createLegacyTxFromBytesArray(
txFixtures[3].raw.map((rawTxData) => hexToBytes(rawTxData as PrefixedHexString)),
{
common,
}
)
assert.equal(tx.getDataFee(), BigInt(1716))
assert.equal(tx.getDataGas(), BigInt(1716))
})

it('getDataFee() -> should invalidate cached value on hardfork change', () => {
it('getDataGas() -> should invalidate cached value on hardfork change', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Byzantium })
const tx = createLegacyTxFromBytesArray(
txFixtures[0].raw.map((rawTxData) => hexToBytes(rawTxData as PrefixedHexString)),
{
common,
}
)
assert.equal(tx.getDataFee(), BigInt(656))
assert.equal(tx.getDataGas(), BigInt(656))
tx.common.setHardfork(Hardfork.Istanbul)
assert.equal(tx.getDataFee(), BigInt(240))
assert.equal(tx.getDataGas(), BigInt(240))
})

it('getEffectivePriorityFee() -> should return correct values', () => {
Expand Down
15 changes: 8 additions & 7 deletions packages/tx/test/typedTxsAndEIP2930.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,18 @@ describe('[AccessListEIP2930Transaction / FeeMarketEIP1559Transaction] -> EIP-29
})
})

it('getDataFee()', () => {
it('getDataGas()', () => {
for (const txType of txTypes) {
let tx = txType.create.txData({}, { common })
assert.equal(tx.getDataFee(), BigInt(0), 'Should return data fee when frozen')
assert.equal(tx.getDataGas(), BigInt(0), 'Should return data fee when frozen')

tx = txType.create.txData({}, { common, freeze: false })
assert.equal(tx.getDataFee(), BigInt(0), 'Should return data fee when not frozen')
assert.equal(tx.getDataGas(), BigInt(0), 'Should return data fee when not frozen')

const mutableCommon = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
tx = txType.create.txData({}, { common: mutableCommon })
tx.common.setHardfork(Hardfork.Istanbul)
assert.equal(tx.getDataFee(), BigInt(0), 'Should invalidate cached value on hardfork change')
assert.equal(tx.getDataGas(), BigInt(0), 'Should invalidate cached value on hardfork change')
}
})
})
Expand Down Expand Up @@ -496,7 +496,7 @@ describe('[AccessListEIP2930Transaction] -> Class Specific Tests', () => {
const creationFee: number = Number(common.param('txCreationGas'))

assert.ok(
tx.getBaseFee() ===
tx.getIntrinsicGas() ===
BigInt(
txDataNonZero * 2 +
txDataZero +
Expand All @@ -517,7 +517,7 @@ describe('[AccessListEIP2930Transaction] -> Class Specific Tests', () => {
)

assert.ok(
tx.getBaseFee() ===
tx.getIntrinsicGas() ===
BigInt(
txDataNonZero * 2 +
txDataZero +
Expand All @@ -542,7 +542,8 @@ describe('[AccessListEIP2930Transaction] -> Class Specific Tests', () => {
)

assert.ok(
tx.getBaseFee() === BigInt(baseFee + accessListAddressCost * 2 + accessListStorageKeyCost * 3)
tx.getIntrinsicGas() ===
BigInt(baseFee + accessListAddressCost * 2 + accessListStorageKeyCost * 3)
)
})

Expand Down
14 changes: 7 additions & 7 deletions packages/vm/src/runTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,22 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise<RunTxResult> {
}

// Validate gas limit against tx base fee (DataFee + TxFee + Creation Fee)
const txBaseFee = tx.getBaseFee()
const intrinsicGas = tx.getIntrinsicGas()
let gasLimit = tx.gasLimit
if (gasLimit < txBaseFee) {
if (gasLimit < intrinsicGas) {
const msg = _errorMsg(
`tx gas limit ${Number(gasLimit)} is lower than the minimum gas limit of ${Number(
txBaseFee
intrinsicGas
)}`,
vm,
block,
tx
)
throw new Error(msg)
}
gasLimit -= txBaseFee
gasLimit -= intrinsicGas
if (vm.DEBUG) {
debugGas(`Subtracting base fee (${txBaseFee}) from gasLimit (-> ${gasLimit})`)
debugGas(`Subtracting base fee (${intrinsicGas}) from gasLimit (-> ${gasLimit})`)
}

if (vm.common.isActivatedEIP(1559)) {
Expand Down Expand Up @@ -576,9 +576,9 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise<RunTxResult> {
}

// Calculate the total gas used
results.totalGasSpent = results.execResult.executionGasUsed + txBaseFee
results.totalGasSpent = results.execResult.executionGasUsed + intrinsicGas
if (vm.DEBUG) {
debugGas(`tx add baseFee ${txBaseFee} to totalGasSpent (-> ${results.totalGasSpent})`)
debugGas(`tx add baseFee ${intrinsicGas} to totalGasSpent (-> ${results.totalGasSpent})`)
}

// Add blob gas used to result
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/test/api/EIPs/eip-3198-BaseFee.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('EIP3198 tests', () => {
tx: block.transactions[0],
block,
})
const txBaseFee = block.transactions[0].getBaseFee()
const txBaseFee = block.transactions[0].getIntrinsicGas()
const gasUsed = results.totalGasSpent - txBaseFee
assert.equal(gasUsed, BigInt(2), 'gas used correct')
assert.equal(stack[0], fee, 'right item pushed on stack')
Expand Down
4 changes: 2 additions & 2 deletions packages/vm/test/api/runTx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ describe('runTx() -> API return values', () => {

assert.equal(
res.totalGasSpent,
tx.getBaseFee(),
`runTx result -> gasUsed -> tx.getBaseFee() (${txType.name})`
tx.getIntrinsicGas(),
`runTx result -> gasUsed -> tx.getIntrinsicGas() (${txType.name})`
)
if (tx instanceof FeeMarketEIP1559Transaction) {
const baseFee = BigInt(7)
Expand Down

0 comments on commit 759dcd2

Please sign in to comment.