Skip to content

Commit 861d1c0

Browse files
tx: increase test coverage
1 parent 7d12beb commit 861d1c0

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

packages/tx/src/transactionFactory.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export default class TransactionFactory {
2121
// Assume LegacyTransaction
2222
return LegacyTransaction.fromTxData(txData, txOptions)
2323
} else {
24-
if (!common.isActivatedEIP(2718)) {
25-
throw new Error('Common support for TypedTransactions (EIP-2718) not activated')
26-
}
2724
const txType = new BN(txData.type).toNumber()
2825
return TransactionFactory.getTransactionClass(txType, common).fromTxData(txData, txOptions)
2926
}

packages/tx/test/eip2930.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ tape('[EIP2930Transaction]', function (t) {
228228
// Data from
229229
// https://github.com/INFURA/go-ethlibs/blob/75b2a52a39d353ed8206cffaf68d09bd1b154aae/eth/transaction_signing_test.go#L87
230230

231-
t.test('should sign transaction correctly', function (t) {
231+
t.test('should sign transaction correctly and return expected JSON', function (t) {
232232
const address = Buffer.from('0000000000000000000000000000000000001337', 'hex')
233233
const slot1 = Buffer.from(
234234
'0000000000000000000000000000000000000000000000000000000000000000',
@@ -291,6 +291,24 @@ tape('[EIP2930Transaction]', function (t) {
291291
t.ok(expectedSigned.equals(signed.serialize()), 'serialized signed message correct')
292292
t.ok(expectedHash.equals(signed.hash()), 'hash correct')
293293

294+
const expectedJSON = {
295+
chainId: '0x796f6c6f763378',
296+
nonce: '0x0',
297+
gasPrice: '0x3b9aca00',
298+
gasLimit: '0x62d4',
299+
to: '0xdf0a88b2b68c673713a8ec826003676f272e3573',
300+
value: '0x1',
301+
data: '0x',
302+
accessList: [
303+
{
304+
address: '0x0000000000000000000000000000000000001337',
305+
storageKeys: ['0x0000000000000000000000000000000000000000000000000000000000000000'],
306+
},
307+
],
308+
}
309+
310+
t.deepEqual(signed.toJSON(), expectedJSON)
311+
294312
t.end()
295313
})
296314
})

packages/tx/test/transactionFactory.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ const EIP2930Common = new Common({
1313
hardfork: 'berlin',
1414
})
1515

16+
const pKey = Buffer.from('4646464646464646464646464646464646464646464646464646464646464646', 'hex')
17+
1618
const simpleUnsignedEIP2930Transaction = EIP2930Transaction.fromTxData(
1719
{ chainId: new BN(1) },
1820
{ common: EIP2930Common }
1921
)
2022

23+
const simpleUnsignedLegacyTransaction = LegacyTransaction.fromTxData({})
24+
25+
const simpleSignedEIP2930Transaction = simpleUnsignedEIP2930Transaction.sign(pKey)
26+
const simpleSignedLegacyTransaction = simpleUnsignedLegacyTransaction.sign(pKey)
27+
2128
tape('[TransactionFactory]: Basic functions', function (t) {
2229
t.test('should return the right type', function (st) {
2330
const serialized = simpleUnsignedEIP2930Transaction.serialize()
@@ -78,4 +85,38 @@ tape('[TransactionFactory]: Basic functions', function (t) {
7885
})
7986
st.end()
8087
})
88+
89+
t.test('should decode raw block body data', function (st) {
90+
const rawLegacy = simpleSignedLegacyTransaction.raw()
91+
const rawEIP2930 = simpleSignedEIP2930Transaction.raw()
92+
93+
const legacyTx = TransactionFactory.fromBlockBodyData(rawLegacy)
94+
const eip2930Tx = TransactionFactory.fromBlockBodyData(rawEIP2930, { common: EIP2930Common })
95+
96+
st.equals(legacyTx.constructor.name, LegacyTransaction.name)
97+
st.equals(eip2930Tx.constructor.name, EIP2930Transaction.name)
98+
st.end()
99+
})
100+
101+
t.test('should create the right transaction types from tx data', function (st) {
102+
const legacyTx = TransactionFactory.fromTxData({ type: 0 })
103+
const legacyTx2 = TransactionFactory.fromTxData({})
104+
const eip2930Tx = TransactionFactory.fromTxData({ type: 1 }, { common: EIP2930Common })
105+
st.throws(() => {
106+
TransactionFactory.fromTxData({ type: 1 })
107+
})
108+
109+
st.equals(legacyTx.constructor.name, LegacyTransaction.name)
110+
st.equals(legacyTx2.constructor.name, LegacyTransaction.name)
111+
st.equals(eip2930Tx.constructor.name, EIP2930Transaction.name)
112+
st.end()
113+
})
114+
115+
t.test('if eip2718 is not activated, always return that the eip is not activated', function (st) {
116+
const newCommon = new Common({ chain: 'mainnet', hardfork: 'istanbul' })
117+
118+
const eip2930Active = TransactionFactory.eipSupport(newCommon, 2930)
119+
st.ok(!eip2930Active)
120+
st.end()
121+
})
81122
})

0 commit comments

Comments
 (0)