Skip to content

Commit 3ab7adb

Browse files
author
ScottyPoi
committed
vm/tests: use testdata signers in tests
1 parent 85bca74 commit 3ab7adb

File tree

9 files changed

+82
-135
lines changed

9 files changed

+82
-135
lines changed

packages/vm/test/api/EIPs/eip-1153.spec.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { assert, describe, it } from 'vitest'
55

66
import { createVM, runTx } from '../../../src/index.ts'
77

8+
import { SIGNER_A } from '@ethereumjs/testdata'
89
import type { TypedTransaction } from '@ethereumjs/tx'
910
import type { PrefixedHexString } from '@ethereumjs/util'
1011

@@ -14,8 +15,6 @@ interface Test {
1415
transactions: TypedTransaction[]
1516
}
1617

17-
const senderKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
18-
1918
describe('EIP 1153: transient storage', () => {
2019
const initialGas = BigInt(0xffffffffff)
2120
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin, eips: [1153] })
@@ -59,7 +58,7 @@ describe('EIP 1153: transient storage', () => {
5958
await vm.stateManager.putCode(address, hexToBytes(code as PrefixedHexString))
6059
}
6160

62-
const fromAddress = new Address(privateToAddress(senderKey))
61+
const fromAddress = new Address(privateToAddress(SIGNER_A.privateKey))
6362
await vm.stateManager.putAccount(fromAddress, new Account(BigInt(0), BigInt(0xfffffffff)))
6463
const results = []
6564
for (const tx of test.transactions) {
@@ -80,7 +79,7 @@ describe('EIP 1153: transient storage', () => {
8079
gasLimit: BigInt(21000 + 9000),
8180
to: address,
8281
value: BigInt(1),
83-
}).sign(senderKey)
82+
}).sign(SIGNER_A.privateKey)
8483

8584
const test = {
8685
contracts: [{ address, code }],
@@ -123,12 +122,12 @@ describe('EIP 1153: transient storage', () => {
123122
gasLimit: BigInt(15000000),
124123
to: address,
125124
data: new Uint8Array(32),
126-
}).sign(senderKey),
125+
}).sign(SIGNER_A.privateKey),
127126
createLegacyTx({
128127
nonce: 1,
129128
gasLimit: BigInt(15000000),
130129
to: address,
131-
}).sign(senderKey),
130+
}).sign(SIGNER_A.privateKey),
132131
],
133132
steps: [
134133
// first tx
@@ -193,7 +192,7 @@ describe('EIP 1153: transient storage', () => {
193192
to: callingAddress,
194193
})
195194

196-
const tx = unsignedTx.sign(senderKey)
195+
const tx = unsignedTx.sign(SIGNER_A.privateKey)
197196

198197
const test = {
199198
contracts: [

packages/vm/test/api/EIPs/eip-2929.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { assert, describe, it } from 'vitest'
55

66
import { createVM, runTx } from '../../../src/index.ts'
77

8+
import { SIGNER_A } from '@ethereumjs/testdata'
89
import type { PrefixedHexString } from '@ethereumjs/util'
910

1011
// Test cases source: https://gist.github.com/holiman/174548cad102096858583c6fbbb0649a
1112
describe('EIP 2929: gas cost tests', () => {
1213
const initialGas = BigInt(0xffffffffff)
1314
const address = new Address(hexToBytes('0x000000000000000000000000636F6E7472616374'))
14-
const senderKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
1515
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin, eips: [2929] })
1616

1717
const runTest = async function (test: any) {
@@ -55,7 +55,7 @@ describe('EIP 2929: gas cost tests', () => {
5555
to: address, // call to the contract address,
5656
})
5757

58-
const tx = unsignedTx.sign(senderKey)
58+
const tx = unsignedTx.sign(SIGNER_A.privateKey)
5959

6060
const result = await runTx(vm, { tx, skipHardForkValidation: true })
6161

@@ -66,9 +66,7 @@ describe('EIP 2929: gas cost tests', () => {
6666

6767
const runCodeTest = async function (code: PrefixedHexString, expectedGasUsed: bigint) {
6868
// setup the accounts for this test
69-
const privateKey = hexToBytes(
70-
'0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109',
71-
)
69+
const privateKey = SIGNER_A.privateKey
7270
const contractAddress = new Address(hexToBytes('0x00000000000000000000000000000000000000ff'))
7371

7472
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin, eips: [2929] })

packages/vm/test/api/EIPs/eip-2930-accesslists.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@ethereumjs/util'
1010
import { assert, describe, it } from 'vitest'
1111

12+
import { SIGNER_A } from '@ethereumjs/testdata'
1213
import { createVM, runTx } from '../../../src/index.ts'
1314

1415
const common = new Common({
@@ -21,7 +22,6 @@ const validAddress = hexToBytes('0x00000000000000000000000000000000000000ff')
2122
const validSlot = hexToBytes(`0x${'00'.repeat(32)}`)
2223

2324
// setup the accounts for this test
24-
const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
2525
const contractAddress = new Address(validAddress)
2626

2727
describe('EIP-2930 Optional Access Lists tests', () => {
@@ -40,7 +40,7 @@ describe('EIP-2930 Optional Access Lists tests', () => {
4040
to: contractAddress,
4141
},
4242
{ common },
43-
).sign(privateKey)
43+
).sign(SIGNER_A.privateKey)
4444
const txnWithoutAccessList = createAccessList2930Tx(
4545
{
4646
accessList: [],
@@ -49,14 +49,14 @@ describe('EIP-2930 Optional Access Lists tests', () => {
4949
to: contractAddress,
5050
},
5151
{ common },
52-
).sign(privateKey)
52+
).sign(SIGNER_A.privateKey)
5353

5454
const vm = await createVM({ common })
5555

5656
// contract code PUSH1 0x00 SLOAD STOP
5757
await vm.stateManager.putCode(contractAddress, hexToBytes('0x60005400'))
5858

59-
const address = createAddressFromPrivateKey(privateKey)
59+
const address = createAddressFromPrivateKey(SIGNER_A.privateKey)
6060
const initialBalance = BigInt(10) ** BigInt(18)
6161

6262
const account = await vm.stateManager.getAccount(address)

packages/vm/test/api/buildBlock.spec.ts

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,15 @@ import {
1515
} from '@ethereumjs/common'
1616
import { Ethash } from '@ethereumjs/ethash'
1717
import { createFeeMarket1559Tx, createLegacyTx } from '@ethereumjs/tx'
18-
import {
19-
Address,
20-
concatBytes,
21-
createAccount,
22-
createAddressFromPrivateKey,
23-
createZeroAddress,
24-
hexToBytes,
25-
} from '@ethereumjs/util'
18+
import { concatBytes, createAccount, createZeroAddress } from '@ethereumjs/util'
2619
import { assert, describe, expect, it } from 'vitest'
2720

2821
import { buildBlock, createVM, runBlock } from '../../src/index.ts'
2922

3023
import { setBalance } from './utils.ts'
3124

3225
import type { Blockchain, ConsensusDict } from '@ethereumjs/blockchain'
33-
34-
const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
35-
const pKeyAddress = createAddressFromPrivateKey(privateKey)
26+
import { SIGNER_A } from '@ethereumjs/testdata'
3627

3728
describe('BlockBuilder', () => {
3829
it('should build a valid block', async () => {
@@ -41,7 +32,7 @@ describe('BlockBuilder', () => {
4132
const blockchain = await createBlockchain({ genesisBlock, common, validateConsensus: false })
4233
const vm = await createVM({ common, blockchain })
4334

44-
await setBalance(vm, pKeyAddress)
35+
await setBalance(vm, SIGNER_A.address)
4536

4637
const vmCopy = await vm.shallowCopy()
4738

@@ -55,7 +46,7 @@ describe('BlockBuilder', () => {
5546
const tx = createLegacyTx(
5647
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, gasPrice: 1 },
5748
{ common, freeze: false },
58-
).sign(privateKey)
49+
).sign(SIGNER_A.privateKey)
5950

6051
await blockBuilder.addTransaction(tx)
6152
const { block } = await blockBuilder.build()
@@ -114,7 +105,7 @@ describe('BlockBuilder', () => {
114105
})
115106
const vm = await createVM({ common, blockchain })
116107

117-
await setBalance(vm, pKeyAddress)
108+
await setBalance(vm, SIGNER_A.address)
118109

119110
const blockBuilder = await buildBlock(vm, {
120111
parentBlock: genesisBlock,
@@ -125,7 +116,7 @@ describe('BlockBuilder', () => {
125116
const tx = createLegacyTx(
126117
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, gasPrice: 1 },
127118
{ common, freeze: false },
128-
).sign(privateKey)
119+
).sign(SIGNER_A.privateKey)
129120

130121
await blockBuilder.addTransaction(tx)
131122

@@ -143,14 +134,6 @@ describe('BlockBuilder', () => {
143134
})
144135

145136
it('should correctly seal a PoA block', async () => {
146-
const signer = {
147-
address: new Address(hexToBytes('0x0b90087d864e82a284dca15923f3776de6bb016f')),
148-
privateKey: hexToBytes('0x64bf9cc30328b0e42387b3c82c614e6386259136235e20c1357bd11cdee86993'),
149-
publicKey: hexToBytes(
150-
'0x40b2ebdf4b53206d2d3d3d59e7e2f13b1ea68305aec71d5d24cefe7f24ecae886d241f9267f04702d7f693655eb7b4aa23f30dcd0c3c5f2b970aad7c8a828195',
151-
),
152-
}
153-
154137
// const common = new Common({ chain: Chain.Rinkeby, hardfork: Hardfork.Istanbul })
155138
const consensusConfig = {
156139
clique: {
@@ -187,11 +170,7 @@ describe('BlockBuilder', () => {
187170
alloc: {},
188171
}
189172

190-
const A = {
191-
address: new Address(hexToBytes('0x0b90087d864e82a284dca15923f3776de6bb016f')),
192-
privateKey: hexToBytes('0x64bf9cc30328b0e42387b3c82c614e6386259136235e20c1357bd11cdee86993'),
193-
}
194-
const addr = A.address.toString().slice(2)
173+
const addr = SIGNER_A.address.toString().slice(2)
195174

196175
const extraData2 = `0x${'0'.repeat(64)}${addr}${'0'.repeat(130)}`
197176
const chainData = {
@@ -205,8 +184,12 @@ describe('BlockBuilder', () => {
205184
})
206185

207186
// extraData: [vanity, activeSigner, seal]
208-
const extraData = concatBytes(new Uint8Array(32), signer.address.toBytes(), new Uint8Array(65))
209-
const cliqueSignerKey = signer.privateKey
187+
const extraData = concatBytes(
188+
new Uint8Array(32),
189+
SIGNER_A.address.toBytes(),
190+
new Uint8Array(65),
191+
)
192+
const cliqueSignerKey = SIGNER_A.privateKey
210193
const genesisBlock = createSealedCliqueBlock(
211194
{ header: { gasLimit: 50000, extraData } },
212195
cliqueSignerKey,
@@ -216,7 +199,7 @@ describe('BlockBuilder', () => {
216199
const vm = await createVM({ common, blockchain })
217200

218201
// add balance for tx
219-
await vm.stateManager.putAccount(signer.address, createAccount({ balance: 100000 }))
202+
await vm.stateManager.putAccount(SIGNER_A.address, createAccount({ balance: 100000 }))
220203

221204
const blockBuilder = await buildBlock(vm, {
222205
parentBlock: genesisBlock,
@@ -228,16 +211,19 @@ describe('BlockBuilder', () => {
228211
const tx = createLegacyTx(
229212
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, gasPrice: 1 },
230213
{ common, freeze: false },
231-
).sign(signer.privateKey)
214+
).sign(SIGNER_A.privateKey)
232215

233216
await blockBuilder.addTransaction(tx)
234217

235218
const { block } = await blockBuilder.build()
236219

237-
assert.isTrue(cliqueVerifySignature(block.header, [signer.address]), 'should verify signature')
220+
assert.isTrue(
221+
cliqueVerifySignature(block.header, [SIGNER_A.address]),
222+
'should verify signature',
223+
)
238224
assert.deepEqual(
239225
cliqueSigner(block.header),
240-
signer.address,
226+
SIGNER_A.address,
241227
'should recover the correct signer address',
242228
)
243229
})
@@ -248,7 +234,7 @@ describe('BlockBuilder', () => {
248234
const blockchain = await createBlockchain({ genesisBlock, common, validateConsensus: false })
249235
const vm = await createVM({ common, blockchain })
250236

251-
await setBalance(vm, pKeyAddress)
237+
await setBalance(vm, SIGNER_A.address)
252238

253239
let blockBuilder = await buildBlock(vm, {
254240
parentBlock: genesisBlock,
@@ -258,7 +244,7 @@ describe('BlockBuilder', () => {
258244
const tx = createLegacyTx(
259245
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, gasPrice: 1 },
260246
{ common, freeze: false },
261-
).sign(privateKey)
247+
).sign(SIGNER_A.privateKey)
262248

263249
await blockBuilder.addTransaction(tx)
264250
await blockBuilder.build()
@@ -279,7 +265,7 @@ describe('BlockBuilder', () => {
279265
const tx2 = createLegacyTx(
280266
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, gasPrice: 1, nonce: 1 },
281267
{ common, freeze: false },
282-
).sign(privateKey)
268+
).sign(SIGNER_A.privateKey)
283269

284270
await blockBuilder.addTransaction(tx2)
285271
await blockBuilder.revert()
@@ -327,7 +313,7 @@ describe('BlockBuilder', () => {
327313
const blockchain = await createBlockchain({ genesisBlock, common, validateConsensus: false })
328314
const vm = await createVM({ common, blockchain })
329315

330-
await setBalance(vm, pKeyAddress)
316+
await setBalance(vm, SIGNER_A.address)
331317

332318
const vmCopy = await vm.shallowCopy()
333319

@@ -341,12 +327,12 @@ describe('BlockBuilder', () => {
341327
const tx1 = createLegacyTx(
342328
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, gasPrice: 1 },
343329
{ common, freeze: false },
344-
).sign(privateKey)
330+
).sign(SIGNER_A.privateKey)
345331

346332
const tx2 = createFeeMarket1559Tx(
347333
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, maxFeePerGas: 10 },
348334
{ common, freeze: false },
349-
).sign(privateKey)
335+
).sign(SIGNER_A.privateKey)
350336

351337
for (const tx of [tx1, tx2]) {
352338
await expect(async () => {
@@ -359,12 +345,12 @@ describe('BlockBuilder', () => {
359345
const tx3 = createLegacyTx(
360346
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, gasPrice: 101 },
361347
{ common, freeze: false },
362-
).sign(privateKey)
348+
).sign(SIGNER_A.privateKey)
363349

364350
const tx4 = createFeeMarket1559Tx(
365351
{ to: createZeroAddress(), value: 1000, gasLimit: 21000, maxFeePerGas: 101, nonce: 1 },
366352
{ common, freeze: false },
367-
).sign(privateKey)
353+
).sign(SIGNER_A.privateKey)
368354

369355
for (const tx of [tx3, tx4]) {
370356
await blockBuilder.addTransaction(tx)

packages/vm/test/api/customChain.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { createBlock } from '@ethereumjs/block'
22
import { createBlockchain } from '@ethereumjs/blockchain'
33
import { Hardfork, Mainnet, createCustomCommon } from '@ethereumjs/common'
4-
import { customChainConfig, testnetMergeChainConfig } from '@ethereumjs/testdata'
4+
import {
5+
SIGNER_A,
6+
SIGNER_G,
7+
customChainConfig,
8+
testnetMergeChainConfig,
9+
} from '@ethereumjs/testdata'
510
import { createTx } from '@ethereumjs/tx'
611
import {
712
bytesToHex,
@@ -42,8 +47,8 @@ const accountState: AccountState = [
4247

4348
const contractAddress = '0x3651539F2E119a27c606cF0cB615410eCDaAE62a'
4449
const genesisState: GenesisState = {
45-
'0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b': '0x6d6172697573766477000000',
46-
'0xbe862ad9abfe6f22bcb087716c7d89a26051f74c': '0x6d6172697573766477000000',
50+
[SIGNER_A.address.toString()]: '0x6d6172697573766477000000',
51+
[SIGNER_G.address.toString()]: '0x6d6172697573766477000000',
4752
[contractAddress]: accountState,
4853
}
4954

@@ -60,7 +65,6 @@ const block = createBlock(
6065
common,
6166
},
6267
)
63-
const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
6468

6569
describe('VM initialized with custom state', () => {
6670
it('should transfer eth from already existent account', async () => {
@@ -79,7 +83,7 @@ describe('VM initialized with custom state', () => {
7983
{
8084
common,
8185
},
82-
).sign(privateKey)
86+
).sign(SIGNER_A.privateKey)
8387
const result = await runTx(vm, {
8488
tx,
8589
block,
@@ -106,7 +110,7 @@ describe('VM initialized with custom state', () => {
106110
const callResult = await vm.evm.runCall({
107111
to: createAddressFromString(contractAddress),
108112
data: hexToBytes(calldata as PrefixedHexString),
109-
caller: createAddressFromPrivateKey(privateKey),
113+
caller: createAddressFromPrivateKey(SIGNER_A.privateKey),
110114
})
111115

112116
const storage = genesisState[contractAddress][2]

0 commit comments

Comments
 (0)