Skip to content

Commit

Permalink
chore: 06-solomachine adding CheckForMisbehaviour and UpdateStateOnMi…
Browse files Browse the repository at this point in the history
…sbehaviour (cosmos#1111)

* adding VerifyClientMessage and tests

* splitting tests for verify header and misbehaviour

* rename update to UpdateState

* adding CheckForMisbehaviour and UpdateStateOnMisbehaviour

* adding misbehaviour checks

* updating CheckForMisbehaviour codestyle and adding basic test cases
  • Loading branch information
damiannolan authored and seunlanlege committed Aug 9, 2022
1 parent 291c9a0 commit 4824ffd
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
23 changes: 23 additions & 0 deletions modules/light-clients/06-solomachine/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func (cs ClientState) CheckHeaderAndUpdateState(
return nil, nil, err
}

foundMisbehaviour := cs.CheckForMisbehaviour(ctx, cdc, clientStore, msg)
if foundMisbehaviour {
return cs.UpdateStateOnMisbehaviour(ctx, cdc, clientStore)
}

return cs.UpdateState(ctx, cdc, clientStore, msg)
}

Expand Down Expand Up @@ -109,3 +114,21 @@ func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, client
cs.ConsensusState = consensusState
return &cs, consensusState, nil
}

// CheckForMisbehaviour returns true for type Misbehaviour (passed VerifyClientMessage check), otherwise returns false
func (cs ClientState) CheckForMisbehaviour(_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore, clientMsg exported.ClientMessage) bool {
if _, ok := clientMsg.(*Misbehaviour); ok {
return true
}

return false
}

// UpdateStateOnMisbehaviour updates state upon misbehaviour. This method should only be called on misbehaviour
// as it does not perform any misbehaviour checks.
func (cs ClientState) UpdateStateOnMisbehaviour(
_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore, // prematurely include args for self storage of consensus state
) (*ClientState, exported.ConsensusState, error) {
cs.IsFrozen = true
return &cs, cs.ConsensusState, nil
}
82 changes: 82 additions & 0 deletions modules/light-clients/06-solomachine/types/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,85 @@ func (suite *SoloMachineTestSuite) TestUpdateState() {
}
}
}

func (suite *SoloMachineTestSuite) TestCheckForMisbehaviour() {
var (
clientMsg exported.ClientMessage
)

// test singlesig and multisig public keys
for _, solomachine := range []*ibctesting.Solomachine{suite.solomachine, suite.solomachineMulti} {
testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"success",
func() {
clientMsg = solomachine.CreateMisbehaviour()
},
true,
},
{
"normal header returns false",
func() {
clientMsg = solomachine.CreateHeader()
},
false,
},
}

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

suite.Run(tc.name, func() {
clientState := solomachine.ClientState()

tc.malleate()

foundMisbehaviour := clientState.CheckForMisbehaviour(suite.chainA.GetContext(), suite.chainA.Codec, suite.store, clientMsg)

if tc.expPass {
suite.Require().True(foundMisbehaviour)
} else {
suite.Require().False(foundMisbehaviour)
}

})
}
}
}

func (suite *SoloMachineTestSuite) TestUpdateStateOnMisbehaviour() {
// test singlesig and multisig public keys
for _, solomachine := range []*ibctesting.Solomachine{suite.solomachine, suite.solomachineMulti} {
testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"success",
func() {},
true,
},
}

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

suite.Run(tc.name, func() {
clientState := solomachine.ClientState()

tc.malleate()

cs, _, _ := clientState.UpdateStateOnMisbehaviour(suite.chainA.GetContext(), suite.chainA.Codec, suite.store)

if tc.expPass {
suite.Require().True(cs.IsFrozen)
}
})
}
}
}

0 comments on commit 4824ffd

Please sign in to comment.