From 98dab4ca97c6c14f1a8805740f3b8e475530699a Mon Sep 17 00:00:00 2001 From: Naohiro Yoshida Date: Sat, 16 Sep 2023 15:07:42 +0900 Subject: [PATCH] refactor testdata maker Signed-off-by: Naohiro Yoshida --- tool/testdata/README.md | 9 +++--- tool/testdata/internal/common.go | 20 ++++++------ tool/testdata/internal/histroy.go | 43 +++++++++++++++++++++---- tool/testdata/internal/update_client.go | 16 ++++----- 4 files changed, 59 insertions(+), 29 deletions(-) diff --git a/tool/testdata/README.md b/tool/testdata/README.md index a21bcb7..6b84849 100644 --- a/tool/testdata/README.md +++ b/tool/testdata/README.md @@ -2,33 +2,34 @@ Set bsc rpc addr. -```sh -export BSC_MAINNET_RPC_ADDR="rpc node" -``` - ### Misbehavior ```sh # src/client.rs test_success_submit_misbehavior go run main.go misbehavior success # src/client.rs test_error_submit_misbehavior +export BSC_RPC_ADDR="rpc node" go run main.go misbehavior error ``` ### Header ```sh # src/client.rs test_success_update_client_epoch +export BSC_RPC_ADDR="rpc node" go run main.go update success epoch # src/client.rs test_success_update_client_non_epoch +export BSC_RPC_ADDR="rpc node" go run main.go update success latest # src/client.rs test_error_update_client +export BSC_RPC_ADDR="rpc node" go run main.go update error ``` ### LCP integration data ```sh +export BSC_RPC_ADDR="rpc node" go run main.go history mainnet --num 240 go run main.go history testnet --num 240 ``` \ No newline at end of file diff --git a/tool/testdata/internal/common.go b/tool/testdata/internal/common.go index 54cbdc5..e69208e 100644 --- a/tool/testdata/internal/common.go +++ b/tool/testdata/internal/common.go @@ -10,23 +10,23 @@ import ( ) const ( - hdwMnemonic = "math razor capable expose worth grape metal sunset metal sudden usage scheme" - hdwPath = "m/44'/60'/0'/0/0" - ibcAddress = "0x702E40245797c5a2108A566b3CE2Bf14Bc6aF841" - localNetValidatorSize = 3 - mainNetValidatorSize = 21 - mainNetIbcAddress = "0x151f3951FA218cac426edFe078fA9e5C6dceA500" + hdwMnemonic = "math razor capable expose worth grape metal sunset metal sudden usage scheme" + hdwPath = "m/44'/60'/0'/0/0" + ibcAddress = "0x702E40245797c5a2108A566b3CE2Bf14Bc6aF841" + localNetValidatorSize = 3 + mainNetValidatorSize = 21 + mainAndTestNetIbcAddress = "0x151f3951FA218cac426edFe078fA9e5C6dceA500" ) func createRPCAddr() (string, error) { - rpcAddr, ok := viper.Get("BSC_MAINNET_RPC_ADDR").(string) + rpcAddr, ok := viper.Get("BSC_RPC_ADDR").(string) if !ok { - return "", fmt.Errorf("BSC_MAINNET_RPC_ADDR is required") + return "", fmt.Errorf("BSC_RPC_ADDR is required") } return rpcAddr, nil } -func createMainnetProver() (*module.Prover, core.Chain, error) { +func createProver() (*module.Prover, core.Chain, error) { rpcAddr, err := createRPCAddr() if err != nil { return nil, nil, err @@ -36,7 +36,7 @@ func createMainnetProver() (*module.Prover, core.Chain, error) { RpcAddr: rpcAddr, HdwMnemonic: hdwMnemonic, HdwPath: hdwPath, - IbcAddress: mainNetIbcAddress, + IbcAddress: mainAndTestNetIbcAddress, }) if err != nil { return nil, chain, err diff --git a/tool/testdata/internal/histroy.go b/tool/testdata/internal/histroy.go index 82b9bcd..6bda831 100644 --- a/tool/testdata/internal/histroy.go +++ b/tool/testdata/internal/histroy.go @@ -23,7 +23,7 @@ func (m *historyModule) mainnet() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { log.Printf("num = %d\n", num) - prover, chain, err := createMainnetProver() + prover, chain, err := createProver() if err != nil { return err } @@ -32,19 +32,48 @@ func (m *historyModule) mainnet() *cobra.Command { return err } - createdEpoch, err := m.outputMsgClient(prover, latest.GetRevisionHeight()-num) + createdEpoch, err := m.outputMsgClient(prover, latest.GetRevisionHeight()-num, "create_mainnet.json") if err != nil { return err } - return m.outputMsgUpdate(prover, createdEpoch, latest.GetRevisionHeight(), num) + return m.outputMsgUpdate(prover, createdEpoch, latest.GetRevisionHeight(), num, "update_mainnet.json") }, } cmd.Flags().Uint64Var(&num, "num", 240, "--num") return cmd } -func (m *historyModule) outputMsgUpdate(prover *module.Prover, createdEpoch, latest uint64, num uint64) error { +func (m *historyModule) testnet() *cobra.Command { + var num uint64 + cmd := &cobra.Command{ + Use: "testnet", + Short: "create many data testdata", + RunE: func(cmd *cobra.Command, args []string) error { + + log.Printf("num = %d\n", num) + prover, chain, err := createProver() + if err != nil { + return err + } + latest, err := chain.LatestHeight() + if err != nil { + return err + } + + createdEpoch, err := m.outputMsgClient(prover, latest.GetRevisionHeight()-num, "create_testnet.json") + if err != nil { + return err + } + + return m.outputMsgUpdate(prover, createdEpoch, latest.GetRevisionHeight(), num, "create_testnet.json") + }, + } + cmd.Flags().Uint64Var(&num, "num", 240, "--num") + return cmd +} + +func (m *historyModule) outputMsgUpdate(prover *module.Prover, createdEpoch, latest uint64, num uint64, path string) error { type updatingData struct { Header string `json:"header"` } @@ -89,10 +118,10 @@ func (m *historyModule) outputMsgUpdate(prover *module.Prover, createdEpoch, lat if err != nil { return err } - return os.WriteFile("update_mainnet.json", serialized, 777) + return os.WriteFile(path, serialized, 777) } -func (m *historyModule) outputMsgClient(prover *module.Prover, firstNumber uint64) (uint64, error) { +func (m *historyModule) outputMsgClient(prover *module.Prover, firstNumber uint64, path string) (uint64, error) { firstHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(firstNumber) if err != nil { return 0, err @@ -122,7 +151,7 @@ func (m *historyModule) outputMsgClient(prover *module.Prover, firstNumber uint6 return 0, err } epochs := firstHeader.GetHeight().GetRevisionHeight() / constant.BlocksPerEpoch - return (epochs - 1) * constant.BlocksPerEpoch, os.WriteFile("create_mainnet.json", serialized, 777) + return (epochs - 1) * constant.BlocksPerEpoch, os.WriteFile(path, serialized, 777) } func CreateHistoryClient() *cobra.Command { diff --git a/tool/testdata/internal/update_client.go b/tool/testdata/internal/update_client.go index f85f2e2..b1aedcd 100644 --- a/tool/testdata/internal/update_client.go +++ b/tool/testdata/internal/update_client.go @@ -24,7 +24,7 @@ func (m *updateClientModule) success() *cobra.Command { Use: "latest", Short: "for latest block", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createMainnetProver() + prover, chain, err := createProver() if err != nil { return err } @@ -32,14 +32,14 @@ func (m *updateClientModule) success() *cobra.Command { if err != nil { return err } - return m.printMainnetHeader(prover, latest.GetRevisionHeight()) + return m.printHeader(prover, latest.GetRevisionHeight()) }, }) cmd.AddCommand(&cobra.Command{ Use: "epoch", Short: "for epoch block", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createMainnetProver() + prover, chain, err := createProver() if err != nil { return err } @@ -48,7 +48,7 @@ func (m *updateClientModule) success() *cobra.Command { return err } epochCount := latest.GetRevisionHeight() / constant.BlocksPerEpoch - return m.printMainnetHeader(prover, epochCount*constant.BlocksPerEpoch+3) + return m.printHeader(prover, epochCount*constant.BlocksPerEpoch+3) }, }) return cmd @@ -59,7 +59,7 @@ func (m *updateClientModule) error() *cobra.Command { Use: "error", Short: "create updateClient testdata for error", RunE: func(cmd *cobra.Command, args []string) error { - prover, chain, err := createMainnetProver() + prover, chain, err := createProver() if err != nil { return err } @@ -99,8 +99,8 @@ func (m *updateClientModule) error() *cobra.Command { } } -func (m *updateClientModule) printMainnetHeader(prover *module.Prover, height uint64) error { - log.Println("printMainnetHeader latest=", height) +func (m *updateClientModule) printHeader(prover *module.Prover, height uint64) error { + log.Println("printHeader latest=", height) iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(height) if err != nil { return err @@ -114,7 +114,7 @@ func (m *updateClientModule) printMainnetHeader(prover *module.Prover, height ui return err } - account, err := header.Account(common.HexToAddress(mainNetIbcAddress)) + account, err := header.Account(common.HexToAddress(mainAndTestNetIbcAddress)) if err != nil { return err }