From 6e075ab922f3e1e3e3ccc3334b2404c26a0af4b7 Mon Sep 17 00:00:00 2001 From: stana-ethernal Date: Tue, 16 May 2023 18:34:25 +0200 Subject: [PATCH] cr fix - introduce startCluster flag --- benchmark/root_child_send_tx.go | 12 +++++++----- command/benchmark/benchmark.go | 26 +++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/benchmark/root_child_send_tx.go b/benchmark/root_child_send_tx.go index 3e8fca7a21..d9de979972 100644 --- a/benchmark/root_child_send_tx.go +++ b/benchmark/root_child_send_tx.go @@ -23,6 +23,7 @@ var ( RootJSONRPC = flag.String("rootJSONRPC", "", "JSONRPC address of the root node") ChildJSONRPC = flag.String("childJSONRPC", "", "JSONRPC address of the child node") PrivateKey = flag.String("privateKey", "", "private key that will be used to send tx") + StartCluster = flag.Bool("startCluster", true, "starts the cluster if sets to true") ) // The rootChildSendTx function executes test cases that measure transaction execution on both the root and child chains @@ -30,13 +31,13 @@ var ( // which may include starting the cluster, deploying contracts, and building the test cases. // After building the test cases, rootChildSendTx returns them along with a cleanup function that should be called // after the test cases have been executed. The test cases are executed by the TxTestCasesExecutor. -// The rootJSONRPC, childJSONRPC, and privateKey flags are used to configure the testing environment. -// If all of these flags are set, then the local cluster will not be started and the provided addresses +// The rootJSONRPC, childJSONRPC, privateKey and startCluster flags are used to configure the testing environment. +// If startCluster is false, then the local cluster will not be started and the provided addresses // will be used as the endpoints to the root and child chains. -// If any of these flags is not set, the local cluster will be started automatically. +// If startCluster is set to true, the local cluster will be started automatically. // If the private key is specified, it will be used as the transaction sender. // Otherwise, the local cluster will generate a sender key. -// If the cluster is not run locally, then the sender must have enough funds for sending transactions. +// If startCluster is set to false, then the sender must have enough funds for sending transactions. func rootChildSendTx(b *testing.B) { b.Helper() // set up environment, get test cases and clean up fn @@ -58,7 +59,8 @@ func RootChildSendTxSetUp(b *testing.B) ([]TxTestCase, func()) { rootNodeAddr := *RootJSONRPC childNodeAddr := *ChildJSONRPC privateKeyRaw := *PrivateKey - startCluster := rootNodeAddr == "" || childNodeAddr == "" || privateKeyRaw == "" + startCluster := *StartCluster + require.True(b, startCluster || rootNodeAddr != "" && childNodeAddr != "" && privateKeyRaw != "") var sender ethgo.Key // if the privateKey flag is set then recover the key, otherwise recover the key diff --git a/command/benchmark/benchmark.go b/command/benchmark/benchmark.go index 6240af2bb1..0f806bf997 100644 --- a/command/benchmark/benchmark.go +++ b/command/benchmark/benchmark.go @@ -2,6 +2,7 @@ package benchmark import ( "bytes" + "errors" "fmt" "testing" @@ -14,12 +15,14 @@ const ( rootJSONRPCFlag = "rootJSONRPC" childJSONRPCFlag = "childJSONRPC" privateKeyFlag = "privateKey" + startClusterFlag = "startCluster" ) type benchmarkParams struct { rootJSONRPC string childJSONRPC string privateKey string + startCluster bool } type benchmarkResult struct { @@ -36,6 +39,7 @@ func GetCommand() *cobra.Command { Use: "benchmark-test", Short: "Run benchmark tests", Example: getCmdExample(), + PreRunE: preRunCommand, Run: runCommand, } @@ -55,6 +59,7 @@ func runCommand(cmd *cobra.Command, _ []string) { benchmark.RootJSONRPC = ¶ms.rootJSONRPC benchmark.ChildJSONRPC = ¶ms.childJSONRPC benchmark.PrivateKey = ¶ms.privateKey + benchmark.StartCluster = ¶ms.startCluster // set up environment, get test cases and clean up fn testCases, cleanUpFn := benchmark.RootChildSendTxSetUp(&testing.B{}) @@ -94,10 +99,15 @@ func setFlags(cmd *cobra.Command) { "", "private key that will be used to send tx (it is expected that this address has enough funds)", ) + cmd.Flags().BoolVar( + ¶ms.startCluster, + startClusterFlag, + false, + "startCluster tells if the local cluster should be started", + ) - _ = cmd.MarkFlagRequired(rootJSONRPCFlag) - _ = cmd.MarkFlagRequired(childJSONRPCFlag) - _ = cmd.MarkFlagRequired(privateKeyFlag) + cmd.MarkFlagsMutuallyExclusive(startClusterFlag, rootJSONRPCFlag) + cmd.MarkFlagsMutuallyExclusive(startClusterFlag, childJSONRPCFlag) } func (br *benchmarkResult) GetOutput() string { @@ -113,3 +123,13 @@ func getCmdExample() string { "--childJSONRPC=\"http://127.0.0.1:12001\"", "--rootJSONRPC=\"http://127.0.0.1:8545\"", "--privateKey=\"aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d\"") } + +func preRunCommand(_ *cobra.Command, _ []string) error { + if !params.startCluster { + if params.rootJSONRPC == "" || params.childJSONRPC == "" || params.privateKey == "" { + return errors.New("if startCluster is not set then rootJSONRPC, childJSONRPC and privateKey must be set") + } + } + + return nil +}