-
Notifications
You must be signed in to change notification settings - Fork 829
/
Copy pathloadtest_client.go
280 lines (264 loc) · 8.36 KB
/
loadtest_client.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
package main
import (
"context"
"crypto/tls"
"fmt"
"math/rand"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/time/rate"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
typestx "github.com/cosmos/cosmos-sdk/types/tx"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/sync/semaphore"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
)
type LoadTestClient struct {
LoadTestConfig Config
TestConfig EncodingConfig
AccountKeys []cryptotypes.PrivKey
TxClients []typestx.ServiceClient
EvmTxClients []*EvmTxClient
SignerClient *SignerClient
ChainID string
GrpcConns []*grpc.ClientConn
StakingQueryClient stakingtypes.QueryClient
// Staking specific variables
Validators []stakingtypes.Validator
// DelegationMap is a map of delegator -> validator -> delegated amount
DelegationMap map[string]map[string]int
// Tokenfactory specific variables
TokenFactoryDenomOwner map[string]string
// Only one admin message can go in per block
generatedAdminMessageForBlock bool
// Messages that has to be sent from the admin account
isAdminMessageMapping map[string]bool
mtx *sync.RWMutex
}
func NewLoadTestClient(config Config) *LoadTestClient {
signerClient := NewSignerClient(config.NodeURI)
keys := signerClient.GetTestAccountsKeys(int(config.MaxAccounts))
txClients, grpcConns := BuildGrpcClients(&config)
var evmTxClients []*EvmTxClient
if config.EvmRpcEndpoints != "" {
if config.ContainsAnyMessageTypes(EVM, ERC20, ERC721, UNIV2) {
evmTxClients = BuildEvmTxClients(&config, keys)
}
}
return &LoadTestClient{
LoadTestConfig: config,
AccountKeys: keys,
TestConfig: TestConfig,
TxClients: txClients,
EvmTxClients: evmTxClients,
SignerClient: signerClient,
ChainID: config.ChainID,
GrpcConns: grpcConns,
StakingQueryClient: stakingtypes.NewQueryClient(grpcConns[0]),
DelegationMap: map[string]map[string]int{},
TokenFactoryDenomOwner: map[string]string{},
generatedAdminMessageForBlock: false,
isAdminMessageMapping: map[string]bool{CollectRewards: true, DistributeRewards: true},
mtx: &sync.RWMutex{},
}
}
func (c *LoadTestClient) SetValidators() {
if strings.Contains(c.LoadTestConfig.MessageType, "staking") {
resp, err := c.StakingQueryClient.Validators(context.Background(), &stakingtypes.QueryValidatorsRequest{})
if err != nil {
panic(err)
}
c.Validators = resp.Validators
}
}
// BuildGrpcClients build a list of grpc clients
func BuildGrpcClients(config *Config) ([]typestx.ServiceClient, []*grpc.ClientConn) {
grpcEndpoints := strings.Split(config.GrpcEndpoints, ",")
txClients := make([]typestx.ServiceClient, len(grpcEndpoints))
grpcConns := make([]*grpc.ClientConn, len(grpcEndpoints))
var dialOptions []grpc.DialOption
dialOptions = append(dialOptions, grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(20*1024*1024),
grpc.MaxCallSendMsgSize(20*1024*1024)),
)
dialOptions = append(dialOptions, grpc.WithBlock())
if config.TLS {
dialOptions = append(dialOptions, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{InsecureSkipVerify: true}))) //nolint:gosec // Use insecure skip verify.
} else {
dialOptions = append(dialOptions, grpc.WithInsecure())
}
for i, endpoint := range grpcEndpoints {
grpcConn, _ := grpc.Dial(
endpoint,
dialOptions...)
txClients[i] = typestx.NewServiceClient(grpcConn)
grpcConns[i] = grpcConn
// spin up goroutine for monitoring and reconnect purposes
go func() {
for {
state := grpcConn.GetState()
if state == connectivity.TransientFailure || state == connectivity.Shutdown {
fmt.Println("GRPC Connection lost, attempting to reconnect...")
for {
if grpcConn.WaitForStateChange(context.Background(), state) {
break
}
time.Sleep(10 * time.Second)
}
}
time.Sleep(10 * time.Second)
}
}()
}
return txClients, grpcConns
}
// BuildEvmTxClients build a list of EvmTxClients with a list of go-ethereum client
func BuildEvmTxClients(config *Config, keys []cryptotypes.PrivKey) []*EvmTxClient {
clients := make([]*EvmTxClient, len(keys))
ethEndpoints := strings.Split(config.EvmRpcEndpoints, ",")
if len(ethEndpoints) == 0 {
return clients
}
ethClients := make([]*ethclient.Client, len(ethEndpoints))
for i, endpoint := range ethEndpoints {
client, err := ethclient.Dial(endpoint)
if err != nil {
fmt.Printf("Failed to connect to endpoint %s with error %s", endpoint, err.Error())
}
ethClients[i] = client
}
// Get chainId
chainID, err := ethClients[0].NetworkID(context.Background())
if err != nil {
panic(fmt.Sprintf("Failed to get chain ID: %v \n", err))
}
// Get gas price
gasPrice, err := ethClients[0].SuggestGasPrice(context.Background())
if err != nil {
panic(fmt.Sprintf("Failed to suggest gas price: %v\n", err))
}
// Build one client per key
for i, key := range keys {
clients[i] = NewEvmTxClient(key, chainID, gasPrice, ethClients, config.EVMAddresses)
}
return clients
}
func (c *LoadTestClient) Close() {
for _, grpcConn := range c.GrpcConns {
_ = grpcConn.Close()
}
}
func (c *LoadTestClient) BuildTxs(
txQueue chan SignedTx,
keyIndex int,
wg *sync.WaitGroup,
done <-chan struct{},
rateLimiter *rate.Limiter,
producedCount *atomic.Int64,
) {
wg.Add(1)
defer wg.Done()
config := c.LoadTestConfig
for {
select {
case <-done:
return
default:
if !rateLimiter.Allow() {
continue
}
// Generate a message type first
messageTypes := strings.Split(config.MessageType, ",")
messageType := c.getRandomMessageType(messageTypes)
var signedTx SignedTx
// Sign EVM and Cosmos TX differently
switch messageType {
case EVM, ERC20, ERC721, UNIV2:
signedTx = SignedTx{EvmTx: c.generateSignedEvmTx(keyIndex, messageType)}
default:
signedTx = SignedTx{TxBytes: c.generateSignedCosmosTxs(keyIndex, messageType, producedCount)}
}
select {
case txQueue <- signedTx:
producedCount.Add(1)
case <-done:
return
}
}
}
}
func (c *LoadTestClient) generateSignedEvmTx(keyIndex int, msgType string) *ethtypes.Transaction {
return c.EvmTxClients[keyIndex].GetTxForMsgType(msgType)
}
func (c *LoadTestClient) generateSignedCosmosTxs(keyIndex int, msgType string, producedCount *atomic.Int64) []byte {
key := c.AccountKeys[keyIndex]
msgs, _, _, gas, fee := c.generateMessage(key, msgType)
txBuilder := TestConfig.TxConfig.NewTxBuilder()
_ = txBuilder.SetMsgs(msgs...)
txBuilder.SetGasLimit(gas)
txBuilder.SetFeeAmount([]types.Coin{
types.NewCoin("usei", types.NewInt(fee)),
})
// Use random seqno to get around txs that might already be seen in mempool
c.SignerClient.SignTx(c.ChainID, &txBuilder, key, uint64(producedCount.Load()))
txBytes, _ := TestConfig.TxConfig.TxEncoder()(txBuilder.GetTx())
return txBytes
}
func (c *LoadTestClient) SendTxs(
txQueue chan SignedTx,
keyIndex int,
done <-chan struct{},
sentCount *atomic.Int64,
semaphore *semaphore.Weighted,
wg *sync.WaitGroup,
) {
wg.Add(1)
defer wg.Done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
select {
case <-done:
return
case tx, ok := <-txQueue:
if !ok {
fmt.Printf("Stopping consumers\n")
return
}
// Acquire a semaphore
if err := semaphore.Acquire(ctx, 1); err != nil {
fmt.Printf("Failed to acquire semaphore: %v", err)
break
}
if tx.TxBytes != nil && len(tx.TxBytes) > 0 {
// Send Cosmos Transactions
if SendTx(ctx, tx.TxBytes, typestx.BroadcastMode_BROADCAST_MODE_BLOCK, *c) {
sentCount.Add(1)
}
} else if tx.EvmTx != nil {
// Send EVM Transactions
c.EvmTxClients[keyIndex].SendEvmTx(tx.EvmTx, func() {
sentCount.Add(1)
})
}
// Release the semaphore
semaphore.Release(1)
}
}
}
//nolint:staticcheck
func (c *LoadTestClient) GetTxClient() typestx.ServiceClient {
numClients := len(c.TxClients)
if numClients <= 0 {
panic("There's no Tx client available, make sure your connection are valid")
}
rand.Seed(time.Now().Unix())
return c.TxClients[rand.Int()%len(c.TxClients)]
}