-
Notifications
You must be signed in to change notification settings - Fork 829
/
Copy pathmain.go
706 lines (646 loc) · 22.1 KB
/
main.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/exec"
"os/signal"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/sync/semaphore"
"golang.org/x/time/rate"
"github.com/sei-protocol/sei-chain/app"
dextypes "github.com/sei-protocol/sei-chain/x/dex/types"
tokenfactorytypes "github.com/sei-protocol/sei-chain/x/tokenfactory/types"
)
var TestConfig EncodingConfig
const (
VortexData = "{\"position_effect\":\"Open\",\"leverage\":\"1\"}"
)
var (
FromMili = sdk.NewDec(1000000)
producedCount = atomic.Int64{}
sentCount = atomic.Int64{}
prevSentCount = atomic.Int64{}
)
type BlockData struct {
Txs []string `json:"txs"`
}
type BlockHeader struct {
Time string `json:"time"`
}
func init() {
cdc := codec.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(interfaceRegistry)
TestConfig = EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: tx.NewTxConfig(marshaler, tx.DefaultSignModes),
Amino: cdc,
}
std.RegisterLegacyAminoCodec(TestConfig.Amino)
std.RegisterInterfaces(TestConfig.InterfaceRegistry)
app.ModuleBasics.RegisterLegacyAminoCodec(TestConfig.Amino)
app.ModuleBasics.RegisterInterfaces(TestConfig.InterfaceRegistry)
// Add this so that we don't end up getting disconnected for EVM client
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
}
// deployEvmContract executes a bash script and returns its output as a string.
//
//nolint:gosec
func deployEvmContract(scriptPath string, config *Config) (common.Address, error) {
cmd := exec.Command(scriptPath, config.EVMRpcEndpoint())
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return common.Address{}, err
}
return common.HexToAddress(out.String()), nil
}
func deployEvmContracts(config *Config) {
config.EVMAddresses = &EVMAddresses{}
if config.ContainsAnyMessageTypes(ERC20) {
fmt.Println("Deploying ERC20 contract")
erc20, err := deployEvmContract("loadtest/contracts/deploy_erc20.sh", config)
if err != nil {
fmt.Println("error deploying, make sure 0xF87A299e6bC7bEba58dbBe5a5Aa21d49bCD16D52 is funded")
panic(err)
}
config.EVMAddresses.ERC20 = erc20
}
if config.ContainsAnyMessageTypes(ERC721) {
fmt.Println("Deploying ERC721 contract")
erc721, err := deployEvmContract("loadtest/contracts/deploy_erc721.sh", config)
if err != nil {
fmt.Println("error deploying, make sure 0xF87A299e6bC7bEba58dbBe5a5Aa21d49bCD16D52 is funded")
panic(err)
}
config.EVMAddresses.ERC721 = erc721
}
}
//nolint:gosec
func deployUniswapContracts(client *LoadTestClient, config *Config) {
config.EVMAddresses = &EVMAddresses{}
if config.ContainsAnyMessageTypes(UNIV2) {
fmt.Println("Deploying Uniswap contracts")
cmd := exec.Command("loadtest/contracts/deploy_univ2.sh", config.EVMRpcEndpoint())
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
fmt.Println("script output: ", out.String())
if err != nil {
panic("deploy_univ2.sh failed with error: " + err.Error())
}
UniV2SwapperRe := regexp.MustCompile(`Swapper Address: "(\w+)"`)
match := UniV2SwapperRe.FindStringSubmatch(out.String())
uniV2SwapperAddress := common.HexToAddress(match[1])
fmt.Println("Found UniV2Swapper Address: ", uniV2SwapperAddress.String())
for _, txClient := range client.EvmTxClients {
txClient.evmAddresses.UniV2Swapper = uniV2SwapperAddress
}
}
}
func run(config *Config) {
// Start metrics collector in another thread
metricsServer := MetricsServer{}
go metricsServer.StartMetricsClient(*config)
client := NewLoadTestClient(*config)
client.SetValidators()
deployEvmContracts(config)
deployUniswapContracts(client, config)
startLoadtestWorkers(client, *config)
}
// starts loadtest workers. If config.Constant is true, then we don't gather loadtest results and let producer/consumer
// workers continue running. If config.Constant is false, then we will gather load test results in a file
func startLoadtestWorkers(client *LoadTestClient, config Config) {
fmt.Printf("Starting loadtest workers\n")
configString, _ := json.Marshal(config)
fmt.Printf("Running with \n %s \n", string(configString))
// Catch OS signals for graceful shutdown
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
var blockHeights []int
var blockTimes []string
var startHeight = getLastHeight(config.BlockchainEndpoint)
keys := client.AccountKeys
// Create producers and consumers
fmt.Printf("Starting loadtest producers and consumers\n")
txQueues := make([]chan SignedTx, len(keys))
for i := range txQueues {
txQueues[i] = make(chan SignedTx, 10)
}
done := make(chan struct{})
producerRateLimiter := rate.NewLimiter(rate.Limit(config.TargetTps), int(config.TargetTps))
consumerSemaphore := semaphore.NewWeighted(int64(config.TargetTps))
var wg sync.WaitGroup
for i := 0; i < len(keys); i++ {
go client.BuildTxs(txQueues[i], i, &wg, done, producerRateLimiter, &producedCount)
go client.SendTxs(txQueues[i], i, done, &sentCount, consumerSemaphore, &wg)
}
// Statistics reporting goroutine
ticker := time.NewTicker(10 * time.Second)
go func() {
start := time.Now()
for {
select {
case <-ticker.C:
currHeight := getLastHeight(config.BlockchainEndpoint)
for i := startHeight; i <= currHeight; i++ {
_, blockTime, err := getTxBlockInfo(config.BlockchainEndpoint, strconv.Itoa(i))
if err != nil {
fmt.Printf("Encountered error scraping data: %s\n", err)
return
}
blockHeights = append(blockHeights, i)
blockTimes = append(blockTimes, blockTime)
}
totalProduced := producedCount.Load()
totalSent := sentCount.Load()
prevTotalSent := prevSentCount.Load()
printStats(start, totalProduced, totalSent, prevTotalSent, blockHeights, blockTimes)
startHeight = currHeight
blockHeights, blockTimes = nil, nil
start = time.Now()
prevSentCount.Store(totalSent)
case <-done:
ticker.Stop()
return
}
}
}()
// Wait for a termination signal
<-signals
fmt.Println("SIGINT received, shutting down producers and consumers...")
close(done)
fmt.Println("Waiting for wait groups...")
wg.Wait()
fmt.Println("Closing channels...")
for i := range txQueues {
close(txQueues[i])
}
}
func printStats(startTime time.Time, totalProduced int64, totalSent int64, prevTotalSent int64, blockHeights []int, blockTimes []string) {
elapsed := time.Since(startTime)
tps := float64(totalSent-prevTotalSent) / elapsed.Seconds()
var totalDuration time.Duration
var prevTime time.Time
for i, blockTimeStr := range blockTimes {
blockTime, _ := time.Parse(time.RFC3339Nano, blockTimeStr)
if i > 0 {
duration := blockTime.Sub(prevTime)
totalDuration += duration
}
prevTime = blockTime
}
if len(blockTimes)-1 < 1 {
fmt.Printf("Unable to calculate stats, not enough data. Skipping...\n")
} else {
avgDuration := totalDuration.Milliseconds() / int64(len(blockTimes)-1)
fmt.Printf("High Level - Time Elapsed: %v, Produced: %d, Sent: %d, TPS: %f, Avg Block Time: %d ms\nBlock Heights %v\n\n", elapsed, totalProduced, totalSent, tps, avgDuration, blockHeights)
}
}
// Generate a random message, only generate one admin message per block to prevent acc seq errors
func (c *LoadTestClient) getRandomMessageType(messageTypes []string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
messageType := messageTypes[r.Intn(len(messageTypes))]
for c.generatedAdminMessageForBlock && c.isAdminMessageMapping[messageType] {
messageType = messageTypes[r.Intn(len(messageTypes))]
}
if c.isAdminMessageMapping[messageType] {
c.generatedAdminMessageForBlock = true
}
return messageType
}
func (c *LoadTestClient) generateMessage(key cryptotypes.PrivKey, msgType string) ([]sdk.Msg, bool, cryptotypes.PrivKey, uint64, int64) {
var msgs []sdk.Msg
config := c.LoadTestConfig
msgPerTx := config.MsgsPerTx
signer := key
r := rand.New(rand.NewSource(time.Now().UnixNano()))
defer IncrTxMessageType(msgType)
defaultMessageTypeConfig := config.PerMessageConfigs["default"]
gas := defaultMessageTypeConfig.Gas
fee := defaultMessageTypeConfig.Fee
messageTypeConfig, ok := config.PerMessageConfigs[msgType]
if ok {
gas = messageTypeConfig.Gas
fee = messageTypeConfig.Fee
}
switch msgType {
case Vortex:
price := config.PriceDistr.Sample()
quantity := config.QuantityDistr.Sample()
msgs = c.generateVortexOrder(config, key, config.WasmMsgTypes.Vortex.NumOrdersPerTx, price, quantity)
case WasmMintNft:
contract := config.WasmMsgTypes.MintNftType.ContractAddr
// TODO: Potentially just hard code the Funds amount here
price := config.PriceDistr.Sample()
quantity := config.QuantityDistr.Sample()
amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", price.Mul(quantity).Ceil().RoundInt64(), "usei"))
if err != nil {
panic(err)
}
msgs = []sdk.Msg{&wasmtypes.MsgExecuteContract{
Sender: sdk.AccAddress(key.PubKey().Address()).String(),
Contract: contract,
Msg: wasmtypes.RawContractMessage([]byte("{\"mint\":{\"owner\": \"sei1a27kj2j27c6uz58rn9zmhcjee9s3h3nhyhtvjj\"}}")),
Funds: amount,
}}
case WasmInstantiate:
msgs = []sdk.Msg{&wasmtypes.MsgInstantiateContract{
Sender: sdk.AccAddress(key.PubKey().Address()).String(),
CodeID: config.WasmMsgTypes.Instantiate.CodeID,
Label: "test",
Msg: wasmtypes.RawContractMessage([]byte(config.WasmMsgTypes.Instantiate.Payload)),
Funds: sdk.NewCoins(sdk.Coin{
Denom: "usei",
Amount: sdk.NewInt(1),
}), // maybe make this configurable as well in the future
}}
case Bank:
msgs = []sdk.Msg{}
for i := 0; i < int(msgPerTx); i++ {
msgs = append(msgs, &banktypes.MsgSend{
FromAddress: sdk.AccAddress(key.PubKey().Address()).String(),
ToAddress: sdk.AccAddress(key.PubKey().Address()).String(),
Amount: sdk.NewCoins(sdk.Coin{
Denom: "usei",
Amount: sdk.NewInt(1),
}),
})
}
case DistributeRewards:
adminKey := c.SignerClient.GetAdminKey()
msgs = []sdk.Msg{&banktypes.MsgSend{
FromAddress: sdk.AccAddress(adminKey.PubKey().Address()).String(),
ToAddress: sdk.AccAddress(key.PubKey().Address()).String(),
Amount: sdk.NewCoins(sdk.Coin{
Denom: "usei",
Amount: sdk.NewInt(10000000),
}),
}}
signer = adminKey
gas = 10000000
fee = 1000000
fmt.Printf("Distribute rewards to %s \n", sdk.AccAddress(key.PubKey().Address()).String())
case CollectRewards:
adminKey := c.SignerClient.GetAdminKey()
delegatorAddr := sdk.AccAddress(adminKey.PubKey().Address())
operatorAddress := c.Validators[r.Intn(len(c.Validators))].OperatorAddress
randomValidatorAddr, err := sdk.ValAddressFromBech32(operatorAddress)
if err != nil {
panic(err.Error())
}
msgs = []sdk.Msg{distributiontypes.NewMsgWithdrawDelegatorReward(
delegatorAddr,
randomValidatorAddr,
)}
fmt.Printf("Collecting rewards from %s \n", operatorAddress)
signer = adminKey
gas = 10000000
fee = 1000000
case Dex:
price := config.PriceDistr.Sample()
quantity := config.QuantityDistr.Sample()
contract := config.ContractDistr.Sample()
orderPlacements := generateDexOrderPlacements(config, key, msgPerTx, price, quantity)
amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", price.Mul(quantity).Ceil().RoundInt64(), "usei"))
if err != nil {
panic(err)
}
msgs = []sdk.Msg{&dextypes.MsgPlaceOrders{
Creator: sdk.AccAddress(key.PubKey().Address()).String(),
Orders: orderPlacements,
ContractAddr: contract,
Funds: amount,
}}
case Staking:
delegatorAddr := sdk.AccAddress(key.PubKey().Address()).String()
chosenValidator := c.Validators[r.Intn(len(c.Validators))].OperatorAddress
// Randomly pick someone to redelegate / unbond from
srcAddr := ""
c.mtx.RLock()
for k := range c.DelegationMap[delegatorAddr] {
if k == chosenValidator {
continue
}
srcAddr = k
break
}
c.mtx.RUnlock()
msgs = []sdk.Msg{c.generateStakingMsg(delegatorAddr, chosenValidator, srcAddr)}
case Tokenfactory:
denomCreatorAddr := sdk.AccAddress(key.PubKey().Address()).String()
// No denoms, let's mint
randNum := r.Float64()
denom, ok := c.TokenFactoryDenomOwner[denomCreatorAddr]
switch {
case !ok || randNum <= 0.33:
subDenom := fmt.Sprintf("tokenfactory-created-denom-%d", time.Now().UnixMilli())
denom = fmt.Sprintf("factory/%s/%s", denomCreatorAddr, subDenom)
msgs = []sdk.Msg{&tokenfactorytypes.MsgCreateDenom{
Sender: denomCreatorAddr,
Subdenom: subDenom,
}}
c.TokenFactoryDenomOwner[denomCreatorAddr] = denom
case randNum <= 0.66:
msgs = []sdk.Msg{&tokenfactorytypes.MsgMint{
Sender: denomCreatorAddr,
Amount: sdk.Coin{Denom: denom, Amount: sdk.NewInt(10)},
}}
default:
msgs = []sdk.Msg{&tokenfactorytypes.MsgBurn{
Sender: denomCreatorAddr,
Amount: sdk.Coin{Denom: denom, Amount: sdk.NewInt(1)},
}}
}
case FailureBankMalformed:
var denom string
if r.Float64() < 0.5 {
denom = "unknown"
} else {
denom = "other"
}
msgs = []sdk.Msg{&banktypes.MsgSend{
FromAddress: sdk.AccAddress(key.PubKey().Address()).String(),
ToAddress: sdk.AccAddress(key.PubKey().Address()).String(),
Amount: sdk.NewCoins(sdk.Coin{
Denom: denom,
Amount: sdk.NewInt(1),
}),
}}
case FailureBankInvalid:
var amountUsei int64
amountUsei = 1000000000000000000
msgs = []sdk.Msg{&banktypes.MsgSend{
FromAddress: sdk.AccAddress(key.PubKey().Address()).String(),
ToAddress: sdk.AccAddress(key.PubKey().Address()).String(),
Amount: sdk.NewCoins(sdk.Coin{
Denom: "usei",
Amount: sdk.NewInt(amountUsei),
}),
}}
case FailureDexMalformed:
price := config.PriceDistr.InvalidSample()
quantity := config.QuantityDistr.InvalidSample()
contract := config.ContractDistr.Sample()
orderPlacements := generateDexOrderPlacements(config, key, msgPerTx, price, quantity)
amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", price.Mul(quantity).Ceil().RoundInt64(), "usei"))
if err != nil {
panic(err)
}
msgs = []sdk.Msg{&dextypes.MsgPlaceOrders{
Creator: sdk.AccAddress(key.PubKey().Address()).String(),
Orders: orderPlacements,
ContractAddr: contract,
Funds: amount,
}}
case FailureDexInvalid:
price := config.PriceDistr.Sample()
quantity := config.QuantityDistr.Sample()
contract := config.ContractDistr.Sample()
orderPlacements := generateDexOrderPlacements(config, key, msgPerTx, price, quantity)
var amountUsei int64
if r.Float64() < 0.5 {
amountUsei = 10000 * price.Mul(quantity).Ceil().RoundInt64()
} else {
amountUsei = 0
}
amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", amountUsei, "usei"))
if err != nil {
panic(err)
}
msgs = []sdk.Msg{&dextypes.MsgPlaceOrders{
Creator: sdk.AccAddress(key.PubKey().Address()).String(),
Orders: orderPlacements,
ContractAddr: contract,
Funds: amount,
}}
default:
fmt.Printf("Unrecognized message type %s", config.MessageType)
}
if strings.Contains(config.MessageType, "failure") {
return msgs, true, signer, gas, int64(fee)
}
return msgs, false, signer, gas, int64(fee)
}
func sampleDexOrderType(config Config) (orderType dextypes.OrderType) {
if config.MessageType == "failure_bank_malformed" {
orderType = -1
} else {
msgType := config.MsgTypeDistr.SampleDexMsgs()
switch msgType {
case Limit:
orderType = dextypes.OrderType_LIMIT
case Market:
orderType = dextypes.OrderType_MARKET
default:
panic(fmt.Sprintf("Unknown message type %s\n", msgType))
}
}
return orderType
}
func generateDexOrderPlacements(config Config, key cryptotypes.PrivKey, msgPerTx uint64, price sdk.Dec, quantity sdk.Dec) (orderPlacements []*dextypes.Order) {
orderType := sampleDexOrderType(config)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var direction dextypes.PositionDirection
if r.Float64() < 0.5 {
direction = dextypes.PositionDirection_LONG
} else {
direction = dextypes.PositionDirection_SHORT
}
contract := config.ContractDistr.Sample()
for j := 0; j < int(msgPerTx); j++ {
orderPlacements = append(orderPlacements, &dextypes.Order{
Account: sdk.AccAddress(key.PubKey().Address()).String(),
ContractAddr: contract,
PositionDirection: direction,
Price: price.Quo(FromMili),
Quantity: quantity.Quo(FromMili),
PriceDenom: "SEI",
AssetDenom: "ATOM",
OrderType: orderType,
Data: VortexData,
})
}
return orderPlacements
}
func (c *LoadTestClient) generateStakingMsg(delegatorAddr string, chosenValidator string, srcAddr string) sdk.Msg {
c.mtx.Lock()
defer c.mtx.Unlock()
// Randomly unbond, redelegate or delegate
// However, if there are no delegations, do so first
var msg sdk.Msg
msgType := c.LoadTestConfig.MsgTypeDistr.SampleStakingMsgs()
if _, ok := c.DelegationMap[delegatorAddr]; !ok || msgType == "delegate" || srcAddr == "" {
msg = &stakingtypes.MsgDelegate{
DelegatorAddress: delegatorAddr,
ValidatorAddress: chosenValidator,
Amount: sdk.Coin{Denom: "usei", Amount: sdk.NewInt(1)},
}
c.DelegationMap[delegatorAddr] = map[string]int{}
c.DelegationMap[delegatorAddr][chosenValidator] = 1
} else {
if msgType == "redelegate" {
msg = &stakingtypes.MsgBeginRedelegate{
DelegatorAddress: delegatorAddr,
ValidatorSrcAddress: srcAddr,
ValidatorDstAddress: chosenValidator,
Amount: sdk.Coin{Denom: "usei", Amount: sdk.NewInt(1)},
}
c.DelegationMap[delegatorAddr][chosenValidator]++
} else {
msg = &stakingtypes.MsgUndelegate{
DelegatorAddress: delegatorAddr,
ValidatorAddress: srcAddr,
Amount: sdk.Coin{Denom: "usei", Amount: sdk.NewInt(1)},
}
}
// Update delegation map
c.DelegationMap[delegatorAddr][srcAddr]--
if c.DelegationMap[delegatorAddr][srcAddr] == 0 {
delete(c.DelegationMap, delegatorAddr)
}
}
return msg
}
// generateVortexOrder generates Vortex order messages. If short order, creates a deposit message first
func (c *LoadTestClient) generateVortexOrder(config Config, key cryptotypes.PrivKey, numOrders int64, price sdk.Dec, quantity sdk.Dec) []sdk.Msg {
var msgs []sdk.Msg
contract := config.WasmMsgTypes.Vortex.ContractAddr
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Randomly select Position Direction
var direction dextypes.PositionDirection
if r.Float64() < 0.5 {
direction = dextypes.PositionDirection_LONG
} else {
direction = dextypes.PositionDirection_SHORT
}
orderType := sampleDexOrderType(config)
// If placing short order on vortex, first deposit for buying power
if direction == dextypes.PositionDirection_SHORT {
// TODO: Considering depositing more up front when numOrders > 1
amountDeposit, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", price.Mul(quantity).Ceil().RoundInt64(), "usei"))
if err != nil {
panic(err)
}
vortexDeposit := &wasmtypes.MsgExecuteContract{
Sender: sdk.AccAddress(key.PubKey().Address()).String(),
Contract: contract,
Msg: wasmtypes.RawContractMessage([]byte("{\"deposit\":{}}")),
Funds: amountDeposit,
}
msgs = append(msgs, vortexDeposit)
}
// Create a MsgPlaceOrders with numOrders Orders
var orderPlacements []*dextypes.Order
for j := 0; j < int(numOrders); j++ {
vortexOrder := &dextypes.Order{
Account: sdk.AccAddress(key.PubKey().Address()).String(),
ContractAddr: contract,
PositionDirection: direction,
Price: price.Quo(FromMili),
Quantity: quantity.Quo(FromMili),
PriceDenom: "SEI",
AssetDenom: "ATOM",
OrderType: orderType,
Data: VortexData,
}
orderPlacements = append(orderPlacements, vortexOrder)
}
amount, err := sdk.ParseCoinsNormalized(fmt.Sprintf("%d%s", price.Mul(quantity).Ceil().RoundInt64(), "usei"))
if err != nil {
panic(err)
}
vortexOrderMsg := &dextypes.MsgPlaceOrders{
Creator: sdk.AccAddress(key.PubKey().Address()).String(),
Orders: orderPlacements,
ContractAddr: contract,
Funds: amount,
}
msgs = append(msgs, vortexOrderMsg)
return msgs
}
// nolint
func getLastHeight(blockchainEndpoint string) int {
out, err := exec.Command("curl", blockchainEndpoint+"/blockchain").Output()
if err != nil {
panic(err)
}
var dat map[string]interface{}
if err := json.Unmarshal(out, &dat); err != nil {
panic(err)
}
height, err := strconv.Atoi(dat["last_height"].(string))
if err != nil {
panic(err)
}
return height
}
func getTxBlockInfo(blockchainEndpoint string, height string) (int, string, error) {
resp, err := http.Get(blockchainEndpoint + "/block?height=" + height)
if err != nil {
fmt.Printf("Error query block data: %s\n", err)
return 0, "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading block data: %s\n", err)
return 0, "", err
}
var blockResponse struct {
Block struct {
Header BlockHeader `json:"header"`
Data BlockData `json:"data"`
} `json:"block"`
}
err = json.Unmarshal(body, &blockResponse)
if err != nil {
fmt.Printf("Error reading block data: %s\n", err)
return 0, "", err
}
return len(blockResponse.Block.Data.Txs), blockResponse.Block.Header.Time, nil
}
func GetDefaultConfigFilePath() string {
pwd, _ := os.Getwd()
return pwd + "/loadtest/config.json"
}
func ReadConfig(path string) Config {
config := Config{}
file, _ := os.ReadFile(path)
if err := json.Unmarshal(file, &config); err != nil {
panic(err)
}
return config
}
func main() {
configFilePath := flag.String("config-file", GetDefaultConfigFilePath(), "Path to the config.json file to use for this run")
flag.Parse()
config := ReadConfig(*configFilePath)
fmt.Printf("Using config file: %s\n", *configFilePath)
run(&config)
}