From f64d22db1fbedf7338dd7b2139c83aeb126d71e4 Mon Sep 17 00:00:00 2001 From: Naohiro Yoshida Date: Sat, 16 Sep 2023 14:57:01 +0900 Subject: [PATCH] refactor testdata maker Signed-off-by: Naohiro Yoshida --- .gitignore | 1 + tool/testdata/README.md | 6 + tool/testdata/internal/common.go | 52 +++++++ ...eate_create_client.go => create_client.go} | 22 +-- tool/testdata/internal/histroy.go | 136 ++++++++++++++++++ .../{create_misbehavior.go => misbehavior.go} | 62 +++----- ...eate_update_client.go => update_client.go} | 63 +++----- tool/testdata/main.go | 1 + 8 files changed, 247 insertions(+), 96 deletions(-) create mode 100644 tool/testdata/internal/common.go rename tool/testdata/internal/{create_create_client.go => create_client.go} (86%) create mode 100644 tool/testdata/internal/histroy.go rename tool/testdata/internal/{create_misbehavior.go => misbehavior.go} (81%) rename tool/testdata/internal/{create_update_client.go => update_client.go} (77%) diff --git a/.gitignore b/.gitignore index 912eacc..5d296f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ third_party +**/*.json \ No newline at end of file diff --git a/tool/testdata/README.md b/tool/testdata/README.md index 2fde6db..a21bcb7 100644 --- a/tool/testdata/README.md +++ b/tool/testdata/README.md @@ -25,4 +25,10 @@ go run main.go update success latest # src/client.rs test_error_update_client go run main.go update error +``` + +### LCP integration data +```sh +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 new file mode 100644 index 0000000..54cbdc5 --- /dev/null +++ b/tool/testdata/internal/common.go @@ -0,0 +1,52 @@ +package internal + +import ( + "fmt" + "github.com/datachainlab/ethereum-ibc-relay-chain/pkg/relay/ethereum" + "github.com/datachainlab/ibc-parlia-relay/module" + "github.com/hyperledger-labs/yui-relayer/core" + "github.com/spf13/viper" + "time" +) + +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" +) + +func createRPCAddr() (string, error) { + rpcAddr, ok := viper.Get("BSC_MAINNET_RPC_ADDR").(string) + if !ok { + return "", fmt.Errorf("BSC_MAINNET_RPC_ADDR is required") + } + return rpcAddr, nil +} + +func createMainnetProver() (*module.Prover, core.Chain, error) { + rpcAddr, err := createRPCAddr() + if err != nil { + return nil, nil, err + } + chain, err := ethereum.NewChain(ethereum.ChainConfig{ + EthChainId: 56, + RpcAddr: rpcAddr, + HdwMnemonic: hdwMnemonic, + HdwPath: hdwPath, + IbcAddress: mainNetIbcAddress, + }) + if err != nil { + return nil, chain, err + } + + config := module.ProverConfig{ + Debug: true, + TrustingPeriod: 86400 * time.Second, + MaxClockDrift: 1 * time.Millisecond, + } + ec := module.NewChain(chain) + return module.NewProver(ec, &config).(*module.Prover), chain, nil +} diff --git a/tool/testdata/internal/create_create_client.go b/tool/testdata/internal/create_client.go similarity index 86% rename from tool/testdata/internal/create_create_client.go rename to tool/testdata/internal/create_client.go index 503133a..621d987 100644 --- a/tool/testdata/internal/create_create_client.go +++ b/tool/testdata/internal/create_client.go @@ -10,16 +10,10 @@ import ( "time" ) -func CreateCreateClient() *cobra.Command { - cmd := &cobra.Command{ - Use: "create", - Short: "Create testdata for Create client. ", - } - cmd.AddCommand(CreateClientSuccessCmd()) - return cmd +type createClientModule struct { } -func CreateClientSuccessCmd() *cobra.Command { +func (m *createClientModule) createClientSuccessCmd() *cobra.Command { cmd := &cobra.Command{ Use: "success", Short: "create CreateClient testdata for success", @@ -41,7 +35,7 @@ func CreateClientSuccessCmd() *cobra.Command { ChainId: 56, LatestHeight: &latestHeight, Frozen: false, - IbcStoreAddress: common.HexToAddress(MainNetIbcAddress).Bytes(), + IbcStoreAddress: common.HexToAddress(mainNetIbcAddress).Bytes(), IbcCommitmentsSlot: commitmentsSlot[:], } anyClientState, err := codectypes.NewAnyWithValue(&clientState) @@ -59,3 +53,13 @@ func CreateClientSuccessCmd() *cobra.Command { } return cmd } + +func CreateCreateClient() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Create testdata for Create client. ", + } + m := createClientModule{} + cmd.AddCommand(m.createClientSuccessCmd()) + return cmd +} diff --git a/tool/testdata/internal/histroy.go b/tool/testdata/internal/histroy.go new file mode 100644 index 0000000..82b9bcd --- /dev/null +++ b/tool/testdata/internal/histroy.go @@ -0,0 +1,136 @@ +package internal + +import ( + "github.com/cometbft/cometbft/libs/json" + "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" + "github.com/datachainlab/ibc-parlia-relay/module" + "github.com/datachainlab/ibc-parlia-relay/module/constant" + "github.com/ethereum/go-ethereum/common" + "github.com/spf13/cobra" + "log" + "os" + "time" +) + +type historyModule struct { +} + +func (m *historyModule) mainnet() *cobra.Command { + var num uint64 + cmd := &cobra.Command{ + Use: "mainnet", + Short: "create many data testdata", + RunE: func(cmd *cobra.Command, args []string) error { + + log.Printf("num = %d\n", num) + prover, chain, err := createMainnetProver() + if err != nil { + return err + } + latest, err := chain.LatestHeight() + if err != nil { + return err + } + + createdEpoch, err := m.outputMsgClient(prover, latest.GetRevisionHeight()-num) + if err != nil { + return err + } + + return m.outputMsgUpdate(prover, createdEpoch, latest.GetRevisionHeight(), num) + }, + } + cmd.Flags().Uint64Var(&num, "num", 240, "--num") + return cmd +} + +func (m *historyModule) outputMsgUpdate(prover *module.Prover, createdEpoch, latest uint64, num uint64) error { + type updatingData struct { + Header string `json:"header"` + } + + type updates struct { + Data []updatingData `json:"data"` + } + + data := updates{} + for i := num; i > 0; i-- { + targetLatest := latest - i + header, err := prover.GetLatestFinalizedHeaderByLatestHeight(targetLatest) + if err != nil { + return err + } + target, err := header.(*module.Header).DecodedTarget() + if err != nil { + return err + } + clientStateLatestHeight := types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1) + if i == num { + clientStateLatestHeight = types.NewHeight(header.GetHeight().GetRevisionNumber(), createdEpoch) + } + + blocks, _ := prover.SetupHeadersForUpdateByLatestHeight(clientStateLatestHeight, header.(*module.Header)) + + for _, block := range blocks { + log.Printf("finalzied=%d\n", block.GetHeight()) + pack, err := types.PackClientMessage(block) + if err != nil { + return err + } + marshal, err := pack.Marshal() + if err != nil { + return err + } + data.Data = append(data.Data, updatingData{Header: common.Bytes2Hex(marshal)}) + } + time.Sleep(200 * time.Millisecond) + } + serialized, err := json.Marshal(data) + if err != nil { + return err + } + return os.WriteFile("update_mainnet.json", serialized, 777) +} + +func (m *historyModule) outputMsgClient(prover *module.Prover, firstNumber uint64) (uint64, error) { + firstHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(firstNumber) + if err != nil { + return 0, err + } + type createData struct { + ClientState string `json:"clientState"` + ConsensusState string `json:"consensusState"` + } + msgCreateClient, err := prover.CreateMsgCreateClient("", firstHeader, nil) + if err != nil { + return 0, err + } + anyClientState, err := msgCreateClient.ClientState.Marshal() + if err != nil { + return 0, err + } + anyConsState, err := msgCreateClient.ConsensusState.Marshal() + if err != nil { + return 0, err + } + creating := createData{ + ClientState: common.Bytes2Hex(anyClientState), + ConsensusState: common.Bytes2Hex(anyConsState), + } + serialized, err := json.Marshal(creating) + if err != nil { + return 0, err + } + epochs := firstHeader.GetHeight().GetRevisionHeight() / constant.BlocksPerEpoch + return (epochs - 1) * constant.BlocksPerEpoch, os.WriteFile("create_mainnet.json", serialized, 777) +} + +func CreateHistoryClient() *cobra.Command { + cmd := &cobra.Command{ + Use: "history", + Short: "Create testdata for update client. ", + } + m := historyModule{} + cmd.AddCommand(m.mainnet()) + return cmd +} diff --git a/tool/testdata/internal/create_misbehavior.go b/tool/testdata/internal/misbehavior.go similarity index 81% rename from tool/testdata/internal/create_misbehavior.go rename to tool/testdata/internal/misbehavior.go index 0883604..aa68b03 100644 --- a/tool/testdata/internal/create_misbehavior.go +++ b/tool/testdata/internal/misbehavior.go @@ -13,36 +13,20 @@ import ( "log" ) -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" -) - -func CreateMisbehavior() *cobra.Command { - cmd := &cobra.Command{ - Use: "misbehavior", - Short: "Create testdata for misbehavior. ", - } - cmd.AddCommand(misbehaviorSuccessCmd()) - cmd.AddCommand(misbehaviorErrorCmd()) - return cmd +type misbehaviorModule struct { } -func misbehaviorSuccessCmd() *cobra.Command { +func (m *misbehaviorModule) success() *cobra.Command { return &cobra.Command{ Use: "success", Short: "create misbehavior testdata for success", RunE: func(cmd *cobra.Command, args []string) error { chainID := int64(9999) - targetHeight, header1, err := getLocalHeader(chainID, 8645, 0) + targetHeight, header1, err := m.getLocalHeader(chainID, 8645, 0) if err != nil { log.Panic(err) } - _, header2, err := getLocalHeader(chainID, 8545, targetHeight) + _, header2, err := m.getLocalHeader(chainID, 8545, targetHeight) if err != nil { log.Panic(err) } @@ -62,7 +46,7 @@ func misbehaviorSuccessCmd() *cobra.Command { log.Println("previousTargetValidatorHash", common.Bytes2Hex(crypto.Keccak256(header1.PreviousTargetValidators...))) epochCount := header1.GetHeight().GetRevisionHeight() / constant.BlocksPerEpoch - if header1.GetHeight().GetRevisionHeight()%constant.BlocksPerEpoch >= (LocalNetValidatorSize/2 + 1) { + if header1.GetHeight().GetRevisionHeight()%constant.BlocksPerEpoch >= (localNetValidatorSize/2 + 1) { log.Println("targetValidatorEpoch", epochCount*constant.BlocksPerEpoch) } else { log.Println("targetValidatorEpoch", (epochCount-1)*constant.BlocksPerEpoch) @@ -72,31 +56,16 @@ func misbehaviorSuccessCmd() *cobra.Command { } } -func misbehaviorErrorCmd() *cobra.Command { +func (m *misbehaviorModule) error() *cobra.Command { return &cobra.Command{ Use: "error", Short: "create misbehavior testdata for error", RunE: func(cmd *cobra.Command, args []string) error { - rpcAddr, err := createRPCAddr() - if err != nil { - return err - } - chain, err := ethereum.NewChain(ethereum.ChainConfig{ - EthChainId: 56, - RpcAddr: rpcAddr, - HdwMnemonic: hdwMnemonic, - HdwPath: hdwPath, - IbcAddress: MainNetIbcAddress, - }) + prover, chain, err := createMainnetProver() if err != nil { return err } - config := module.ProverConfig{ - Debug: true, - } - prover := module.NewProver(module.NewChain(chain), &config).(*module.Prover) - latestHeight, err := chain.LatestHeight() if err != nil { return err @@ -141,7 +110,7 @@ func misbehaviorErrorCmd() *cobra.Command { log.Println("Invalid block: prevous_target_validator_hash", common.Bytes2Hex(crypto.Keccak256(header.(*module.Header).PreviousTargetValidators...))) log.Println("Invalid block: trusted_height", updating[0].(*module.Header).TrustedHeight) epochCount := header.GetHeight().GetRevisionHeight() / constant.BlocksPerEpoch - if header.GetHeight().GetRevisionHeight()%constant.BlocksPerEpoch >= (MainNetValidatorSize/2 + 1) { + if header.GetHeight().GetRevisionHeight()%constant.BlocksPerEpoch >= (mainNetValidatorSize/2 + 1) { log.Println("Invalid block: targetValidatorEpoch", epochCount*constant.BlocksPerEpoch) } else { log.Println("Invalid block: targetValidatorEpoch", (epochCount-1)*constant.BlocksPerEpoch) @@ -152,13 +121,13 @@ func misbehaviorErrorCmd() *cobra.Command { } } -func getLocalHeader(chainID int64, port int64, targetHeight uint64) (uint64, *module.Header, error) { +func (m *misbehaviorModule) getLocalHeader(chainID int64, port int64, targetHeight uint64) (uint64, *module.Header, error) { chain, err := ethereum.NewChain(ethereum.ChainConfig{ EthChainId: chainID, RpcAddr: fmt.Sprintf("http://localhost:%d", port), HdwMnemonic: hdwMnemonic, HdwPath: hdwPath, - IbcAddress: IbcAddress, + IbcAddress: ibcAddress, }) if err != nil { return targetHeight, nil, err @@ -191,3 +160,14 @@ func getLocalHeader(chainID int64, port int64, targetHeight uint64) (uint64, *mo header.TrustedHeight = &trustedHeight return latest, header, nil } + +func CreateMisbehavior() *cobra.Command { + cmd := &cobra.Command{ + Use: "misbehavior", + Short: "Create testdata for misbehavior. ", + } + m := misbehaviorModule{} + cmd.AddCommand(m.success()) + cmd.AddCommand(m.error()) + return cmd +} diff --git a/tool/testdata/internal/create_update_client.go b/tool/testdata/internal/update_client.go similarity index 77% rename from tool/testdata/internal/create_update_client.go rename to tool/testdata/internal/update_client.go index 7b47e43..f85f2e2 100644 --- a/tool/testdata/internal/create_update_client.go +++ b/tool/testdata/internal/update_client.go @@ -3,29 +3,19 @@ package internal import ( "fmt" "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - "github.com/datachainlab/ethereum-ibc-relay-chain/pkg/relay/ethereum" "github.com/datachainlab/ibc-parlia-relay/module" "github.com/datachainlab/ibc-parlia-relay/module/constant" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" - "github.com/hyperledger-labs/yui-relayer/core" "github.com/spf13/cobra" - "github.com/spf13/viper" "log" ) -func CreateUpdateClient() *cobra.Command { - cmd := &cobra.Command{ - Use: "update", - Short: "Create testdata for update client. ", - } - cmd.AddCommand(updateClientSuccessCmd()) - cmd.AddCommand(updateClientErrorCmd()) - return cmd +type updateClientModule struct { } -func updateClientSuccessCmd() *cobra.Command { +func (m *updateClientModule) success() *cobra.Command { cmd := &cobra.Command{ Use: "success", Short: "create updateClient testdata for success", @@ -42,7 +32,7 @@ func updateClientSuccessCmd() *cobra.Command { if err != nil { return err } - return printMainnetHeader(prover, latest.GetRevisionHeight()) + return m.printMainnetHeader(prover, latest.GetRevisionHeight()) }, }) cmd.AddCommand(&cobra.Command{ @@ -58,13 +48,13 @@ func updateClientSuccessCmd() *cobra.Command { return err } epochCount := latest.GetRevisionHeight() / constant.BlocksPerEpoch - return printMainnetHeader(prover, epochCount*constant.BlocksPerEpoch+3) + return m.printMainnetHeader(prover, epochCount*constant.BlocksPerEpoch+3) }, }) return cmd } -func updateClientErrorCmd() *cobra.Command { +func (m *updateClientModule) error() *cobra.Command { return &cobra.Command{ Use: "error", Short: "create updateClient testdata for error", @@ -108,7 +98,8 @@ func updateClientErrorCmd() *cobra.Command { }, } } -func printMainnetHeader(prover *module.Prover, height uint64) error { + +func (m *updateClientModule) printMainnetHeader(prover *module.Prover, height uint64) error { log.Println("printMainnetHeader latest=", height) iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(height) if err != nil { @@ -123,7 +114,7 @@ func printMainnetHeader(prover *module.Prover, height uint64) error { return err } - account, err := header.Account(common.HexToAddress(MainNetIbcAddress)) + account, err := header.Account(common.HexToAddress(mainNetIbcAddress)) if err != nil { return err } @@ -158,7 +149,7 @@ func printMainnetHeader(prover *module.Prover, height uint64) error { if err != nil { return err } - if len(newValidators) != MainNetValidatorSize { + if len(newValidators) != mainNetValidatorSize { return fmt.Errorf("invalid validator size for test") } log.Println("newValidatorHash", common.Bytes2Hex(crypto.Keccak256(newValidators...))) @@ -166,33 +157,13 @@ func printMainnetHeader(prover *module.Prover, height uint64) error { return nil } -func createRPCAddr() (string, error) { - rpcAddr, ok := viper.Get("BSC_MAINNET_RPC_ADDR").(string) - if !ok { - return "", fmt.Errorf("BSC_MAINNET_RPC_ADDR is required") - } - return rpcAddr, nil -} - -func createMainnetProver() (*module.Prover, core.Chain, error) { - rpcAddr, err := createRPCAddr() - if err != nil { - return nil, nil, err - } - chain, err := ethereum.NewChain(ethereum.ChainConfig{ - EthChainId: 56, - RpcAddr: rpcAddr, - HdwMnemonic: hdwMnemonic, - HdwPath: hdwPath, - IbcAddress: MainNetIbcAddress, - }) - if err != nil { - return nil, chain, err - } - - config := module.ProverConfig{ - Debug: true, +func CreateUpdateClient() *cobra.Command { + cmd := &cobra.Command{ + Use: "update", + Short: "Create testdata for update client. ", } - ec := module.NewChain(chain) - return module.NewProver(ec, &config).(*module.Prover), chain, nil + m := updateClientModule{} + cmd.AddCommand(m.success()) + cmd.AddCommand(m.error()) + return cmd } diff --git a/tool/testdata/main.go b/tool/testdata/main.go index 0527352..428b617 100644 --- a/tool/testdata/main.go +++ b/tool/testdata/main.go @@ -16,6 +16,7 @@ func main() { rootCmd.AddCommand(internal.CreateMisbehavior()) rootCmd.AddCommand(internal.CreateCreateClient()) rootCmd.AddCommand(internal.CreateUpdateClient()) + rootCmd.AddCommand(internal.CreateHistoryClient()) if err := rootCmd.ExecuteContext(context.Background()); err != nil { log.Panicf("Failed to run command : %+v", err)