Skip to content

Commit

Permalink
refactor testdata maker
Browse files Browse the repository at this point in the history
Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
Naohiro Yoshida committed Sep 16, 2023
1 parent f64d22d commit 98dab4c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
9 changes: 5 additions & 4 deletions tool/testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
20 changes: 10 additions & 10 deletions tool/testdata/internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
43 changes: 36 additions & 7 deletions tool/testdata/internal/histroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"`
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions tool/testdata/internal/update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ 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
}
latest, err := chain.LatestHeight()
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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 98dab4c

Please sign in to comment.