From df5ee72b8e66704effe89a2ed9aff2120322bc03 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 5 Mar 2021 12:20:10 +0100 Subject: [PATCH 1/3] tx, vm: renamed EIP2930Transaction -> AccessListEIP2930Transaction --- packages/tx/src/eip2930Transaction.ts | 12 +++---- packages/tx/src/index.ts | 2 +- packages/tx/src/transactionFactory.ts | 6 ++-- packages/tx/src/types.ts | 4 +-- packages/tx/test/base.spec.ts | 10 +++--- packages/tx/test/eip2930.spec.ts | 34 +++++++++---------- packages/tx/test/transactionFactory.spec.ts | 24 +++++++------ packages/vm/lib/runTx.ts | 4 +-- .../api/EIPs/eip-2930-accesslists.spec.ts | 6 ++-- packages/vm/tests/util.ts | 4 +-- 10 files changed, 54 insertions(+), 52 deletions(-) diff --git a/packages/tx/src/eip2930Transaction.ts b/packages/tx/src/eip2930Transaction.ts index 6fa399c209d..831a1b3684e 100644 --- a/packages/tx/src/eip2930Transaction.ts +++ b/packages/tx/src/eip2930Transaction.ts @@ -38,7 +38,7 @@ type EIP2930ValuesArray = [ Buffer? ] -export default class EIP2930Transaction extends BaseTransaction { +export default class AccessListEIP2930Transaction extends BaseTransaction { public readonly chainId: BN public readonly accessList: AccessListBuffer public readonly v?: BN @@ -68,7 +68,7 @@ export default class EIP2930Transaction extends BaseTransactionvalues const emptyBuffer = Buffer.from([]) - return new EIP2930Transaction( + return new AccessListEIP2930Transaction( { chainId: new BN(chainId), nonce: new BN(nonce), @@ -365,7 +365,7 @@ export default class EIP2930Transaction extends BaseTransaction[] = [] + const eip2930Txs: BaseTransaction[] = [] eip2930Fixtures.forEach(function (tx: any) { - eip2930Txs.push(EIP2930Transaction.fromTxData(tx.data)) + eip2930Txs.push(AccessListEIP2930Transaction.fromTxData(tx.data)) }) const zero = Buffer.alloc(0) @@ -28,8 +28,8 @@ tape('[BaseTransaction]', function (t) { fixtures: legacyFixtures, }, { - class: EIP2930Transaction, - name: 'EIP2930Transaction', + class: AccessListEIP2930Transaction, + name: 'AccessListEIP2930Transaction', values: [Buffer.from([1])].concat(Array(7).fill(zero)), txs: eip2930Txs, fixtures: eip2930Fixtures, diff --git a/packages/tx/test/eip2930.spec.ts b/packages/tx/test/eip2930.spec.ts index df9f65019b2..056478a5ef1 100644 --- a/packages/tx/test/eip2930.spec.ts +++ b/packages/tx/test/eip2930.spec.ts @@ -1,7 +1,7 @@ import Common from '@ethereumjs/common' import { Address, BN, bufferToHex, privateToAddress } from 'ethereumjs-util' import tape from 'tape' -import { AccessList, EIP2930Transaction } from '../src' +import { AccessList, AccessListEIP2930Transaction } from '../src' const pKey = Buffer.from('4646464646464646464646464646464646464646464646464646464646464646', 'hex') const address = privateToAddress(pKey) @@ -17,10 +17,10 @@ const validSlot = Buffer.from('01'.repeat(32), 'hex') const chainId = new BN(1) -tape('[EIP2930Transaction]', function (t) { +tape('[AccessListEIP2930Transaction]', function (t) { t.test('Initialization / Getter', function (t) { t.throws(() => { - EIP2930Transaction.fromTxData( + AccessListEIP2930Transaction.fromTxData( { chainId: chainId.addn(1), }, @@ -29,7 +29,7 @@ tape('[EIP2930Transaction]', function (t) { }, 'should reject transactions with wrong chain ID') t.throws(() => { - EIP2930Transaction.fromTxData( + AccessListEIP2930Transaction.fromTxData( { v: 2, }, @@ -46,7 +46,7 @@ tape('[EIP2930Transaction]', function (t) { storageKeys: [bufferToHex(validSlot)], }, ] - const txn = EIP2930Transaction.fromTxData( + const txn = AccessListEIP2930Transaction.fromTxData( { accessList: access, chainId: 1, @@ -66,7 +66,7 @@ tape('[EIP2930Transaction]', function (t) { // also verify that we can always get the json access list, even if we don't provide one. - const txnRaw = EIP2930Transaction.fromTxData( + const txnRaw = AccessListEIP2930Transaction.fromTxData( { accessList: BufferArray, chainId: 1, @@ -90,7 +90,7 @@ tape('[EIP2930Transaction]', function (t) { ] st.throws(() => { - EIP2930Transaction.fromTxData({ chainId, accessList }, { common }) + AccessListEIP2930Transaction.fromTxData({ chainId, accessList }, { common }) }) accessList = [ @@ -103,38 +103,38 @@ tape('[EIP2930Transaction]', function (t) { ] st.throws(() => { - EIP2930Transaction.fromTxData({ chainId, accessList }, { common }) + AccessListEIP2930Transaction.fromTxData({ chainId, accessList }, { common }) }) accessList = [[]] // Address does not exist st.throws(() => { - EIP2930Transaction.fromTxData({ chainId, accessList }, { common }) + AccessListEIP2930Transaction.fromTxData({ chainId, accessList }, { common }) }) accessList = [[validAddress]] // Slots does not exist st.throws(() => { - EIP2930Transaction.fromTxData({ chainId, accessList }, { common }) + AccessListEIP2930Transaction.fromTxData({ chainId, accessList }, { common }) }) accessList = [[validAddress, validSlot]] // Slots is not an array st.throws(() => { - EIP2930Transaction.fromTxData({ chainId, accessList }, { common }) + AccessListEIP2930Transaction.fromTxData({ chainId, accessList }, { common }) }) accessList = [[validAddress, [], []]] // 3 items where 2 are expected st.throws(() => { - EIP2930Transaction.fromTxData({ chainId, accessList }, { common }) + AccessListEIP2930Transaction.fromTxData({ chainId, accessList }, { common }) }) st.end() }) t.test('should return right upfront cost', (st) => { - let tx = EIP2930Transaction.fromTxData( + let tx = AccessListEIP2930Transaction.fromTxData( { data: Buffer.from('010200', 'hex'), to: validAddress, @@ -165,7 +165,7 @@ tape('[EIP2930Transaction]', function (t) { ) // In this Tx, `to` is `undefined`, so we should charge homestead creation gas. - tx = EIP2930Transaction.fromTxData( + tx = AccessListEIP2930Transaction.fromTxData( { data: Buffer.from('010200', 'hex'), accessList: [[validAddress, [validSlot]]], @@ -188,7 +188,7 @@ tape('[EIP2930Transaction]', function (t) { ) // Explicilty check that even if we have duplicates in our list, we still charge for those - tx = EIP2930Transaction.fromTxData( + tx = AccessListEIP2930Transaction.fromTxData( { to: validAddress, accessList: [ @@ -206,7 +206,7 @@ tape('[EIP2930Transaction]', function (t) { }) t.test('should sign a transaction', function (t) { - const tx = EIP2930Transaction.fromTxData( + const tx = AccessListEIP2930Transaction.fromTxData( { data: Buffer.from('010200', 'hex'), to: validAddress, @@ -277,7 +277,7 @@ tape('[EIP2930Transaction]', function (t) { Buffer.from('0be950468ba1c25a5cb50e9f6d8aa13c8cd21f24ba909402775b262ac76d374d', 'hex') ) - const unsignedTx = EIP2930Transaction.fromTxData(txData, { common: usedCommon }) + const unsignedTx = AccessListEIP2930Transaction.fromTxData(txData, { common: usedCommon }) const serializedMessageRaw = unsignedTx.serialize() diff --git a/packages/tx/test/transactionFactory.spec.ts b/packages/tx/test/transactionFactory.spec.ts index 56c6f606caf..9ee33245ea9 100644 --- a/packages/tx/test/transactionFactory.spec.ts +++ b/packages/tx/test/transactionFactory.spec.ts @@ -1,7 +1,7 @@ import Common from '@ethereumjs/common' import { BN } from 'ethereumjs-util' import tape from 'tape' -import { EIP2930Transaction, TransactionFactory, LegacyTransaction } from '../src' +import { AccessListEIP2930Transaction, TransactionFactory, LegacyTransaction } from '../src' const EIP2930Common = new Common({ eips: [2718, 2929, 2930], @@ -11,21 +11,23 @@ const EIP2930Common = new Common({ const pKey = Buffer.from('4646464646464646464646464646464646464646464646464646464646464646', 'hex') -const simpleUnsignedEIP2930Transaction = EIP2930Transaction.fromTxData( +const simpleUnsignedAccessListEIP2930Transaction = AccessListEIP2930Transaction.fromTxData( { chainId: new BN(1) }, { common: EIP2930Common } ) const simpleUnsignedLegacyTransaction = LegacyTransaction.fromTxData({}) -const simpleSignedEIP2930Transaction = simpleUnsignedEIP2930Transaction.sign(pKey) +const simpleSignedAccessListEIP2930Transaction = simpleUnsignedAccessListEIP2930Transaction.sign( + pKey +) const simpleSignedLegacyTransaction = simpleUnsignedLegacyTransaction.sign(pKey) tape('[TransactionFactory]: Basic functions', function (t) { t.test('should return the right type', function (st) { - const serialized = simpleUnsignedEIP2930Transaction.serialize() + const serialized = simpleUnsignedAccessListEIP2930Transaction.serialize() const factoryTx = TransactionFactory.fromRawData(serialized, { common: EIP2930Common }) - st.equals(factoryTx.constructor.name, EIP2930Transaction.name) + st.equals(factoryTx.constructor.name, AccessListEIP2930Transaction.name) const legacyTx = LegacyTransaction.fromTxData({}) const serializedLegacyTx = legacyTx.serialize() @@ -39,7 +41,7 @@ tape('[TransactionFactory]: Basic functions', function (t) { 'should throw when trying to create EIP-2718 typed transactions when not allowed in Common', function (st) { st.throws(() => { - TransactionFactory.fromRawData(simpleUnsignedEIP2930Transaction.serialize(), {}) + TransactionFactory.fromRawData(simpleUnsignedAccessListEIP2930Transaction.serialize(), {}) }) st.end() } @@ -49,7 +51,7 @@ tape('[TransactionFactory]: Basic functions', function (t) { 'should throw when trying to create EIP-2718 typed transactions when not allowed in Common', function (st) { st.throws(() => { - const serialized = simpleUnsignedEIP2930Transaction.serialize() + const serialized = simpleUnsignedAccessListEIP2930Transaction.serialize() serialized[0] = 2 // edit the transaction type TransactionFactory.fromRawData(serialized, { common: EIP2930Common }) }) @@ -62,7 +64,7 @@ tape('[TransactionFactory]: Basic functions', function (t) { st.equals(legacyTx!.name, LegacyTransaction.name) const eip2930Tx = TransactionFactory.getTransactionClass(1, EIP2930Common) - st.equals(eip2930Tx!.name, EIP2930Transaction.name) + st.equals(eip2930Tx!.name, AccessListEIP2930Transaction.name) st.end() }) @@ -84,13 +86,13 @@ tape('[TransactionFactory]: Basic functions', function (t) { t.test('should decode raw block body data', function (st) { const rawLegacy = simpleSignedLegacyTransaction.raw() - const rawEIP2930 = simpleSignedEIP2930Transaction.raw() + const rawEIP2930 = simpleSignedAccessListEIP2930Transaction.raw() const legacyTx = TransactionFactory.fromBlockBodyData(rawLegacy) const eip2930Tx = TransactionFactory.fromBlockBodyData(rawEIP2930, { common: EIP2930Common }) st.equals(legacyTx.constructor.name, LegacyTransaction.name) - st.equals(eip2930Tx.constructor.name, EIP2930Transaction.name) + st.equals(eip2930Tx.constructor.name, AccessListEIP2930Transaction.name) st.end() }) @@ -104,7 +106,7 @@ tape('[TransactionFactory]: Basic functions', function (t) { st.equals(legacyTx.constructor.name, LegacyTransaction.name) st.equals(legacyTx2.constructor.name, LegacyTransaction.name) - st.equals(eip2930Tx.constructor.name, EIP2930Transaction.name) + st.equals(eip2930Tx.constructor.name, AccessListEIP2930Transaction.name) st.end() }) diff --git a/packages/vm/lib/runTx.ts b/packages/vm/lib/runTx.ts index de7dea0ac3c..623bf821463 100644 --- a/packages/vm/lib/runTx.ts +++ b/packages/vm/lib/runTx.ts @@ -1,7 +1,7 @@ import { debug as createDebugLogger } from 'debug' import { Address, BN } from 'ethereumjs-util' import { Block } from '@ethereumjs/block' -import { AccessListItem, EIP2930Transaction, Transaction } from '@ethereumjs/tx' +import { AccessListItem, AccessListEIP2930Transaction, Transaction } from '@ethereumjs/tx' import VM from './index' import Bloom from './bloom' import { default as EVM, EVMResult } from './evm/evm' @@ -104,7 +104,7 @@ export default async function runTx(this: VM, opts: RunTxOpts): Promiseopts.tx + const castedTx = opts.tx castedTx.AccessListJSON.forEach((accessListItem: AccessListItem) => { const address = Buffer.from(accessListItem.address.slice(2), 'hex') diff --git a/packages/vm/tests/api/EIPs/eip-2930-accesslists.spec.ts b/packages/vm/tests/api/EIPs/eip-2930-accesslists.spec.ts index 2bdf884d095..16aa43c1789 100644 --- a/packages/vm/tests/api/EIPs/eip-2930-accesslists.spec.ts +++ b/packages/vm/tests/api/EIPs/eip-2930-accesslists.spec.ts @@ -2,7 +2,7 @@ import tape from 'tape' import { Account, Address, BN, bufferToHex } from 'ethereumjs-util' import Common from '@ethereumjs/common' import VM from '../../../lib' -import { EIP2930Transaction } from '@ethereumjs/tx' +import { AccessListEIP2930Transaction } from '@ethereumjs/tx' const common = new Common({ eips: [2718, 2929, 2930], @@ -28,7 +28,7 @@ tape('EIP-2930 Optional Access Lists tests', (t) => { storageKeys: [bufferToHex(validSlot)], }, ] - const txnWithAccessList = EIP2930Transaction.fromTxData( + const txnWithAccessList = AccessListEIP2930Transaction.fromTxData( { accessList: access, chainId: 1, @@ -37,7 +37,7 @@ tape('EIP-2930 Optional Access Lists tests', (t) => { }, { common } ).sign(privateKey) - const txnWithoutAccessList = EIP2930Transaction.fromTxData( + const txnWithoutAccessList = AccessListEIP2930Transaction.fromTxData( { accessList: [], chainId: 1, diff --git a/packages/vm/tests/util.ts b/packages/vm/tests/util.ts index c5bec853d6f..7b567faa3bb 100644 --- a/packages/vm/tests/util.ts +++ b/packages/vm/tests/util.ts @@ -8,7 +8,7 @@ import { setLengthLeft, toBuffer, } from 'ethereumjs-util' -import { EIP2930Transaction, LegacyTransaction, TxOptions } from '@ethereumjs/tx' +import { AccessListEIP2930Transaction, LegacyTransaction, TxOptions } from '@ethereumjs/tx' import { Block, BlockHeader, BlockOptions } from '@ethereumjs/block' import Common from '@ethereumjs/common' @@ -101,7 +101,7 @@ const format = (exports.format = function ( export function makeTx(txData: any, opts?: TxOptions) { let tx if (txData.accessLists) { - tx = EIP2930Transaction.fromTxData(txData, opts) + tx = AccessListEIP2930Transaction.fromTxData(txData, opts) } else { tx = LegacyTransaction.fromTxData(txData, opts) } From d5f5969a8cfe7876e7d9dc5184fd89c2b9afa16f Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 5 Mar 2021 12:28:04 +0100 Subject: [PATCH 2/3] tx, vm: renamed LegacyTransaction -> Transaction --- packages/tx/src/baseTransaction.ts | 2 +- packages/tx/src/eip2930Transaction.ts | 2 +- packages/tx/src/index.ts | 2 +- packages/tx/src/legacyTransaction.ts | 10 +- packages/tx/src/transactionFactory.ts | 20 ++-- packages/tx/src/types.ts | 4 +- packages/tx/test/base.spec.ts | 10 +- packages/tx/test/legacy.spec.ts | 94 +++++++++---------- packages/tx/test/transactionFactory.spec.ts | 20 ++-- packages/tx/test/transactionRunner.ts | 4 +- .../examples/run-solidity-contract/index.ts | 6 +- .../run-transactions-complete/index.ts | 4 +- packages/vm/tests/api/EIPs/eip-2929.spec.ts | 6 +- packages/vm/tests/api/events.spec.ts | 14 +-- packages/vm/tests/api/runBlock.spec.ts | 6 +- packages/vm/tests/api/runTx.spec.ts | 8 +- packages/vm/tests/util.ts | 4 +- 17 files changed, 105 insertions(+), 111 deletions(-) diff --git a/packages/tx/src/baseTransaction.ts b/packages/tx/src/baseTransaction.ts index f7f0691c674..f1a82cbfd1a 100644 --- a/packages/tx/src/baseTransaction.ts +++ b/packages/tx/src/baseTransaction.ts @@ -161,7 +161,7 @@ export abstract class BaseTransaction { } /** - * 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 diff --git a/packages/tx/src/eip2930Transaction.ts b/packages/tx/src/eip2930Transaction.ts index 831a1b3684e..191d301464f 100644 --- a/packages/tx/src/eip2930Transaction.ts +++ b/packages/tx/src/eip2930Transaction.ts @@ -273,7 +273,7 @@ export default class AccessListEIP2930Transaction extends BaseTransactionthis.raw() diff --git a/packages/tx/src/index.ts b/packages/tx/src/index.ts index e2134e7223d..cc39cf4d7b8 100644 --- a/packages/tx/src/index.ts +++ b/packages/tx/src/index.ts @@ -1,4 +1,4 @@ -export { default as LegacyTransaction } from './legacyTransaction' +export { default as Transaction } from './legacyTransaction' export { default as AccessListEIP2930Transaction } from './eip2930Transaction' export { default as TransactionFactory } from './transactionFactory' export * from './types' diff --git a/packages/tx/src/legacyTransaction.ts b/packages/tx/src/legacyTransaction.ts index a20ce66117e..e6e867cd2dd 100644 --- a/packages/tx/src/legacyTransaction.ts +++ b/packages/tx/src/legacyTransaction.ts @@ -21,7 +21,7 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46 /** * An Ethereum transaction. */ -export default class LegacyTransaction extends BaseTransaction { +export default class Transaction extends BaseTransaction { public readonly v?: BN public readonly r?: BN public readonly s?: BN @@ -31,7 +31,7 @@ export default class LegacyTransaction extends BaseTransaction= 0x80 && transactionID <= 0xff) if (legacyTxn) { - return LegacyTransaction + return Transaction } switch (transactionID) { diff --git a/packages/tx/src/types.ts b/packages/tx/src/types.ts index 3862a68d2ea..732189f5fa1 100644 --- a/packages/tx/src/types.ts +++ b/packages/tx/src/types.ts @@ -1,6 +1,6 @@ import { AddressLike, BNLike, BufferLike } from 'ethereumjs-util' import Common from '@ethereumjs/common' -import { default as LegacyTransaction } from './legacyTransaction' +import { default as Transaction } from './legacyTransaction' import { default as AccessListEIP2930Transaction } from './eip2930Transaction' /** @@ -131,7 +131,7 @@ export interface TxData { type?: BNLike } -export type Transaction = LegacyTransaction | AccessListEIP2930Transaction +export type Transaction = Transaction | AccessListEIP2930Transaction export type BaseTransactionData = { /** diff --git a/packages/tx/test/base.spec.ts b/packages/tx/test/base.spec.ts index 20a042e1a93..bc987e8e3b8 100644 --- a/packages/tx/test/base.spec.ts +++ b/packages/tx/test/base.spec.ts @@ -1,15 +1,15 @@ import tape from 'tape' import Common from '@ethereumjs/common' -import { LegacyTransaction, AccessListEIP2930Transaction } 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[] = [] + const legacyTxs: BaseTransaction[] = [] 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') @@ -21,8 +21,8 @@ tape('[BaseTransaction]', function (t) { const zero = Buffer.alloc(0) const txTypes = [ { - class: LegacyTransaction, - name: 'LegacyTransaction', + class: Transaction, + name: 'Transaction', values: Array(6).fill(zero), txs: legacyTxs, fixtures: legacyFixtures, diff --git a/packages/tx/test/legacy.spec.ts b/packages/tx/test/legacy.spec.ts index 314a9b004cb..f56d121e763 100644 --- a/packages/tx/test/legacy.spec.ts +++ b/packages/tx/test/legacy.spec.ts @@ -2,19 +2,19 @@ import tape from 'tape' import { Buffer } from 'buffer' import { BN, rlp, toBuffer, bufferToHex, unpadBuffer } from 'ethereumjs-util' import Common from '@ethereumjs/common' -import { LegacyTransaction, TxData } from '../src' +import { Transaction, TxData } from '../src' import { TxsJsonEntry, VitaliksTestsDataEntry } from './types' const txFixtures: TxsJsonEntry[] = require('./json/txs.json') const txFixturesEip155: VitaliksTestsDataEntry[] = require('./json/ttTransactionTestEip155VitaliksTests.json') tape('[Transaction]', function (t) { - const transactions: LegacyTransaction[] = [] + const transactions: Transaction[] = [] t.test('Initialization -> decode with fromValuesArray()', function (st) { txFixtures.slice(0, 4).forEach(function (tx: any) { const txData = tx.raw.map(toBuffer) - const pt = LegacyTransaction.fromValuesArray(txData) + const pt = Transaction.fromValuesArray(txData) st.equal(bufferToHex(unpadBuffer(toBuffer(pt.nonce))), tx.raw[0]) st.equal(bufferToHex(toBuffer(pt.gasPrice)), tx.raw[1]) @@ -45,7 +45,7 @@ tape('[Transaction]', function (t) { chain: 'mainnet', hardfork: 'tangerineWhistle', }) - const tx = LegacyTransaction.fromValuesArray(txFixtures[3].raw.map(toBuffer), { + const tx = Transaction.fromValuesArray(txFixtures[3].raw.map(toBuffer), { common, }) st.deepEqual( @@ -64,7 +64,7 @@ tape('[Transaction]', function (t) { }) t.test('hash() -> with defined chainId', function (st) { - const tx = LegacyTransaction.fromValuesArray(txFixtures[4].raw.map(toBuffer)) + const tx = Transaction.fromValuesArray(txFixtures[4].raw.map(toBuffer)) st.equal( tx.hash().toString('hex'), '0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4' @@ -88,11 +88,11 @@ tape('[Transaction]', function (t) { }) t.test('should round trip decode a tx', function (st) { - const tx = LegacyTransaction.fromTxData({ value: 5000 }) + const tx = Transaction.fromTxData({ value: 5000 }) const s1 = tx.serialize() const s1Rlp = toBuffer('0x' + s1.toString('hex')) - const tx2 = LegacyTransaction.fromRlpSerializedTx(s1Rlp) + const tx2 = Transaction.fromRlpSerializedTx(s1Rlp) const s2 = tx2.serialize() st.ok(s1.equals(s2)) @@ -100,29 +100,29 @@ tape('[Transaction]', function (t) { }) t.test('should accept lesser r values', function (st) { - const tx = LegacyTransaction.fromTxData({ r: new BN(toBuffer('0x0005')) }) + const tx = Transaction.fromTxData({ r: new BN(toBuffer('0x0005')) }) st.equals(tx.r!.toString('hex'), '5') st.end() }) t.test('should return data fee', function (st) { - let tx = LegacyTransaction.fromTxData({}) + let tx = Transaction.fromTxData({}) st.equals(tx.getDataFee().toNumber(), 0) - tx = LegacyTransaction.fromValuesArray(txFixtures[3].raw.map(toBuffer)) + tx = Transaction.fromValuesArray(txFixtures[3].raw.map(toBuffer)) st.equals(tx.getDataFee().toNumber(), 1716) st.end() }) t.test('should return base fee', function (st) { - const tx = LegacyTransaction.fromTxData({}) + const tx = Transaction.fromTxData({}) st.equals(tx.getBaseFee().toNumber(), 53000) st.end() }) t.test('should return upfront cost', function (st) { - const tx = LegacyTransaction.fromTxData({ + const tx = Transaction.fromTxData({ gasPrice: 1000, gasLimit: 10000000, value: 42, @@ -133,7 +133,7 @@ tape('[Transaction]', function (t) { t.test("Verify EIP155 Signature based on Vitalik's tests", function (st) { txFixturesEip155.forEach(function (tx) { - const pt = LegacyTransaction.fromRlpSerializedTx(toBuffer(tx.rlp)) + const pt = Transaction.fromRlpSerializedTx(toBuffer(tx.rlp)) st.equal(pt.getMessageToSign().toString('hex'), tx.hash) st.equal('0x' + pt.serialize().toString('hex'), tx.rlp) st.equal(pt.getSenderAddress().toString(), '0x' + tx.sender) @@ -155,7 +155,7 @@ tape('[Transaction]', function (t) { '4646464646464646464646464646464646464646464646464646464646464646', 'hex' ) - const pt = LegacyTransaction.fromValuesArray(txRaw.map(toBuffer)) + const pt = Transaction.fromValuesArray(txRaw.map(toBuffer)) // Note that Vitalik's example has a very similar value denoted "signing data". // It's not the output of `serialize()`, but the pre-image of the hash returned by `tx.hash(false)`. @@ -192,7 +192,7 @@ tape('[Transaction]', function (t) { 'hex' ) const common = new Common({ chain: 3 }) - const tx = LegacyTransaction.fromValuesArray(txRaw.map(toBuffer), { common }) + const tx = Transaction.fromValuesArray(txRaw.map(toBuffer), { common }) const signedTx = tx.sign(privateKey) st.equal( signedTx.serialize().toString('hex'), @@ -205,7 +205,7 @@ tape('[Transaction]', function (t) { t.test('sign tx with chainId specified in params', function (st) { const common = new Common({ chain: 42, hardfork: 'petersburg' }) - let tx = LegacyTransaction.fromTxData({}, { common }) + let tx = Transaction.fromTxData({}, { common }) st.equal(tx.getChainId(), 42) const privKey = Buffer.from(txFixtures[0].privateKey, 'hex') @@ -213,7 +213,7 @@ tape('[Transaction]', function (t) { const serialized = tx.serialize() - const reTx = LegacyTransaction.fromRlpSerializedTx(serialized, { common }) + const reTx = Transaction.fromRlpSerializedTx(serialized, { common }) st.equal(reTx.verifySignature(), true) st.equal(reTx.getChainId(), 42) @@ -221,7 +221,7 @@ tape('[Transaction]', function (t) { }) t.test('returns correct values for isSigned', function (st) { - let tx = LegacyTransaction.fromTxData({}) + let tx = Transaction.fromTxData({}) st.notOk(tx.isSigned()) const txData: TxData = { @@ -236,29 +236,29 @@ tape('[Transaction]', function (t) { '4646464646464646464646464646464646464646464646464646464646464646', 'hex' ) - tx = LegacyTransaction.fromTxData(txData) + tx = Transaction.fromTxData(txData) st.notOk(tx.isSigned()) tx = tx.sign(privateKey) st.ok(tx.isSigned()) - tx = LegacyTransaction.fromTxData(txData) + tx = Transaction.fromTxData(txData) st.notOk(tx.isSigned()) const rawUnsigned = tx.serialize() tx = tx.sign(privateKey) const rawSigned = tx.serialize() st.ok(tx.isSigned()) - tx = LegacyTransaction.fromRlpSerializedTx(rawUnsigned) + tx = Transaction.fromRlpSerializedTx(rawUnsigned) st.notOk(tx.isSigned()) tx = tx.sign(privateKey) st.ok(tx.isSigned()) - tx = LegacyTransaction.fromRlpSerializedTx(rawSigned) + tx = Transaction.fromRlpSerializedTx(rawSigned) st.ok(tx.isSigned()) const signedValues = (rlp.decode(rawSigned) as any) as Buffer[] - tx = LegacyTransaction.fromValuesArray(signedValues) + tx = Transaction.fromValuesArray(signedValues) st.ok(tx.isSigned()) - tx = LegacyTransaction.fromValuesArray(signedValues.slice(0, 6)) + tx = Transaction.fromValuesArray(signedValues.slice(0, 6)) st.notOk(tx.isSigned()) st.end() }) @@ -267,12 +267,12 @@ tape('[Transaction]', function (t) { 'throws when creating a a transaction with incompatible chainid and v value', function (st) { const common = new Common({ chain: 42, hardfork: 'petersburg' }) - let tx = LegacyTransaction.fromTxData({}, { common }) + let tx = Transaction.fromTxData({}, { common }) st.equal(tx.getChainId(), 42) const privKey = Buffer.from(txFixtures[0].privateKey, 'hex') tx = tx.sign(privKey) const serialized = tx.serialize() - st.throws(() => LegacyTransaction.fromRlpSerializedTx(serialized)) + st.throws(() => Transaction.fromRlpSerializedTx(serialized)) st.end() } ) @@ -282,7 +282,7 @@ tape('[Transaction]', function (t) { function (st) { st.throws(() => { const common = new Common({ chain: 42, hardfork: 'petersburg' }) - LegacyTransaction.fromTxData({ v: new BN(1) }, { common }) + Transaction.fromTxData({ v: new BN(1) }, { common }) }) st.end() } @@ -291,7 +291,7 @@ tape('[Transaction]', function (t) { t.test('EIP155 hashing when singing', function (st) { const common = new Common({ chain: 1, hardfork: 'petersburg' }) txFixtures.slice(0, 3).forEach(function (txData) { - const tx = LegacyTransaction.fromValuesArray(txData.raw.slice(0, 6).map(toBuffer), { + const tx = Transaction.fromValuesArray(txData.raw.slice(0, 6).map(toBuffer), { common, }) @@ -325,39 +325,36 @@ tape('[Transaction]', function (t) { 'hex' ) - const fixtureTxSignedWithEIP155 = LegacyTransaction.fromTxData(txData).sign(privateKey) + const fixtureTxSignedWithEIP155 = Transaction.fromTxData(txData).sign(privateKey) const common = new Common({ chain: 'mainnet', hardfork: 'tangerineWhistle', }) - const fixtureTxSignedWithoutEIP155 = LegacyTransaction.fromTxData(txData, { + const fixtureTxSignedWithoutEIP155 = Transaction.fromTxData(txData, { common, }).sign(privateKey) - let signedWithEIP155 = LegacyTransaction.fromTxData( - fixtureTxSignedWithEIP155.toJSON() - ).sign(privateKey) + let signedWithEIP155 = Transaction.fromTxData(fixtureTxSignedWithEIP155.toJSON()).sign( + privateKey + ) st.true(signedWithEIP155.verifySignature()) st.notEqual(signedWithEIP155.v?.toString('hex'), '1c') st.notEqual(signedWithEIP155.v?.toString('hex'), '1b') - signedWithEIP155 = LegacyTransaction.fromTxData( - fixtureTxSignedWithoutEIP155.toJSON() - ).sign(privateKey) + signedWithEIP155 = Transaction.fromTxData(fixtureTxSignedWithoutEIP155.toJSON()).sign( + privateKey + ) st.true(signedWithEIP155.verifySignature()) st.notEqual(signedWithEIP155.v?.toString('hex'), '1c') st.notEqual(signedWithEIP155.v?.toString('hex'), '1b') - let signedWithoutEIP155 = LegacyTransaction.fromTxData( - fixtureTxSignedWithEIP155.toJSON(), - { - common, - } - ).sign(privateKey) + let signedWithoutEIP155 = Transaction.fromTxData(fixtureTxSignedWithEIP155.toJSON(), { + common, + }).sign(privateKey) st.true(signedWithoutEIP155.verifySignature()) st.true( @@ -366,12 +363,9 @@ tape('[Transaction]', function (t) { "v shouldn't be EIP155 encoded" ) - signedWithoutEIP155 = LegacyTransaction.fromTxData( - fixtureTxSignedWithoutEIP155.toJSON(), - { - common, - } - ).sign(privateKey) + signedWithoutEIP155 = Transaction.fromTxData(fixtureTxSignedWithoutEIP155.toJSON(), { + common, + }).sign(privateKey) st.true(signedWithoutEIP155.verifySignature()) st.true( @@ -386,10 +380,10 @@ tape('[Transaction]', function (t) { t.test('should return correct data fee for istanbul', function (st) { const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' }) - let tx = LegacyTransaction.fromTxData({}, { common }) + let tx = Transaction.fromTxData({}, { common }) st.equals(tx.getDataFee().toNumber(), 0) - tx = LegacyTransaction.fromValuesArray(txFixtures[3].raw.map(toBuffer), { + tx = Transaction.fromValuesArray(txFixtures[3].raw.map(toBuffer), { common, }) st.equals(tx.getDataFee().toNumber(), 1716) diff --git a/packages/tx/test/transactionFactory.spec.ts b/packages/tx/test/transactionFactory.spec.ts index 9ee33245ea9..8f1f6f2449b 100644 --- a/packages/tx/test/transactionFactory.spec.ts +++ b/packages/tx/test/transactionFactory.spec.ts @@ -1,7 +1,7 @@ import Common from '@ethereumjs/common' import { BN } from 'ethereumjs-util' import tape from 'tape' -import { AccessListEIP2930Transaction, TransactionFactory, LegacyTransaction } from '../src' +import { AccessListEIP2930Transaction, TransactionFactory, Transaction } from '../src' const EIP2930Common = new Common({ eips: [2718, 2929, 2930], @@ -16,12 +16,12 @@ const simpleUnsignedAccessListEIP2930Transaction = AccessListEIP2930Transaction. { common: EIP2930Common } ) -const simpleUnsignedLegacyTransaction = LegacyTransaction.fromTxData({}) +const simpleUnsignedTransaction = Transaction.fromTxData({}) const simpleSignedAccessListEIP2930Transaction = simpleUnsignedAccessListEIP2930Transaction.sign( pKey ) -const simpleSignedLegacyTransaction = simpleUnsignedLegacyTransaction.sign(pKey) +const simpleSignedTransaction = simpleUnsignedTransaction.sign(pKey) tape('[TransactionFactory]: Basic functions', function (t) { t.test('should return the right type', function (st) { @@ -29,10 +29,10 @@ tape('[TransactionFactory]: Basic functions', function (t) { const factoryTx = TransactionFactory.fromRawData(serialized, { common: EIP2930Common }) st.equals(factoryTx.constructor.name, AccessListEIP2930Transaction.name) - const legacyTx = LegacyTransaction.fromTxData({}) + const legacyTx = Transaction.fromTxData({}) const serializedLegacyTx = legacyTx.serialize() const factoryLegacyTx = TransactionFactory.fromRawData(serializedLegacyTx, {}) - st.equals(factoryLegacyTx.constructor.name, LegacyTransaction.name) + st.equals(factoryLegacyTx.constructor.name, Transaction.name) st.end() }) @@ -61,7 +61,7 @@ tape('[TransactionFactory]: Basic functions', function (t) { t.test('should give me the right classes in getTransactionClass', function (st) { const legacyTx = TransactionFactory.getTransactionClass() - st.equals(legacyTx!.name, LegacyTransaction.name) + st.equals(legacyTx!.name, Transaction.name) const eip2930Tx = TransactionFactory.getTransactionClass(1, EIP2930Common) st.equals(eip2930Tx!.name, AccessListEIP2930Transaction.name) @@ -85,13 +85,13 @@ tape('[TransactionFactory]: Basic functions', function (t) { }) t.test('should decode raw block body data', function (st) { - const rawLegacy = simpleSignedLegacyTransaction.raw() + const rawLegacy = simpleSignedTransaction.raw() const rawEIP2930 = simpleSignedAccessListEIP2930Transaction.raw() const legacyTx = TransactionFactory.fromBlockBodyData(rawLegacy) const eip2930Tx = TransactionFactory.fromBlockBodyData(rawEIP2930, { common: EIP2930Common }) - st.equals(legacyTx.constructor.name, LegacyTransaction.name) + st.equals(legacyTx.constructor.name, Transaction.name) st.equals(eip2930Tx.constructor.name, AccessListEIP2930Transaction.name) st.end() }) @@ -104,8 +104,8 @@ tape('[TransactionFactory]: Basic functions', function (t) { TransactionFactory.fromTxData({ type: 1 }) }) - st.equals(legacyTx.constructor.name, LegacyTransaction.name) - st.equals(legacyTx2.constructor.name, LegacyTransaction.name) + st.equals(legacyTx.constructor.name, Transaction.name) + st.equals(legacyTx2.constructor.name, Transaction.name) st.equals(eip2930Tx.constructor.name, AccessListEIP2930Transaction.name) st.end() }) diff --git a/packages/tx/test/transactionRunner.ts b/packages/tx/test/transactionRunner.ts index 3a89bf8e5dc..80e86afcaea 100644 --- a/packages/tx/test/transactionRunner.ts +++ b/packages/tx/test/transactionRunner.ts @@ -2,7 +2,7 @@ import tape from 'tape' import minimist from 'minimist' import { toBuffer } from 'ethereumjs-util' import Common from '@ethereumjs/common' -import { LegacyTransaction } from '../src/' +import { Transaction } from '../src/' import { ForkName, ForkNamesMap, OfficialTransactionTestData } from './types' const testing = require('./testLoader') @@ -46,7 +46,7 @@ tape('TransactionTests', (t) => { const rawTx = toBuffer(testData.rlp) const hardfork = forkNameMap[forkName] const common = new Common({ chain: 1, hardfork }) - const tx = LegacyTransaction.fromRlpSerializedTx(rawTx, { common }) + const tx = Transaction.fromRlpSerializedTx(rawTx, { common }) const sender = tx.getSenderAddress().toString() const hash = tx.hash().toString('hex') diff --git a/packages/vm/examples/run-solidity-contract/index.ts b/packages/vm/examples/run-solidity-contract/index.ts index e1cfe75ffaf..c002156f026 100644 --- a/packages/vm/examples/run-solidity-contract/index.ts +++ b/packages/vm/examples/run-solidity-contract/index.ts @@ -3,7 +3,7 @@ import { join } from 'path' import { readFileSync } from 'fs' import { defaultAbiCoder as AbiCoder, Interface } from '@ethersproject/abi' import { Account, Address, BN } from 'ethereumjs-util' -import { LegacyTransaction } from '@ethereumjs/tx' +import { Transaction } from '@ethereumjs/tx' import VM from '../../dist' const solc = require('solc') @@ -96,7 +96,7 @@ async function deployContract( nonce: await getAccountNonce(vm, senderPrivateKey), } - const tx = LegacyTransaction.fromTxData(txData).sign(senderPrivateKey) + const tx = Transaction.fromTxData(txData).sign(senderPrivateKey) const deploymentResult = await vm.runTx({ tx }) @@ -124,7 +124,7 @@ async function setGreeting( nonce: await getAccountNonce(vm, senderPrivateKey), } - const tx = LegacyTransaction.fromTxData(txData).sign(senderPrivateKey) + const tx = Transaction.fromTxData(txData).sign(senderPrivateKey) const setGreetingResult = await vm.runTx({ tx }) diff --git a/packages/vm/examples/run-transactions-complete/index.ts b/packages/vm/examples/run-transactions-complete/index.ts index 41ffa6ce561..0bca72b17de 100644 --- a/packages/vm/examples/run-transactions-complete/index.ts +++ b/packages/vm/examples/run-transactions-complete/index.ts @@ -1,5 +1,5 @@ import { Account, BN, toBuffer, pubToAddress, bufferToHex } from 'ethereumjs-util' -import { LegacyTransaction, TxData } from '@ethereumjs/tx' +import { Transaction, TxData } from '@ethereumjs/tx' import VM from '../..' async function main() { @@ -50,7 +50,7 @@ async function main() { } async function runTx(vm: VM, txData: TxData, privateKey: Buffer) { - const tx = LegacyTransaction.fromTxData(txData).sign(privateKey) + const tx = Transaction.fromTxData(txData).sign(privateKey) console.log('----running tx-------') const results = await vm.runTx({ tx }) diff --git a/packages/vm/tests/api/EIPs/eip-2929.spec.ts b/packages/vm/tests/api/EIPs/eip-2929.spec.ts index b3ef5d0db6b..105ce5ae836 100644 --- a/packages/vm/tests/api/EIPs/eip-2929.spec.ts +++ b/packages/vm/tests/api/EIPs/eip-2929.spec.ts @@ -2,7 +2,7 @@ import tape from 'tape' import { Account, Address, BN } from 'ethereumjs-util' import VM from '../../../lib' import Common from '@ethereumjs/common' -import { LegacyTransaction } from '@ethereumjs/tx' +import { Transaction } from '@ethereumjs/tx' // Test cases source: https://gist.github.com/holiman/174548cad102096858583c6fbbb0649a tape('EIP 2929: gas cost tests', (t) => { @@ -50,7 +50,7 @@ tape('EIP 2929: gas cost tests', (t) => { await vm.stateManager.putContractCode(address, Buffer.from(test.code, 'hex')) - const unsignedTx = LegacyTransaction.fromTxData({ + const unsignedTx = Transaction.fromTxData({ gasLimit: initialGas, // ensure we pass a lot of gas, so we do not run out of gas to: address, // call to the contract address, }) @@ -80,7 +80,7 @@ tape('EIP 2929: gas cost tests', (t) => { await vm.stateManager.putContractCode(contractAddress, Buffer.from(code, 'hex')) // setup the contract code // setup the call arguments - const unsignedTx = LegacyTransaction.fromTxData({ + const unsignedTx = Transaction.fromTxData({ gasLimit: new BN(21000 + 9000), // ensure we pass a lot of gas, so we do not run out of gas to: contractAddress, // call to the contract address, value: new BN(1), diff --git a/packages/vm/tests/api/events.spec.ts b/packages/vm/tests/api/events.spec.ts index 054514bd9f7..325db22df6e 100644 --- a/packages/vm/tests/api/events.spec.ts +++ b/packages/vm/tests/api/events.spec.ts @@ -1,6 +1,6 @@ import tape from 'tape' import { toBuffer, bufferToHex } from 'ethereumjs-util' -import { LegacyTransaction } from '@ethereumjs/tx' +import { Transaction } from '@ethereumjs/tx' import { Block } from '@ethereumjs/block' import VM from '../../lib/index' @@ -58,7 +58,7 @@ tape('VM events', (t) => { emitted = val }) - const tx = LegacyTransaction.fromTxData({ + const tx = Transaction.fromTxData({ gasPrice: 40000, gasLimit: 90000, to: '0x1111111111111111111111111111111111111111', @@ -79,7 +79,7 @@ tape('VM events', (t) => { emitted = val }) - const tx = LegacyTransaction.fromTxData({ + const tx = Transaction.fromTxData({ gasPrice: 40000, gasLimit: 90000, to: '0x1111111111111111111111111111111111111111', @@ -101,7 +101,7 @@ tape('VM events', (t) => { emitted = val }) - const tx = LegacyTransaction.fromTxData({ + const tx = Transaction.fromTxData({ gasPrice: 40000, gasLimit: 90000, to: '0x1111111111111111111111111111111111111111', @@ -124,7 +124,7 @@ tape('VM events', (t) => { emitted = val }) - const tx = LegacyTransaction.fromTxData({ + const tx = Transaction.fromTxData({ gasPrice: 40000, gasLimit: 90000, to: '0x1111111111111111111111111111111111111111', @@ -149,7 +149,7 @@ tape('VM events', (t) => { // This a deployment transaction that pushes 0x41 (i.e. ascii A) followed by 31 0s to // the stack, stores that in memory, and then returns the first byte from memory. // This deploys a contract which a single byte of code, 0x41. - const tx = LegacyTransaction.fromTxData({ + const tx = Transaction.fromTxData({ gasPrice: 40000, gasLimit: 90000, data: '0x7f410000000000000000000000000000000000000000000000000000000000000060005260016000f3', @@ -173,7 +173,7 @@ tape('VM events', (t) => { // This a deployment transaction that pushes 0x41 (i.e. ascii A) followed by 31 0s to // the stack, stores that in memory, and then returns the first byte from memory. // This deploys a contract which a single byte of code, 0x41. - const tx = LegacyTransaction.fromTxData({ + const tx = Transaction.fromTxData({ gasPrice: 40000, gasLimit: 90000, data: '0x7f410000000000000000000000000000000000000000000000000000000000000060005260016000f3', diff --git a/packages/vm/tests/api/runBlock.spec.ts b/packages/vm/tests/api/runBlock.spec.ts index 3b20e0f43a8..d8bc5d63fa7 100644 --- a/packages/vm/tests/api/runBlock.spec.ts +++ b/packages/vm/tests/api/runBlock.spec.ts @@ -2,7 +2,7 @@ import tape from 'tape' import { Address, BN, rlp } from 'ethereumjs-util' import Common from '@ethereumjs/common' import { Block } from '@ethereumjs/block' -import { LegacyTransaction } from '@ethereumjs/tx' +import { Transaction } from '@ethereumjs/tx' import { DefaultStateManager } from '../../lib/state' import runBlock from '../../lib/runBlock' import { setupPreConditions, getDAOCommon } from '../util' @@ -98,7 +98,7 @@ tape('should fail when tx gas limit higher than block gas limit', async (t) => { const gasLimit = new BN(Buffer.from('3fefba', 'hex')) const opts = { common: block._common } - block.transactions[0] = new LegacyTransaction( + block.transactions[0] = new Transaction( { nonce, gasPrice, gasLimit, to, value, data, v, r, s }, opts ) @@ -135,7 +135,7 @@ tape('should correctly use the hardforkByBlockNumber option', async (t) => { number: new BN(10000000), }, transactions: [ - LegacyTransaction.fromTxData( + Transaction.fromTxData( { data: '0x600154', // PUSH 01 SLOAD gasLimit: new BN(100000), diff --git a/packages/vm/tests/api/runTx.spec.ts b/packages/vm/tests/api/runTx.spec.ts index 4d8361d85f7..09da7913eb6 100644 --- a/packages/vm/tests/api/runTx.spec.ts +++ b/packages/vm/tests/api/runTx.spec.ts @@ -2,7 +2,7 @@ import tape from 'tape' import { Account, Address, BN, MAX_INTEGER } from 'ethereumjs-util' import { Block } from '@ethereumjs/block' import Common from '@ethereumjs/common' -import { LegacyTransaction } from '@ethereumjs/tx' +import { Transaction } from '@ethereumjs/tx' import VM from '../../lib' import { DefaultStateManager } from '../../lib/state' import runTx from '../../lib/runTx' @@ -129,7 +129,7 @@ tape('should clear storage cache after every transaction', async (t) => { Buffer.from('00'.repeat(32), 'hex'), Buffer.from('00'.repeat(31) + '01', 'hex') ) - const tx = LegacyTransaction.fromTxData( + const tx = Transaction.fromTxData( { nonce: '0x00', gasPrice: 1, @@ -165,7 +165,7 @@ tape('should be possible to disable the block gas limit validation', async (t) = const transferCost = 21000 - const unsignedTx = LegacyTransaction.fromTxData({ + const unsignedTx = Transaction.fromTxData({ to: address, gasLimit: transferCost, gasPrice: 1, @@ -234,7 +234,7 @@ function getTransaction(sign = false, value = '0x00', createContract = false) { data, } - const tx = LegacyTransaction.fromTxData(txParams) + const tx = Transaction.fromTxData(txParams) if (sign) { const privateKey = Buffer.from( diff --git a/packages/vm/tests/util.ts b/packages/vm/tests/util.ts index 7b567faa3bb..4d92bb6163a 100644 --- a/packages/vm/tests/util.ts +++ b/packages/vm/tests/util.ts @@ -8,7 +8,7 @@ import { setLengthLeft, toBuffer, } from 'ethereumjs-util' -import { AccessListEIP2930Transaction, LegacyTransaction, TxOptions } from '@ethereumjs/tx' +import { AccessListEIP2930Transaction, Transaction, TxOptions } from '@ethereumjs/tx' import { Block, BlockHeader, BlockOptions } from '@ethereumjs/block' import Common from '@ethereumjs/common' @@ -103,7 +103,7 @@ export function makeTx(txData: any, opts?: TxOptions) { if (txData.accessLists) { tx = AccessListEIP2930Transaction.fromTxData(txData, opts) } else { - tx = LegacyTransaction.fromTxData(txData, opts) + tx = Transaction.fromTxData(txData, opts) } if (txData.secretKey) { From c8f340e061437b7daaf986c552aff9ef1020056c Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Fri, 5 Mar 2021 12:47:46 +0100 Subject: [PATCH 3/3] tx, vm, block: renamed tx Transaction type to TypedTransaction, fixed compatibility issues in VM and Block --- packages/block/src/block.ts | 6 +++--- packages/block/src/from-rpc.ts | 4 ++-- packages/tx/src/transactionFactory.ts | 6 +++--- packages/tx/src/types.ts | 2 +- packages/vm/lib/runBlock.ts | 10 ++++++---- packages/vm/lib/runTx.ts | 12 ++++++++---- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/packages/block/src/block.ts b/packages/block/src/block.ts index f710502f341..7cd09dc0990 100644 --- a/packages/block/src/block.ts +++ b/packages/block/src/block.ts @@ -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' @@ -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 @@ -130,7 +130,7 @@ export class Block { */ constructor( header?: BlockHeader, - transactions: Transaction[] = [], + transactions: TypedTransaction[] = [], uncleHeaders: BlockHeader[] = [], opts: BlockOptions = {} ) { diff --git a/packages/block/src/from-rpc.ts b/packages/block/src/from-rpc.ts index 27b4dec518d..da092d274af 100644 --- a/packages/block/src/from-rpc.ts +++ b/packages/block/src/from-rpc.ts @@ -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' @@ -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) { diff --git a/packages/tx/src/transactionFactory.ts b/packages/tx/src/transactionFactory.ts index 1f9aa1c530c..b9206f9b1a0 100644 --- a/packages/tx/src/transactionFactory.ts +++ b/packages/tx/src/transactionFactory.ts @@ -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' }) @@ -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 @@ -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 diff --git a/packages/tx/src/types.ts b/packages/tx/src/types.ts index 732189f5fa1..468590b862c 100644 --- a/packages/tx/src/types.ts +++ b/packages/tx/src/types.ts @@ -131,7 +131,7 @@ export interface TxData { type?: BNLike } -export type Transaction = Transaction | AccessListEIP2930Transaction +export type TypedTransaction = Transaction | AccessListEIP2930Transaction export type BaseTransactionData = { /** diff --git a/packages/vm/lib/runBlock.ts b/packages/vm/lib/runBlock.ts index 7d58fa7df96..a2ecc8c9630 100644 --- a/packages/vm/lib/runBlock.ts +++ b/packages/vm/lib/runBlock.ts @@ -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 @@ -373,7 +373,7 @@ 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, @@ -381,7 +381,9 @@ async function applyTransactions(this: VM, block: Block, opts: RunBlockOpts) { 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) diff --git a/packages/vm/lib/runTx.ts b/packages/vm/lib/runTx.ts index 623bf821463..50fc75a3645 100644 --- a/packages/vm/lib/runTx.ts +++ b/packages/vm/lib/runTx.ts @@ -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' @@ -25,7 +25,7 @@ export interface RunTxOpts { /** * An `@ethereumjs/tx` to run */ - tx: Transaction + tx: TypedTransaction /** * If true, skips the nonce check */ @@ -64,7 +64,7 @@ export interface AfterTxEvent extends RunTxResult { /** * The transaction which just got finished */ - transaction: Transaction + transaction: TypedTransaction } /** @@ -99,7 +99,11 @@ export default async function runTx(this: VM, opts: RunTxOpts): Promise