/
loader.go
242 lines (215 loc) · 7.04 KB
/
loader.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
// Copyright (C) 2023, Lux Partners Limited. All rights reserved.
// See the file LICENSE for licensing terms.
package load
import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/luxdefi/evm/cmd/simulator/config"
"github.com/luxdefi/evm/cmd/simulator/key"
"github.com/luxdefi/evm/cmd/simulator/metrics"
"github.com/luxdefi/evm/cmd/simulator/txs"
"github.com/luxdefi/evm/core/types"
"github.com/luxdefi/evm/ethclient"
"github.com/luxdefi/evm/params"
"github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/sync/errgroup"
)
const (
MetricsEndpoint = "/metrics" // Endpoint for the Prometheus Metrics Server
)
// Loader executes a series of worker/tx sequence pairs.
// Each worker/txSequence pair issues [batchSize] transactions, confirms all
// of them as accepted, and then moves to the next batch until the txSequence
// is exhausted.
type Loader[T txs.THash] struct {
clients []txs.Worker[T]
txSequences []txs.TxSequence[T]
batchSize uint64
metrics *metrics.Metrics
}
func New[T txs.THash](
clients []txs.Worker[T],
txSequences []txs.TxSequence[T],
batchSize uint64,
metrics *metrics.Metrics,
) *Loader[T] {
return &Loader[T]{
clients: clients,
txSequences: txSequences,
batchSize: batchSize,
metrics: metrics,
}
}
func (l *Loader[T]) Execute(ctx context.Context) error {
log.Info("Constructing tx agents...", "numAgents", len(l.txSequences))
agents := make([]txs.Agent[T], 0, len(l.txSequences))
for i := 0; i < len(l.txSequences); i++ {
agents = append(agents, txs.NewIssueNAgent(l.txSequences[i], l.clients[i], l.batchSize, l.metrics))
}
log.Info("Starting tx agents...")
eg := errgroup.Group{}
for _, agent := range agents {
agent := agent
eg.Go(func() error {
return agent.Execute(ctx)
})
}
log.Info("Waiting for tx agents...")
if err := eg.Wait(); err != nil {
return err
}
log.Info("Tx agents completed successfully.")
return nil
}
// ConfirmReachedTip finds the max height any client has reached and then ensures every client
// reaches at least that height.
//
// This allows the network to continue to roll forward and creates a synchronization point to ensure
// that every client in the loader has reached at least the max height observed of any client at
// the time this function was called.
func (l *Loader[T]) ConfirmReachedTip(ctx context.Context) error {
maxHeight := uint64(0)
for i, client := range l.clients {
latestHeight, err := client.LatestHeight(ctx)
if err != nil {
return fmt.Errorf("client %d failed to get latest height: %w", i, err)
}
if latestHeight > maxHeight {
maxHeight = latestHeight
}
}
eg := errgroup.Group{}
for i, client := range l.clients {
i := i
client := client
eg.Go(func() error {
for {
latestHeight, err := client.LatestHeight(ctx)
if err != nil {
return fmt.Errorf("failed to get latest height from client %d: %w", i, err)
}
if latestHeight >= maxHeight {
return nil
}
select {
case <-ctx.Done():
return fmt.Errorf("failed to get latest height from client %d: %w", i, ctx.Err())
case <-time.After(time.Second):
}
}
})
}
return eg.Wait()
}
// ExecuteLoader creates txSequences from [config] and has txAgents execute the specified simulation.
func ExecuteLoader(ctx context.Context, config config.Config) error {
if config.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, config.Timeout)
defer cancel()
}
// Create buffered sigChan to receive SIGINT notifications
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT)
// Create context with cancel
ctx, cancel := context.WithCancel(ctx)
go func() {
// Blocks until we receive a SIGINT notification or if parent context is done
select {
case <-sigChan:
case <-ctx.Done():
}
// Cancel the child context and end all processes
cancel()
}()
m := metrics.NewDefaultMetrics()
ms := m.Serve(ctx, strconv.Itoa(int(config.MetricsPort)), MetricsEndpoint)
defer ms.Shutdown()
// Construct the arguments for the load simulator
clients := make([]ethclient.Client, 0, len(config.Endpoints))
for i := 0; i < config.Workers; i++ {
clientURI := config.Endpoints[i%len(config.Endpoints)]
client, err := ethclient.Dial(clientURI)
if err != nil {
return fmt.Errorf("failed to dial client at %s: %w", clientURI, err)
}
clients = append(clients, client)
}
keys, err := key.LoadAll(ctx, config.KeyDir)
if err != nil {
return err
}
// Ensure there are at least [config.Workers] keys and save any newly generated ones.
if len(keys) < config.Workers {
for i := 0; len(keys) < config.Workers; i++ {
newKey, err := key.Generate()
if err != nil {
return fmt.Errorf("failed to generate %d new key: %w", i, err)
}
if err := newKey.Save(config.KeyDir); err != nil {
return fmt.Errorf("failed to save %d new key: %w", i, err)
}
keys = append(keys, newKey)
}
}
// Each address needs: params.GWei * MaxFeeCap * params.TxGas * TxsPerWorker total wei
// to fund gas for all of their transactions.
maxFeeCap := new(big.Int).Mul(big.NewInt(params.GWei), big.NewInt(config.MaxFeeCap))
minFundsPerAddr := new(big.Int).Mul(maxFeeCap, big.NewInt(int64(config.TxsPerWorker*params.TxGas)))
log.Info("Distributing funds", "numTxsPerWorker", config.TxsPerWorker, "minFunds", minFundsPerAddr)
keys, err = DistributeFunds(ctx, clients[0], keys, config.Workers, minFundsPerAddr, m)
if err != nil {
return err
}
log.Info("Distributed funds successfully")
pks := make([]*ecdsa.PrivateKey, 0, len(keys))
senders := make([]common.Address, 0, len(keys))
for _, key := range keys {
pks = append(pks, key.PrivKey)
senders = append(senders, key.Address)
}
bigGwei := big.NewInt(params.GWei)
gasTipCap := new(big.Int).Mul(bigGwei, big.NewInt(config.MaxTipCap))
gasFeeCap := new(big.Int).Mul(bigGwei, big.NewInt(config.MaxFeeCap))
client := clients[0]
chainID, err := client.ChainID(ctx)
if err != nil {
return fmt.Errorf("failed to fetch chainID: %w", err)
}
signer := types.LatestSignerForChainID(chainID)
log.Info("Creating transaction sequences...")
txGenerator := func(key *ecdsa.PrivateKey, nonce uint64) (*types.Transaction, error) {
addr := ethcrypto.PubkeyToAddress(key.PublicKey)
return types.SignNewTx(key, signer, &types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Gas: params.TxGas,
To: &addr,
Data: nil,
Value: common.Big0,
})
}
txSequences, err := txs.GenerateTxSequences(ctx, txGenerator, clients[0], pks, config.TxsPerWorker, false)
if err != nil {
return err
}
workers := make([]txs.Worker[*types.Transaction], 0, len(clients))
for i, client := range clients {
workers = append(workers, NewSingleAddressTxWorker(ctx, client, ethcrypto.PubkeyToAddress(pks[i].PublicKey)))
}
loader := New(workers, txSequences, config.BatchSize, m)
err = loader.Execute(ctx)
ms.Print() // Print regardless of execution error
return err
}