Skip to content

Commit

Permalink
updating CheckForMisbehaviour codestyle and adding basic test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Mar 14, 2022
1 parent 0158c18 commit ae9290e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
10 changes: 3 additions & 7 deletions modules/light-clients/06-solomachine/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,11 @@ func (cs ClientState) CheckForMisbehaviour(
_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore,
msg exported.Header, // TODO: Update to exported.ClientMessage
) bool {
var foundMisbehaviour bool
switch msg.(type) {
case *Header:
foundMisbehaviour = false
case *Misbehaviour:
foundMisbehaviour = true
if _, ok := msg.(*Misbehaviour); ok {
return true
}

return foundMisbehaviour
return false
}

// UpdateStateOnMisbehaviour updates state upon misbehaviour. This method should only be called on misbehaviour
Expand Down
87 changes: 87 additions & 0 deletions modules/light-clients/06-solomachine/types/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,93 @@ func (suite *SoloMachineTestSuite) TestCheckHeaderAndUpdateState() {
}
}

func (suite *SoloMachineTestSuite) TestCheckForMisbehaviour() {
var (
clientMsg exported.Header // TODO: Update to ClientMessage interface
clientState exported.ClientState
)

// 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()

// TODO: Remove type assertion when ClientState interface includes CheckForMisbehaviour
smClientState, ok := clientState.(*types.ClientState)
if ok {
foundMisbehaviour := smClientState.CheckForMisbehaviour(suite.chainA.GetContext(), suite.chainA.Codec, nil, 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()

// TODO: Update to pass client store and make assertions on state changes
cs, _, _ := clientState.UpdateStateOnMisbehaviour(suite.chainA.GetContext(), suite.chainA.Codec, nil)

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

func (suite *SoloMachineTestSuite) TestVerifyClientMessageHeader() {
var (
clientMsg exported.Header // TODO: Update to ClientMessage interface
Expand Down

0 comments on commit ae9290e

Please sign in to comment.