From 7b0908c7d4ebd74ccacb846d994c28d62851d433 Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Mon, 29 Aug 2022 17:26:33 +0200 Subject: [PATCH 1/4] feat: remove redundant app code-base from 2 test-cases Move the creation of a validator to the common dir Adjusted the init-validator case to accept the pers-peers param Move home path to app struct to eliminate passing home var to all methods --- compositions/cluster-k8s/gen-validators.toml | 3 + compositions/local-docker/gen-validators.toml | 3 + manifest.toml | 2 + testkit/appkit/app.go | 51 ++-- tests/app-sync/run_seeds.go | 6 +- tests/app-sync/run_validator.go | 88 +++++++ tests/common/doc.go | 15 ++ .../run_validators.go => common/validator.go} | 196 ++++++--------- tests/node-sync/run_app.go | 230 +----------------- tests/node-sync/run_light.go | 1 - 10 files changed, 215 insertions(+), 380 deletions(-) create mode 100644 tests/app-sync/run_validator.go create mode 100644 tests/common/doc.go rename tests/{app-sync/run_validators.go => common/validator.go} (55%) diff --git a/compositions/cluster-k8s/gen-validators.toml b/compositions/cluster-k8s/gen-validators.toml index 4fb45e6f..627ef6bf 100644 --- a/compositions/cluster-k8s/gen-validators.toml +++ b/compositions/cluster-k8s/gen-validators.toml @@ -10,6 +10,9 @@ runner = "cluster:k8s" disable_metrics = false +[global.run.test_params] + persistent-peers = "10" + [[groups]] id = "validators" builder = "docker:generic" diff --git a/compositions/local-docker/gen-validators.toml b/compositions/local-docker/gen-validators.toml index 04ff7533..e2c9d12f 100644 --- a/compositions/local-docker/gen-validators.toml +++ b/compositions/local-docker/gen-validators.toml @@ -10,6 +10,9 @@ runner = "local:docker" disable_metrics = false +[global.run.test_params] + persistent-peers = "3" + [[groups]] id = "validators" builder = "docker:generic" diff --git a/manifest.toml b/manifest.toml index 6f9166fa..e94f8cd7 100644 --- a/manifest.toml +++ b/manifest.toml @@ -19,6 +19,8 @@ enabled = true [[testcases]] name = "init-val" instances = { min = 1, max = 100, default = 3 } + [testcases.params] + persistent-peers = { type = "int", default = 3} [[testcases]] name = "node-sync" diff --git a/testkit/appkit/app.go b/testkit/appkit/app.go index 542c2dca..318a8986 100644 --- a/testkit/appkit/app.go +++ b/testkit/appkit/app.go @@ -26,13 +26,17 @@ type ValidatorNode struct { } type AppKit struct { - m sync.Mutex - Cmd *cobra.Command + m sync.Mutex + home string + AccountAddress string + ChainId string + Cmd *cobra.Command } -func New() *AppKit { +func New(path string) *AppKit { return &AppKit{ - Cmd: appcmd.NewRootCmd(), + home: path, + Cmd: appcmd.NewRootCmd(), } } @@ -64,50 +68,55 @@ func (ak *AppKit) execCmd(args []string) (output string, err error) { return output, nil } -func (ak *AppKit) InitChain(moniker string, chainId string, home string) (string, error) { - return ak.execCmd([]string{"init", moniker, "--chain-id", chainId, "--home", home}) +func (ak *AppKit) GetHomePath() string { + return ak.home } -func (ak *AppKit) CreateKey(name string, krbackend string, home string) (string, error) { - _, err := ak.execCmd([]string{"keys", "add", name, "--keyring-backend", krbackend, "--home", home, "--keyring-dir", home}) +func (ak *AppKit) InitChain(moniker string, chainId string) (string, error) { + ak.AccountAddress = chainId + return ak.execCmd([]string{"init", moniker, "--chain-id", chainId, "--home", ak.home}) +} + +func (ak *AppKit) CreateKey(name string, krbackend string, krpath string) (string, error) { + _, err := ak.execCmd([]string{"keys", "add", name, "--keyring-backend", krbackend, "--home", ak.home, "--keyring-dir", krpath}) if err != nil { return "", err } - return ak.execCmd([]string{"keys", "show", name, "-a", "--keyring-backend", krbackend, "--home", home, "--keyring-dir", home}) + return ak.execCmd([]string{"keys", "show", name, "-a", "--keyring-backend", krbackend, "--home", ak.home, "--keyring-dir", krpath}) } -func (ak *AppKit) AddGenAccount(addr string, amount string, home string) (string, error) { - return ak.execCmd([]string{"add-genesis-account", addr, amount, "--home", home}) +func (ak *AppKit) AddGenAccount(addr string, amount string) (string, error) { + return ak.execCmd([]string{"add-genesis-account", addr, amount, "--home", ak.home}) } -func (ak *AppKit) SignGenTx(accName string, amount string, krbackend string, chainId string, home string) (string, error) { - return ak.execCmd([]string{"gentx", accName, amount, "--keyring-backend", krbackend, "--chain-id", chainId, "--home", home, "--keyring-dir", home}) +func (ak *AppKit) SignGenTx(accName string, amount string, krbackend string, chainId string, krpath string) (string, error) { + return ak.execCmd([]string{"gentx", accName, amount, "--keyring-backend", krbackend, "--chain-id", chainId, "--home", ak.home, "--keyring-dir", krpath}) } -func (ak *AppKit) CollectGenTxs(home string) (string, error) { - return ak.execCmd([]string{"collect-gentxs", "--home", home}) +func (ak *AppKit) CollectGenTxs() (string, error) { + return ak.execCmd([]string{"collect-gentxs", "--home", ak.home}) } -func (ak *AppKit) GetNodeId(home string) (string, error) { - return ak.execCmd([]string{"tendermint", "show-node-id", "--home", home}) +func (ak *AppKit) GetNodeId() (string, error) { + return ak.execCmd([]string{"tendermint", "show-node-id", "--home", ak.home}) } -func (ak *AppKit) StartNode(home, loglvl string) error { +func (ak *AppKit) StartNode(loglvl string) error { ak.Cmd.ResetFlags() ak.Cmd.SetErr(os.Stdout) - ak.Cmd.SetArgs([]string{"start", "--home", home, "--log_level", loglvl, "--log_format", "plain"}) + ak.Cmd.SetArgs([]string{"start", "--home", ak.home, "--log_level", loglvl, "--log_format", "plain"}) return svrcmd.Execute(ak.Cmd, appcmd.EnvPrefix, app.DefaultNodeHome) } -func (ak *AppKit) PayForData(accAdr string, msg int, krbackend, chainId, home string) error { +func (ak *AppKit) PayForData(accAdr string, msg int, krbackend, chainId, krpath string) error { ak.Cmd.ResetFlags() ak.Cmd.SetArgs([]string{ "tx", "payment", "payForData", fmt.Sprint(msg), "--from", accAdr, "-b", "block", "-y", "--gas", "1000000000", "--fees", "100000000000utia", - "--keyring-backend", krbackend, "--chain-id", chainId, "--home", home, "--keyring-dir", home, + "--keyring-backend", krbackend, "--chain-id", chainId, "--home", ak.home, "--keyring-dir", krpath, }) return svrcmd.Execute(ak.Cmd, appcmd.EnvPrefix, app.DefaultNodeHome) diff --git a/tests/app-sync/run_seeds.go b/tests/app-sync/run_seeds.go index 2d322f02..f710215d 100644 --- a/tests/app-sync/run_seeds.go +++ b/tests/app-sync/run_seeds.go @@ -53,9 +53,9 @@ func RunSeed(runenv *runtime.RunEnv, initCtx *run.InitContext) error { home := fmt.Sprintf("/.celestia-app-%d", initCtx.GroupSeq) runenv.RecordMessage(home) - cmd := appkit.New() + cmd := appkit.New(home) - nodeId, err := cmd.GetNodeId(home) + nodeId, err := cmd.GetNodeId() if err != nil { return err } @@ -95,7 +95,7 @@ func RunSeed(runenv *runtime.RunEnv, initCtx *run.InitContext) error { return err } - go cmd.StartNode(home, "info") + go cmd.StartNode("info") // // wait for a new block to be produced time.Sleep(1 * time.Minute) diff --git a/tests/app-sync/run_validator.go b/tests/app-sync/run_validator.go new file mode 100644 index 00000000..4385a12d --- /dev/null +++ b/tests/app-sync/run_validator.go @@ -0,0 +1,88 @@ +package appsync + +import ( + "context" + "net" + "time" + + "github.com/celestiaorg/test-infra/testkit/appkit" + "github.com/celestiaorg/test-infra/tests/common" + + "github.com/testground/sdk-go/network" + "github.com/testground/sdk-go/run" + "github.com/testground/sdk-go/runtime" +) + +func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*4) + defer cancel() + + syncclient := initCtx.SyncClient + netclient := network.NewClient(syncclient, runenv) + + netclient.MustWaitNetworkInitialized(ctx) + + config := network.Config{ + Network: "default", + Enable: true, + Default: network.LinkShape{ + Bandwidth: 5 << 26, // 320Mib + }, + CallbackState: "network-configured", + RoutingPolicy: network.AllowAll, + } + + config.IPv4 = runenv.TestSubnet + + // using the assigned `GlobalSequencer` id per each of instance + // to fill in the last 2 octects of the new IP address for the instance + ipC := byte((initCtx.GlobalSeq >> 8) + 1) + ipD := byte(initCtx.GlobalSeq) + config.IPv4.IP = append(config.IPv4.IP[0:2:2], ipC, ipD) + + err := netclient.ConfigureNetwork(ctx, &config) + if err != nil { + return err + } + + appcmd, err := common.BuildValidator(ctx, runenv, initCtx) + if err != nil { + return err + } + + runenv.RecordMessage("starting........") + go appcmd.StartNode("info") + + // // wait for a new block to be produced + time.Sleep(1 * time.Minute) + + // If all 3 validators submit pfd - it will take too long to produce a new block + for i := 0; i < 10; i++ { + runenv.RecordMessage("Submitting PFD with 90k bytes random data") + err = appcmd.PayForData( + appcmd.AccountAddress, + 50000, + "test", + appcmd.ChainId, + appcmd.GetHomePath(), + ) + + if err != nil { + runenv.RecordFailure(err) + return err + } + go func() { + s, err := appkit.GetLatestsBlockSize(net.ParseIP("127.0.0.1")) + if err != nil { + runenv.RecordMessage("err in last size call, %s", err.Error()) + } + + runenv.RecordMessage("latest size on iteration %d of the block is - %d", i, s) + }() + } + + time.Sleep(30 * time.Second) + runenv.RecordSuccess() + + return nil +} diff --git a/tests/common/doc.go b/tests/common/doc.go new file mode 100644 index 00000000..9512566b --- /dev/null +++ b/tests/common/doc.go @@ -0,0 +1,15 @@ +/* +Package common is a helper around redundant creation of App or Node part + +In order to eliminate the boilerplate code of creating a validators' set, +please use `common.BuildValidator`. This Func does: +- InitChain +- Add-Gen-Account +- Collect-GenTxs +- Add-Persistent-Peers +In addition, the func returns initialized cobra cmd, so you can continue +operating with the validator + +appcmd, err := common.BuildValidator(ctx, runenv, initCtx) +appcmd.PayForData(...) +*/ \ No newline at end of file diff --git a/tests/app-sync/run_validators.go b/tests/common/validator.go similarity index 55% rename from tests/app-sync/run_validators.go rename to tests/common/validator.go index 785be601..5c3edd7b 100644 --- a/tests/app-sync/run_validators.go +++ b/tests/common/validator.go @@ -1,4 +1,4 @@ -package appsync +package common import ( "context" @@ -8,62 +8,31 @@ import ( "os" "path/filepath" "strings" - "time" "github.com/celestiaorg/test-infra/testkit" "github.com/celestiaorg/test-infra/testkit/appkit" - - "github.com/testground/sdk-go/network" "github.com/testground/sdk-go/run" "github.com/testground/sdk-go/runtime" ) -func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { - ctx, cancel := context.WithTimeout(context.Background(), time.Minute*4) - defer cancel() - +func BuildValidator(ctx context.Context, runenv *runtime.RunEnv, initCtx *run.InitContext) (*appkit.AppKit, error) { syncclient := initCtx.SyncClient - netclient := network.NewClient(syncclient, runenv) - - netclient.MustWaitNetworkInitialized(ctx) - - config := network.Config{ - Network: "default", - Enable: true, - Default: network.LinkShape{ - Bandwidth: 5 << 26, // 320Mib - }, - CallbackState: "network-configured", - RoutingPolicy: network.AllowAll, - } - - config.IPv4 = runenv.TestSubnet - - // using the assigned `GlobalSequencer` id per each of instance - // to fill in the last 2 octects of the new IP address for the instance - ipC := byte((initCtx.GlobalSeq >> 8) + 1) - ipD := byte(initCtx.GlobalSeq) - config.IPv4.IP = append(config.IPv4.IP[0:2:2], ipC, ipD) - - err := netclient.ConfigureNetwork(ctx, &config) - if err != nil { - return err - } home := fmt.Sprintf("/.celestia-app-%d", initCtx.GlobalSeq) runenv.RecordMessage(home) - cmd := appkit.New() + cmd := appkit.New(home) keyringName := fmt.Sprintf("keyName-%d", initCtx.GlobalSeq) accAddr, err := cmd.CreateKey(keyringName, "test", home) if err != nil { - return err + return nil, err } + cmd.AccountAddress = accAddr _, err = syncclient.Publish(ctx, testkit.AccountAddressTopic, accAddr) if err != nil { - return err + return nil, err } // Here we assign the first instance to be the orchestrator role @@ -76,11 +45,11 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { accAddrCh := make(chan string) _, err = syncclient.Subscribe(ctx, testkit.AccountAddressTopic, accAddrCh) if err != nil { - return err + return nil, err } var accounts []string - for i := 0; i < runenv.TestGroupInstanceCount; i++ { + for i := 0; i < runenv.IntParam("validator"); i++ { addr := <-accAddrCh runenv.RecordMessage("Received address: %s", addr) accounts = append(accounts, addr) @@ -88,31 +57,31 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { moniker := fmt.Sprintf("validator-%d", initCtx.GlobalSeq) - _, err = cmd.InitChain(moniker, chainId, home) + _, err = cmd.InitChain(moniker, chainId) if err != nil { - return err + return nil, err } for _, v := range accounts { - _, err := cmd.AddGenAccount(v, "100000000000000000utia", home) + _, err := cmd.AddGenAccount(v, "1000000000000000utia") if err != nil { - return err + return nil, err } } gen, err := os.Open(fmt.Sprintf("%s/config/genesis.json", home)) if err != nil { - return err + return nil, err } bt, err := ioutil.ReadAll(gen) if err != nil { - return err + return nil, err } _, err = syncclient.Publish(ctx, testkit.InitialGenenesisTopic, string(bt)) if err != nil { - return err + return nil, err } runenv.RecordMessage("Orchestrator has sent initial genesis with accounts") @@ -120,17 +89,17 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { initGenCh := make(chan string) sub, err := syncclient.Subscribe(ctx, testkit.InitialGenenesisTopic, initGenCh) if err != nil { - return err + return nil, err } select { case err = <-sub.Done(): if err != nil { - return err + return nil, err } case initGen := <-initGenCh: err = os.WriteFile(fmt.Sprintf("%s/config/genesis.json", home), []byte(initGen), 0777) if err != nil { - return err + return nil, err } } runenv.RecordMessage("Validator has received the initial genesis") @@ -138,28 +107,28 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { _, err = cmd.SignGenTx(keyringName, "5000000000utia", "test", chainId, home) if err != nil { - return err + return nil, err } fs, err := os.ReadDir(fmt.Sprintf("%s/config/gentx", home)) if err != nil { - return err + return nil, err } // slice is needed because of auto-gen gentx-name for _, f := range fs { gentx, err := os.Open(fmt.Sprintf("%s/config/gentx/%s", home, f.Name())) if err != nil { - return err + return nil, err } bt, err := ioutil.ReadAll(gentx) if err != nil { - return err + return nil, err } _, err = syncclient.Publish(ctx, testkit.GenesisTxTopic, string(bt)) if err != nil { - return err + return nil, err } } @@ -167,36 +136,50 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { genTxCh := make(chan string) sub, err := syncclient.Subscribe(ctx, testkit.GenesisTxTopic, genTxCh) if err != nil { - return err + return nil, err } - for i := 0; i < runenv.TestInstanceCount; i++ { + for i := 0; i < runenv.IntParam("validator"); i++ { select { case err = <-sub.Done(): if err != nil { - return err + return nil, err } case genTx := <-genTxCh: if !strings.Contains(genTx, accAddr) { err := ioutil.WriteFile(fmt.Sprintf("%s/config/gentx/%d.json", home, i), []byte(genTx), 0777) if err != nil { - return err + return nil, err } } } } - _, err = cmd.CollectGenTxs(home) + _, err = cmd.CollectGenTxs() if err != nil { - return err + return nil, err } configPath := filepath.Join(home, "config", "config.toml") + err = appkit.ChangeRPCServerAddress(configPath, net.ParseIP("0.0.0.0")) + if err != nil { + return nil, err + } - if initCtx.GlobalSeq <= 10 { - nodeId, err := cmd.GetNodeId(home) + err = changeConfig(configPath) + if err != nil { + return nil, err + } + + ip, err := initCtx.NetClient.GetDataNetworkIP() + if err != nil { + return nil, err + } + + if initCtx.GlobalSeq <= int64(runenv.IntParam("persistent-peers")) { + nodeId, err := cmd.GetNodeId() if err != nil { - return err + return nil, err } _, err = syncclient.Publish( @@ -204,83 +187,40 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { testkit.ValidatorPeerTopic, &appkit.ValidatorNode{ PubKey: nodeId, - IP: config.IPv4.IP}, + IP: ip}, ) if err != nil { - return err - } - } else { - valCh := make(chan *appkit.ValidatorNode) - sub, err = syncclient.Subscribe(ctx, testkit.ValidatorPeerTopic, valCh) - if err != nil { - return err - } - - var persPeers []string - for i := 0; i < 10; i++ { - select { - case err = <-sub.Done(): - if err != nil { - return err - } - case val := <-valCh: - runenv.RecordMessage("Validator Received: %s, %s", val.IP, val.PubKey) - if !val.IP.Equal(config.IPv4.IP) { - persPeers = append(persPeers, fmt.Sprintf("%s@%s", val.PubKey, val.IP.To4().String())) - } - - err = appkit.AddPersistentPeers(configPath, persPeers) - if err != nil { - return err - } - } + return nil, err } } - err = appkit.ChangeConfigParam(configPath, "p2p", "external_address", fmt.Sprintf("%s:26656", config.IPv4.IP.To4().String())) + valCh := make(chan *appkit.ValidatorNode) + sub, err = syncclient.Subscribe(ctx, testkit.ValidatorPeerTopic, valCh) if err != nil { - return err + return nil, err } - runenv.RecordMessage("starting........") - err = changeConfig(configPath) - if err != nil { - return err - } - go cmd.StartNode(home, "info") - - // // wait for a new block to be produced - time.Sleep(1 * time.Minute) - - // If all 3 validators submit pfd - it will take too long to produce a new block - for i := 0; i < 10; i++ { - runenv.RecordMessage("Submitting PFD with 90k bytes random data") - err = cmd.PayForData( - accAddr, - 50000, - "test", - chainId, - home, - ) - - if err != nil { - runenv.RecordFailure(err) - return err - } - go func() { - s, err := appkit.GetLatestsBlockSize(net.ParseIP("127.0.0.1")) + var persPeers []string + for i := 0; i < runenv.IntParam("validator"); i++ { + select { + case err = <-sub.Done(): if err != nil { - runenv.RecordMessage("err in last size call, %s", err.Error()) + return nil, err + } + case val := <-valCh: + runenv.RecordMessage("Validator Received: %s, %s", val.IP, val.PubKey) + if !val.IP.Equal(ip) { + persPeers = append(persPeers, fmt.Sprintf("%s@%s", val.PubKey, val.IP.To4().String())) } - runenv.RecordMessage("latest size on iteration %d of the block is - %d", i, s) - }() + err = appkit.AddPersistentPeers(configPath, persPeers) + if err != nil { + return nil, err + } + } } - time.Sleep(30 * time.Second) - runenv.RecordSuccess() - - return nil + return cmd, nil } func changeConfig(path string) error { @@ -289,7 +229,7 @@ func changeConfig(path string) error { "timeout_propose": "3s", "timeout_prevote": "1s", "timeout_precommit": "1s", - "timeout_commit": "30s", + "timeout_commit": "25s", }, "rpc": { "timeout_broadcast_tx_commit": "90s", diff --git a/tests/node-sync/run_app.go b/tests/node-sync/run_app.go index f0929daf..5ceed9e8 100644 --- a/tests/node-sync/run_app.go +++ b/tests/node-sync/run_app.go @@ -2,16 +2,10 @@ package nodesync import ( "context" - "fmt" - "io/ioutil" - "net" - "os" - "path/filepath" - "strings" "time" "github.com/celestiaorg/test-infra/testkit" - "github.com/celestiaorg/test-infra/testkit/appkit" + "github.com/celestiaorg/test-infra/tests/common" "github.com/testground/sdk-go/network" "github.com/testground/sdk-go/run" "github.com/testground/sdk-go/runtime" @@ -49,205 +43,13 @@ func RunAppValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { return err } - home := fmt.Sprintf("/.celestia-app-%d", initCtx.GlobalSeq) - runenv.RecordMessage(home) - - cmd := appkit.New() - - keyringName := fmt.Sprintf("keyName-%d", initCtx.GlobalSeq) - accAddr, err := cmd.CreateKey(keyringName, "test", home) - if err != nil { - return err - } - - _, err = syncclient.Publish(ctx, testkit.AccountAddressTopic, accAddr) - if err != nil { - return err - } - - // Here we assign the first instance to be the orchestrator role - // - // Orchestrator is receiving all accounts by subscription, to then - // execute the `add-genesis-account` command and send back to the rest - // of the validators to set the initial genesis.json - const chainId string = "tia-test" - if initCtx.GlobalSeq == 1 { - accAddrCh := make(chan string) - _, err = syncclient.Subscribe(ctx, testkit.AccountAddressTopic, accAddrCh) - if err != nil { - return err - } - - var accounts []string - for i := 0; i < runenv.IntParam("validator"); i++ { - addr := <-accAddrCh - runenv.RecordMessage("Received address: %s", addr) - accounts = append(accounts, addr) - } - - moniker := fmt.Sprintf("validator-%d", initCtx.GlobalSeq) - - _, err = cmd.InitChain(moniker, chainId, home) - if err != nil { - return err - } - - for _, v := range accounts { - _, err := cmd.AddGenAccount(v, "1000000000000000utia", home) - if err != nil { - return err - } - } - - gen, err := os.Open(fmt.Sprintf("%s/config/genesis.json", home)) - if err != nil { - return err - } - - bt, err := ioutil.ReadAll(gen) - if err != nil { - return err - } - - _, err = syncclient.Publish(ctx, testkit.InitialGenenesisTopic, string(bt)) - if err != nil { - return err - } - - runenv.RecordMessage("Orchestrator has sent initial genesis with accounts") - } else { - initGenCh := make(chan string) - sub, err := syncclient.Subscribe(ctx, testkit.InitialGenenesisTopic, initGenCh) - if err != nil { - return err - } - select { - case err = <-sub.Done(): - if err != nil { - return err - } - case initGen := <-initGenCh: - err = os.WriteFile(fmt.Sprintf("%s/config/genesis.json", home), []byte(initGen), 0777) - if err != nil { - return err - } - } - runenv.RecordMessage("Validator has received the initial genesis") - } - - _, err = cmd.SignGenTx(keyringName, "5000000000utia", "test", chainId, home) - if err != nil { - return err - } - - fs, err := os.ReadDir(fmt.Sprintf("%s/config/gentx", home)) - if err != nil { - return err - } - // slice is needed because of auto-gen gentx-name - for _, f := range fs { - gentx, err := os.Open(fmt.Sprintf("%s/config/gentx/%s", home, f.Name())) - if err != nil { - return err - } - - bt, err := ioutil.ReadAll(gentx) - if err != nil { - return err - } - - _, err = syncclient.Publish(ctx, testkit.GenesisTxTopic, string(bt)) - if err != nil { - return err - } - - } - - genTxCh := make(chan string) - sub, err := syncclient.Subscribe(ctx, testkit.GenesisTxTopic, genTxCh) - if err != nil { - return err - } - - for i := 0; i < runenv.IntParam("validator"); i++ { - select { - case err = <-sub.Done(): - if err != nil { - return err - } - case genTx := <-genTxCh: - if !strings.Contains(genTx, accAddr) { - err := ioutil.WriteFile(fmt.Sprintf("%s/config/gentx/%d.json", home, i), []byte(genTx), 0777) - if err != nil { - return err - } - } - } - } - - _, err = cmd.CollectGenTxs(home) - if err != nil { - return err - } - - configPath := filepath.Join(home, "config", "config.toml") - err = appkit.ChangeRPCServerAddress(configPath, net.ParseIP("0.0.0.0")) + appcmd, err := common.BuildValidator(ctx, runenv, initCtx) if err != nil { return err } runenv.RecordMessage("starting........") - - err = changeConfig(configPath) - if err != nil { - return err - } - - if initCtx.GlobalSeq <= int64(runenv.IntParam("persistent-peers")) { - nodeId, err := cmd.GetNodeId(home) - if err != nil { - return err - } - - _, err = syncclient.Publish( - ctx, - testkit.ValidatorPeerTopic, - &appkit.ValidatorNode{ - PubKey: nodeId, - IP: config.IPv4.IP}, - ) - if err != nil { - return err - } - } - - valCh := make(chan *appkit.ValidatorNode) - sub, err = syncclient.Subscribe(ctx, testkit.ValidatorPeerTopic, valCh) - if err != nil { - return err - } - - var persPeers []string - for i := 0; i < runenv.IntParam("validator"); i++ { - select { - case err = <-sub.Done(): - if err != nil { - return err - } - case val := <-valCh: - runenv.RecordMessage("Validator Received: %s, %s", val.IP, val.PubKey) - if !val.IP.Equal(config.IPv4.IP) { - persPeers = append(persPeers, fmt.Sprintf("%s@%s", val.PubKey, val.IP.To4().String())) - } - - err = appkit.AddPersistentPeers(configPath, persPeers) - if err != nil { - return err - } - } - } - - go cmd.StartNode(home, "info") + go appcmd.StartNode("info") // wait for a new block to be produced // RPC is also being initialized... @@ -278,29 +80,3 @@ func RunAppValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { return nil } - -func changeConfig(path string) error { - cfg := map[string]map[string]string{ - "consensus": { - "timeout_propose": "3s", - "timeout_prevote": "1s", - "timeout_precommit": "1s", - "timeout_commit": "25s", - }, - "rpc": { - "timeout_broadcast_tx_commit": "90s", - "max_body_bytes": "1000000", - "max_header_bytes": "1048576", - }, - } - - for i, j := range cfg { - for k, v := range j { - err := appkit.ChangeConfigParam(path, i, k, v) - if err != nil { - return err - } - } - } - return nil -} diff --git a/tests/node-sync/run_light.go b/tests/node-sync/run_light.go index ec4b4180..b25ae816 100644 --- a/tests/node-sync/run_light.go +++ b/tests/node-sync/run_light.go @@ -31,7 +31,6 @@ func RunLightNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error { Network: "default", Enable: true, Default: network.LinkShape{ - // Latency: 100 * time.Millisecond, Bandwidth: 5 << 26, // 320Mib }, CallbackState: "network-configured", From 59400f2c81bee7ef25c7b813bdbada4d1fa9edbe Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Mon, 29 Aug 2022 17:34:02 +0200 Subject: [PATCH 2/4] fix: add package common to doc.go file --- tests/common/doc.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/common/doc.go b/tests/common/doc.go index 9512566b..3fefe721 100644 --- a/tests/common/doc.go +++ b/tests/common/doc.go @@ -12,4 +12,5 @@ operating with the validator appcmd, err := common.BuildValidator(ctx, runenv, initCtx) appcmd.PayForData(...) -*/ \ No newline at end of file +*/ +package common From a3172d7685fd23f1fa50b0de71b6b66d8d295337 Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Tue, 30 Aug 2022 11:44:05 +0200 Subject: [PATCH 3/4] fix: copy i instead of capturing Co-authored-by: Viacheslav Gonkivskyi --- tests/app-sync/run_validator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/app-sync/run_validator.go b/tests/app-sync/run_validator.go index 4385a12d..c6fa35fd 100644 --- a/tests/app-sync/run_validator.go +++ b/tests/app-sync/run_validator.go @@ -71,14 +71,14 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { runenv.RecordFailure(err) return err } - go func() { + go func(i int) { s, err := appkit.GetLatestsBlockSize(net.ParseIP("127.0.0.1")) if err != nil { runenv.RecordMessage("err in last size call, %s", err.Error()) } runenv.RecordMessage("latest size on iteration %d of the block is - %d", i, s) - }() + }(i) } time.Sleep(30 * time.Second) From 5c6ea3464260c45168fd437e5ff53d11e7b43a14 Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Mon, 29 Aug 2022 18:53:08 +0200 Subject: [PATCH 4/4] feat: adding latency/bandwidth configuration through .toml files This gives us more flexibility in test-case development Related to Test-Plan #001 --- compositions/cluster-k8s/gen-validators.toml | 2 ++ compositions/cluster-k8s/node-sync-15.toml | 2 ++ compositions/local-docker/gen-validators.toml | 2 ++ compositions/local-docker/node-sync.toml | 2 ++ manifest.toml | 4 ++++ tests/app-sync/run_seeds.go | 4 +++- tests/app-sync/run_validator.go | 3 ++- tests/common/doc.go | 10 +++++++++- tests/common/network.go | 18 ++++++++++++++++++ tests/node-sync/run_app.go | 3 ++- tests/node-sync/run_bridge.go | 4 +++- tests/node-sync/run_full.go | 5 +++-- tests/node-sync/run_light.go | 4 +++- 13 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 tests/common/network.go diff --git a/compositions/cluster-k8s/gen-validators.toml b/compositions/cluster-k8s/gen-validators.toml index 627ef6bf..a1c61791 100644 --- a/compositions/cluster-k8s/gen-validators.toml +++ b/compositions/cluster-k8s/gen-validators.toml @@ -11,6 +11,8 @@ disable_metrics = false [global.run.test_params] + latency = "0" + bandwidth = "320Mib" persistent-peers = "10" [[groups]] diff --git a/compositions/cluster-k8s/node-sync-15.toml b/compositions/cluster-k8s/node-sync-15.toml index 2c2e877b..f7380c69 100644 --- a/compositions/cluster-k8s/node-sync-15.toml +++ b/compositions/cluster-k8s/node-sync-15.toml @@ -11,6 +11,8 @@ disable_metrics = false [global.run.test_params] + latency = "0" + bandwidth = "320Mib" validator = "3" persistent-peers = "3" bridge = "4" diff --git a/compositions/local-docker/gen-validators.toml b/compositions/local-docker/gen-validators.toml index e2c9d12f..d7aa2ecf 100644 --- a/compositions/local-docker/gen-validators.toml +++ b/compositions/local-docker/gen-validators.toml @@ -11,6 +11,8 @@ disable_metrics = false [global.run.test_params] + latency = "0" + bandwidth = "320Mib" persistent-peers = "3" [[groups]] diff --git a/compositions/local-docker/node-sync.toml b/compositions/local-docker/node-sync.toml index 4e7e8639..7831c7e4 100644 --- a/compositions/local-docker/node-sync.toml +++ b/compositions/local-docker/node-sync.toml @@ -11,6 +11,8 @@ disable_metrics = false [global.run.test_params] + latency = "0" + bandwidth = "320Mib" validator = "3" persistent-peers = "3" bridge = "3" diff --git a/manifest.toml b/manifest.toml index e94f8cd7..4d3e607b 100644 --- a/manifest.toml +++ b/manifest.toml @@ -20,12 +20,16 @@ enabled = true name = "init-val" instances = { min = 1, max = 100, default = 3 } [testcases.params] + latency = { type = "int", default = 0} + bandwidth = { type = "string", default = "256Mib"} persistent-peers = { type = "int", default = 3} [[testcases]] name = "node-sync" instances = { min = 4, max = 100, default = 12 } [testcases.params] + latency = { type = "int", default = 0} + bandwidth = { type = "string", default = "256Mib"} validator = { type = "int", default = 3} persistent-peers = { type = "int", default = 3} bridge = { type = "int", default = 3} diff --git a/tests/app-sync/run_seeds.go b/tests/app-sync/run_seeds.go index f710215d..98272832 100644 --- a/tests/app-sync/run_seeds.go +++ b/tests/app-sync/run_seeds.go @@ -9,6 +9,7 @@ import ( "github.com/celestiaorg/test-infra/testkit" "github.com/celestiaorg/test-infra/testkit/appkit" + "github.com/celestiaorg/test-infra/tests/common" "github.com/testground/sdk-go/network" "github.com/testground/sdk-go/run" @@ -31,7 +32,8 @@ func RunSeed(runenv *runtime.RunEnv, initCtx *run.InitContext) error { Network: "default", Enable: true, Default: network.LinkShape{ - Bandwidth: 4 << 26, // 256Mib + Latency: time.Duration(runenv.IntParam("latency")), + Bandwidth: common.GetBandwidthValue(runenv.StringParam("bandwidth")), }, CallbackState: "network-configured", RoutingPolicy: network.AllowAll, diff --git a/tests/app-sync/run_validator.go b/tests/app-sync/run_validator.go index c6fa35fd..dcaf8a2b 100644 --- a/tests/app-sync/run_validator.go +++ b/tests/app-sync/run_validator.go @@ -26,7 +26,8 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { Network: "default", Enable: true, Default: network.LinkShape{ - Bandwidth: 5 << 26, // 320Mib + Latency: time.Duration(runenv.IntParam("latency")), + Bandwidth: common.GetBandwidthValue(runenv.StringParam("bandwidth")), }, CallbackState: "network-configured", RoutingPolicy: network.AllowAll, diff --git a/tests/common/doc.go b/tests/common/doc.go index 3fefe721..0d50e49f 100644 --- a/tests/common/doc.go +++ b/tests/common/doc.go @@ -1,5 +1,9 @@ /* -Package common is a helper around redundant creation of App or Node part +Package common is a helper around redundant creation of Network, App and Node part + +As testground doesn't have native support of uint64 conversion from .toml files, +GetBandwidthValue in network.go is used to adjust the bandwidth per instance +for each of the test-case In order to eliminate the boilerplate code of creating a validators' set, please use `common.BuildValidator`. This Func does: @@ -10,6 +14,10 @@ please use `common.BuildValidator`. This Func does: In addition, the func returns initialized cobra cmd, so you can continue operating with the validator +Default: network.LinkShape{ + Bandwidth: common.GetBandwidthValue(runenv.StringParam("bandwidth")), +} + appcmd, err := common.BuildValidator(ctx, runenv, initCtx) appcmd.PayForData(...) */ diff --git a/tests/common/network.go b/tests/common/network.go new file mode 100644 index 00000000..bc0be8bf --- /dev/null +++ b/tests/common/network.go @@ -0,0 +1,18 @@ +package common + +import "fmt" + +func GetBandwidthValue(v string) uint64 { + var bandwidthMap = map[string]uint64{ + "100Mib": 13 << 23, + "256Mib": 4 << 26, + "320Mib": 5 << 26, + "512Mib": 8 << 26, + "1024Mib": 16 << 26, + } + if val, ok := bandwidthMap[v]; ok { + return val + } else { + panic(fmt.Errorf("can't find any bandwidth value to given - %s", v)) + } +} diff --git a/tests/node-sync/run_app.go b/tests/node-sync/run_app.go index 5ceed9e8..94608eb7 100644 --- a/tests/node-sync/run_app.go +++ b/tests/node-sync/run_app.go @@ -24,7 +24,8 @@ func RunAppValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error { Network: "default", Enable: true, Default: network.LinkShape{ - Bandwidth: 5 << 26, // 320Mib + Latency: time.Duration(runenv.IntParam("latency")), + Bandwidth: common.GetBandwidthValue(runenv.StringParam("bandwidth")), }, CallbackState: "network-configured", RoutingPolicy: network.AllowAll, diff --git a/tests/node-sync/run_bridge.go b/tests/node-sync/run_bridge.go index f3cbc6c4..1f846ddc 100644 --- a/tests/node-sync/run_bridge.go +++ b/tests/node-sync/run_bridge.go @@ -9,6 +9,7 @@ import ( "github.com/celestiaorg/test-infra/testkit" "github.com/celestiaorg/test-infra/testkit/appkit" "github.com/celestiaorg/test-infra/testkit/nodekit" + "github.com/celestiaorg/test-infra/tests/common" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" "github.com/testground/sdk-go/network" @@ -35,7 +36,8 @@ func RunBridgeNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error { Network: "default", Enable: true, Default: network.LinkShape{ - Bandwidth: 5 << 26, // 320Mib + Latency: time.Duration(runenv.IntParam("latency")), + Bandwidth: common.GetBandwidthValue(runenv.StringParam("bandwidth")), }, CallbackState: "network-configured", RoutingPolicy: network.AllowAll, diff --git a/tests/node-sync/run_full.go b/tests/node-sync/run_full.go index 6c7a20e4..f3b13170 100644 --- a/tests/node-sync/run_full.go +++ b/tests/node-sync/run_full.go @@ -8,6 +8,7 @@ import ( "github.com/celestiaorg/celestia-node/node" "github.com/celestiaorg/test-infra/testkit" "github.com/celestiaorg/test-infra/testkit/nodekit" + "github.com/celestiaorg/test-infra/tests/common" "github.com/testground/sdk-go/network" "github.com/testground/sdk-go/run" "github.com/testground/sdk-go/runtime" @@ -31,8 +32,8 @@ func RunFullNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error { Network: "default", Enable: true, Default: network.LinkShape{ - // Latency: 100 * time.Millisecond, - Bandwidth: 5 << 26, // 320Mib + Latency: time.Duration(runenv.IntParam("latency")), + Bandwidth: common.GetBandwidthValue(runenv.StringParam("bandwidth")), }, CallbackState: "network-configured", RoutingPolicy: network.AllowAll, diff --git a/tests/node-sync/run_light.go b/tests/node-sync/run_light.go index b25ae816..0cf40794 100644 --- a/tests/node-sync/run_light.go +++ b/tests/node-sync/run_light.go @@ -8,6 +8,7 @@ import ( "github.com/celestiaorg/celestia-node/node" "github.com/celestiaorg/test-infra/testkit" "github.com/celestiaorg/test-infra/testkit/nodekit" + "github.com/celestiaorg/test-infra/tests/common" "github.com/testground/sdk-go/network" "github.com/testground/sdk-go/run" "github.com/testground/sdk-go/runtime" @@ -31,7 +32,8 @@ func RunLightNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error { Network: "default", Enable: true, Default: network.LinkShape{ - Bandwidth: 5 << 26, // 320Mib + Latency: time.Duration(runenv.IntParam("latency")), + Bandwidth: common.GetBandwidthValue(runenv.StringParam("bandwidth")), }, CallbackState: "network-configured", RoutingPolicy: network.AllowAll,