Skip to content

Commit

Permalink
sanity check command
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed May 16, 2024
1 parent 3428971 commit b5d7e81
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 20 deletions.
8 changes: 4 additions & 4 deletions command/loadtest/load_test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func preRunCommand(cmd *cobra.Command, _ []string) error {
func setFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(
&params.mnemonic,
mnemonicFlag,
MnemonicFlag,
"",
"the mnemonic used to generate and fund virtual users",
)
Expand Down Expand Up @@ -79,7 +79,7 @@ func setFlags(cmd *cobra.Command) {

cmd.Flags().DurationVar(
&params.receiptsTimeout,
receiptsTimeoutFlag,
ReceiptsTimeoutFlag,
30*time.Second,
"the timeout for waiting for transaction receipts",
)
Expand All @@ -93,7 +93,7 @@ func setFlags(cmd *cobra.Command) {

cmd.Flags().BoolVar(
&params.toJSON,
saveToJSONFlag,
SaveToJSONFlag,
false,
"saves results to JSON file",
)
Expand All @@ -112,7 +112,7 @@ func setFlags(cmd *cobra.Command) {
"size of a batch of transactions to send to rpc node",
)

_ = cmd.MarkFlagRequired(mnemonicFlag)
_ = cmd.MarkFlagRequired(MnemonicFlag)
_ = cmd.MarkFlagRequired(loadTestTypeFlag)
}

Expand Down
13 changes: 7 additions & 6 deletions command/loadtest/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import (
)

const (
mnemonicFlag = "mnemonic"
MnemonicFlag = "mnemonic"
SaveToJSONFlag = "to-json"
ReceiptsTimeoutFlag = "receipts-timeout"

loadTestTypeFlag = "type"
loadTestNameFlag = "name"

receiptsTimeoutFlag = "receipts-timeout"
txPoolTimeoutFlag = "txpool-timeout"
txPoolTimeoutFlag = "txpool-timeout"

vusFlag = "vus"
txsPerUserFlag = "txs-per-user"
dynamicTxsFlag = "dynamic"
batchSizeFlag = "batch-size"

saveToJSONFlag = "to-json"
waitForTxPoolToEmptyFlag = "wait-txpool"
)

var (
errNoMnemonicProvided = errors.New("no mnemonic provided")
ErrNoMnemonicProvided = errors.New("no mnemonic provided")
errNoLoadTestTypeProvided = errors.New("no load test type provided")
errUnsupportedLoadTestType = errors.New("unsupported load test type")
errInvalidVUs = errors.New("vus must be greater than 0")
Expand Down Expand Up @@ -53,7 +54,7 @@ type loadTestParams struct {

func (ltp *loadTestParams) validateFlags() error {
if ltp.mnemonic == "" {
return errNoMnemonicProvided
return ErrNoMnemonicProvided
}

if ltp.loadTestType == "" {
Expand Down
2 changes: 2 additions & 0 deletions command/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/monitor"
"github.com/0xPolygon/polygon-edge/command/peers"
"github.com/0xPolygon/polygon-edge/command/regenesis"
"github.com/0xPolygon/polygon-edge/command/sanitycheck"
"github.com/0xPolygon/polygon-edge/command/secrets"
polybftsecrets "github.com/0xPolygon/polygon-edge/command/secrets/init"
"github.com/0xPolygon/polygon-edge/command/server"
Expand Down Expand Up @@ -59,6 +60,7 @@ func (rc *RootCommand) registerSubCommands() {
mint.GetCommand(),
validator.GetCommand(),
loadtest.GetCommand(),
sanitycheck.GetCommand(),
)
}

Expand Down
43 changes: 43 additions & 0 deletions command/sanitycheck/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sanitycheck

import (
"errors"
"time"

"github.com/0xPolygon/polygon-edge/command/loadtest"
)

const (
epochSizeFlag = "epoch-size"
validatorKeysFlag = "validator-keys"
)

var (
errInvalidEpochSize = errors.New("epoch size must be greater than 0")
)

// sanityCheckParams holds the parameters for the sanity check command
type sanityCheckParams struct {
mnemonic string
jsonRPCAddress string

receiptsTimeout time.Duration

epochSize uint64
toJSON bool

validatorKeys []string
}

// validateFlags checks if the provided flags are valid
func (scp *sanityCheckParams) validateFlags() error {
if scp.mnemonic == "" {
return loadtest.ErrNoMnemonicProvided
}

if scp.epochSize == 0 {
return errInvalidEpochSize
}

return nil
}
99 changes: 99 additions & 0 deletions command/sanitycheck/sanity_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package sanitycheck

import (
"time"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/loadtest"
"github.com/0xPolygon/polygon-edge/loadtest/sanitycheck"
"github.com/spf13/cobra"
)

var (
params sanityCheckParams
)

func GetCommand() *cobra.Command {
loadTestCmd := &cobra.Command{
Use: "sanity-check",
Short: "Runs a sanity check tests on a specified network",
PreRunE: preRunCommand,
Run: runCommand,
}

helper.RegisterJSONRPCFlag(loadTestCmd)

setFlags(loadTestCmd)

return loadTestCmd
}

func preRunCommand(cmd *cobra.Command, _ []string) error {
params.jsonRPCAddress = helper.GetJSONRPCAddress(cmd)

return params.validateFlags()
}

func setFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(
&params.mnemonic,
loadtest.MnemonicFlag,
"",
"the mnemonic used to fund accounts if needed for the sanity check",
)

cmd.Flags().Uint64Var(
&params.epochSize,
epochSizeFlag,
10,
"epoch size on the network for which the sanity check is run",
)

cmd.Flags().DurationVar(
&params.receiptsTimeout,
loadtest.ReceiptsTimeoutFlag,
30*time.Second,
"the timeout for waiting for transaction receipts",
)

cmd.Flags().BoolVar(
&params.toJSON,
loadtest.SaveToJSONFlag,
false,
"saves results to JSON file",
)

cmd.Flags().StringSliceVar(
&params.validatorKeys,
validatorKeysFlag,
nil,
"private keys of validators on the network for which the sanity check is run",
)

_ = cmd.MarkFlagRequired(loadtest.MnemonicFlag)
}

func runCommand(cmd *cobra.Command, _ []string) {
outputter := command.InitializeOutputter(cmd)
defer outputter.WriteOutput()

sanityCheckRunner, err := sanitycheck.NewSanityCheckTestRunner(
&sanitycheck.SanityCheckTestConfig{
Mnemonic: params.mnemonic,
JSONRPCUrl: params.jsonRPCAddress,
ReceiptsTimeout: params.receiptsTimeout,
EpochSize: params.epochSize,
ValidatorKeys: params.validatorKeys,
ResultsToJSON: params.toJSON,
},
)

if err != nil {
outputter.SetError(err)

return
}

sanityCheckRunner.Run()
}
8 changes: 7 additions & 1 deletion loadtest/sanitycheck/base_sanity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,16 @@ func (t *BaseSanityCheckTest) waitForEpochEnding() (*types.Header, error) {
return currentBlock.Header, nil
}

currentBlock, err = t.client.GetBlockByNumber(jsonrpc.BlockNumber(currentBlock.Number()+1), false)
block, err := t.client.GetBlockByNumber(jsonrpc.BlockNumber(currentBlock.Number()+1), false)
if err != nil {
return nil, err
}

if block == nil {
continue
}

currentBlock = block
}
}
}
14 changes: 7 additions & 7 deletions loadtest/sanitycheck/sanity_check_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

// SanityCheckTestConfig represents the configuration for sanity check tests.
type SanityCheckTestConfig struct {
Mnemonnic string // Mnemonnic is the mnemonic phrase used for account funding.
Mnemonic string // Mnemonnic is the mnemonic phrase used for account funding.

JSONRPCUrl string // JSONRPCUrl is the URL of the JSON-RPC server.
ReceiptsTimeout time.Duration // ReceiptsTimeout is the timeout for waiting for transaction receipts.
Expand All @@ -41,7 +41,7 @@ type SanityCheckTestRunner struct {

// NewSanityCheckTestRunner creates a new SanityCheckTestRunner
func NewSanityCheckTestRunner(cfg *SanityCheckTestConfig) (*SanityCheckTestRunner, error) {
key, err := wallet.NewWalletFromMnemonic(cfg.Mnemonnic)
key, err := wallet.NewWalletFromMnemonic(cfg.Mnemonic)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -76,18 +76,18 @@ func NewSanityCheckTestRunner(cfg *SanityCheckTestConfig) (*SanityCheckTestRunne

// registerTests registers the sanity check tests that will be run by the SanityCheckTestRunner.
func registerTests(cfg *SanityCheckTestConfig, testAccountKey *crypto.ECDSAKey, client *jsonrpc.EthClient) ([]SanityCheckTest, error) {

Check failure on line 78 in loadtest/sanitycheck/sanity_check_runner.go

View workflow job for this annotation

GitHub Actions / Lint / Run Lint

line is 135 characters (lll)
// stakeTest, err := NewStakeTest(cfg, testAccountKey, client)
// if err != nil {
// return nil, err
// }
stakeTest, err := NewStakeTest(cfg, testAccountKey, client)
if err != nil {
return nil, err
}

unsstakeTest, err := NewUnstakeTest(cfg, testAccountKey, client)
if err != nil {
return nil, err
}

return []SanityCheckTest{
//stakeTest,
stakeTest,
unsstakeTest,
}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions loadtest/sanitycheck/sanity_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

func TestSanityCheck(t *testing.T) {
//t.Skip("this is only added for the sake of the example and running it in local")
t.Skip("this is only added for the sake of the example and running it in local")

config := &SanityCheckTestConfig{
Mnemonnic: "code code code code code code code code code code code quality",
Mnemonic: "code code code code code code code code code code code quality",
JSONRPCUrl: "http://localhost:10002",
ReceiptsTimeout: time.Minute,
ValidatorKeys: []string{"1a7626c5a1d89030f300ca5f63eecac3bae3e56f14033ea2d9ad471e7c93020e"},
Expand Down

0 comments on commit b5d7e81

Please sign in to comment.