Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tx: migrate test data to js #3684

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/tx/test/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import {
paramsTx,
} from '../src/index.js'

import eip1559Fixtures from './json/eip1559txs.json'
import eip2930Fixtures from './json/eip2930txs.json'
import legacyFixtures from './json/txs.json'
import { eip1559TxsData } from './testData/eip1559txs.js'
import { eip2930TxsData } from './testData/eip2930txs.js'
import { txsData } from './testData/txs.js'

import type { BaseTransaction } from '../src/baseTransaction.js'
import type { AccessList2930TxData, FeeMarketEIP1559TxData, LegacyTxData } from '../src/index.js'
Expand All @@ -42,17 +42,17 @@ describe('[BaseTransaction]', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })

const legacyTxs: BaseTransaction<TransactionType.Legacy>[] = []
for (const tx of legacyFixtures.slice(0, 4)) {
for (const tx of txsData.slice(0, 4)) {
legacyTxs.push(createLegacyTx(tx.data as LegacyTxData, { common }))
}

const eip2930Txs: BaseTransaction<TransactionType.AccessListEIP2930>[] = []
for (const tx of eip2930Fixtures) {
for (const tx of eip2930TxsData) {
eip2930Txs.push(createAccessList2930Tx(tx.data as AccessList2930TxData, { common }))
}

const eip1559Txs: BaseTransaction<TransactionType.FeeMarketEIP1559>[] = []
for (const tx of eip1559Fixtures) {
for (const tx of eip1559TxsData) {
eip1559Txs.push(createFeeMarket1559Tx(tx.data as FeeMarketEIP1559TxData, { common }))
}

Expand All @@ -64,7 +64,7 @@ describe('[BaseTransaction]', () => {
type: TransactionType.Legacy,
values: Array(6).fill(zero),
txs: legacyTxs,
fixtures: legacyFixtures,
fixtures: txsData,
activeCapabilities: [],
create: {
txData: createLegacyTx,
Expand All @@ -84,7 +84,7 @@ describe('[BaseTransaction]', () => {
type: TransactionType.AccessListEIP2930,
values: [new Uint8Array([1])].concat(Array(7).fill(zero)),
txs: eip2930Txs,
fixtures: eip2930Fixtures,
fixtures: eip2930TxsData,
activeCapabilities: [Capability.EIP2718TypedTransaction, Capability.EIP2930AccessLists],
create: {
txData: createAccessList2930Tx,
Expand All @@ -99,7 +99,7 @@ describe('[BaseTransaction]', () => {
type: TransactionType.FeeMarketEIP1559,
values: [new Uint8Array([1])].concat(Array(8).fill(zero)),
txs: eip1559Txs,
fixtures: eip1559Fixtures,
fixtures: eip1559TxsData,
activeCapabilities: [
Capability.EIP1559FeeMarket,
Capability.EIP2718TypedTransaction,
Expand Down
47 changes: 23 additions & 24 deletions packages/tx/test/eip1559.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { assert, describe, it } from 'vitest'

import { createFeeMarket1559Tx } from '../src/index.js'

import testdata from './json/eip1559.json' // Source: Besu
import { eip1559Data } from './testData/eip1559.js' // Source: Besu

import type { FeeMarketEIP1559TxData, JSONTx } from '../src/index.js'
import type { PrefixedHexString } from '@ethereumjs/util'
import type { JSONTx } from '../src/index.js'

const common = createCustomCommon({ chainId: 4 }, Mainnet)
common.setHardfork(Hardfork.London)
Expand Down Expand Up @@ -106,21 +105,21 @@ describe('[FeeMarket1559Tx]', () => {
})

it('sign()', () => {
for (let index = 0; index < testdata.length; index++) {
const data = testdata[index]
const pkey = hexToBytes(data.privateKey as PrefixedHexString)
const txn = createFeeMarket1559Tx(data as FeeMarketEIP1559TxData, { common })
for (let index = 0; index < eip1559Data.length; index++) {
const data = eip1559Data[index]
const pkey = hexToBytes(data.privateKey)
const txn = createFeeMarket1559Tx(data, { common })
const signed = txn.sign(pkey)
const rlpSerialized = RLP.encode(Uint8Array.from(signed.serialize()))
assert.ok(
equalsBytes(rlpSerialized, hexToBytes(data.signedTransactionRLP as PrefixedHexString)),
equalsBytes(rlpSerialized, hexToBytes(data.signedTransactionRLP)),
'Should sign txs correctly',
)
}
})

it('addSignature() -> correctly adds correct signature values', () => {
const privKey = hexToBytes(testdata[0].privateKey as PrefixedHexString)
const privKey = hexToBytes(eip1559Data[0].privateKey)
const tx = createFeeMarket1559Tx({})
const signedTx = tx.sign(privKey)
const addSignatureTx = tx.addSignature(signedTx.v!, signedTx.r!, signedTx.s!)
Expand All @@ -129,7 +128,7 @@ describe('[FeeMarket1559Tx]', () => {
})

it('addSignature() -> correctly converts raw ecrecover values', () => {
const privKey = hexToBytes(testdata[0].privateKey as PrefixedHexString)
const privKey = hexToBytes(eip1559Data[0].privateKey)
const tx = createFeeMarket1559Tx({})

const msgHash = tx.getHashedMessageToSign()
Expand All @@ -142,7 +141,7 @@ describe('[FeeMarket1559Tx]', () => {
})

it('addSignature() -> throws when adding the wrong v value', () => {
const privKey = hexToBytes(testdata[0].privateKey as PrefixedHexString)
const privKey = hexToBytes(eip1559Data[0].privateKey)
const tx = createFeeMarket1559Tx({})

const msgHash = tx.getHashedMessageToSign()
Expand All @@ -155,9 +154,9 @@ describe('[FeeMarket1559Tx]', () => {
})

it('hash()', () => {
const data = testdata[0]
const pkey = hexToBytes(data.privateKey as PrefixedHexString)
let txn = createFeeMarket1559Tx(data as FeeMarketEIP1559TxData, { common })
const data = eip1559Data[0]
const pkey = hexToBytes(data.privateKey)
let txn = createFeeMarket1559Tx(data, { common })
let signed = txn.sign(pkey)
const expectedHash = hexToBytes(
'0x2e564c87eb4b40e7f469b2eec5aa5d18b0b46a24e8bf0919439cfb0e8fcae446',
Expand All @@ -166,7 +165,7 @@ describe('[FeeMarket1559Tx]', () => {
equalsBytes(signed.hash(), expectedHash),
'Should provide the correct hash when frozen',
)
txn = createFeeMarket1559Tx(data as FeeMarketEIP1559TxData, {
txn = createFeeMarket1559Tx(data, {
common,
freeze: false,
})
Expand All @@ -178,9 +177,9 @@ describe('[FeeMarket1559Tx]', () => {
})

it('freeze property propagates from unsigned tx to signed tx', () => {
const data = testdata[0]
const pkey = hexToBytes(data.privateKey as PrefixedHexString)
const txn = createFeeMarket1559Tx(data as FeeMarketEIP1559TxData, {
const data = eip1559Data[0]
const pkey = hexToBytes(data.privateKey)
const txn = createFeeMarket1559Tx(data, {
common,
freeze: false,
})
Expand All @@ -190,9 +189,9 @@ describe('[FeeMarket1559Tx]', () => {
})

it('common propagates from the common of tx, not the common in TxOptions', () => {
const data = testdata[0]
const pkey = hexToBytes(data.privateKey as PrefixedHexString)
const txn = createFeeMarket1559Tx(data as FeeMarketEIP1559TxData, {
const data = eip1559Data[0]
const pkey = hexToBytes(data.privateKey)
const txn = createFeeMarket1559Tx(data, {
common,
freeze: false,
})
Expand Down Expand Up @@ -239,9 +238,9 @@ describe('[FeeMarket1559Tx]', () => {
})

it('toJSON()', () => {
const data = testdata[0]
const pkey = hexToBytes(data.privateKey as PrefixedHexString)
const txn = createFeeMarket1559Tx(data as FeeMarketEIP1559TxData, { common })
const data = eip1559Data[0]
const pkey = hexToBytes(data.privateKey)
const txn = createFeeMarket1559Tx(data, { common })
const signed = txn.sign(pkey)

const json = signed.toJSON()
Expand Down
6 changes: 3 additions & 3 deletions packages/tx/test/eip4844.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
paramsTx,
} from '../src/index.js'

import blobTx from './json/serialized4844tx.json'
import { serialized4844TxData } from './testData/serialized4844tx.js'

import type { BlobEIP4844TxData } from '../src/index.js'
import type { Common } from '@ethereumjs/common'
Expand Down Expand Up @@ -657,7 +657,7 @@ describe('Network wrapper deserialization test', () => {
const commitments = blobsToCommitments(kzg, blobs)
const proofs = blobsToProofs(kzg, blobs, commitments)

const wrapper = hexToBytes(blobTx.tx as PrefixedHexString)
const wrapper = hexToBytes(serialized4844TxData.tx as PrefixedHexString)
const deserializedTx = createBlob4844TxFromSerializedNetworkWrapper(wrapper, {
common,
})
Expand All @@ -677,7 +677,7 @@ describe('Network wrapper deserialization test', () => {
const networkSerialized = bytesToHex(deserializedTx.serializeNetworkWrapper())
const serialized = bytesToHex(deserializedTx.serialize())
const sender = deserializedTx.getSenderAddress().toString()
assert.equal(networkSerialized, blobTx.tx, 'network serialization should match')
assert.equal(networkSerialized, serialized4844TxData.tx, 'network serialization should match')

assert.deepEqual(
txMeta,
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/test/eip7702.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const common = new Common({ chain: Mainnet, hardfork: Hardfork.Cancun, eips: [77
const pkey = hexToBytes(`0x${'20'.repeat(32)}`)
const addr = createAddressFromPrivateKey(pkey)

const ones32 = `0x${'01'.repeat(32)}` as PrefixedHexString
const ones32: PrefixedHexString = `0x${'01'.repeat(32)}`

function getTxData(override: Partial<AuthorizationListItem> = {}): TxData {
const validAuthorizationList: AuthorizationListItem = {
Expand Down
20 changes: 9 additions & 11 deletions packages/tx/test/fromRpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
} from '../src/index.js'
import { normalizeTxParams } from '../src/util.js'

import optimismTx from './json/optimismTx.json'
import rpcTx from './json/rpcTx.json'
import v0Tx from './json/v0tx.json'
import { optimismTxData } from './testData/optimismTx.js'
import { rpcTxData } from './testData/rpcTx.js'
import { v0txData } from './testData/v0tx.js'

import type { TypedTxData } from '../src/index.js'

Expand All @@ -32,13 +32,13 @@ describe('[fromJSONRPCProvider]', () => {
global.fetch = async (_url: string, req: any) => {
const json = JSON.parse(req.body)
if (json.params[0] === '0xed1960aa7d0d7b567c946d94331dddb37a1c67f51f30bf51f256ea40db88cfb0') {
const txData = await import(`./json/rpcTx.json`)
const { rpcTxData } = await import(`./testData/rpcTx.js`)
return {
ok: true,
status: 200,
json: () => {
return {
result: txData,
result: rpcTxData,
}
},
}
Expand Down Expand Up @@ -73,12 +73,12 @@ describe('[fromJSONRPCProvider]', () => {

describe('[normalizeTxParams]', () => {
it('should work', () => {
const normedTx = normalizeTxParams(rpcTx)
const normedTx = normalizeTxParams(rpcTxData)
const tx = createTx(normedTx)
assert.equal(normedTx.gasLimit, 21000n, 'correctly converted "gas" to "gasLimit"')
assert.equal(
bytesToHex(tx.hash()),
rpcTx.hash,
rpcTxData.hash,
'converted normed tx data to transaction object',
)
})
Expand All @@ -87,8 +87,7 @@ describe('[normalizeTxParams]', () => {
describe('fromRPC: interpret v/r/s values of 0x0 as undefined for Optimism system txs', () => {
it('should work', async () => {
for (const txType of txTypes) {
;(optimismTx as any).type = txType
const tx = await createTxFromRPC(optimismTx as TypedTxData)
const tx = await createTxFromRPC({ ...optimismTxData, type: txType } as TypedTxData)
assert.ok(tx.v === undefined)
assert.ok(tx.s === undefined)
assert.ok(tx.r === undefined)
Expand All @@ -106,9 +105,8 @@ describe('fromRPC: ensure `v="0x0"` is correctly decoded for signed txs', () =>
// legacy tx cannot have v=0
continue
}
;(v0Tx as any).type = txType
const common = createCustomCommon({ chainId: 0x10f2c }, Mainnet)
const tx = await createTxFromRPC(v0Tx as TypedTxData, { common })
const tx = await createTxFromRPC({ ...v0txData, type: txType } as TypedTxData, { common })
assert.ok(tx.isSigned())
}
})
Expand Down
102 changes: 0 additions & 102 deletions packages/tx/test/json/eip1559.json

This file was deleted.

Loading
Loading