Skip to content

Commit

Permalink
Merge branch 'main' into update_relayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Naohiro Yoshida committed Oct 19, 2023
2 parents 479f8c0 + 4ba9b53 commit 4d4639c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
112 changes: 112 additions & 0 deletions tool/testdata/internal/membership/verify_membership.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package membership

import (
"context"
"fmt"
"github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
types3 "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/relay/ethereum"
"github.com/datachainlab/ibc-parlia-relay/module"
"github.com/ethereum/go-ethereum/common"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/spf13/cobra"
"log"
"math/big"
)

const (
hdwMnemonic = "math razor capable expose worth grape metal sunset metal sudden usage scheme"
hdwPath = "m/44'/60'/0'/0/0"
ibcAddress = "0x702E40245797c5a2108A566b3CE2Bf14Bc6aF841"
)

type verifyMembershipModule struct {
}

func (m *verifyMembershipModule) latest() *cobra.Command {
return &cobra.Command{
Use: "latest",
RunE: func(cmd *cobra.Command, args []string) error {
chainID := int64(9999)
connection := conntypes.ConnectionEnd{
ClientId: "xx-parlia-0",
Versions: []*conntypes.Version{{
Identifier: "1",
Features: []string{"ORDER_ORDERED", "ORDER_UNORDERED"},
}},
State: conntypes.OPEN,
Counterparty: conntypes.Counterparty{
ClientId: "xx-parlia-0",
ConnectionId: "connection-0",
Prefix: types3.MerklePrefix{
KeyPrefix: []byte("ibc"),
},
},
DelayPeriod: 0,
}

commitment, err := connection.Marshal()
if err != nil {
return err
}

path := host.ConnectionPath("connection-0")
stateRoot, proof, proofHeight, err := m.proveState(chainID, 8645, path, commitment)
if err != nil {
log.Panic(err)
}
log.Println("proofHeight", proofHeight)
log.Println("proof", common.Bytes2Hex(proof))
log.Println("stateRoot", stateRoot)
log.Println("value", common.Bytes2Hex(commitment))
return nil
},
}
}

func (m *verifyMembershipModule) proveState(chainID int64, port int64, path string, value []byte) (common.Hash, []byte, types.Height, error) {
chain, err := ethereum.NewChain(ethereum.ChainConfig{
EthChainId: chainID,
RpcAddr: fmt.Sprintf("http://localhost:%d", port),
HdwMnemonic: hdwMnemonic,
HdwPath: hdwPath,
IbcAddress: ibcAddress,
})
if err != nil {
return common.Hash{}, nil, types.Height{}, err
}
latest, err := chain.LatestHeight()
if err != nil {
return common.Hash{}, nil, types.Height{}, err
}
config := module.ProverConfig{
Debug: true,
}
prover := module.NewProver(module.NewChain(chain), &config).(*module.Prover)

ctx := core.NewQueryContext(context.Background(), latest)

header, err := chain.Client().HeaderByNumber(ctx.Context(), big.NewInt(int64(latest.GetRevisionHeight())))
if err != nil {
return common.Hash{}, nil, types.Height{}, err
}

stateRoot, err := prover.GetStorageRoot(header)
if err != nil {
return common.Hash{}, nil, types.Height{}, err
}

proof, proofHeight, err := prover.ProveState(ctx, path, value)
return stateRoot, proof, proofHeight, err
}

func CreateVerifyMembership() *cobra.Command {
cmd := &cobra.Command{
Use: "verify_membership",
}
m := verifyMembershipModule{}
cmd.AddCommand(m.latest())
return cmd
}
2 changes: 2 additions & 0 deletions tool/testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"github.com/datachainlab/ibc-parlia-relay/tool/testdata/internal"
"github.com/datachainlab/ibc-parlia-relay/tool/testdata/internal/membership"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand All @@ -18,6 +19,7 @@ func main() {
rootCmd.AddCommand(internal.CreateUpdateClient())
rootCmd.AddCommand(internal.CreateHistoryClient())
rootCmd.AddCommand(internal.CreateHeader())
rootCmd.AddCommand(membership.CreateVerifyMembership())

if err := rootCmd.ExecuteContext(context.Background()); err != nil {
log.Panicf("Failed to run command : %+v", err)
Expand Down

0 comments on commit 4d4639c

Please sign in to comment.