Skip to content

Commit

Permalink
Merge c8f340e into 11a29cf
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 authored Mar 5, 2021
2 parents 11a29cf + c8f340e commit 711a643
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 174 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
2 changes: 1 addition & 1 deletion packages/tx/src/baseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export abstract class BaseTransaction<TransactionObject> {
}

/**
* Returns the raw `Buffer[]` (LegacyTransaction) or `Buffer` (typed transaction).
* Returns the raw `Buffer[]` (Transaction) or `Buffer` (typed transaction).
* This is the data which is found in the transactions of the block body.
*/
abstract raw(): Buffer[] | Buffer
Expand Down
14 changes: 7 additions & 7 deletions packages/tx/src/eip2930Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type EIP2930ValuesArray = [
Buffer?
]

export default class EIP2930Transaction extends BaseTransaction<EIP2930Transaction> {
export default class AccessListEIP2930Transaction extends BaseTransaction<AccessListEIP2930Transaction> {
public readonly chainId: BN
public readonly accessList: AccessListBuffer
public readonly v?: BN
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
}

public static fromTxData(txData: TxData, opts: TxOptions = {}) {
return new EIP2930Transaction(txData, opts)
return new AccessListEIP2930Transaction(txData, opts)
}

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

return EIP2930Transaction.fromValuesArray(values, opts)
return AccessListEIP2930Transaction.fromValuesArray(values, opts)
}

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

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

return new EIP2930Transaction(
return new AccessListEIP2930Transaction(
{
chainId: new BN(chainId),
nonce: new BN(nonce),
Expand Down Expand Up @@ -273,7 +273,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti

/**
* Returns the encoding of the transaction. For typed transaction, this is the raw Buffer.
* In LegacyTransaction, this is a Buffer array.
* In Transaction, this is a Buffer array.
*/
serialize(): Buffer {
return <Buffer>this.raw()
Expand Down Expand Up @@ -365,7 +365,7 @@ export default class EIP2930Transaction extends BaseTransaction<EIP2930Transacti
common: this.common,
}

return EIP2930Transaction.fromTxData(
return AccessListEIP2930Transaction.fromTxData(
{
chainId: this.chainId,
nonce: this.nonce,
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as LegacyTransaction } from './legacyTransaction'
export { default as EIP2930Transaction } from './eip2930Transaction'
export { default as Transaction } from './legacyTransaction'
export { default as AccessListEIP2930Transaction } from './eip2930Transaction'
export { default as TransactionFactory } from './transactionFactory'
export * from './types'
10 changes: 5 additions & 5 deletions packages/tx/src/legacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46
/**
* An Ethereum transaction.
*/
export default class LegacyTransaction extends BaseTransaction<LegacyTransaction> {
export default class Transaction extends BaseTransaction<Transaction> {
public readonly v?: BN
public readonly r?: BN
public readonly s?: BN
Expand All @@ -31,7 +31,7 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
}

public static fromTxData(txData: TxData, opts: TxOptions = {}) {
return new LegacyTransaction(txData, opts)
return new Transaction(txData, opts)
}

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

// alias of fromRlpSerializedTx
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
return LegacyTransaction.fromRlpSerializedTx(serialized, opts)
return Transaction.fromRlpSerializedTx(serialized, opts)
}

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

const emptyBuffer = Buffer.from([])

return new LegacyTransaction(
return new Transaction(
{
nonce: new BN(nonce),
gasPrice: new BN(gasPrice),
Expand Down Expand Up @@ -209,7 +209,7 @@ export default class LegacyTransaction extends BaseTransaction<LegacyTransaction
common: this.common,
}

return LegacyTransaction.fromTxData(
return Transaction.fromTxData(
{
nonce: this.nonce,
gasPrice: this.gasPrice,
Expand Down
32 changes: 16 additions & 16 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 LegacyTransaction } from './legacyTransaction'
import { default as EIP2930Transaction } from './eip2930Transaction'
import { TxOptions, Transaction, TxData } from './types'
import { default as Transaction } from './legacyTransaction'
import { default as AccessListEIP2930Transaction } from './eip2930Transaction'
import { TxOptions, TypedTransaction, TxData } from './types'
import { BN } from 'ethereumjs-util'

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

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

return EIP2930Transaction.fromRlpSerializedTx(rawData, txOptions)
return AccessListEIP2930Transaction.fromRlpSerializedTx(rawData, txOptions)
} else {
return LegacyTransaction.fromRlpSerializedTx(rawData, txOptions)
return Transaction.fromRlpSerializedTx(rawData, txOptions)
}
}

/**
* When decoding a BlockBody, in the transactions field, a field is either:
* A Buffer (a TypedTransaction - encoded as TransactionType || rlp(TransactionPayload))
* A Buffer[] (LegacyTransaction)
* A Buffer[] (Transaction)
* This method returns the right transaction.
* @param rawData - Either a Buffer or a Buffer[]
* @param txOptions - The transaction options
Expand All @@ -73,16 +73,16 @@ export default class TransactionFactory {
if (Buffer.isBuffer(rawData)) {
return this.fromRawData(rawData, txOptions)
} else if (Array.isArray(rawData)) {
// It is a LegacyTransaction
return LegacyTransaction.fromValuesArray(rawData, txOptions)
// It is a Transaction
return Transaction.fromValuesArray(rawData, txOptions)
} else {
throw new Error('Cannot decode transaction: unknown type input')
}
}

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

if (legacyTxn) {
return LegacyTransaction
return Transaction
}

switch (transactionID) {
case 1:
return EIP2930Transaction
return AccessListEIP2930Transaction
default:
throw new Error(`TypedTransaction with ID ${transactionID} unknown`)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/tx/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AddressLike, BNLike, BufferLike } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { default as LegacyTransaction } from './legacyTransaction'
import { default as EIP2930Transaction } from './eip2930Transaction'
import { default as Transaction } from './legacyTransaction'
import { default as AccessListEIP2930Transaction } from './eip2930Transaction'

/**
* The options for initializing a Transaction.
Expand Down Expand Up @@ -131,7 +131,7 @@ export interface TxData {
type?: BNLike
}

export type Transaction = LegacyTransaction | EIP2930Transaction
export type TypedTransaction = Transaction | AccessListEIP2930Transaction

export type BaseTransactionData = {
/**
Expand Down
18 changes: 9 additions & 9 deletions packages/tx/test/base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import tape from 'tape'
import Common from '@ethereumjs/common'
import { LegacyTransaction, EIP2930Transaction } from '../src'
import { Transaction, AccessListEIP2930Transaction } from '../src'
import { TxsJsonEntry } from './types'
import { BaseTransaction } from '../src/baseTransaction'
import { privateToPublic } from 'ethereumjs-util'

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

const eip2930Fixtures = require('./json/eip2930txs.json')
const eip2930Txs: BaseTransaction<EIP2930Transaction>[] = []
const eip2930Txs: BaseTransaction<AccessListEIP2930Transaction>[] = []
eip2930Fixtures.forEach(function (tx: any) {
eip2930Txs.push(EIP2930Transaction.fromTxData(tx.data))
eip2930Txs.push(AccessListEIP2930Transaction.fromTxData(tx.data))
})

const zero = Buffer.alloc(0)
const txTypes = [
{
class: LegacyTransaction,
name: 'LegacyTransaction',
class: Transaction,
name: 'Transaction',
values: Array(6).fill(zero),
txs: legacyTxs,
fixtures: legacyFixtures,
},
{
class: EIP2930Transaction,
name: 'EIP2930Transaction',
class: AccessListEIP2930Transaction,
name: 'AccessListEIP2930Transaction',
values: [Buffer.from([1])].concat(Array(7).fill(zero)),
txs: eip2930Txs,
fixtures: eip2930Fixtures,
Expand Down
Loading

0 comments on commit 711a643

Please sign in to comment.