-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
rpc.spec.ts
373 lines (319 loc) · 11.8 KB
/
rpc.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { injectL2Context } from '@eth-optimism/core-utils'
import { Wallet, BigNumber, Contract, ContractFactory } from 'ethers'
import { ethers } from 'hardhat'
import chai, { expect } from 'chai'
import {
sleep,
l2Provider,
DEFAULT_TRANSACTION,
fundUser,
expectApprox,
L2_CHAINID,
IS_LIVE_NETWORK,
} from './shared/utils'
import chaiAsPromised from 'chai-as-promised'
import { OptimismEnv } from './shared/env'
import {
TransactionReceipt,
TransactionRequest,
} from '@ethersproject/providers'
import { solidity } from 'ethereum-waffle'
chai.use(chaiAsPromised)
chai.use(solidity)
describe('Basic RPC tests', () => {
let env: OptimismEnv
let wallet: Wallet
const provider = injectL2Context(l2Provider)
let Reverter: Contract
let revertMessage: string
let revertingTx: TransactionRequest
let revertingDeployTx: TransactionRequest
before(async () => {
env = await OptimismEnv.new()
wallet = env.l2Wallet
const Factory__Reverter = await ethers.getContractFactory(
'Reverter',
wallet
)
Reverter = await Factory__Reverter.connect(env.l2Wallet).deploy()
await Reverter.deployTransaction.wait()
revertMessage = await Reverter.revertMessage()
revertingTx = {
to: Reverter.address,
data: Reverter.interface.encodeFunctionData('doRevert'),
}
const Factory__ConstructorReverter = await ethers.getContractFactory(
'ConstructorReverter',
wallet
)
revertingDeployTx = {
data: Factory__ConstructorReverter.bytecode,
}
})
describe('eth_sendRawTransaction', () => {
it('should correctly process a valid transaction', async () => {
const tx = DEFAULT_TRANSACTION
const nonce = await wallet.getTransactionCount()
const result = await wallet.sendTransaction(tx)
expect(result.from).to.equal(wallet.address)
expect(result.nonce).to.equal(nonce)
expect(result.gasLimit.toNumber()).to.equal(tx.gasLimit)
expect(result.gasPrice.toNumber()).to.equal(tx.gasPrice)
expect(result.data).to.equal(tx.data)
})
it('should not accept a transaction with the wrong chain ID', async () => {
const tx = {
...DEFAULT_TRANSACTION,
chainId: (await wallet.getChainId()) + 1,
}
await expect(
provider.sendTransaction(await wallet.signTransaction(tx))
).to.be.rejectedWith('invalid transaction: invalid sender')
})
it('should not accept a transaction without a chain ID', async () => {
const tx = {
...DEFAULT_TRANSACTION,
chainId: null, // Disables EIP155 transaction signing.
}
await expect(
provider.sendTransaction(await wallet.signTransaction(tx))
).to.be.rejectedWith('Cannot submit unprotected transaction')
})
it('should accept a transaction with a value', async () => {
const tx = {
...DEFAULT_TRANSACTION,
chainId: await env.l2Wallet.getChainId(),
data: '0x',
value: ethers.utils.parseEther('0.1'),
}
const balanceBefore = await provider.getBalance(env.l2Wallet.address)
const result = await env.l2Wallet.sendTransaction(tx)
const receipt = await result.wait()
expect(receipt.status).to.deep.equal(1)
expect(await provider.getBalance(env.l2Wallet.address)).to.deep.equal(
balanceBefore.sub(ethers.utils.parseEther('0.1'))
)
})
it('should reject a transaction with higher value than user balance', async () => {
const balance = await env.l2Wallet.getBalance()
const tx = {
...DEFAULT_TRANSACTION,
chainId: await env.l2Wallet.getChainId(),
data: '0x',
value: balance.add(ethers.utils.parseEther('1')),
}
await expect(env.l2Wallet.sendTransaction(tx)).to.be.rejectedWith(
'invalid transaction: insufficient funds for gas * price + value'
)
})
it('should correctly report OOG for contract creations', async () => {
const factory = await ethers.getContractFactory('TestOOGInConstructor')
await expect(factory.connect(wallet).deploy()).to.be.rejectedWith(
'gas required exceeds allowance'
)
})
})
describe('eth_call', () => {
it('should correctly identify call out-of-gas', async () => {
await expect(
provider.call({
...revertingTx,
gasLimit: 1,
})
).to.be.rejectedWith('out of gas')
})
it('should correctly return solidity revert data from a call', async () => {
await expect(provider.call(revertingTx)).to.be.revertedWith(revertMessage)
})
it('should produce error when called from ethers', async () => {
await expect(Reverter.doRevert()).to.be.revertedWith(revertMessage)
})
it('should correctly return revert data from contract creation', async () => {
await expect(provider.call(revertingDeployTx)).to.be.revertedWith(
revertMessage
)
})
it('should correctly identify contract creation out of gas', async () => {
await expect(
provider.call({
...revertingDeployTx,
gasLimit: 1,
})
).to.be.rejectedWith('out of gas')
})
it('should allow eth_calls with nonzero value', async () => {
// Deploy a contract to check msg.value of the call
const Factory__ValueContext: ContractFactory =
await ethers.getContractFactory('ValueContext', wallet)
const ValueContext: Contract = await Factory__ValueContext.deploy()
await ValueContext.deployTransaction.wait()
// Fund account to call from
const from = wallet.address
const value = 15
await fundUser(env.watcher, env.l1Bridge, value, from)
// Do the call and check msg.value
const data = ValueContext.interface.encodeFunctionData('getCallValue')
const res = await provider.call({
to: ValueContext.address,
from,
data,
value,
})
expect(res).to.eq(BigNumber.from(value))
})
})
describe('eth_getTransactionReceipt', () => {
it('correctly exposes revert data for contract calls', async () => {
const req: TransactionRequest = {
...revertingTx,
gasLimit: 8_000_000, // override gas estimation
}
const tx = await wallet.sendTransaction(req)
let errored = false
try {
await tx.wait()
} catch (e) {
errored = true
}
expect(errored).to.be.true
const receipt: TransactionReceipt = await provider.getTransactionReceipt(
tx.hash
)
expect(receipt.status).to.eq(0)
})
it('correctly exposes revert data for contract creations', async () => {
const req: TransactionRequest = {
...revertingDeployTx,
gasLimit: 8_000_000, // override gas estimation
}
const tx = await wallet.sendTransaction(req)
let errored = false
try {
await tx.wait()
} catch (e) {
errored = true
}
expect(errored).to.be.true
const receipt: TransactionReceipt = await provider.getTransactionReceipt(
tx.hash
)
expect(receipt.status).to.eq(0)
})
})
describe('eth_getTransactionByHash', () => {
it('should be able to get all relevant l1/l2 transaction data', async () => {
const tx = DEFAULT_TRANSACTION
const result = await wallet.sendTransaction(tx)
await result.wait()
const transaction = (await provider.getTransaction(result.hash)) as any
expect(transaction.queueOrigin).to.equal('sequencer')
expect(transaction.transactionIndex).to.be.eq(0)
expect(transaction.gasLimit).to.be.deep.eq(BigNumber.from(tx.gasLimit))
})
})
describe('eth_getBlockByHash', () => {
it('should return the block and all included transactions', async () => {
// Send a transaction and wait for it to be mined.
const tx = DEFAULT_TRANSACTION
const result = await wallet.sendTransaction(tx)
const receipt = await result.wait()
const block = (await provider.getBlockWithTransactions(
receipt.blockHash
)) as any
expect(block.number).to.not.equal(0)
expect(typeof block.stateRoot).to.equal('string')
expect(block.transactions.length).to.equal(1)
expect(block.transactions[0].queueOrigin).to.equal('sequencer')
expect(block.transactions[0].l1TxOrigin).to.equal(null)
})
})
describe('eth_getBlockByNumber', () => {
// There was a bug that causes transactions to be reingested over
// and over again only when a single transaction was in the
// canonical transaction chain. This test catches this by
// querying for the latest block and then waits and then queries
// the latest block again and then asserts that they are the same.
//
// Needs to be skipped on Prod networks because this test doesn't work when
// other people are sending transactions to the Sequencer at the same time
// as this test is running.
it('should return the same result when new transactions are not applied', async function () {
if (IS_LIVE_NETWORK) {
this.skip()
}
// Get latest block once to start.
const prev = await provider.getBlockWithTransactions('latest')
// set wait to null to allow a deep object comparison
prev.transactions[0].wait = null
// Over ten seconds, repeatedly check the latest block to make sure nothing has changed.
for (let i = 0; i < 5; i++) {
const latest = await provider.getBlockWithTransactions('latest')
latest.transactions[0].wait = null
// Check each key of the transaction individually
// for easy debugging if one field changes
for (const [key, value] of Object.entries(latest.transactions[0])) {
expect(value).to.deep.equal(
prev.transactions[0][key],
`mismatch ${key}`
)
}
expect(latest).to.deep.equal(prev)
await sleep(2000)
}
})
})
describe('eth_getBalance', () => {
it('should get the OVM_ETH balance', async () => {
const rpcBalance = await provider.getBalance(env.l2Wallet.address)
const contractBalance = await env.ovmEth.balanceOf(env.l2Wallet.address)
expect(rpcBalance).to.be.deep.eq(contractBalance)
})
})
describe('eth_chainId', () => {
it('should get the correct chainid', async () => {
const { chainId } = await provider.getNetwork()
expect(chainId).to.be.eq(L2_CHAINID)
})
})
describe('eth_estimateGas', () => {
it('gas estimation is deterministic', async () => {
let lastEstimate: BigNumber
for (let i = 0; i < 10; i++) {
const estimate = await l2Provider.estimateGas({
to: DEFAULT_TRANSACTION.to,
value: 0,
})
if (i > 0) {
expect(lastEstimate).to.be.eq(estimate)
}
lastEstimate = estimate
}
})
it('should return a gas estimate for txs with empty data', async () => {
const estimate = await l2Provider.estimateGas({
to: DEFAULT_TRANSACTION.to,
value: 0,
})
// Expect gas to be less than or equal to the target plus 1%
expectApprox(estimate, 21000, { upperPercentDeviation: 1 })
})
it('should fail for a reverting call transaction', async () => {
await expect(provider.send('eth_estimateGas', [revertingTx])).to.be
.reverted
})
it('should fail for a reverting deploy transaction', async () => {
await expect(provider.send('eth_estimateGas', [revertingDeployTx])).to.be
.reverted
})
})
// SKIP: ?
describe.skip('rollup_gasPrices', () => {
it('should return the L1 and L2 gas prices', async () => {
const result = await provider.send('rollup_gasPrices', [])
const l1GasPrice = await env.l1Wallet.provider.getGasPrice()
const l2GasPrice = await env.gasPriceOracle.gasPrice()
expect(BigNumber.from(result.l1GasPrice)).to.deep.eq(l1GasPrice)
expect(BigNumber.from(result.l2GasPrice)).to.deep.eq(l2GasPrice)
})
})
})