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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package rollup
import (
"context"
"errors"
"fmt"
"math/big"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/gasprice"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
)
func setupLatestEthContextTest() (*SyncService, *EthContext) {
service, _, _, _ := newTestSyncService(false)
resp := &EthContext{
BlockNumber: uint64(10),
BlockHash: common.Hash{},
Timestamp: uint64(service.timestampRefreshThreshold.Seconds()) + 1,
}
setupMockClient(service, map[string]interface{}{
"GetLatestEthContext": resp,
})
return service, resp
}
// Test that if applying a transaction fails
func TestSyncServiceContextUpdated(t *testing.T) {
service, resp := setupLatestEthContextTest()
// should get the expected context
expectedCtx := &OVMContext{
blockNumber: 0,
timestamp: 0,
}
if service.OVMContext != *expectedCtx {
t.Fatal("context was not instantiated to the expected value")
}
// run the update context call once
err := service.updateContext()
if err != nil {
t.Fatal(err)
}
// should get the expected context
expectedCtx = &OVMContext{
blockNumber: resp.BlockNumber,
timestamp: resp.Timestamp,
}
if service.OVMContext != *expectedCtx {
t.Fatal("context was not updated to the expected response even though enough time passed")
}
// updating the context should be a no-op if time advanced by less than
// the refresh period
resp.BlockNumber += 1
resp.Timestamp += uint64(service.timestampRefreshThreshold.Seconds())
setupMockClient(service, map[string]interface{}{
"GetLatestEthContext": resp,
})
// call it again
err = service.updateContext()
if err != nil {
t.Fatal(err)
}
// should not get the context from the response because it was too soon
unexpectedCtx := &OVMContext{
blockNumber: resp.BlockNumber,
timestamp: resp.Timestamp,
}
if service.OVMContext == *unexpectedCtx {
t.Fatal("context should not be updated because not enough time passed")
}
}
// Test that the `RollupTransaction` ends up in the transaction cache
// after the transaction enqueued event is emitted. Set `false` as
// the argument to start as a sequencer
func TestSyncServiceTransactionEnqueued(t *testing.T) {
service, txCh, _, err := newTestSyncService(false)
if err != nil {
t.Fatal(err)
}
// The timestamp is in the rollup transaction
timestamp := uint64(24)
// The target is the `to` field on the transaction
target := common.HexToAddress("0x04668ec2f57cc15c381b461b9fedab5d451c8f7f")
// The layer one transaction origin is in the txmeta on the transaction
l1TxOrigin := common.HexToAddress("0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8")
// The gasLimit is the `gasLimit` on the transaction
gasLimit := uint64(66)
// The data is the `data` on the transaction
data := []byte{0x02, 0x92}
// The L1 blocknumber for the transaction's evm context
l1BlockNumber := big.NewInt(100)
// The queue index of the L1 to L2 transaction
queueIndex := uint64(0)
// The index in the ctc
index := uint64(5)
tx := types.NewTransaction(0, target, big.NewInt(0), gasLimit, big.NewInt(0), data)
txMeta := types.NewTransactionMeta(
l1BlockNumber,
timestamp,
&l1TxOrigin,
types.SighashEIP155,
types.QueueOriginL1ToL2,
&index,
&queueIndex,
nil,
)
tx.SetTransactionMeta(txMeta)
setupMockClient(service, map[string]interface{}{
"GetEnqueue": []*types.Transaction{
tx,
},
})
// Run an iteration of the eloop
err = service.sequence()
if err != nil {
t.Fatal("sequencing failed", err)
}
// Wait for the tx to be confirmed into the chain and then
// make sure it is the transactions that was set up with in the mockclient
event := <-txCh
if len(event.Txs) != 1 {
t.Fatal("Unexpected number of transactions")
}
confirmed := event.Txs[0]
if !reflect.DeepEqual(tx, confirmed) {
t.Fatal("different txs")
}
}
func TestSyncServiceL1GasPrice(t *testing.T) {
service, _, _, err := newTestSyncService(true)
setupMockClient(service, map[string]interface{}{})
service.RollupGpo = gasprice.NewRollupOracle(big.NewInt(0), big.NewInt(0))
if err != nil {
t.Fatal(err)
}
gasBefore, err := service.RollupGpo.SuggestDataPrice(context.Background())
if err != nil {
t.Fatal(err)
}
if gasBefore.Cmp(big.NewInt(0)) != 0 {
t.Fatal("expected 0 gas price, got", gasBefore)
}
// Update the gas price
service.updateL1GasPrice()
gasAfter, err := service.RollupGpo.SuggestDataPrice(context.Background())
if err != nil {
t.Fatal(err)
}
if gasAfter.Cmp(big.NewInt(100*int64(params.GWei))) != 0 {
t.Fatal("expected 100 gas price, got", gasAfter)
}
}
// Pass true to set as a verifier
func TestSyncServiceSync(t *testing.T) {
service, txCh, sub, err := newTestSyncService(true)
defer sub.Unsubscribe()
if err != nil {
t.Fatal(err)
}
timestamp := uint64(24)
target := common.HexToAddress("0x04668ec2f57cc15c381b461b9fedab5d451c8f7f")
l1TxOrigin := common.HexToAddress("0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8")
gasLimit := uint64(66)
data := []byte{0x02, 0x92}
l1BlockNumber := big.NewInt(100)
queueIndex := uint64(0)
index := uint64(0)
tx := types.NewTransaction(0, target, big.NewInt(0), gasLimit, big.NewInt(0), data)
txMeta := types.NewTransactionMeta(
l1BlockNumber,
timestamp,
&l1TxOrigin,
types.SighashEIP155,
types.QueueOriginL1ToL2,
&index,
&queueIndex,
nil,
)
tx.SetTransactionMeta(txMeta)
setupMockClient(service, map[string]interface{}{
"GetTransaction": []*types.Transaction{
tx,
},
})
err = service.verify()
if err != nil {
t.Fatal("verification failed", err)
}
event := <-txCh
if len(event.Txs) != 1 {
t.Fatal("Unexpected number of transactions")
}
confirmed := event.Txs[0]
if !reflect.DeepEqual(tx, confirmed) {
t.Fatal("different txs")
}
}
func TestInitializeL1ContextPostGenesis(t *testing.T) {
service, _, _, err := newTestSyncService(true)
if err != nil {
t.Fatal(err)
}
timestamp := uint64(24)
target := common.HexToAddress("0x04668ec2f57cc15c381b461b9fedab5d451c8f7f")
l1TxOrigin := common.HexToAddress("0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8")
gasLimit := uint64(66)
data := []byte{0x02, 0x92}
l1BlockNumber := big.NewInt(100)
queueIndex := uint64(100)
index := uint64(120)
tx := types.NewTransaction(0, target, big.NewInt(0), gasLimit, big.NewInt(0), data)
txMeta := types.NewTransactionMeta(
l1BlockNumber,
timestamp,
&l1TxOrigin,
types.SighashEIP155,
types.QueueOriginL1ToL2,
&index,
&queueIndex,
nil,
)
tx.SetTransactionMeta(txMeta)
setupMockClient(service, map[string]interface{}{
"GetEnqueue": []*types.Transaction{
tx,
},
"GetEthContext": []*EthContext{
{
BlockNumber: uint64(10),
BlockHash: common.Hash{},
Timestamp: timestamp,
},
},
})
header := types.Header{
Number: big.NewInt(0),
Time: 11,
}
number := uint64(10)
tx.SetL1Timestamp(timestamp)
tx.SetL1BlockNumber(number)
block := types.NewBlock(&header, []*types.Transaction{tx}, []*types.Header{}, []*types.Receipt{})
service.bc.SetCurrentBlock(block)
err = service.initializeLatestL1(big.NewInt(0))
if err != nil {
t.Fatal(err)
}
latestL1Timestamp := service.GetLatestL1Timestamp()
latestL1BlockNumber := service.GetLatestL1BlockNumber()
if number != latestL1BlockNumber {
t.Fatalf("number does not match, got %d, expected %d", latestL1BlockNumber, number)
}
if latestL1Timestamp != timestamp {
t.Fatal("timestamp does not match")
}
}
func newTestSyncService(isVerifier bool) (*SyncService, chan core.NewTxsEvent, event.Subscription, error) {
chainCfg := params.AllEthashProtocolChanges
chainID := big.NewInt(420)
chainCfg.ChainID = chainID
engine := ethash.NewFaker()
db := rawdb.NewMemoryDatabase()
_ = new(core.Genesis).MustCommit(db)
chain, err := core.NewBlockChain(db, nil, chainCfg, engine, vm.Config{}, nil)
if err != nil {
return nil, nil, nil, fmt.Errorf("Cannot initialize blockchain: %w", err)
}
chaincfg := params.ChainConfig{ChainID: chainID}
txPool := core.NewTxPool(core.TxPoolConfig{PriceLimit: 0}, &chaincfg, chain)
cfg := Config{
CanonicalTransactionChainDeployHeight: big.NewInt(0),
IsVerifier: isVerifier,
// Set as an empty string as this is a dummy value anyways.
// The client needs to be mocked with a mockClient
RollupClientHttp: "",
}
service, err := NewSyncService(context.Background(), cfg, txPool, chain, db)
if err != nil {
return nil, nil, nil, fmt.Errorf("Cannot initialize syncservice: %w", err)
}
txCh := make(chan core.NewTxsEvent, 1)
sub := service.SubscribeNewTxsEvent(txCh)
return service, txCh, sub, nil
}
type mockClient struct {
getEnqueueCallCount int
getEnqueue []*types.Transaction
getTransactionCallCount int
getTransaction []*types.Transaction
getEthContextCallCount int
getEthContext []*EthContext
getLatestEthContext *EthContext
}
func setupMockClient(service *SyncService, responses map[string]interface{}) {
client := newMockClient(responses)
service.client = client
service.RollupGpo = gasprice.NewRollupOracle(big.NewInt(0), big.NewInt(0))
}
func newMockClient(responses map[string]interface{}) *mockClient {
getEnqueueResponses := []*types.Transaction{}
getTransactionResponses := []*types.Transaction{}
getEthContextResponses := []*EthContext{}
getLatestEthContextResponse := &EthContext{}
enqueue, ok := responses["GetEnqueue"]
if ok {
getEnqueueResponses = enqueue.([]*types.Transaction)
}
getTx, ok := responses["GetTransaction"]
if ok {
getTransactionResponses = getTx.([]*types.Transaction)
}
getCtx, ok := responses["GetEthContext"]
if ok {
getEthContextResponses = getCtx.([]*EthContext)
}
getLatestCtx, ok := responses["GetLatestEthContext"]
if ok {
getLatestEthContextResponse = getLatestCtx.(*EthContext)
}
return &mockClient{
getEnqueue: getEnqueueResponses,
getTransaction: getTransactionResponses,
getEthContext: getEthContextResponses,
getLatestEthContext: getLatestEthContextResponse,
}
}
func (m *mockClient) GetEnqueue(index uint64) (*types.Transaction, error) {
if m.getEnqueueCallCount < len(m.getEnqueue) {
tx := m.getEnqueue[m.getEnqueueCallCount]
m.getEnqueueCallCount++
return tx, nil
}
return nil, errors.New("")
}
func (m *mockClient) GetLatestEnqueue() (*types.Transaction, error) {
if len(m.getEnqueue) == 0 {
return &types.Transaction{}, errors.New("")
}
return m.getEnqueue[len(m.getEnqueue)-1], nil
}
func (m *mockClient) GetTransaction(index uint64) (*types.Transaction, error) {
if m.getTransactionCallCount < len(m.getTransaction) {
tx := m.getTransaction[m.getTransactionCallCount]
m.getTransactionCallCount++
return tx, nil
}
return nil, errors.New("")
}
func (m *mockClient) GetLatestTransaction() (*types.Transaction, error) {
if len(m.getTransaction) == 0 {
return nil, errors.New("")
}
return m.getTransaction[len(m.getTransaction)-1], nil
}
func (m *mockClient) GetEthContext(index uint64) (*EthContext, error) {
if m.getEthContextCallCount < len(m.getEthContext) {
ctx := m.getEthContext[m.getEthContextCallCount]
m.getEthContextCallCount++
return ctx, nil
}
return nil, errors.New("")
}
func (m *mockClient) GetLatestEthContext() (*EthContext, error) {
return m.getLatestEthContext, nil
}
func (m *mockClient) GetLastConfirmedEnqueue() (*types.Transaction, error) {
return nil, nil
}
func (m *mockClient) GetLatestTransactionBatch() (*Batch, []*types.Transaction, error) {
return nil, nil, nil
}
func (m *mockClient) GetTransactionBatch(index uint64) (*Batch, []*types.Transaction, error) {
return nil, nil, nil
}
func (m *mockClient) SyncStatus() (*SyncStatus, error) {
return &SyncStatus{
Syncing: false,
}, nil
}
func (m *mockClient) GetL1GasPrice() (*big.Int, error) {
return big.NewInt(100 * int64(params.GWei)), nil
}