Skip to content

Commit

Permalink
updating misbehaviour checking
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Jul 15, 2022
1 parent 78ddf8e commit ec8e4c4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
18 changes: 18 additions & 0 deletions modules/light-clients/06-solomachine/misbehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ func (sd SignatureAndData) ValidateBasic() error {
return nil
}

// ValidateBasic ensures that the signature and data fields are non-empty.
func (sd SignatureAndDataV2) ValidateBasic() error {
if len(sd.Signature) == 0 {
return sdkerrors.Wrap(ErrInvalidSignatureAndData, "signature cannot be empty")
}
if len(sd.Data) == 0 {
return sdkerrors.Wrap(ErrInvalidSignatureAndData, "data for signature cannot be empty")
}
if len(sd.Path) == 0 {
return sdkerrors.Wrap(ErrInvalidSignatureAndData, "path for signature cannot be empty")
}
if sd.Timestamp == 0 {
return sdkerrors.Wrap(ErrInvalidSignatureAndData, "timestamp cannot be 0")
}

return nil
}

// TODO: Remove GetHeight()
// GetHeight implements the curret exported.Header interface, to be updated
func (misbehaviour Misbehaviour) GetHeight() exported.Height {
Expand Down
25 changes: 13 additions & 12 deletions modules/light-clients/06-solomachine/misbehaviour_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ package solomachine

import (
"github.com/cosmos/cosmos-sdk/codec"

commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types"
)

// verifySignatureAndData verifies that the currently registered public key has signed
// over the provided data and that the data is valid. The data is valid if it can be
// unmarshaled into the specified data type.
func (cs ClientState) verifySignatureAndData(cdc codec.BinaryCodec, misbehaviour *Misbehaviour, sigAndData *SignatureAndData) error {

func (cs ClientState) verifySignatureAndData(cdc codec.BinaryCodec, misbehaviour *Misbehaviour, sigAndData *SignatureAndDataV2) error {
// do not check misbehaviour timestamp since we want to allow processing of past misbehaviour

// ensure data can be unmarshaled to the specified data type
if _, err := UnmarshalDataByType(cdc, sigAndData.DataType, sigAndData.Data); err != nil {
if err := cdc.Unmarshal(sigAndData.Path, new(commitmenttypes.MerklePath)); err != nil {
return err
}

data, err := MisbehaviourSignBytes(
cdc,
misbehaviour.Sequence, sigAndData.Timestamp,
cs.ConsensusState.Diversifier,
sigAndData.DataType,
sigAndData.Data,
)
signBytes := SignBytesV2{
Sequence: misbehaviour.Sequence,
Timestamp: sigAndData.Timestamp,
Diversifier: cs.ConsensusState.Diversifier,
Path: sigAndData.Path,
Data: sigAndData.Data,
}

data, err := cdc.Marshal(&signBytes)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions modules/light-clients/06-solomachine/misbehaviour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ func (suite *SoloMachineTestSuite) TestMisbehaviourValidateBasic() {
false,
},
{
"data type for SignatureOne is unspecified",
"data path for SignatureOne is unspecified",
func(misbehaviour *solomachine.Misbehaviour) {
misbehaviour.SignatureOne.DataType = solomachine.UNSPECIFIED
misbehaviour.SignatureOne.Path = []byte{}
}, false,
},
{
"data type for SignatureTwo is unspecified",
"data path for SignatureTwo is unspecified",
func(misbehaviour *solomachine.Misbehaviour) {
misbehaviour.SignatureTwo.DataType = solomachine.UNSPECIFIED
misbehaviour.SignatureTwo.Path = []byte{}
}, false,
},
{
Expand Down
38 changes: 22 additions & 16 deletions testing/solomachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,52 +155,58 @@ func (solo *Solomachine) CreateHeader() *solomachinetypes.Header {
// CreateMisbehaviour constructs testing misbehaviour for the solo machine client
// by signing over two different data bytes at the same sequence.
func (solo *Solomachine) CreateMisbehaviour() *solomachinetypes.Misbehaviour {
path := solo.GetClientStatePath("counterparty")
dataOne, err := solomachinetypes.ClientStateDataBytes(solo.cdc, path, solo.ClientState())
merklePath := solo.GetClientStatePath("counterparty")
path, err := solo.cdc.Marshal(&merklePath)
require.NoError(solo.t, err)

path = solo.GetConsensusStatePath("counterparty", clienttypes.NewHeight(0, 1))
dataTwo, err := solomachinetypes.ConsensusStateDataBytes(solo.cdc, path, solo.ConsensusState())
data, err := solo.cdc.Marshal(solo.ClientState())
require.NoError(solo.t, err)

signBytes := &solomachinetypes.SignBytes{
signBytes := &solomachinetypes.SignBytesV2{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
DataType: solomachinetypes.CLIENT,
Data: dataOne,
Path: path,
Data: data,
}

bz, err := solo.cdc.Marshal(signBytes)
require.NoError(solo.t, err)

sig := solo.GenerateSignature(bz)
signatureOne := solomachinetypes.SignatureAndData{
signatureOne := solomachinetypes.SignatureAndDataV2{
Signature: sig,
DataType: solomachinetypes.CLIENT,
Data: dataOne,
Path: path,
Data: data,
Timestamp: solo.Time,
}

// misbehaviour signaturess can have different timestamps
solo.Time++

signBytes = &solomachinetypes.SignBytes{
merklePath = solo.GetConsensusStatePath("counterparty", clienttypes.NewHeight(0, 1))
path, err = solo.cdc.Marshal(&merklePath)
require.NoError(solo.t, err)

data, err = solo.cdc.Marshal(solo.ConsensusState())
require.NoError(solo.t, err)

signBytes = &solomachinetypes.SignBytesV2{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
DataType: solomachinetypes.CONSENSUS,
Data: dataTwo,
Path: path,
Data: data,
}

bz, err = solo.cdc.Marshal(signBytes)
require.NoError(solo.t, err)

sig = solo.GenerateSignature(bz)
signatureTwo := solomachinetypes.SignatureAndData{
signatureTwo := solomachinetypes.SignatureAndDataV2{
Signature: sig,
DataType: solomachinetypes.CONSENSUS,
Data: dataTwo,
Path: path,
Data: data,
Timestamp: solo.Time,
}

Expand Down

0 comments on commit ec8e4c4

Please sign in to comment.