-
Notifications
You must be signed in to change notification settings - Fork 674
/
manager.go
629 lines (532 loc) · 16.8 KB
/
manager.go
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package operations
import (
"context"
"fmt"
"math"
"math/big"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/0xPolygonHermez/zkevm-node/db"
"github.com/0xPolygonHermez/zkevm-node/event"
"github.com/0xPolygonHermez/zkevm-node/event/nileventstorage"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/merkletree"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
"github.com/0xPolygonHermez/zkevm-node/test/constants"
"github.com/0xPolygonHermez/zkevm-node/test/dbutils"
"github.com/0xPolygonHermez/zkevm-node/test/testutils"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
const (
cmdFolder = "test"
)
// Public shared
const (
DefaultSequencerAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
DefaultSequencerPrivateKey = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
DefaultSequencerBalance = 400000
DefaultMaxCumulativeGasUsed = 800000
DefaultL1ZkEVMSmartContract = "0x610178dA211FEF7D417bC0e6FeD39F05609AD788"
DefaultL1NetworkURL = "http://localhost:8545"
DefaultL1NetworkWebSocketURL = "ws://localhost:8546"
DefaultL1ChainID uint64 = 1337
DefaultL2NetworkURL = "http://localhost:8123"
PermissionlessL2NetworkURL = "http://localhost:8125"
DefaultL2NetworkWebSocketURL = "ws://localhost:8133"
DefaultL2ChainID uint64 = 1001
DefaultTimeoutTxToBeMined = 1 * time.Minute
DefaultWaitPeriodSendSequence = "15s"
DefaultLastBatchVirtualizationTimeMaxWaitPeriod = "10s"
DefaultMaxTxSizeForL1 uint64 = 131072
)
var (
stateDBCfg = dbutils.NewStateConfigFromEnv()
poolDBCfg = dbutils.NewPoolConfigFromEnv()
executorURI = testutils.GetEnv(constants.ENV_ZKPROVER_URI, "127.0.0.1:50071")
merkleTreeURI = testutils.GetEnv(constants.ENV_MERKLETREE_URI, "127.0.0.1:50061")
executorConfig = executor.Config{URI: executorURI, MaxGRPCMessageSize: 100000000}
merkleTreeConfig = merkletree.Config{URI: merkleTreeURI}
)
// SequenceSenderConfig is the configuration for the sequence sender operations
type SequenceSenderConfig struct {
WaitPeriodSendSequence string
LastBatchVirtualizationTimeMaxWaitPeriod string
MaxTxSizeForL1 uint64
SenderAddress string
PrivateKey string
}
// Config is the main Manager configuration.
type Config struct {
State *state.Config
SequenceSender *SequenceSenderConfig
}
// Manager controls operations and has knowledge about how to set up and tear
// down a functional environment.
type Manager struct {
cfg *Config
ctx context.Context
st *state.State
wait *Wait
}
// NewManager returns a manager ready to be used and a potential error caused
// during its creation (which can come from the setup of the db connection).
func NewManager(ctx context.Context, cfg *Config) (*Manager, error) {
// Init database instance
initOrResetDB()
opsman := &Manager{
cfg: cfg,
ctx: ctx,
wait: NewWait(),
}
st, err := initState(cfg.State.MaxCumulativeGasUsed)
if err != nil {
return nil, err
}
opsman.st = st
return opsman, nil
}
// State is a getter for the st field.
func (m *Manager) State() *state.State {
return m.st
}
// CheckVirtualRoot verifies if the given root is the current root of the
// merkletree for virtual state.
func (m *Manager) CheckVirtualRoot(expectedRoot string) error {
panic("not implemented yet")
// root, err := m.st.Getroot(m.ctx, true, "")
// if err != nil {
// return err
// }
// return m.checkRoot(root, expectedRoot)
}
// CheckConsolidatedRoot verifies if the given root is the current root of the
// merkletree for consolidated state.
func (m *Manager) CheckConsolidatedRoot(expectedRoot string) error {
panic("not implemented yet")
// root, err := m.st.GetStateRoot(m.ctx, false, "")
// if err != nil {
// return err
// }
// return m.checkRoot(root, expectedRoot)
}
// SetGenesisAccountsBalance creates the genesis block in the state.
func (m *Manager) SetGenesisAccountsBalance(genesisAccounts map[string]big.Int) error {
var genesisActions []*state.GenesisAction
for address, balanceValue := range genesisAccounts {
action := &state.GenesisAction{
Address: address,
Type: int(merkletree.LeafTypeBalance),
Value: balanceValue.String(),
}
genesisActions = append(genesisActions, action)
}
return m.SetGenesis(genesisActions)
}
func (m *Manager) SetGenesis(genesisActions []*state.GenesisAction) error {
genesisBlock := state.Block{
BlockNumber: 102,
BlockHash: state.ZeroHash,
ParentHash: state.ZeroHash,
ReceivedAt: time.Now(),
}
genesis := state.Genesis{
GenesisActions: genesisActions,
}
dbTx, err := m.st.BeginStateTransaction(m.ctx)
if err != nil {
return err
}
_, err = m.st.SetGenesis(m.ctx, genesisBlock, genesis, dbTx)
errCommit := dbTx.Commit(m.ctx)
if errCommit != nil {
return errCommit
}
return err
}
// SetForkID sets the initial forkID in db for testing purposes
func (m *Manager) SetForkID(forkID uint64) error {
dbTx, err := m.st.BeginStateTransaction(m.ctx)
if err != nil {
return err
}
// Add initial forkID
fID := state.ForkIDInterval{
FromBatchNumber: 1,
ToBatchNumber: math.MaxUint64,
ForkId: forkID,
Version: "forkID",
BlockNumber: 102,
}
err = m.st.AddForkIDInterval(m.ctx, fID, dbTx)
errCommit := dbTx.Commit(m.ctx)
if errCommit != nil {
return errCommit
}
return err
}
// ApplyL1Txs sends the given L1 txs, waits for them to be consolidated and
// checks the final state.
func ApplyL1Txs(ctx context.Context, txs []*types.Transaction, auth *bind.TransactOpts, client *ethclient.Client) error {
_, err := applyTxs(ctx, txs, auth, client, true)
return err
}
// ConfirmationLevel type used to describe the confirmation level of a transaction
type ConfirmationLevel int
// PoolConfirmationLevel indicates that transaction is added into the pool
const PoolConfirmationLevel ConfirmationLevel = 0
// TrustedConfirmationLevel indicates that transaction is added into the trusted state
const TrustedConfirmationLevel ConfirmationLevel = 1
// VirtualConfirmationLevel indicates that transaction is added into the virtual state
const VirtualConfirmationLevel ConfirmationLevel = 2
// VerifiedConfirmationLevel indicates that transaction is added into the verified state
const VerifiedConfirmationLevel ConfirmationLevel = 3
// ApplyL2Txs sends the given L2 txs, waits for them to be consolidated and
// checks the final state.
func ApplyL2Txs(ctx context.Context, txs []*types.Transaction, auth *bind.TransactOpts, client *ethclient.Client, confirmationLevel ConfirmationLevel) ([]*big.Int, error) {
var err error
if auth == nil {
auth, err = GetAuth(DefaultSequencerPrivateKey, DefaultL2ChainID)
if err != nil {
return nil, err
}
}
if client == nil {
client, err = ethclient.Dial(DefaultL2NetworkURL)
if err != nil {
return nil, err
}
}
waitToBeMined := confirmationLevel != PoolConfirmationLevel
sentTxs, err := applyTxs(ctx, txs, auth, client, waitToBeMined)
if err != nil {
return nil, err
}
if confirmationLevel == PoolConfirmationLevel {
return nil, nil
}
l2BlockNumbers := make([]*big.Int, 0, len(sentTxs))
for _, tx := range sentTxs {
// check transaction nonce against transaction reported L2 block number
receipt, err := client.TransactionReceipt(ctx, tx.Hash())
if err != nil {
return nil, err
}
// get L2 block number
l2BlockNumbers = append(l2BlockNumbers, receipt.BlockNumber)
expectedNonce := receipt.BlockNumber.Uint64() - 1 + 8 //nolint:gomnd
if tx.Nonce() != expectedNonce {
return nil, fmt.Errorf("mismatching nonce for tx %v: want %d, got %d\n", tx.Hash(), expectedNonce, tx.Nonce())
}
if confirmationLevel == TrustedConfirmationLevel {
continue
}
// wait for l2 block to be virtualized
log.Infof("waiting for the block number %v to be virtualized", receipt.BlockNumber.String())
err = WaitL2BlockToBeVirtualized(receipt.BlockNumber, 4*time.Minute) //nolint:gomnd
if err != nil {
return nil, err
}
if confirmationLevel == VirtualConfirmationLevel {
continue
}
// wait for l2 block number to be consolidated
log.Infof("waiting for the block number %v to be consolidated", receipt.BlockNumber.String())
err = WaitL2BlockToBeConsolidated(receipt.BlockNumber, 4*time.Minute) //nolint:gomnd
if err != nil {
return nil, err
}
}
return l2BlockNumbers, nil
}
func applyTxs(ctx context.Context, txs []*types.Transaction, auth *bind.TransactOpts, client *ethclient.Client, waitToBeMined bool) ([]*types.Transaction, error) {
var sentTxs []*types.Transaction
for i := 0; i < len(txs); i++ {
signedTx, err := auth.Signer(auth.From, txs[i])
if err != nil {
return nil, err
}
log.Infof("Sending Tx %v Nonce %v", signedTx.Hash(), signedTx.Nonce())
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return nil, err
}
sentTxs = append(sentTxs, signedTx)
}
if !waitToBeMined {
return nil, nil
}
// wait for TX to be mined
timeout := 180 * time.Second //nolint:gomnd
for _, tx := range sentTxs {
log.Infof("Waiting Tx %s to be mined", tx.Hash())
err := WaitTxToBeMined(ctx, client, tx, timeout)
if err != nil {
return nil, err
}
log.Infof("Tx %s mined successfully", tx.Hash())
}
nTxs := len(txs)
if nTxs > 1 {
log.Infof("%d transactions added into the trusted state successfully.", nTxs)
} else {
log.Info("transaction added into the trusted state successfully.")
}
return sentTxs, nil
}
// GetAuth configures and returns an auth object.
func GetAuth(privateKeyStr string, chainID uint64) (*bind.TransactOpts, error) {
privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(privateKeyStr, "0x"))
if err != nil {
return nil, err
}
return bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(0).SetUint64(chainID))
}
// MustGetAuth GetAuth but panics if err
func MustGetAuth(privateKeyStr string, chainID uint64) *bind.TransactOpts {
auth, err := GetAuth(privateKeyStr, chainID)
if err != nil {
panic(err)
}
return auth
}
// Setup creates all the required components and initializes them according to
// the manager config.
func (m *Manager) Setup() error {
// Run network container
err := m.StartNetwork()
if err != nil {
return err
}
// Approve matic
err = ApproveMatic()
if err != nil {
return err
}
// Run node container
err = m.StartNode()
if err != nil {
return err
}
return nil
}
// SetupWithPermissionless creates all the required components for both trusted and permissionless nodes
// and initializes them according to the manager config.
func (m *Manager) SetupWithPermissionless() error {
// Run network container
err := m.StartNetwork()
if err != nil {
return err
}
// Approve matic
err = ApproveMatic()
if err != nil {
return err
}
err = m.StartTrustedAndPermissionlessNode()
if err != nil {
return err
}
// Run node container
return nil
}
// StartEthTxSender stops the eth tx sender service
func (m *Manager) StartEthTxSender() error {
return StartComponent("eth-tx-manager")
}
// StopEthTxSender stops the eth tx sender service
func (m *Manager) StopEthTxSender() error {
return StopComponent("eth-tx-manager")
}
// StartSequencer starts the sequencer
func (m *Manager) StartSequencer() error {
return StartComponent("seq")
}
// StopSequencer stops the sequencer
func (m *Manager) StopSequencer() error {
return StopComponent("seq")
}
// StartSequenceSender starts the sequence sender
func (m *Manager) StartSequenceSender() error {
return StartComponent("seqsender")
}
// StopSequenceSender stops the sequence sender
func (m *Manager) StopSequenceSender() error {
return StopComponent("seqsender")
}
// Teardown stops all the components.
func Teardown() error {
err := stopNode()
if err != nil {
return err
}
err = stopNetwork()
if err != nil {
return err
}
return nil
}
// TeardownPermissionless stops all the components.
func TeardownPermissionless() error {
err := stopPermissionlessNode()
if err != nil {
return err
}
err = stopNetwork()
if err != nil {
return err
}
return nil
}
func initState(maxCumulativeGasUsed uint64) (*state.State, error) {
sqlDB, err := db.NewSQLDB(stateDBCfg)
if err != nil {
return nil, err
}
ctx := context.Background()
stateDb := state.NewPostgresStorage(sqlDB)
executorClient, _, _ := executor.NewExecutorClient(ctx, executorConfig)
stateDBClient, _, _ := merkletree.NewMTDBServiceClient(ctx, merkleTreeConfig)
stateTree := merkletree.NewStateTree(stateDBClient)
stateCfg := state.Config{
MaxCumulativeGasUsed: maxCumulativeGasUsed,
}
eventStorage, err := nileventstorage.NewNilEventStorage()
if err != nil {
return nil, err
}
eventLog := event.NewEventLog(event.Config{}, eventStorage)
st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog)
return st, nil
}
// StartNetwork starts the L1 network container
func (m *Manager) StartNetwork() error {
return StartComponent("network", networkUpCondition)
}
// InitNetwork Initializes the L2 network registering the sequencer and adding funds via the bridge
func (m *Manager) InitNetwork() error {
if err := RunMakeTarget("init-network"); err != nil {
return err
}
// Wait network to be ready
return Poll(DefaultInterval, DefaultDeadline, networkUpCondition)
}
// DeployUniswap deploys a uniswap environment and perform swaps
func (m *Manager) DeployUniswap() error {
if err := RunMakeTarget("deploy-uniswap"); err != nil {
return err
}
// Wait network to be ready
return Poll(DefaultInterval, DefaultDeadline, networkUpCondition)
}
func stopNetwork() error {
return StopComponent("network")
}
// StartNode starts the node container
func (m *Manager) StartNode() error {
return StartComponent("node", nodeUpCondition)
}
// StartTrustedAndPermissionlessNode starts the node container
func (m *Manager) StartTrustedAndPermissionlessNode() error {
return StartComponent("permissionless", nodeUpCondition)
}
// ApproveMatic runs the approving matic command
func ApproveMatic() error {
return StartComponent("approve-matic")
}
func stopNode() error {
return StopComponent("node")
}
func stopPermissionlessNode() error {
return StopComponent("permissionless")
}
func runCmd(c *exec.Cmd) error {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get current work directory: %v", err)
}
if strings.Contains(dir, cmdFolder) {
// Making the change dir to work in any nesting directory level inside cmd folder
base := filepath.Base(dir)
for base != cmdFolder {
dir = filepath.Dir(dir)
base = filepath.Base(dir)
}
} else {
dir = fmt.Sprintf("../../%s", cmdFolder)
}
c.Dir = dir
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
// StartComponent starts a docker-compose component.
func StartComponent(component string, conditions ...ConditionFunc) error {
cmdDown := fmt.Sprintf("stop-%s", component)
if err := RunMakeTarget(cmdDown); err != nil {
return err
}
cmdUp := fmt.Sprintf("run-%s", component)
if err := RunMakeTarget(cmdUp); err != nil {
return err
}
// Wait component to be ready
for _, condition := range conditions {
if err := Poll(DefaultInterval, DefaultDeadline, condition); err != nil {
return err
}
}
return nil
}
// StopComponent stops a docker-compose component.
func StopComponent(component string) error {
cmdDown := fmt.Sprintf("stop-%s", component)
return RunMakeTarget(cmdDown)
}
// RunMakeTarget runs a Makefile target.
func RunMakeTarget(target string) error {
cmd := exec.Command("make", target)
return runCmd(cmd)
}
// GetDefaultOperationsConfig provides a default configuration to run the environment
func GetDefaultOperationsConfig() *Config {
return &Config{
State: &state.Config{MaxCumulativeGasUsed: DefaultMaxCumulativeGasUsed},
SequenceSender: &SequenceSenderConfig{
WaitPeriodSendSequence: DefaultWaitPeriodSendSequence,
LastBatchVirtualizationTimeMaxWaitPeriod: DefaultWaitPeriodSendSequence,
MaxTxSizeForL1: DefaultMaxTxSizeForL1,
SenderAddress: DefaultSequencerAddress,
PrivateKey: DefaultSequencerPrivateKey},
}
}
// GetClient returns an ethereum client to the provided URL
func GetClient(URL string) (*ethclient.Client, error) {
client, err := ethclient.Dial(URL)
if err != nil {
return nil, err
}
return client, nil
}
// MustGetClient GetClient but panic if err
func MustGetClient(URL string) *ethclient.Client {
client, err := GetClient(URL)
if err != nil {
panic(err)
}
return client
}
func initOrResetDB() {
if err := dbutils.InitOrResetState(stateDBCfg); err != nil {
panic(err)
}
if err := dbutils.InitOrResetPool(poolDBCfg); err != nil {
panic(err)
}
}