-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsimulation.go
336 lines (288 loc) · 11.1 KB
/
simulation.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
package simulation
import (
"context"
"errors"
"fmt"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
gethparams "github.com/ethereum/go-ethereum/params"
"github.com/ten-protocol/go-ten/contracts/generated/MessageBus"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/retry"
"github.com/ten-protocol/go-ten/go/ethadapter"
"github.com/ten-protocol/go-ten/go/wallet"
"github.com/ten-protocol/go-ten/integration/common/testlog"
"github.com/ten-protocol/go-ten/integration/erc20contract"
"github.com/ten-protocol/go-ten/integration/ethereummock"
"github.com/ten-protocol/go-ten/integration/simulation/network"
"github.com/ten-protocol/go-ten/integration/simulation/params"
"github.com/ten-protocol/go-ten/integration/simulation/stats"
gethcommon "github.com/ethereum/go-ethereum/common"
testcommon "github.com/ten-protocol/go-ten/integration/common"
)
var initialBalance = common.ValueInWei(big.NewInt(5000))
// Simulation represents all the data required to inject transactions on a network
type Simulation struct {
RPCHandles *network.RPCHandles
AvgBlockDuration uint64
TxInjector *TransactionInjector
SimulationTime time.Duration
Stats *stats.Stats
Params *params.SimParams
LogChannels map[string][]chan types.Log // Maps an owner to the channels on which they receive logs for each client.
Subscriptions []ethereum.Subscription // A slice of all created event subscriptions.
ctx context.Context
}
// Start executes the simulation given all the Params. Injects transactions.
func (s *Simulation) Start() {
testlog.Logger().Info(fmt.Sprintf("Genesis block: b_%d.", common.ShortHash(ethereummock.MockGenesisBlock.Hash())))
s.ctx = context.Background() // use injected context for graceful shutdowns
s.waitForTenGenesisOnL1()
// Arbitrary sleep to wait for RPC clients to get up and running
// and for all l2 nodes to receive the genesis l2 batch
// todo - instead of sleeping, it would be better to poll
time.Sleep(10 * time.Second)
s.bridgeFundingToTen()
s.trackLogs() // Create log subscriptions, to validate that they're working correctly later.
s.prefundTenAccounts() // Prefund every L2 wallet
s.deployTenERC20s() // Deploy the TEN HOC and POC ERC20 contracts
s.prefundL1Accounts() // Prefund every L1 wallet
s.checkHealthStatus() // Checks the nodes health status
timer := time.Now()
fmt.Printf("Starting injection\n")
testlog.Logger().Info("Starting injection")
go s.TxInjector.Start()
// Allow for some time after tx injection was stopped so that the network can process all transactions, catch up
// on missed batches, etc.
// Wait for the simulation time
time.Sleep(s.SimulationTime - s.Params.StoppingDelay)
fmt.Printf("Stopping injection\n")
testlog.Logger().Info("Stopping injection")
s.TxInjector.Stop()
time.Sleep(s.Params.StoppingDelay)
fmt.Printf("Ran simulation for %f secs, configured to run for: %s ... \n", time.Since(timer).Seconds(), s.SimulationTime)
testlog.Logger().Info(fmt.Sprintf("Ran simulation for %f secs, configured to run for: %s ... \n", time.Since(timer).Seconds(), s.SimulationTime))
}
func (s *Simulation) Stop() {
// nothing to do for now
}
func (s *Simulation) waitForTenGenesisOnL1() {
// grab an L1 client
client := s.RPCHandles.EthClients[0]
for {
// spin through the L1 blocks periodically to see if the genesis rollup has arrived
head, err := client.FetchHeadBlock()
if err != nil && !errors.Is(err, errutil.ErrNotFound) {
panic(fmt.Errorf("could not fetch head block. Cause: %w", err))
}
if err == nil {
for _, b := range client.BlocksBetween(ethereummock.MockGenesisBlock.Header(), head) {
for _, tx := range b.Transactions() {
t := s.Params.MgmtContractLib.DecodeTx(tx)
if t == nil {
continue
}
if _, ok := t.(*ethadapter.L1RollupTx); ok {
// exit at the first TEN rollup we see
return
}
}
}
}
time.Sleep(s.Params.AvgBlockDuration)
testlog.Logger().Trace("Waiting for the TEN genesis rollup...")
}
}
func (s *Simulation) bridgeFundingToTen() {
if s.Params.IsInMem {
return
}
destAddr := s.Params.L1TenData.MessageBusAddr
value, _ := big.NewInt(0).SetString("7400000000000000000000000000000", 10)
wallets := []wallet.Wallet{
s.Params.Wallets.PrefundedEthWallets.Faucet,
s.Params.Wallets.PrefundedEthWallets.HOC,
s.Params.Wallets.PrefundedEthWallets.POC,
}
receivers := []gethcommon.Address{
gethcommon.HexToAddress("0xA58C60cc047592DE97BF1E8d2f225Fc5D959De77"),
gethcommon.HexToAddress("0x987E0a0692475bCc5F13D97E700bb43c1913EFfe"),
gethcommon.HexToAddress("0xDEe530E22045939e6f6a0A593F829e35A140D3F1"),
}
busCtr, err := MessageBus.NewMessageBus(destAddr, s.RPCHandles.RndEthClient().EthClient())
if err != nil {
panic(err)
}
for idx, w := range wallets {
opts, err := bind.NewKeyedTransactorWithChainID(w.PrivateKey(), w.ChainID())
if err != nil {
panic(err)
}
opts.Value = value
_, err = busCtr.SendValueToL2(opts, receivers[idx], value)
if err != nil {
panic(err)
}
}
time.Sleep(15 * time.Second)
// todo - fix the wait group, for whatever reason it does not find a receipt...
/*wg := sync.WaitGroup{}
for _, tx := range transactions {
wg.Add(1)
transaction := tx
go func() {
defer wg.Done()
err := testcommon.AwaitReceiptEth(s.ctx, s.RPCHandles.RndEthClient(), transaction.Hash(), 20*time.Second)
if err != nil {
panic(err)
}
}()
}
wg.Wait()*/
}
// We subscribe to logs on every client for every wallet.
func (s *Simulation) trackLogs() {
// In-memory clients cannot handle subscriptions for now.
if s.Params.IsInMem {
return
}
for owner, clients := range s.RPCHandles.AuthObsClients {
// There is a subscription, and corresponding log channel, per owner per client.
s.LogChannels[owner] = []chan types.Log{}
for _, client := range clients {
channel := make(chan types.Log, 1000)
// To exercise the filtering mechanism, we subscribe for HOC events only, ignoring POC events.
hocFilter := common.FilterCriteria{
Addresses: []gethcommon.Address{gethcommon.HexToAddress("0x" + testcommon.HOCAddr)},
}
sub, err := client.SubscribeFilterLogs(context.Background(), hocFilter, channel)
if err != nil {
panic(fmt.Errorf("subscription failed. Cause: %w", err))
}
s.Subscriptions = append(s.Subscriptions, sub)
s.LogChannels[owner] = append(s.LogChannels[owner], channel)
}
}
}
// Prefunds the L2 wallets with `allocObsWallets` each.
func (s *Simulation) prefundTenAccounts() {
faucetWallet := s.Params.Wallets.L2FaucetWallet
faucetClient := s.RPCHandles.TenWalletClient(faucetWallet.Address(), 0) // get sequencer, else errors on submission get swallowed
nonce := NextNonce(s.ctx, s.RPCHandles, faucetWallet)
// Give 1000 ether per account - ether is 1e18 so best convert it by code
// as a lot of the hardcodes were giving way too little and choking the gas payments
allocObsWallets := big.NewInt(0).Mul(big.NewInt(1000), big.NewInt(gethparams.Ether))
testcommon.PrefundWallets(s.ctx, faucetWallet, faucetClient, nonce, s.Params.Wallets.AllObsWallets(), allocObsWallets, s.Params.ReceiptTimeout)
}
// This deploys an ERC20 contract on Ten, which is used for token arithmetic.
func (s *Simulation) deployTenERC20s() {
tokens := []testcommon.ERC20{testcommon.HOC, testcommon.POC}
wg := sync.WaitGroup{}
for _, token := range tokens {
wg.Add(1)
go func(token testcommon.ERC20) {
defer wg.Done()
owner := s.Params.Wallets.Tokens[token].L2Owner
// 0x526c84529b2b8c11f57d93d3f5537aca3aecef9b - this is the address of the L2 contract which is currently hardcoded.
contractBytes := erc20contract.L2BytecodeWithDefaultSupply(string(token), gethcommon.HexToAddress("0x526c84529b2b8c11f57d93d3f5537aca3aecef9b"))
fmt.Printf("Deploy contract from: %s\n", owner.Address().Hex())
deployContractTxData := types.DynamicFeeTx{
Nonce: NextNonce(s.ctx, s.RPCHandles, owner),
Gas: 5_000_000,
GasFeeCap: gethcommon.Big1, // This field is used to derive the gas price for dynamic fee transactions.
Data: contractBytes,
GasTipCap: gethcommon.Big1,
}
deployContractTx := s.RPCHandles.TenWalletRndClient(owner).EstimateGasAndGasPrice(&deployContractTxData)
signedTx, err := owner.SignTransaction(deployContractTx)
if err != nil {
panic(err)
}
rpc := s.RPCHandles.TenWalletClient(owner.Address(), 1)
err = rpc.SendTransaction(s.ctx, signedTx)
if err != nil {
panic(err)
}
err = testcommon.AwaitReceipt(s.ctx, rpc, signedTx.Hash(), s.Params.ReceiptTimeout)
if err != nil {
panic(fmt.Sprintf("ERC20 deployment transaction unsuccessful. Cause: %s", err))
}
}(token)
}
wg.Wait()
}
// Sends an amount from the faucet to each L1 account, to pay for transactions.
func (s *Simulation) prefundL1Accounts() {
for _, w := range s.Params.Wallets.SimEthWallets {
ethClient := s.RPCHandles.RndEthClient()
receiver := w.Address()
tokenOwner := s.Params.Wallets.Tokens[testcommon.HOC].L1Owner
ownerAddr := tokenOwner.Address()
txData := ðadapter.L1DepositTx{
Amount: initialBalance,
To: &receiver,
TokenContract: s.Params.Wallets.Tokens[testcommon.HOC].L1ContractAddress,
Sender: &ownerAddr,
}
tx := s.Params.ERC20ContractLib.CreateDepositTx(txData)
estimatedTx, err := ethClient.PrepareTransactionToSend(s.ctx, tx, tokenOwner.Address())
if err != nil {
// ignore txs that are not able to be estimated/execute
testlog.Logger().Error("unable to estimate tx", log.ErrKey, err)
tokenOwner.SetNonce(tokenOwner.GetNonce() - 1)
continue
}
signedTx, err := tokenOwner.SignTransaction(estimatedTx)
if err != nil {
panic(err)
}
err = s.RPCHandles.RndEthClient().SendTransaction(signedTx)
if err != nil {
panic(err)
}
go s.TxInjector.TxTracker.trackL1Tx(txData)
}
}
func (s *Simulation) checkHealthStatus() {
for _, client := range s.RPCHandles.TenClients {
err := retry.Do(func() error {
healthy, err := client.Health()
if !healthy || err != nil {
return fmt.Errorf("client is not healthy: %w", err)
}
return nil
}, retry.NewTimeoutStrategy(30*time.Second, 100*time.Millisecond))
if err != nil {
panic(err)
}
}
}
func NextNonce(ctx context.Context, clients *network.RPCHandles, w wallet.Wallet) uint64 {
counter := 0
// only returns the nonce when the previous transaction was recorded
for {
remoteNonce, err := clients.TenWalletRndClient(w).NonceAt(ctx, nil)
if err != nil {
panic(err)
}
localNonce := w.GetNonce()
if remoteNonce == localNonce {
return w.GetNonceAndIncrement()
}
if remoteNonce > localNonce {
panic("remote nonce exceeds local nonce")
}
counter++
if counter > nonceTimeoutMillis {
panic(fmt.Sprintf("transaction injector could not retrieve nonce after thirty seconds for address %s. "+
"Local nonce was %d, remote nonce was %d", w.Address().Hex(), localNonce, remoteNonce))
}
time.Sleep(time.Millisecond)
}
}