Skip to content

Commit

Permalink
Merge pull request #11 from dymensionxyz/liorzilp/07-additions-light-…
Browse files Browse the repository at this point in the history
…client-agnostic

Liorzilp/07 additions light client agnostic
  • Loading branch information
liorzilp authored Nov 22, 2022
2 parents 6eb5038 + 95151b9 commit 40169cc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
3 changes: 1 addition & 2 deletions modules/core/02-client/keeper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {

// Must create header creation functions since suite.header gets recreated on each test case
createFutureUpdateFn := func(trustedHeight clienttypes.Height) *ibctmtypes.Header {
chainATendermint := suite.chainA.TestChainClient.(*ibctesting.TestChainTendermint)
header, err := chainATendermint.ConstructUpdateTMClientHeaderWithTrustedHeight(path.EndpointB.Chain, path.EndpointA.ClientID, trustedHeight)
header, err := ibctesting.ConstructUpdateTMClientHeaderWithTrustedHeight(path.EndpointB.Chain, path.EndpointA.ClientID, trustedHeight)
suite.Require().NoError(err)
return header
}
Expand Down
16 changes: 8 additions & 8 deletions modules/light-clients/07-tendermint/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ func (cs ClientState) VerifyClientState(
return sdkerrors.Wrap(clienttypes.ErrInvalidClient, "client state cannot be empty")
}

_, ok := clientState.(*ClientState)
if !ok {
return sdkerrors.Wrapf(clienttypes.ErrInvalidClient, "invalid client type %T, expected %T", clientState, &ClientState{})
}
// _, ok := clientState.(*ClientState)
// if !ok {
// return sdkerrors.Wrapf(clienttypes.ErrInvalidClient, "invalid client type %T, expected %T", clientState, &ClientState{})
// }

bz, err := cdc.MarshalInterface(clientState)
if err != nil {
Expand Down Expand Up @@ -256,10 +256,10 @@ func (cs ClientState) VerifyClientConsensusState(
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state cannot be empty")
}

_, ok := consensusState.(*ConsensusState)
if !ok {
return sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "invalid consensus type %T, expected %T", consensusState, &ConsensusState{})
}
// _, ok := consensusState.(*ConsensusState)
// if !ok {
// return sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "invalid consensus type %T, expected %T", consensusState, &ConsensusState{})
// }

bz, err := cdc.MarshalInterface(consensusState)
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type TestChainClientI interface {
ClientConfigToState(ClientConfig ClientConfig) exported.ClientState
GetConsensusState() exported.ConsensusState
NewConfig() ClientConfig
ConstructUpdateClientHeader(counterparty *TestChain, clientID string) (exported.Header, error)
GetSelfClientType() string
}

func NewTestChainClient(chain *TestChain, chainConsensusType string) TestChainClientI {
Expand Down Expand Up @@ -427,3 +427,16 @@ func (chain *TestChain) GetChannelCapability(portID, channelID string) *capabili

return cap
}

// ConstructUpdateClientHeader will construct a valid 01-dymint Header to update the
// light client on the source chain.
func (chain *TestChain) ConstructUpdateClientHeader(counterparty *TestChain, clientID string) (exported.Header, error) {
// Relayer must query for LatestHeight on client to get TrustedHeight if the trusted height is not set
trustedHeight := chain.GetClientState(clientID).GetLatestHeight().(clienttypes.Height)
switch counterparty.TestChainClient.GetSelfClientType() {
case exported.Tendermint:
return ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty, clientID, trustedHeight)
default:
panic(fmt.Sprintf("client type %s is not supported", counterparty.TestChainClient.GetSelfClientType()))
}
}
21 changes: 10 additions & 11 deletions testing/chain_tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func NewChainTendermintClient(tc *TestChain) *TestChainTendermint {
return chain
}

func (chain *TestChainTendermint) GetSelfClientType() string {
return exported.Tendermint
}

func (chain *TestChainTendermint) NewConfig() ClientConfig {
return &TendermintConfig{
TrustLevel: DefaultTrustLevel,
Expand Down Expand Up @@ -112,27 +116,22 @@ func (chain *TestChainTendermint) NextBlock() {
chain.BeginBlock()
}

// ConstructUpdateClientHeader will construct a valid 07-tendermint Header to update the
// light client on the source chain.
func (chain *TestChainTendermint) ConstructUpdateClientHeader(counterparty *TestChain, clientID string) (exported.Header, error) {
return chain.ConstructUpdateTMClientHeader(counterparty, clientID)
}

// ConstructUpdateTMClientHeader will construct a valid 07-tendermint Header to update the
// light client on the source chain.
func (chain *TestChainTendermint) ConstructUpdateTMClientHeader(counterparty *TestChain, clientID string) (*ibctmtypes.Header, error) {
return chain.ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty, clientID, clienttypes.ZeroHeight())
// Relayer must query for LatestHeight on client to get TrustedHeight if the trusted height is not set
trustedHeight := chain.TC.GetClientState(clientID).GetLatestHeight().(clienttypes.Height)
return ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty, clientID, trustedHeight)
}

// ConstructUpdateTMClientHeader will construct a valid 07-tendermint Header to update the
// light client on the source chain.
func (chain *TestChainTendermint) ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty *TestChain, clientID string, trustedHeight clienttypes.Height) (*ibctmtypes.Header, error) {
func ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty *TestChain, clientID string, trustedHeight clienttypes.Height) (*ibctmtypes.Header, error) {
counterpartyTestChainTendermint := counterparty.TestChainClient.(*TestChainTendermint)
header := counterpartyTestChainTendermint.LastHeader
// Relayer must query for LatestHeight on client to get TrustedHeight if the trusted height is not set
if trustedHeight.IsZero() {
trustedHeight = chain.TC.GetClientState(clientID).GetLatestHeight().(clienttypes.Height)
}
require.False(counterparty.T, trustedHeight.IsZero())

var (
tmTrustedVals *tmtypes.ValidatorSet
ok bool
Expand Down
4 changes: 2 additions & 2 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (endpoint *Endpoint) CreateClient() (err error) {
// ensure counterparty has committed state
endpoint.Chain.Coordinator.CommitBlock(endpoint.Counterparty.Chain)

clientState := endpoint.Counterparty.Chain.TestChainClient.ClientConfigToState(endpoint.ClientConfig)
clientState := endpoint.Counterparty.Chain.TestChainClient.ClientConfigToState(endpoint.Counterparty.ClientConfig)
consensusState := endpoint.Counterparty.Chain.TestChainClient.GetConsensusState()

if err != nil {
Expand Down Expand Up @@ -106,7 +106,7 @@ func (endpoint *Endpoint) UpdateClient() (err error) {
// ensure counterparty has committed state
endpoint.Chain.Coordinator.CommitBlock(endpoint.Counterparty.Chain)

header, err := endpoint.Chain.TestChainClient.ConstructUpdateClientHeader(endpoint.Counterparty.Chain, endpoint.ClientID)
header, err := endpoint.Chain.ConstructUpdateClientHeader(endpoint.Counterparty.Chain, endpoint.ClientID)

if err != nil {
return err
Expand Down

0 comments on commit 40169cc

Please sign in to comment.