Skip to content

Commit

Permalink
tx, vm, block: renamed tx Transaction type to TypedTransaction, fixed…
Browse files Browse the repository at this point in the history
… compatibility issues in VM and Block
  • Loading branch information
holgerd77 committed Mar 5, 2021
1 parent d5f5969 commit c8f340e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { BaseTrie as Trie } from 'merkle-patricia-tree'
import { BN, rlp, keccak256, KECCAK256_RLP } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { TransactionFactory, Transaction, TxOptions } from '@ethereumjs/tx'
import { TransactionFactory, TypedTransaction, TxOptions } from '@ethereumjs/tx'
import { BlockHeader } from './header'
import { BlockData, BlockOptions, JsonBlock, BlockBuffer, Blockchain } from './types'

Expand All @@ -12,7 +12,7 @@ import { BlockData, BlockOptions, JsonBlock, BlockBuffer, Blockchain } from './t
*/
export class Block {
public readonly header: BlockHeader
public readonly transactions: Transaction[] = []
public readonly transactions: TypedTransaction[] = []
public readonly uncleHeaders: BlockHeader[] = []
public readonly txTrie = new Trie()
public readonly _common: Common
Expand Down Expand Up @@ -130,7 +130,7 @@ export class Block {
*/
constructor(
header?: BlockHeader,
transactions: Transaction[] = [],
transactions: TypedTransaction[] = [],
uncleHeaders: BlockHeader[] = [],
opts: BlockOptions = {}
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/from-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TransactionFactory, Transaction, TxData } from '@ethereumjs/tx'
import { TransactionFactory, TypedTransaction, TxData } from '@ethereumjs/tx'
import { toBuffer, setLengthLeft } from 'ethereumjs-util'
import { Block, BlockOptions } from './index'

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

const transactions: Transaction[] = []
const transactions: TypedTransaction[] = []
if (blockParams.transactions) {
const opts = { common: header._common }
for (const _txParams of blockParams.transactions) {
Expand Down
6 changes: 3 additions & 3 deletions packages/tx/src/transactionFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Common from '@ethereumjs/common'
import { default as Transaction } from './legacyTransaction'
import { default as AccessListEIP2930Transaction } from './eip2930Transaction'
import { TxOptions, Transaction, TxData } from './types'
import { TxOptions, TypedTransaction, TxData } from './types'
import { BN } from 'ethereumjs-util'

const DEFAULT_COMMON = new Common({ chain: 'mainnet' })
Expand All @@ -15,7 +15,7 @@ export default class TransactionFactory {
* @param txData - The transaction data. The `type` field will determine which transaction type is returned (if undefined, create a Transaction)
* @param txOptions - Options to pass on to the constructor of the transaction
*/
public static fromTxData(txData: TxData, txOptions: TxOptions = {}): Transaction {
public static fromTxData(txData: TxData, txOptions: TxOptions = {}): TypedTransaction {
const common = txOptions.common ?? DEFAULT_COMMON
if (txData.type === undefined) {
// Assume Transaction
Expand All @@ -32,7 +32,7 @@ export default class TransactionFactory {
* @param rawData - The raw data buffer
* @param txOptions - The transaction options
*/
public static fromRawData(rawData: Buffer, txOptions: TxOptions = {}): Transaction {
public static fromRawData(rawData: Buffer, txOptions: TxOptions = {}): TypedTransaction {
const common = txOptions.common ?? DEFAULT_COMMON
if (rawData[0] <= 0x7f) {
// It is an EIP-2718 Typed Transaction
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export interface TxData {
type?: BNLike
}

export type Transaction = Transaction | AccessListEIP2930Transaction
export type TypedTransaction = Transaction | AccessListEIP2930Transaction

export type BaseTransactionData = {
/**
Expand Down
10 changes: 6 additions & 4 deletions packages/vm/lib/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,11 @@ async function applyTransactions(this: VM, block: Block, opts: RunBlockOpts) {
let txReceipt
let encodedReceipt
let receiptLog = `Generate tx receipt transactionType=${
tx.transactionType
'transactionType' in tx ? tx.transactionType : 'NaN'
} gasUsed=${gasUsed} bitvector=${short(abstractTxReceipt.bitvector)} (${
abstractTxReceipt.bitvector.length
} bytes) logs=${abstractTxReceipt.logs.length}`
if (tx.transactionType === 0) {
if (!('transactionType' in tx) || tx.transactionType === 0) {
if (this._common.gteHardfork('byzantium')) {
txReceipt = {
status: txRes.execResult.exceptionError ? 0 : 1, // Receipts have a 0 as status on error
Expand All @@ -373,15 +373,17 @@ async function applyTransactions(this: VM, block: Block, opts: RunBlockOpts) {
receiptLog += ` stateRoot=${txReceipt.stateRoot.toString('hex')} (< Byzantium)`
}
encodedReceipt = encode(Object.values(txReceipt))
} else if (tx.transactionType === 1) {
} else if ('transactionType' in tx && tx.transactionType === 1) {
txReceipt = {
status: txRes.execResult.exceptionError ? 0 : 1,
...abstractTxReceipt,
} as EIP2930Receipt

encodedReceipt = Buffer.concat([Buffer.from('01', 'hex'), encode(Object.values(txReceipt))])
} else {
throw new Error(`Unsupported transaction type ${tx.transactionType}`)
throw new Error(
`Unsupported transaction type ${'transactionType' in tx ? tx.transactionType : 'NaN'}`
)
}
debug(receiptLog)

Expand Down
12 changes: 8 additions & 4 deletions packages/vm/lib/runTx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { debug as createDebugLogger } from 'debug'
import { Address, BN } from 'ethereumjs-util'
import { Block } from '@ethereumjs/block'
import { AccessListItem, AccessListEIP2930Transaction, Transaction } from '@ethereumjs/tx'
import { AccessListItem, AccessListEIP2930Transaction, TypedTransaction } from '@ethereumjs/tx'
import VM from './index'
import Bloom from './bloom'
import { default as EVM, EVMResult } from './evm/evm'
Expand All @@ -25,7 +25,7 @@ export interface RunTxOpts {
/**
* An `@ethereumjs/tx` to run
*/
tx: Transaction
tx: TypedTransaction
/**
* If true, skips the nonce check
*/
Expand Down Expand Up @@ -64,7 +64,7 @@ export interface AfterTxEvent extends RunTxResult {
/**
* The transaction which just got finished
*/
transaction: Transaction
transaction: TypedTransaction
}

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

// Is it an Access List transaction?
if (opts.tx.transactionType === 1 && this._common.isActivatedEIP(2929)) {
if (
'transactionType' in opts.tx &&
opts.tx.transactionType === 1 &&
this._common.isActivatedEIP(2929)
) {
if (!this._common.isActivatedEIP(2930)) {
throw new Error('Cannot run transaction: EIP 2930 is not activated.')
}
Expand Down

0 comments on commit c8f340e

Please sign in to comment.