forked from lavanet/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
175 lines (170 loc) · 8.08 KB
/
testing.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
package rpcconsumer
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/lavanet/lava/protocol/chainlib"
"github.com/lavanet/lava/protocol/chainlib/chainproxy"
"github.com/lavanet/lava/protocol/chaintracker"
commonlib "github.com/lavanet/lava/protocol/common"
"github.com/lavanet/lava/protocol/lavasession"
"github.com/lavanet/lava/protocol/rpcprovider"
"github.com/lavanet/lava/protocol/statetracker/updaters"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/rand"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func startTesting(ctx context.Context, clientCtx client.Context, txFactory tx.Factory, rpcEndpoints []*lavasession.RPCProviderEndpoint, parallelConnections uint) error {
ctx, cancel := context.WithCancel(ctx)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
defer func() {
signal.Stop(signalChan)
cancel()
}()
stateQuery := updaters.NewConsumerStateQuery(ctx, clientCtx)
for _, rpcProviderEndpoint := range rpcEndpoints {
go func(rpcProviderEndpoint *lavasession.RPCProviderEndpoint) error {
chainParser, err := chainlib.NewChainParser(rpcProviderEndpoint.ApiInterface)
if err != nil {
utils.LavaFormatError("panic severity critical error, aborting support for chain api due to invalid chain parser, continuing with others", err, utils.Attribute{Key: "endpoint", Value: rpcProviderEndpoint.String()})
}
specResponse, err := stateQuery.GetSpec(ctx, rpcProviderEndpoint.ChainID)
if err != nil {
return err
}
chainParser.SetSpec(*specResponse)
chainProxy, err := chainlib.GetChainRouter(ctx, parallelConnections, rpcProviderEndpoint, chainParser)
if err != nil {
return utils.LavaFormatError("panic severity critical error, failed creating chain proxy, continuing with others endpoints", err, utils.Attribute{Key: "parallelConnections", Value: uint64(parallelConnections)}, utils.Attribute{Key: "rpcProviderEndpoint", Value: rpcProviderEndpoint})
}
printOnNewLatestCallback := func(blockFrom int64, blockTo int64, hash string) {
for block := blockFrom + 1; block <= blockTo; block++ {
utils.LavaFormatInfo("Received a new Block",
utils.Attribute{Key: "block", Value: block},
utils.Attribute{Key: "hash", Value: hash},
utils.Attribute{Key: "Chain", Value: rpcProviderEndpoint.ChainID},
utils.Attribute{Key: "apiInterface", Value: rpcProviderEndpoint.ApiInterface},
)
}
}
consistencyErrorCallback := func(oldBlock, newBlock int64) {
utils.LavaFormatError("Consistency issue detected", nil,
utils.Attribute{Key: "oldBlock", Value: oldBlock},
utils.Attribute{Key: "newBlock", Value: newBlock},
utils.Attribute{Key: "Chain", Value: rpcProviderEndpoint.ChainID},
utils.Attribute{Key: "apiInterface", Value: rpcProviderEndpoint.ApiInterface},
)
}
_, averageBlockTime, blocksToFinalization, blocksInFinalizationData := chainParser.ChainBlockStats()
blocksToSaveChainTracker := uint64(blocksToFinalization + blocksInFinalizationData)
chainTrackerConfig := chaintracker.ChainTrackerConfig{
BlocksToSave: blocksToSaveChainTracker,
AverageBlockTime: averageBlockTime,
ServerBlockMemory: rpcprovider.ChainTrackerDefaultMemory + blocksToSaveChainTracker,
NewLatestCallback: printOnNewLatestCallback,
ConsistencyCallback: consistencyErrorCallback,
}
chainFetcher := chainlib.NewChainFetcher(ctx, chainProxy, chainParser, rpcProviderEndpoint, nil)
chainTracker, err := chaintracker.NewChainTracker(ctx, chainFetcher, chainTrackerConfig)
if err != nil {
return utils.LavaFormatError("panic severity critical error, aborting support for chain api due to node access, continuing with other endpoints", err, utils.Attribute{Key: "chainTrackerConfig", Value: chainTrackerConfig}, utils.Attribute{Key: "endpoint", Value: rpcProviderEndpoint})
}
_ = chainTracker // let the chain tracker work and make queries
return nil
}(rpcProviderEndpoint)
}
select {
case <-ctx.Done():
utils.LavaFormatInfo("test rpcconsumer ctx.Done")
case <-signalChan:
utils.LavaFormatInfo("test rpcconsumer signalChan")
}
return nil
}
func CreateTestRPCConsumerCobraCommand() *cobra.Command {
cmdTestRPCConsumer := &cobra.Command{
Use: `rpcconsumer {listen-ip:listen-port spec-chain-id api-interface} ... `,
Short: `test an rpc consumer by making calls in the chain api interface requested`,
Long: `sets up a client that requests for blocks in the requested api on the listen port to perform tests on an rpcconsumer that is active`,
Example: `rpcconsumer "http://127.0.0.1:3333 ETH1 jsonrpc http://127.0.0.1:3334 LAV1 rest 127.0.0.1:3334 LAV1 grpc"`,
Args: func(cmd *cobra.Command, args []string) error {
argLen := len(args)
if argLen == 0 || argLen%len(Yaml_config_properties) != 0 {
return fmt.Errorf("invalid number of arguments, needs to be a repeated groups of %d, while arg count is %d", len(Yaml_config_properties), argLen)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
utils.LavaFormatInfo("RPCConsumer Test started", utils.Attribute{Key: "args", Value: strings.Join(args, ";")})
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// handle flags, pass necessary fields
ctx := context.Background()
networkChainId, err := cmd.Flags().GetString(flags.FlagChainID)
if err != nil {
return err
}
logLevel, err := cmd.Flags().GetString(flags.FlagLogLevel)
if err != nil {
utils.LavaFormatFatal("failed to read log level flag", err)
}
utils.SetGlobalLoggingLevel(logLevel)
var viper_endpoints *viper.Viper
viper_endpoints, err = commonlib.ParseEndpointArgs(args, Yaml_config_properties, commonlib.EndpointsConfigName)
if err != nil {
return utils.LavaFormatError("invalid endpoints arguments", err, utils.Attribute{Key: "endpoint_strings", Value: strings.Join(args, "")})
}
viper.MergeConfigMap(viper_endpoints.AllSettings())
var rpcEndpoints []*lavasession.RPCEndpoint
rpcEndpoints, err = ParseEndpoints(viper.GetViper(), 1)
if err != nil || len(rpcEndpoints) == 0 {
return utils.LavaFormatError("invalid endpoints definition", err)
}
modifiedProviderEndpoints := make([]*lavasession.RPCProviderEndpoint, len(rpcEndpoints))
for idx := range modifiedProviderEndpoints {
endpoint := rpcEndpoints[idx]
modifiedProviderEndpoints[idx] = &lavasession.RPCProviderEndpoint{
NetworkAddress: lavasession.NetworkAddressData{Address: ""},
ChainID: endpoint.ChainID,
ApiInterface: endpoint.ApiInterface,
Geolocation: 1, // doesn't matter
NodeUrls: []commonlib.NodeUrl{{
Url: endpoint.NetworkAddress,
AuthConfig: commonlib.AuthConfig{
UseTLS: viper.GetBool(chainproxy.GRPCUseTls),
AllowInsecure: viper.GetBool(chainproxy.GRPCAllowInsecureConnection),
},
}},
}
}
clientCtx = clientCtx.WithChainID(networkChainId)
txFactory, err := tx.NewFactoryCLI(clientCtx, cmd.Flags())
if err != nil {
utils.LavaFormatFatal("failed to create txFactory", err)
}
utils.LavaFormatInfo("lavad Binary Version: " + version.Version)
rand.InitRandomSeed()
numberOfNodeParallelConnections, err := cmd.Flags().GetUint(chainproxy.ParallelConnectionsFlag)
if err != nil {
utils.LavaFormatFatal("error fetching chainproxy.ParallelConnectionsFlag", err)
}
return startTesting(ctx, clientCtx, txFactory, modifiedProviderEndpoints, numberOfNodeParallelConnections)
},
}
// RPCConsumer command flags
flags.AddTxFlagsToCmd(cmdTestRPCConsumer)
cmdTestRPCConsumer.Flags().Uint(chainproxy.ParallelConnectionsFlag, chainproxy.NumberOfParallelConnections, "parallel connections")
cmdTestRPCConsumer.Flags().Bool(chainproxy.GRPCAllowInsecureConnection, false, "used to test grpc, to allow insecure (self signed cert).")
cmdTestRPCConsumer.Flags().Bool(chainproxy.GRPCUseTls, true, "use tls configuration for grpc connections to your consumer")
return cmdTestRPCConsumer
}