/
setup.go
150 lines (125 loc) · 4.29 KB
/
setup.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
package setup
import (
"context"
"fmt"
"math/big"
"testing"
"time"
"github.com/0xPolygonHermez/zkevm-node/config/types"
"github.com/0xPolygonHermez/zkevm-node/event"
"github.com/0xPolygonHermez/zkevm-node/event/nileventstorage"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/pool/pgpoolstorage"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)
const (
sleepDuration = 5 * time.Second
minAllowedGasPriceIntervalMinutes = 5
pollMinAllowedGasPriceIntervalSeconds = 15
defaultGasPrice = 1000000000
)
var (
bc = state.BatchConstraintsCfg{
MaxTxsPerBatch: 300,
MaxBatchBytesSize: 120000,
MaxCumulativeGasUsed: uint64(1125899906842624),
MaxKeccakHashes: 2145,
MaxPoseidonHashes: 252357,
MaxPoseidonPaddings: 135191,
MaxMemAligns: 236585,
MaxArithmetics: 236585,
MaxBinaries: 473170,
MaxSteps: 7570538,
MaxSHA256Hashes: 1596,
}
)
// Environment sets up the environment for the benchmark
func Environment(ctx context.Context, b *testing.B) (*operations.Manager, *ethclient.Client, *pool.Pool, *bind.TransactOpts) {
if testing.Short() {
b.Skip()
}
err := operations.Teardown()
require.NoError(b, err)
params.OpsCfg.State.MaxCumulativeGasUsed = params.MaxCumulativeGasUsed
opsman, err := operations.NewManager(ctx, params.OpsCfg)
require.NoError(b, err)
err = Components(opsman)
require.NoError(b, err)
time.Sleep(sleepDuration)
// Load account with balance on local genesis
auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL2ChainID)
require.NoError(b, err)
// Load params client
client, err := ethclient.Dial(operations.DefaultL2NetworkURL)
require.NoError(b, err)
st := opsman.State()
s, err := pgpoolstorage.NewPostgresPoolStorage(params.PoolDbConfig)
require.NoError(b, err)
config := pool.Config{
DB: params.PoolDbConfig,
MinAllowedGasPriceInterval: types.NewDuration(minAllowedGasPriceIntervalMinutes * time.Minute),
PollMinAllowedGasPriceInterval: types.NewDuration(pollMinAllowedGasPriceIntervalSeconds * time.Second),
}
eventStorage, err := nileventstorage.NewNilEventStorage()
require.NoError(b, err)
eventLog := event.NewEventLog(event.Config{}, eventStorage)
pl := pool.NewPool(config, bc, s, st, params.ChainID, eventLog)
// Print Info before send
senderBalance, err := client.BalanceAt(ctx, auth.From, nil)
require.NoError(b, err)
senderNonce, err := client.PendingNonceAt(ctx, auth.From)
require.NoError(b, err)
// Print Initial Stats
fmt.Printf("Receiver Addr: %v\n", params.To.String())
fmt.Printf("Sender Addr: %v\n", auth.From.String())
fmt.Printf("Sender Balance: %v\n", senderBalance.String())
fmt.Printf("Sender Nonce: %v\n", senderNonce)
gasPrice, err := client.SuggestGasPrice(ctx)
require.NoError(b, err)
if gasPrice == nil || gasPrice.Int64() == 0 {
gasPrice = big.NewInt(defaultGasPrice)
}
// PrivateKey is the private key of the sender
// Auth is the auth of the sender
auth, err = bind.NewKeyedTransactorWithChainID(params.PrivateKey, new(big.Int).SetUint64(params.ChainID))
if err != nil {
panic(err)
}
auth.GasPrice = gasPrice
return opsman, client, pl, auth
}
// Components runs the network container, starts synchronizer and JSON-RPC components, and approves pol
func Components(opsman *operations.Manager) error {
// Run network container
err := opsman.StartNetwork()
if err != nil {
return err
}
// Approve pol
err = operations.ApprovePol()
if err != nil {
return err
}
err = operations.StartComponent("sync")
if err != nil {
return err
}
err = operations.StartComponent("json-rpc")
if err != nil {
return err
}
time.Sleep(sleepDuration)
return nil
}
// BootstrapSequencer starts the sequencer and waits for it to be ready
func BootstrapSequencer(b *testing.B, opsman *operations.Manager) {
fmt.Println("Starting sequencer ....")
err := operations.StartComponent("seq")
require.NoError(b, err)
fmt.Println("Sequencer Started!")
}