Skip to content

Commit

Permalink
Update ibc proof to be bytes (#6393)
Browse files Browse the repository at this point in the history
* update interfaces to use []byte instead of commitment proof

* update 03/04 msgs with bytes

* fix compile errors

* fix channel tests

* fix connection tests

* fix testing, tm, and localhost tests

* fix ante tests

* update 03-connection spec

* update channel msgs spec

* small fixes after self review

* rm unused import

* Update x/ibc/03-connection/client/utils/utils.go

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* add empty proof test and rm ics 20 from ibc spec

* fix merge issues

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
  • Loading branch information
colin-axner and Alessio Treglia authored Jun 11, 2020
1 parent b09d672 commit 9048ffa
Show file tree
Hide file tree
Showing 30 changed files with 1,010 additions and 762 deletions.
21 changes: 13 additions & 8 deletions x/ibc/02-client/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ type ClientState interface {

VerifyClientConsensusState(
store sdk.KVStore,
cdc *codec.Codec,
cdc codec.Marshaler,
aminoCdc *codec.Codec,
root commitmentexported.Root,
height uint64,
counterpartyClientIdentifier string,
consensusHeight uint64,
prefix commitmentexported.Prefix,
proof commitmentexported.Proof,
proof []byte,
consensusState ConsensusState,
) error
VerifyConnectionState(
store sdk.KVStore,
cdc codec.Marshaler,
height uint64,
prefix commitmentexported.Prefix,
proof commitmentexported.Proof,
proof []byte,
connectionID string,
connectionEnd connectionexported.ConnectionI,
consensusState ConsensusState,
Expand All @@ -50,17 +51,18 @@ type ClientState interface {
cdc codec.Marshaler,
height uint64,
prefix commitmentexported.Prefix,
proof commitmentexported.Proof,
proof []byte,
portID,
channelID string,
channel channelexported.ChannelI,
consensusState ConsensusState,
) error
VerifyPacketCommitment(
store sdk.KVStore,
cdc codec.Marshaler,
height uint64,
prefix commitmentexported.Prefix,
proof commitmentexported.Proof,
proof []byte,
portID,
channelID string,
sequence uint64,
Expand All @@ -69,9 +71,10 @@ type ClientState interface {
) error
VerifyPacketAcknowledgement(
store sdk.KVStore,
cdc codec.Marshaler,
height uint64,
prefix commitmentexported.Prefix,
proof commitmentexported.Proof,
proof []byte,
portID,
channelID string,
sequence uint64,
Expand All @@ -80,19 +83,21 @@ type ClientState interface {
) error
VerifyPacketAcknowledgementAbsence(
store sdk.KVStore,
cdc codec.Marshaler,
height uint64,
prefix commitmentexported.Prefix,
proof commitmentexported.Proof,
proof []byte,
portID,
channelID string,
sequence uint64,
consensusState ConsensusState,
) error
VerifyNextSequenceRecv(
store sdk.KVStore,
cdc codec.Marshaler,
height uint64,
prefix commitmentexported.Prefix,
proof commitmentexported.Proof,
proof []byte,
portID,
channelID string,
nextSequenceRecv uint64,
Expand Down
30 changes: 20 additions & 10 deletions x/ibc/03-connection/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ func GetCmdConnectionOpenTry(storeKey string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: strings.TrimSpace(`open-try [connection-id] [client-id]
[counterparty-connection-id] [counterparty-client-id] [path/to/counterparty_prefix.json]
[counterparty-versions] [path/to/proof_init.json]`),
[counterparty-versions] [path/to/proof_init.json] [path/to/proof_consensus.json]`),
Short: "initiate connection handshake between two chains",
Long: strings.TrimSpace(
fmt.Sprintf(`initialize a connection on chain A with a given counterparty chain B:
Example:
$ %s tx ibc connection open-try connection-id] [client-id] \
[counterparty-connection-id] [counterparty-client-id] [path/to/counterparty_prefix.json] \
[counterparty-versions] [path/to/proof_init.json]
[counterparty-versions] [path/to/proof_init.json] [path/tp/proof_consensus.json]
`, version.ClientName),
),
Args: cobra.ExactArgs(7),
Args: cobra.ExactArgs(8),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := authtypes.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
Expand All @@ -113,7 +113,12 @@ $ %s tx ibc connection open-try connection-id] [client-id] \
// TODO: parse strings?
counterpartyVersions := args[5]

proofInit, err := utils.ParseProof(clientCtx.Codec, args[1])
proofInit, err := utils.ParseProof(clientCtx.Codec, args[6])
if err != nil {
return err
}

proofConsensus, err := utils.ParseProof(clientCtx.Codec, args[7])
if err != nil {
return err
}
Expand All @@ -126,7 +131,7 @@ $ %s tx ibc connection open-try connection-id] [client-id] \

msg := types.NewMsgConnectionOpenTry(
connectionID, clientID, counterpartyConnectionID, counterpartyClientID,
counterpartyPrefix, []string{counterpartyVersions}, proofInit, proofInit, proofHeight,
counterpartyPrefix, []string{counterpartyVersions}, proofInit, proofConsensus, proofHeight,
consensusHeight, clientCtx.GetFromAddress(),
)

Expand All @@ -145,16 +150,16 @@ $ %s tx ibc connection open-try connection-id] [client-id] \
// connection open attempt from chain B to chain A
func GetCmdConnectionOpenAck(storeKey string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "open-ack [connection-id] [path/to/proof_try.json] [version]",
Use: "open-ack [connection-id] [path/to/proof_try.json] [path/to/proof_consensus.json] [version]",
Short: "relay the acceptance of a connection open attempt from chain B to chain A",
Long: strings.TrimSpace(
fmt.Sprintf(`relay the acceptance of a connection open attempt from chain B to chain A:
Example:
$ %s tx ibc connection open-ack [connection-id] [path/to/proof_try.json] [version]
$ %s tx ibc connection open-ack [connection-id] [path/to/proof_try.json] [path/to/proof_consensus.json] [version]
`, version.ClientName),
),
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := authtypes.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
Expand All @@ -167,16 +172,21 @@ $ %s tx ibc connection open-ack [connection-id] [path/to/proof_try.json] [versio
return err
}

proofConsensus, err := utils.ParseProof(clientCtx.Codec, args[2])
if err != nil {
return err
}

proofHeight := uint64(clientCtx.Height)
consensusHeight, err := lastHeight(clientCtx)
if err != nil {
return err
}

version := args[4]
version := args[3]

msg := types.NewMsgConnectionOpenAck(
connectionID, proofTry, proofTry, proofHeight,
connectionID, proofTry, proofConsensus, proofHeight,
consensusHeight, version, clientCtx.GetFromAddress(),
)

Expand Down
22 changes: 11 additions & 11 deletions x/ibc/03-connection/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ type ConnectionOpenTryReq struct {
CounterpartyConnectionID string `json:"counterparty_connection_id" yaml:"counterparty_connection_id"`
CounterpartyPrefix commitmenttypes.MerklePrefix `json:"counterparty_prefix" yaml:"counterparty_prefix"`
CounterpartyVersions []string `json:"counterparty_versions" yaml:"counterparty_versions"`
ProofInit commitmenttypes.MerkleProof `json:"proof_init" yaml:"proof_init"`
ProofConsensus commitmenttypes.MerkleProof `json:"proof_consensus" yaml:"proof_consensus"`
ProofInit []byte `json:"proof_init" yaml:"proof_init"`
ProofConsensus []byte `json:"proof_consensus" yaml:"proof_consensus"`
ProofHeight uint64 `json:"proof_height" yaml:"proof_height"`
ConsensusHeight uint64 `json:"consensus_height" yaml:"consensus_height"`
}

// ConnectionOpenAckReq defines the properties of a connection open ack request's body.
type ConnectionOpenAckReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ProofTry commitmenttypes.MerkleProof `json:"proof_try" yaml:"proof_try"`
ProofConsensus commitmenttypes.MerkleProof `json:"proof_consensus" yaml:"proof_consensus"`
ProofHeight uint64 `json:"proof_height" yaml:"proof_height"`
ConsensusHeight uint64 `json:"consensus_height" yaml:"consensus_height"`
Version string `json:"version" yaml:"version"`
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ProofTry []byte `json:"proof_try" yaml:"proof_try"`
ProofConsensus []byte `json:"proof_consensus" yaml:"proof_consensus"`
ProofHeight uint64 `json:"proof_height" yaml:"proof_height"`
ConsensusHeight uint64 `json:"consensus_height" yaml:"consensus_height"`
Version string `json:"version" yaml:"version"`
}

// ConnectionOpenConfirmReq defines the properties of a connection open confirm request's body.
type ConnectionOpenConfirmReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ProofAck commitmenttypes.MerkleProof `json:"proof_ack" yaml:"proof_ack"`
ProofHeight uint64 `json:"proof_height" yaml:"proof_height"`
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ProofAck []byte `json:"proof_ack" yaml:"proof_ack"`
ProofHeight uint64 `json:"proof_height" yaml:"proof_height"`
}
20 changes: 11 additions & 9 deletions x/ibc/03-connection/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,21 @@ func ParsePrefix(cdc *codec.Codec, arg string) (commitmenttypes.MerklePrefix, er
return prefix, nil
}

// ParseProof unmarshals an cmd input argument from a JSON string to a commitment
// Proof. If the input is not a JSON, it looks for a path to the JSON file.
func ParseProof(cdc *codec.Codec, arg string) (commitmenttypes.MerkleProof, error) {
var proof commitmenttypes.MerkleProof
if err := cdc.UnmarshalJSON([]byte(arg), &proof); err != nil {
// ParseProof unmarshals a cmd input argument from a JSON string to a commitment
// Proof. If the input is not a JSON, it looks for a path to the JSON file. It
// then marshals the commitment proof into a proto encoded byte array.
func ParseProof(cdc *codec.Codec, arg string) ([]byte, error) {
var merkleProof commitmenttypes.MerkleProof
if err := cdc.UnmarshalJSON([]byte(arg), &merkleProof); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(arg)
if err != nil {
return commitmenttypes.MerkleProof{}, errors.New("neither JSON input nor path to .json file were provided")
return nil, errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &proof); err != nil {
return commitmenttypes.MerkleProof{}, errors.Wrap(err, "error unmarshalling commitment proof")
if err := cdc.UnmarshalJSON(contents, &merkleProof); err != nil {
return nil, fmt.Errorf("error unmarshalling commitment proof: %w", err)
}
}
return proof, nil

return cdc.MarshalBinaryBare(&merkleProof)
}
11 changes: 5 additions & 6 deletions x/ibc/03-connection/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"
commitmentexported "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
)

Expand Down Expand Up @@ -50,8 +49,8 @@ func (k Keeper) ConnOpenTry(
counterparty types.Counterparty, // counterpartyConnectionIdentifier, counterpartyPrefix and counterpartyClientIdentifier
clientID string, // clientID of chainA
counterpartyVersions []string, // supported versions of chain A
proofInit commitmentexported.Proof, // proof that chainA stored connectionEnd in state (on ConnOpenInit)
proofConsensus commitmentexported.Proof, // proof that chainA stored chainB's consensus state at consensus height
proofInit []byte, // proof that chainA stored connectionEnd in state (on ConnOpenInit)
proofConsensus []byte, // proof that chainA stored chainB's consensus state at consensus height
proofHeight uint64, // height at which relayer constructs proof of A storing connectionEnd in state
consensusHeight uint64, // latest height of chain B which chain A has stored in its chain B client
) error {
Expand Down Expand Up @@ -124,8 +123,8 @@ func (k Keeper) ConnOpenAck(
ctx sdk.Context,
connectionID string,
version string, // version that ChainB chose in ConnOpenTry
proofTry commitmentexported.Proof, // proof that connectionEnd was added to ChainB state in ConnOpenTry
proofConsensus commitmentexported.Proof, // proof that chainB has stored ConsensusState of chainA on its client
proofTry []byte, // proof that connectionEnd was added to ChainB state in ConnOpenTry
proofConsensus []byte, // proof that chainB has stored ConsensusState of chainA on its client
proofHeight uint64, // height that relayer constructed proofTry
consensusHeight uint64, // latest height of chainA that chainB has stored on its chainA client
) error {
Expand Down Expand Up @@ -196,7 +195,7 @@ func (k Keeper) ConnOpenAck(
func (k Keeper) ConnOpenConfirm(
ctx sdk.Context,
connectionID string,
proofAck commitmentexported.Proof, // proof that connection opened on ChainA during ConnOpenAck
proofAck []byte, // proof that connection opened on ChainA during ConnOpenAck
proofHeight uint64, // height that relayer constructed proofAck
) error {
// Retrieve connection
Expand Down
6 changes: 4 additions & 2 deletions x/ibc/03-connection/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,20 @@ func (suite *KeeperTestSuite) SetupTest() {
}

// nolint: unused
func queryProof(chain *TestChain, key []byte) (commitmenttypes.MerkleProof, uint64) {
func queryProof(chain *TestChain, key []byte) ([]byte, uint64) {
res := chain.App.Query(abci.RequestQuery{
Path: fmt.Sprintf("store/%s/key", storeKey),
Height: chain.App.LastBlockHeight(),
Data: key,
Prove: true,
})

proof := commitmenttypes.MerkleProof{
merkleProof := commitmenttypes.MerkleProof{
Proof: res.Proof,
}

proof, _ := chain.App.AppCodec().MarshalBinaryBare(&merkleProof)

return proof, uint64(res.Height)
}
func TestKeeperTestSuite(t *testing.T) {
Expand Down
Loading

0 comments on commit 9048ffa

Please sign in to comment.