Skip to content

Commit c8f340e

Browse files
committed
tx, vm, block: renamed tx Transaction type to TypedTransaction, fixed compatibility issues in VM and Block
1 parent d5f5969 commit c8f340e

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

packages/block/src/block.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { BaseTrie as Trie } from 'merkle-patricia-tree'
44
import { BN, rlp, keccak256, KECCAK256_RLP } from 'ethereumjs-util'
55
import Common from '@ethereumjs/common'
6-
import { TransactionFactory, Transaction, TxOptions } from '@ethereumjs/tx'
6+
import { TransactionFactory, TypedTransaction, TxOptions } from '@ethereumjs/tx'
77
import { BlockHeader } from './header'
88
import { BlockData, BlockOptions, JsonBlock, BlockBuffer, Blockchain } from './types'
99

@@ -12,7 +12,7 @@ import { BlockData, BlockOptions, JsonBlock, BlockBuffer, Blockchain } from './t
1212
*/
1313
export class Block {
1414
public readonly header: BlockHeader
15-
public readonly transactions: Transaction[] = []
15+
public readonly transactions: TypedTransaction[] = []
1616
public readonly uncleHeaders: BlockHeader[] = []
1717
public readonly txTrie = new Trie()
1818
public readonly _common: Common
@@ -130,7 +130,7 @@ export class Block {
130130
*/
131131
constructor(
132132
header?: BlockHeader,
133-
transactions: Transaction[] = [],
133+
transactions: TypedTransaction[] = [],
134134
uncleHeaders: BlockHeader[] = [],
135135
opts: BlockOptions = {}
136136
) {

packages/block/src/from-rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TransactionFactory, Transaction, TxData } from '@ethereumjs/tx'
1+
import { TransactionFactory, TypedTransaction, TxData } from '@ethereumjs/tx'
22
import { toBuffer, setLengthLeft } from 'ethereumjs-util'
33
import { Block, BlockOptions } from './index'
44

@@ -32,7 +32,7 @@ function normalizeTxParams(_txParams: any) {
3232
export default function blockFromRpc(blockParams: any, uncles: any[] = [], options?: BlockOptions) {
3333
const header = blockHeaderFromRpc(blockParams, options)
3434

35-
const transactions: Transaction[] = []
35+
const transactions: TypedTransaction[] = []
3636
if (blockParams.transactions) {
3737
const opts = { common: header._common }
3838
for (const _txParams of blockParams.transactions) {

packages/tx/src/transactionFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Common from '@ethereumjs/common'
22
import { default as Transaction } from './legacyTransaction'
33
import { default as AccessListEIP2930Transaction } from './eip2930Transaction'
4-
import { TxOptions, Transaction, TxData } from './types'
4+
import { TxOptions, TypedTransaction, TxData } from './types'
55
import { BN } from 'ethereumjs-util'
66

77
const DEFAULT_COMMON = new Common({ chain: 'mainnet' })
@@ -15,7 +15,7 @@ export default class TransactionFactory {
1515
* @param txData - The transaction data. The `type` field will determine which transaction type is returned (if undefined, create a Transaction)
1616
* @param txOptions - Options to pass on to the constructor of the transaction
1717
*/
18-
public static fromTxData(txData: TxData, txOptions: TxOptions = {}): Transaction {
18+
public static fromTxData(txData: TxData, txOptions: TxOptions = {}): TypedTransaction {
1919
const common = txOptions.common ?? DEFAULT_COMMON
2020
if (txData.type === undefined) {
2121
// Assume Transaction
@@ -32,7 +32,7 @@ export default class TransactionFactory {
3232
* @param rawData - The raw data buffer
3333
* @param txOptions - The transaction options
3434
*/
35-
public static fromRawData(rawData: Buffer, txOptions: TxOptions = {}): Transaction {
35+
public static fromRawData(rawData: Buffer, txOptions: TxOptions = {}): TypedTransaction {
3636
const common = txOptions.common ?? DEFAULT_COMMON
3737
if (rawData[0] <= 0x7f) {
3838
// It is an EIP-2718 Typed Transaction

packages/tx/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export interface TxData {
131131
type?: BNLike
132132
}
133133

134-
export type Transaction = Transaction | AccessListEIP2930Transaction
134+
export type TypedTransaction = Transaction | AccessListEIP2930Transaction
135135

136136
export type BaseTransactionData = {
137137
/**

packages/vm/lib/runBlock.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ async function applyTransactions(this: VM, block: Block, opts: RunBlockOpts) {
352352
let txReceipt
353353
let encodedReceipt
354354
let receiptLog = `Generate tx receipt transactionType=${
355-
tx.transactionType
355+
'transactionType' in tx ? tx.transactionType : 'NaN'
356356
} gasUsed=${gasUsed} bitvector=${short(abstractTxReceipt.bitvector)} (${
357357
abstractTxReceipt.bitvector.length
358358
} bytes) logs=${abstractTxReceipt.logs.length}`
359-
if (tx.transactionType === 0) {
359+
if (!('transactionType' in tx) || tx.transactionType === 0) {
360360
if (this._common.gteHardfork('byzantium')) {
361361
txReceipt = {
362362
status: txRes.execResult.exceptionError ? 0 : 1, // Receipts have a 0 as status on error
@@ -373,15 +373,17 @@ async function applyTransactions(this: VM, block: Block, opts: RunBlockOpts) {
373373
receiptLog += ` stateRoot=${txReceipt.stateRoot.toString('hex')} (< Byzantium)`
374374
}
375375
encodedReceipt = encode(Object.values(txReceipt))
376-
} else if (tx.transactionType === 1) {
376+
} else if ('transactionType' in tx && tx.transactionType === 1) {
377377
txReceipt = {
378378
status: txRes.execResult.exceptionError ? 0 : 1,
379379
...abstractTxReceipt,
380380
} as EIP2930Receipt
381381

382382
encodedReceipt = Buffer.concat([Buffer.from('01', 'hex'), encode(Object.values(txReceipt))])
383383
} else {
384-
throw new Error(`Unsupported transaction type ${tx.transactionType}`)
384+
throw new Error(
385+
`Unsupported transaction type ${'transactionType' in tx ? tx.transactionType : 'NaN'}`
386+
)
385387
}
386388
debug(receiptLog)
387389

packages/vm/lib/runTx.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { debug as createDebugLogger } from 'debug'
22
import { Address, BN } from 'ethereumjs-util'
33
import { Block } from '@ethereumjs/block'
4-
import { AccessListItem, AccessListEIP2930Transaction, Transaction } from '@ethereumjs/tx'
4+
import { AccessListItem, AccessListEIP2930Transaction, TypedTransaction } from '@ethereumjs/tx'
55
import VM from './index'
66
import Bloom from './bloom'
77
import { default as EVM, EVMResult } from './evm/evm'
@@ -25,7 +25,7 @@ export interface RunTxOpts {
2525
/**
2626
* An `@ethereumjs/tx` to run
2727
*/
28-
tx: Transaction
28+
tx: TypedTransaction
2929
/**
3030
* If true, skips the nonce check
3131
*/
@@ -64,7 +64,7 @@ export interface AfterTxEvent extends RunTxResult {
6464
/**
6565
* The transaction which just got finished
6666
*/
67-
transaction: Transaction
67+
transaction: TypedTransaction
6868
}
6969

7070
/**
@@ -99,7 +99,11 @@ export default async function runTx(this: VM, opts: RunTxOpts): Promise<RunTxRes
9999
debug(`tx checkpoint`)
100100

101101
// Is it an Access List transaction?
102-
if (opts.tx.transactionType === 1 && this._common.isActivatedEIP(2929)) {
102+
if (
103+
'transactionType' in opts.tx &&
104+
opts.tx.transactionType === 1 &&
105+
this._common.isActivatedEIP(2929)
106+
) {
103107
if (!this._common.isActivatedEIP(2930)) {
104108
throw new Error('Cannot run transaction: EIP 2930 is not activated.')
105109
}

0 commit comments

Comments
 (0)