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 c9120cb commit f64d22d
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
third_party
**/*.json
6 changes: 6 additions & 0 deletions tool/testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
52 changes: 52 additions & 0 deletions tool/testdata/internal/common.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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
}
136 changes: 136 additions & 0 deletions tool/testdata/internal/histroy.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit f64d22d

Please sign in to comment.