Skip to content

Commit

Permalink
Cherry-pick: Add MsgRegisterCounterparty Struct and Handler from ibc-…
Browse files Browse the repository at this point in the history
…lite (#6982)

* feat(lite): counterparty client logic (#6307)

* imp: added counterparty client store

* imp: added provide counterparty to proto

* imp: ran 'make proto-all'

* imp: added logic to counterparty client

* imp: fix proto

* imp: fix proto

* imp: ran 'make proto-all'

* feat: finished counterparty client logic

* change counterparty to include custom prefix

* fix imports

* import fixes, review suggestions

* rm lite comment

* applying review suggestions

* add creator tests

* addressing aditya review

* Update proto/ibc/core/client/v1/client.proto

Co-authored-by: DimitrisJim <[email protected]>

* Update modules/core/02-client/types/msgs.go

Co-authored-by: DimitrisJim <[email protected]>

* Update modules/core/keeper/msg_server.go

Co-authored-by: colin axnér <[email protected]>

* addressing jim review

* refactor(proto): use counterparty type in MsgProvideCounterparty. Validate Counterparty type.

* refactor(keys): move Counterparty key to 02-client keys.go

* feat(core): delete creator after registering counterparty.

* chore(core): make GetCreator return a boolean if not found.

* tests(02-client): add tests for counterparty validation.

* tests(02-client): add tests for msg_server provide counterparty handler.

* nit(core): remove stale key for counterparty in host.

* Update modules/core/02-client/keeper/keeper_test.go

---------

Co-authored-by: srdtrk <[email protected]>
Co-authored-by: Aditya Sripal <[email protected]>
Co-authored-by: Stefano Angieri <[email protected]>
Co-authored-by: DimitrisJim <[email protected]>
Co-authored-by: colin axnér <[email protected]>
  • Loading branch information
6 people authored Jul 31, 2024
1 parent 3a192be commit 8d10fed
Show file tree
Hide file tree
Showing 14 changed files with 1,064 additions and 89 deletions.
39 changes: 39 additions & 0 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,45 @@ func (k *Keeper) GetSelfConsensusState(ctx sdk.Context, height exported.Height)
return k.consensusHost.GetSelfConsensusState(ctx, height)
}

// SetCounterparty sets the Counterparty for a given client identifier.
func (k *Keeper) SetCounterparty(ctx sdk.Context, clientID string, counterparty types.Counterparty) {
bz := k.cdc.MustMarshal(&counterparty)
k.ClientStore(ctx, clientID).Set([]byte(types.CounterpartyKey), bz)
}

// GetCounterparty gets the counterparty client's identifier for a given client identifier.
func (k *Keeper) GetCounterparty(ctx sdk.Context, clientID string) (types.Counterparty, bool) {
store := k.ClientStore(ctx, clientID)
bz := store.Get([]byte(types.CounterpartyKey))
if len(bz) == 0 {
return types.Counterparty{}, false
}

var counterparty types.Counterparty
k.cdc.MustUnmarshal(bz, &counterparty)
return counterparty, true
}

// GetCreator returns the creator of the client.
func (k *Keeper) GetCreator(ctx sdk.Context, clientID string) (string, bool) {
bz := k.ClientStore(ctx, clientID).Get([]byte(types.CreatorKey))
if len(bz) == 0 {
return "", false
}

return string(bz), true
}

// SetCreator sets the creator of the client.
func (k *Keeper) SetCreator(ctx sdk.Context, clientID, creator string) {
k.ClientStore(ctx, clientID).Set([]byte(types.CreatorKey), []byte(creator))
}

// DeleteCreator deletes the creator associated with the client.
func (k *Keeper) DeleteCreator(ctx sdk.Context, clientID string) {
k.ClientStore(ctx, clientID).Delete([]byte(types.CreatorKey))
}

// ValidateSelfClient validates the client parameters for a client of the running chain.
// This function is only used to validate the client state the counterparty stores for this chain.
// NOTE: If the client type is not of type Tendermint then delegate to a custom client validator function.
Expand Down
37 changes: 37 additions & 0 deletions modules/core/02-client/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ func (suite *KeeperTestSuite) TestSetClientState() {
suite.Require().Equal(clientState, retrievedState, "Client states are not equal")
}

func (suite *KeeperTestSuite) TestSetCounterparty() {
merklePathPrefix := commitmenttypes.NewMerklePath([]byte("ibc"), []byte(""))
counterparty := types.Counterparty{
ClientId: testClientID,
MerklePathPrefix: &merklePathPrefix,
}
suite.keeper.SetCounterparty(suite.ctx, testClientID, counterparty)

retrievedCounterparty, found := suite.keeper.GetCounterparty(suite.ctx, testClientID)
suite.Require().True(found, "GetCounterparty does not return counterparty")
suite.Require().Equal(counterparty, retrievedCounterparty, "Counterparty retrieved not equal")

retrievedCounterparty, found = suite.keeper.GetCounterparty(suite.ctx, "client-0")
suite.Require().False(found, "GetCounterparty unexpectedly returned a counterparty")
suite.Require().Equal(types.Counterparty{}, retrievedCounterparty, "Counterparty retrieved not empty")
}

func (suite *KeeperTestSuite) TestSetCreator() {
clientID := "test-client"
expectedCreator := "test-creator"

// Set the creator for the client
suite.keeper.SetCreator(suite.ctx, clientID, expectedCreator)

// Retrieve the creator from the store
retrievedCreator, found := suite.keeper.GetCreator(suite.ctx, clientID)

// Verify that the retrieved creator matches the expected creator
suite.Require().True(found, "GetCreator did not return stored creator")
suite.Require().Equal(expectedCreator, retrievedCreator, "Creator is not retrieved correctly")

// Verify non stored creator is not found
retrievedCreator, found = suite.keeper.GetCreator(suite.ctx, "client-0")
suite.Require().False(found, "GetCreator unexpectedly returned a creator")
suite.Require().Equal(retrievedCreator, "", "Creator is not empty")
}

func (suite *KeeperTestSuite) TestSetClientConsensusState() {
suite.keeper.SetClientConsensusState(suite.ctx, testClientID, testClientHeight, suite.consensusState)

Expand Down
22 changes: 22 additions & 0 deletions modules/core/02-client/types/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"

commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)
Expand Down Expand Up @@ -71,6 +72,27 @@ func (ics IdentifiedClientStates) Sort() IdentifiedClientStates {
return ics
}

// NewCounterparty creates a new Counterparty instance
func NewCounterparty(clientID string, merklePathPrefix *commitmenttypes.MerklePath) Counterparty {
return Counterparty{
ClientId: clientID,
MerklePathPrefix: merklePathPrefix,
}
}

// Validate validates the Counterparty
func (c Counterparty) Validate() error {
if err := host.ClientIdentifierValidator(c.ClientId); err != nil {
return err
}

if c.MerklePathPrefix.Empty() {
return errorsmod.Wrap(ErrInvalidCounterparty, "counterparty prefix cannot be empty")
}

return nil
}

// NewConsensusStateWithHeight creates a new ConsensusStateWithHeight instance
func NewConsensusStateWithHeight(height Height, consensusState exported.ConsensusState) ConsensusStateWithHeight {
msg, ok := consensusState.(proto.Message)
Expand Down
Loading

0 comments on commit 8d10fed

Please sign in to comment.