Skip to content

Commit eddd88c

Browse files
committed
tx: set default EIP2930 tx HF to the Common default for backwards-compatibility (EIP2930 tx will throw without explicitly being activated)
1 parent c8908ea commit eddd88c

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

packages/tx/src/baseTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ecsign,
99
publicToAddress,
1010
} from 'ethereumjs-util'
11-
import { BaseTransactionData, BaseTxOptions, DEFAULT_COMMON, JsonTx } from './types'
11+
import { BaseTransactionData, BaseTxOptions, JsonTx } from './types'
1212

1313
export abstract class BaseTransaction<TransactionObject> {
1414
public readonly nonce: BN
@@ -41,7 +41,7 @@ export abstract class BaseTransaction<TransactionObject> {
4141
this.common =
4242
(txOptions.common &&
4343
Object.assign(Object.create(Object.getPrototypeOf(txOptions.common)), txOptions.common)) ??
44-
DEFAULT_COMMON
44+
new Common({ chain: 'mainnet' })
4545
}
4646

4747
protected validateExceedsMaxInteger(validateCannotExceedMaxInteger: { [key: string]: BN }) {

packages/tx/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,3 @@ export interface JsonTx {
184184
accessList?: JsonAccessListItem[]
185185
type?: string
186186
}
187-
188-
export const DEFAULT_COMMON = new Common({ chain: 'mainnet', hardfork: 'berlin' })

packages/tx/test/base.spec.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ import { BaseTransaction } from '../src/baseTransaction'
66
import { privateToPublic } from 'ethereumjs-util'
77

88
tape('[BaseTransaction]', function (t) {
9+
// EIP-2930 is not enabled in Common by default (2021-03-06)
10+
const common = new Common({ chain: 'mainnet', hardfork: 'berlin' })
11+
912
const legacyFixtures: TxsJsonEntry[] = require('./json/txs.json')
1013
const legacyTxs: BaseTransaction<Transaction>[] = []
1114
legacyFixtures.slice(0, 4).forEach(function (tx: any) {
12-
legacyTxs.push(Transaction.fromTxData(tx.data))
15+
legacyTxs.push(Transaction.fromTxData(tx.data, { common }))
1316
})
1417

1518
const eip2930Fixtures = require('./json/eip2930txs.json')
1619
const eip2930Txs: BaseTransaction<AccessListEIP2930Transaction>[] = []
1720
eip2930Fixtures.forEach(function (tx: any) {
18-
eip2930Txs.push(AccessListEIP2930Transaction.fromTxData(tx.data))
21+
eip2930Txs.push(AccessListEIP2930Transaction.fromTxData(tx.data, { common }))
1922
})
2023

2124
const zero = Buffer.alloc(0)
@@ -38,58 +41,58 @@ tape('[BaseTransaction]', function (t) {
3841

3942
t.test('Initialization', function (st) {
4043
for (const txType of txTypes) {
41-
let tx = txType.class.fromTxData({})
44+
let tx = txType.class.fromTxData({}, { common })
4245
st.equal(
4346
tx.common.hardfork(),
4447
'berlin',
4548
`${txType.name}: should initialize with correct default HF`
4649
)
4750
st.ok(Object.isFrozen(tx), `${txType.name}: tx should be frozen by default`)
4851

49-
const common = new Common({
52+
const initCommon = new Common({
5053
chain: 'mainnet',
5154
hardfork: 'istanbul',
5255
eips: [2718, 2929, 2930],
5356
})
54-
tx = txType.class.fromTxData({}, { common })
57+
tx = txType.class.fromTxData({}, { common: initCommon })
5558
st.equal(
5659
tx.common.hardfork(),
5760
'istanbul',
5861
`${txType.name}: should initialize with correct HF provided`
5962
)
6063

61-
common.setHardfork('byzantium')
64+
initCommon.setHardfork('byzantium')
6265
st.equal(
6366
tx.common.hardfork(),
6467
'istanbul',
6568
`${txType.name}: should stay on correct HF if outer common HF changes`
6669
)
6770

68-
tx = txType.class.fromTxData({}, { freeze: false })
69-
tx = txType.class.fromTxData({}, { freeze: false })
71+
tx = txType.class.fromTxData({}, { common, freeze: false })
72+
tx = txType.class.fromTxData({}, { common, freeze: false })
7073
st.ok(
7174
!Object.isFrozen(tx),
7275
`${txType.name}: tx should not be frozen when freeze deactivated in options`
7376
)
7477

7578
// Perform the same test as above, but now using a different construction method. This also implies that passing on the
7679
// options object works as expected.
77-
tx = txType.class.fromTxData({}, { freeze: false })
80+
tx = txType.class.fromTxData({}, { common, freeze: false })
7881
const rlpData = tx.serialize()
7982

80-
tx = txType.class.fromRlpSerializedTx(rlpData)
83+
tx = txType.class.fromRlpSerializedTx(rlpData, { common })
8184
st.ok(Object.isFrozen(tx), `${txType.name}: tx should be frozen by default`)
8285

83-
tx = txType.class.fromRlpSerializedTx(rlpData, { freeze: false })
86+
tx = txType.class.fromRlpSerializedTx(rlpData, { common, freeze: false })
8487
st.ok(
8588
!Object.isFrozen(tx),
8689
`${txType.name}: tx should not be frozen when freeze deactivated in options`
8790
)
8891

89-
tx = txType.class.fromValuesArray(txType.values)
92+
tx = txType.class.fromValuesArray(txType.values, { common })
9093
st.ok(Object.isFrozen(tx), `${txType.name}: tx should be frozen by default`)
9194

92-
tx = txType.class.fromValuesArray(txType.values, { freeze: false })
95+
tx = txType.class.fromValuesArray(txType.values, { common, freeze: false })
9396
st.ok(
9497
!Object.isFrozen(tx),
9598
`${txType.name}: tx should not be frozen when freeze deactivated in options`
@@ -102,11 +105,11 @@ tape('[BaseTransaction]', function (t) {
102105
for (const txType of txTypes) {
103106
txType.txs.forEach(function (tx: any) {
104107
st.ok(
105-
txType.class.fromRlpSerializedTx(tx.serialize()),
108+
txType.class.fromRlpSerializedTx(tx.serialize(), { common }),
106109
`${txType.name}: should do roundtrip serialize() -> fromRlpSerializedTx()`
107110
)
108111
st.ok(
109-
txType.class.fromSerializedTx(tx.serialize()),
112+
txType.class.fromSerializedTx(tx.serialize(), { common }),
110113
`${txType.name}: should do roundtrip serialize() -> fromRlpSerializedTx()`
111114
)
112115
})
@@ -118,7 +121,7 @@ tape('[BaseTransaction]', function (t) {
118121
for (const txType of txTypes) {
119122
txType.txs.forEach(function (tx: any) {
120123
st.ok(
121-
txType.class.fromValuesArray(tx.raw(true)),
124+
txType.class.fromValuesArray(tx.raw(true), { common }),
122125
`${txType.name}: should do roundtrip raw() -> fromValuesArray()`
123126
)
124127
})
@@ -140,7 +143,7 @@ tape('[BaseTransaction]', function (t) {
140143
txType.fixtures.slice(0, 4).forEach(function (txFixture: any) {
141144
// set `s` to zero
142145
txFixture.data.s = `0x` + '0'.repeat(16)
143-
const tx = txType.class.fromTxData(txFixture.data)
146+
const tx = txType.class.fromTxData(txFixture.data, { common })
144147
st.equals(tx.verifySignature(), false, `${txType.name}: signature should not be valid`)
145148
st.ok(
146149
(<string[]>tx.validate(true)).includes('Invalid Signature'),

packages/tx/test/eip2930.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ const chainId = new BN(1)
1919

2020
tape('[AccessListEIP2930Transaction]', function (t) {
2121
t.test('Initialization / Getter', function (t) {
22+
t.ok(AccessListEIP2930Transaction.fromTxData({}, { common }), 'should initialize correctly')
23+
24+
const nonEIP2930Common = new Common({ chain: 'mainnet', hardfork: 'istanbul' })
25+
t.throws(() => {
26+
AccessListEIP2930Transaction.fromTxData({}, { common: nonEIP2930Common })
27+
}, 'should throw on a pre-Berlin Harfork (EIP-2930 not activated)')
28+
2229
t.throws(() => {
2330
AccessListEIP2930Transaction.fromTxData(
2431
{

packages/tx/test/legacy.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ const txFixturesEip155: VitaliksTestsDataEntry[] = require('./json/ttTransaction
1111
tape('[Transaction]', function (t) {
1212
const transactions: Transaction[] = []
1313

14+
t.test('Initialization', function (st) {
15+
const nonEIP2930Common = new Common({ chain: 'mainnet', hardfork: 'istanbul' })
16+
st.ok(
17+
Transaction.fromTxData({}, { common: nonEIP2930Common }),
18+
'should initialize on a pre-Berlin Harfork (EIP-2930 not activated)'
19+
)
20+
st.end()
21+
})
22+
1423
t.test('Initialization -> decode with fromValuesArray()', function (st) {
1524
txFixtures.slice(0, 4).forEach(function (tx: any) {
1625
const txData = tx.raw.map(toBuffer)

0 commit comments

Comments
 (0)