Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 06-solomachine rename update to UpdateState #1110

Merged
merged 14 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions modules/light-clients/06-solomachine/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ func (cs ClientState) CheckHeaderAndUpdateState(
return nil, nil, err
}

// TODO: Remove this type assertion, replace with misbehaviour checking and update state
smHeader, ok := msg.(*Header)
if !ok {
return nil, nil, sdkerrors.Wrapf(
clienttypes.ErrInvalidHeader, "expected %T, got %T", &Header{}, msg,
)
}

clientState, consensusState := update(&cs, smHeader)
return clientState, consensusState, nil
return cs.UpdateState(ctx, cdc, clientStore, msg)
}

// VerifyClientMessage introspects the provided ClientMessage and checks its validity
Expand Down Expand Up @@ -105,16 +96,16 @@ func (cs ClientState) verifyMisbehaviour(ctx sdk.Context, cdc codec.BinaryCodec,
return nil
}

// update the consensus state to the new public key and an incremented sequence
func update(clientState *ClientState, header *Header) (*ClientState, *ConsensusState) {
// UpdateState updates the consensus state to the new public key and an incremented sequence.
func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) (exported.ClientState, exported.ConsensusState, error) {
smHeader := clientMsg.(*Header)
consensusState := &ConsensusState{
PublicKey: header.NewPublicKey,
Diversifier: header.NewDiversifier,
Timestamp: header.Timestamp,
PublicKey: smHeader.NewPublicKey,
Diversifier: smHeader.NewDiversifier,
Timestamp: smHeader.Timestamp,
}

// increment sequence number
clientState.Sequence++
clientState.ConsensusState = consensusState
return clientState, consensusState
cs.Sequence++
cs.ConsensusState = consensusState
return &cs, consensusState, nil
}
57 changes: 55 additions & 2 deletions modules/light-clients/06-solomachine/types/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (suite *SoloMachineTestSuite) TestVerifyClientMessageHeader() {
// setup test
tc.setup()

err := clientState.VerifyClientMessage(suite.chainA.GetContext(), suite.chainA.Codec, nil, clientMsg)
err := clientState.VerifyClientMessage(suite.chainA.GetContext(), suite.chainA.Codec, suite.store, clientMsg)

if tc.expPass {
suite.Require().NoError(err)
Expand Down Expand Up @@ -560,7 +560,7 @@ func (suite *SoloMachineTestSuite) TestVerifyClientMessageMisbehaviour() {
// setup test
tc.setup()

err := clientState.VerifyClientMessage(suite.chainA.GetContext(), suite.chainA.Codec, nil, clientMsg)
err := clientState.VerifyClientMessage(suite.chainA.GetContext(), suite.chainA.Codec, suite.store, clientMsg)

if tc.expPass {
suite.Require().NoError(err)
Expand All @@ -571,3 +571,56 @@ func (suite *SoloMachineTestSuite) TestVerifyClientMessageMisbehaviour() {
}
}
}

func (suite *SoloMachineTestSuite) TestUpdateState() {
var (
clientState exported.ClientState
clientMsg exported.ClientMessage
)

// test singlesig and multisig public keys
for _, solomachine := range []*ibctesting.Solomachine{suite.solomachine, suite.solomachineMulti} {

testCases := []struct {
name string
setup func()
expPass bool
}{
{
"successful update",
func() {
clientState = solomachine.ClientState()
clientMsg = solomachine.CreateHeader()
},
true,
},
}

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

suite.Run(tc.name, func() {
// setup test
tc.setup()

clientState, ok := clientState.(*types.ClientState)
if ok {
cs, consensusState, err := clientState.UpdateState(suite.chainA.GetContext(), suite.chainA.Codec, suite.store, clientMsg)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().Equal(clientMsg.(*types.Header).NewPublicKey, cs.(*types.ClientState).ConsensusState.PublicKey)
suite.Require().Equal(false, cs.(*types.ClientState).IsFrozen)
suite.Require().Equal(clientMsg.(*types.Header).Sequence+1, cs.(*types.ClientState).Sequence)
suite.Require().Equal(consensusState, cs.(*types.ClientState).ConsensusState)
} else {
suite.Require().Error(err)
suite.Require().Nil(clientState)
suite.Require().Nil(consensusState)
}
}

})
}
}
}