Skip to content

Commit 2e77693

Browse files
authored
Merge pull request #1138 from ethereumjs/improve-tx-backwards-compatibility
Tx Renaming / Improve backwards-compatibility
2 parents 11a29cf + c8f340e commit 2e77693

23 files changed

+176
-174
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/baseTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export abstract class BaseTransaction<TransactionObject> {
161161
}
162162

163163
/**
164-
* Returns the raw `Buffer[]` (LegacyTransaction) or `Buffer` (typed transaction).
164+
* Returns the raw `Buffer[]` (Transaction) or `Buffer` (typed transaction).
165165
* This is the data which is found in the transactions of the block body.
166166
*/
167167
abstract raw(): Buffer[] | Buffer

packages/tx/src/eip2930Transaction.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type EIP2930ValuesArray = [
3838
Buffer?
3939
]
4040

41-
export default class EIP2930Transaction extends BaseTransaction<EIP2930Transaction> {
41+
export default class AccessListEIP2930Transaction extends BaseTransaction<AccessListEIP2930Transaction> {
4242
public readonly chainId: BN
4343
public readonly accessList: AccessListBuffer
4444
public readonly v?: BN
@@ -68,7 +68,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
6868
}
6969

7070
public static fromTxData(txData: TxData, opts: TxOptions = {}) {
71-
return new EIP2930Transaction(txData, opts)
71+
return new AccessListEIP2930Transaction(txData, opts)
7272
}
7373

7474
// Instantiate a transaction from the serialized tx. This means that the Buffer should start with 0x01.
@@ -84,13 +84,13 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
8484
throw new Error('Invalid serialized tx input: must be array')
8585
}
8686

87-
return EIP2930Transaction.fromValuesArray(values, opts)
87+
return AccessListEIP2930Transaction.fromValuesArray(values, opts)
8888
}
8989

9090
// Instantiate a transaction from the serialized tx. This means that the Buffer should start with 0x01.
9191
// Alias of fromSerializedTx
9292
public static fromRlpSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
93-
return EIP2930Transaction.fromSerializedTx(serialized, opts)
93+
return AccessListEIP2930Transaction.fromSerializedTx(serialized, opts)
9494
}
9595

9696
// Create a transaction from a values array.
@@ -102,7 +102,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
102102
>values
103103
const emptyBuffer = Buffer.from([])
104104

105-
return new EIP2930Transaction(
105+
return new AccessListEIP2930Transaction(
106106
{
107107
chainId: new BN(chainId),
108108
nonce: new BN(nonce),
@@ -273,7 +273,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
273273

274274
/**
275275
* Returns the encoding of the transaction. For typed transaction, this is the raw Buffer.
276-
* In LegacyTransaction, this is a Buffer array.
276+
* In Transaction, this is a Buffer array.
277277
*/
278278
serialize(): Buffer {
279279
return <Buffer>this.raw()
@@ -365,7 +365,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
365365
common: this.common,
366366
}
367367

368-
return EIP2930Transaction.fromTxData(
368+
return AccessListEIP2930Transaction.fromTxData(
369369
{
370370
chainId: this.chainId,
371371
nonce: this.nonce,

packages/tx/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { default as LegacyTransaction } from './legacyTransaction'
2-
export { default as EIP2930Transaction } from './eip2930Transaction'
1+
export { default as Transaction } from './legacyTransaction'
2+
export { default as AccessListEIP2930Transaction } from './eip2930Transaction'
33
export { default as TransactionFactory } from './transactionFactory'
44
export * from './types'

packages/tx/src/legacyTransaction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46
2121
/**
2222
* An Ethereum transaction.
2323
*/
24-
export default class LegacyTransaction extends BaseTransaction<LegacyTransaction> {
24+
export default class Transaction extends BaseTransaction<Transaction> {
2525
public readonly v?: BN
2626
public readonly r?: BN
2727
public readonly s?: BN
@@ -31,7 +31,7 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
3131
}
3232

3333
public static fromTxData(txData: TxData, opts: TxOptions = {}) {
34-
return new LegacyTransaction(txData, opts)
34+
return new Transaction(txData, opts)
3535
}
3636

3737
public static fromRlpSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
@@ -46,7 +46,7 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
4646

4747
// alias of fromRlpSerializedTx
4848
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
49-
return LegacyTransaction.fromRlpSerializedTx(serialized, opts)
49+
return Transaction.fromRlpSerializedTx(serialized, opts)
5050
}
5151

5252
public static fromValuesArray(values: Buffer[], opts: TxOptions = {}) {
@@ -63,7 +63,7 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
6363

6464
const emptyBuffer = Buffer.from([])
6565

66-
return new LegacyTransaction(
66+
return new Transaction(
6767
{
6868
nonce: new BN(nonce),
6969
gasPrice: new BN(gasPrice),
@@ -209,7 +209,7 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
209209
common: this.common,
210210
}
211211

212-
return LegacyTransaction.fromTxData(
212+
return Transaction.fromTxData(
213213
{
214214
nonce: this.nonce,
215215
gasPrice: this.gasPrice,

packages/tx/src/transactionFactory.ts

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

77
const DEFAULT_COMMON = new Common({ chain: 'mainnet' })
@@ -12,14 +12,14 @@ export default class TransactionFactory {
1212

1313
/**
1414
* Create a transaction from a `txData` object
15-
* @param txData - The transaction data. The `type` field will determine which transaction type is returned (if undefined, create a LegacyTransaction)
15+
* @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) {
21-
// Assume LegacyTransaction
22-
return LegacyTransaction.fromTxData(txData, txOptions)
21+
// Assume Transaction
22+
return Transaction.fromTxData(txData, txOptions)
2323
} else {
2424
const txType = new BN(txData.type).toNumber()
2525
return TransactionFactory.getTransactionClass(txType, common).fromTxData(txData, txOptions)
@@ -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
@@ -55,16 +55,16 @@ export default class TransactionFactory {
5555
)
5656
}
5757

58-
return EIP2930Transaction.fromRlpSerializedTx(rawData, txOptions)
58+
return AccessListEIP2930Transaction.fromRlpSerializedTx(rawData, txOptions)
5959
} else {
60-
return LegacyTransaction.fromRlpSerializedTx(rawData, txOptions)
60+
return Transaction.fromRlpSerializedTx(rawData, txOptions)
6161
}
6262
}
6363

6464
/**
6565
* When decoding a BlockBody, in the transactions field, a field is either:
6666
* A Buffer (a TypedTransaction - encoded as TransactionType || rlp(TransactionPayload))
67-
* A Buffer[] (LegacyTransaction)
67+
* A Buffer[] (Transaction)
6868
* This method returns the right transaction.
6969
* @param rawData - Either a Buffer or a Buffer[]
7070
* @param txOptions - The transaction options
@@ -73,16 +73,16 @@ export default class TransactionFactory {
7373
if (Buffer.isBuffer(rawData)) {
7474
return this.fromRawData(rawData, txOptions)
7575
} else if (Array.isArray(rawData)) {
76-
// It is a LegacyTransaction
77-
return LegacyTransaction.fromValuesArray(rawData, txOptions)
76+
// It is a Transaction
77+
return Transaction.fromValuesArray(rawData, txOptions)
7878
} else {
7979
throw new Error('Cannot decode transaction: unknown type input')
8080
}
8181
}
8282

8383
/**
8484
* This helper method allows one to retrieve the class which matches the transactionID
85-
* If transactionID is undefined, return the LegacyTransaction class.
85+
* If transactionID is undefined, return the Transaction class.
8686
* @param transactionID
8787
* @param common
8888
*/
@@ -97,12 +97,12 @@ export default class TransactionFactory {
9797
const legacyTxn = transactionID == 0 || (transactionID >= 0x80 && transactionID <= 0xff)
9898

9999
if (legacyTxn) {
100-
return LegacyTransaction
100+
return Transaction
101101
}
102102

103103
switch (transactionID) {
104104
case 1:
105-
return EIP2930Transaction
105+
return AccessListEIP2930Transaction
106106
default:
107107
throw new Error(`TypedTransaction with ID ${transactionID} unknown`)
108108
}

packages/tx/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AddressLike, BNLike, BufferLike } from 'ethereumjs-util'
22
import Common from '@ethereumjs/common'
3-
import { default as LegacyTransaction } from './legacyTransaction'
4-
import { default as EIP2930Transaction } from './eip2930Transaction'
3+
import { default as Transaction } from './legacyTransaction'
4+
import { default as AccessListEIP2930Transaction } from './eip2930Transaction'
55

66
/**
77
* The options for initializing a Transaction.
@@ -131,7 +131,7 @@ export interface TxData {
131131
type?: BNLike
132132
}
133133

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

136136
export type BaseTransactionData = {
137137
/**

packages/tx/test/base.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import tape from 'tape'
22
import Common from '@ethereumjs/common'
3-
import { LegacyTransaction, EIP2930Transaction } from '../src'
3+
import { Transaction, AccessListEIP2930Transaction } from '../src'
44
import { TxsJsonEntry } from './types'
55
import { BaseTransaction } from '../src/baseTransaction'
66
import { privateToPublic } from 'ethereumjs-util'
77

88
tape('[BaseTransaction]', function (t) {
99
const legacyFixtures: TxsJsonEntry[] = require('./json/txs.json')
10-
const legacyTxs: BaseTransaction<LegacyTransaction>[] = []
10+
const legacyTxs: BaseTransaction<Transaction>[] = []
1111
legacyFixtures.slice(0, 4).forEach(function (tx: any) {
12-
legacyTxs.push(LegacyTransaction.fromTxData(tx.data))
12+
legacyTxs.push(Transaction.fromTxData(tx.data))
1313
})
1414

1515
const eip2930Fixtures = require('./json/eip2930txs.json')
16-
const eip2930Txs: BaseTransaction<EIP2930Transaction>[] = []
16+
const eip2930Txs: BaseTransaction<AccessListEIP2930Transaction>[] = []
1717
eip2930Fixtures.forEach(function (tx: any) {
18-
eip2930Txs.push(EIP2930Transaction.fromTxData(tx.data))
18+
eip2930Txs.push(AccessListEIP2930Transaction.fromTxData(tx.data))
1919
})
2020

2121
const zero = Buffer.alloc(0)
2222
const txTypes = [
2323
{
24-
class: LegacyTransaction,
25-
name: 'LegacyTransaction',
24+
class: Transaction,
25+
name: 'Transaction',
2626
values: Array(6).fill(zero),
2727
txs: legacyTxs,
2828
fixtures: legacyFixtures,
2929
},
3030
{
31-
class: EIP2930Transaction,
32-
name: 'EIP2930Transaction',
31+
class: AccessListEIP2930Transaction,
32+
name: 'AccessListEIP2930Transaction',
3333
values: [Buffer.from([1])].concat(Array(7).fill(zero)),
3434
txs: eip2930Txs,
3535
fixtures: eip2930Fixtures,

0 commit comments

Comments
 (0)