Skip to content

Commit

Permalink
chore: move initialization logic to light client module method. Adjus…
Browse files Browse the repository at this point in the history
…t testing.
  • Loading branch information
DimitrisJim committed Mar 21, 2024
1 parent ee4549b commit 617e241
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 75 deletions.
15 changes: 0 additions & 15 deletions modules/light-clients/06-solomachine/client_state.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package solomachine

import (
"reflect"

errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

Expand Down Expand Up @@ -67,19 +65,6 @@ func (cs ClientState) Validate() error {
return cs.ConsensusState.ValidateBasic()
}

// Initialize checks that the initial consensus state is equal to the latest consensus state of the initial client and
// sets the client state in the provided client store.
func (cs ClientState) Initialize(_ sdk.Context, cdc codec.BinaryCodec, clientStore storetypes.KVStore, consState exported.ConsensusState) error {
if !reflect.DeepEqual(cs.ConsensusState, consState) {
return errorsmod.Wrapf(clienttypes.ErrInvalidConsensus, "consensus state in initial client does not equal initial consensus state. expected: %s, got: %s",
cs.ConsensusState, consState)
}

setClientState(clientStore, cdc, &cs)

return nil
}

// VerifyMembership is a generic proof verification method which verifies a proof of the existence of a value at a given CommitmentPath at the latest sequence.
// The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24).
func (cs *ClientState) VerifyMembership(
Expand Down
59 changes: 0 additions & 59 deletions modules/light-clients/06-solomachine/client_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v8/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
solomachine "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
ibcmock "github.com/cosmos/ibc-go/v8/testing/mock"
)
Expand Down Expand Up @@ -92,63 +90,6 @@ func (suite *SoloMachineTestSuite) TestClientStateValidateBasic() {
}
}

func (suite *SoloMachineTestSuite) TestInitialize() {
// test singlesig and multisig public keys
for _, sm := range []*ibctesting.Solomachine{suite.solomachine, suite.solomachineMulti} {
malleatedConsensus := sm.ClientState().ConsensusState
malleatedConsensus.Timestamp += 10

testCases := []struct {
name string
consState exported.ConsensusState
expPass bool
}{
{
"valid consensus state",
sm.ConsensusState(),
true,
},
{
"nil consensus state",
nil,
false,
},
{
"invalid consensus state: Tendermint consensus state",
&ibctm.ConsensusState{},
false,
},
{
"invalid consensus state: consensus state does not match consensus state in client",
malleatedConsensus,
false,
},
}

for _, tc := range testCases {
tc := tc

suite.Run(tc.name, func() {
suite.SetupTest()

store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), "solomachine")
err := sm.ClientState().Initialize(
suite.chainA.GetContext(), suite.chainA.Codec,
store, tc.consState,
)

if tc.expPass {
suite.Require().NoError(err, "valid testcase: %s failed", tc.name)
suite.Require().True(store.Has(host.ClientStateKey()))
} else {
suite.Require().Error(err, "invalid testcase: %s passed", tc.name)
suite.Require().False(store.Has(host.ClientStateKey()))
}
})
}
}
}

func (suite *SoloMachineTestSuite) TestVerifyMembership() {
// test singlesig and multisig public keys
for _, sm := range []*ibctesting.Solomachine{suite.solomachine, suite.solomachineMulti} {
Expand Down
12 changes: 11 additions & 1 deletion modules/light-clients/06-solomachine/light_client_module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package solomachine

import (
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism

errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -56,7 +58,15 @@ func (l LightClientModule) Initialize(ctx sdk.Context, clientID string, clientSt
}

clientStore := l.storeProvider.ClientStore(ctx, clientID)
return clientState.Initialize(ctx, l.cdc, clientStore, &consensusState)

if !reflect.DeepEqual(clientState.ConsensusState, &consensusState) {
return errorsmod.Wrapf(clienttypes.ErrInvalidConsensus, "consensus state in initial client does not equal initial consensus state. expected: %s, got: %s",
clientState.ConsensusState, &consensusState)
}

setClientState(clientStore, l.cdc, &clientState)

return nil
}

// VerifyClientMessage obtains the client state associated with the client identifier and calls into the clientState.VerifyClientMessage method.
Expand Down
63 changes: 63 additions & 0 deletions modules/light-clients/06-solomachine/light_client_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
solomachine "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
)

Expand All @@ -13,6 +14,68 @@ const (
wasmClientID = "08-wasm-0"
)

func (suite *SoloMachineTestSuite) TestInitialize() {
// test singlesig and multisig public keys
for _, sm := range []*ibctesting.Solomachine{suite.solomachine, suite.solomachineMulti} {
malleatedConsensus := sm.ClientState().ConsensusState
malleatedConsensus.Timestamp += 10

testCases := []struct {
name string
consState exported.ConsensusState
expPass bool
}{
{
"valid consensus state",
sm.ConsensusState(),
true,
},
{
"nil consensus state",
nil,
false,
},
{
"invalid consensus state: Tendermint consensus state",
&ibctm.ConsensusState{},
false,
},
{
"invalid consensus state: consensus state does not match consensus state in client",
malleatedConsensus,
false,
},
}

for _, tc := range testCases {
tc := tc

suite.Run(tc.name, func() {
suite.SetupTest()

clientStateBz := suite.chainA.Codec.MustMarshal(sm.ClientState())
consStateBz := suite.chainA.Codec.MustMarshal(tc.consState)

clientID := suite.chainA.App.GetIBCKeeper().ClientKeeper.GenerateClientIdentifier(suite.chainA.GetContext(), exported.Solomachine)

lcm, found := suite.chainA.GetSimApp().IBCKeeper.ClientKeeper.Route(clientID)
suite.Require().True(found)

err := lcm.Initialize(suite.chainA.GetContext(), clientID, clientStateBz, consStateBz)
store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), clientID)

if tc.expPass {
suite.Require().NoError(err, "valid testcase: %s failed", tc.name)
suite.Require().True(store.Has(host.ClientStateKey()))
} else {
suite.Require().Error(err, "invalid testcase: %s passed", tc.name)
suite.Require().False(store.Has(host.ClientStateKey()))
}
})
}
}
}

func (suite *SoloMachineTestSuite) TestRecoverClient() {
var (
subjectClientID, substituteClientID string
Expand Down

0 comments on commit 617e241

Please sign in to comment.