Add solo machine timestamp check (#7392)

* add check in header updates for non decreasing timestamp

Add check in update.go that the header timestamp is non decreasing compared to the consensus state timestamp. Unit test added in update_test.go

* update error message

* update godoc

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
colin axnér 2020-09-25 18:22:52 +02:00 committed by GitHub
parent 0ddb2bd74f
commit 2a4d0ec62c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -11,6 +11,8 @@ import (
// CheckHeaderAndUpdateState checks if the provided header is valid and updates
// the consensus state if appropriate. It returns an error if:
// - the client or header provided are not parseable to solo machine types
// - the header sequence does not match the current sequence
// - the header timestamp is less than the consensus state timestamp
// - the currently registered public key did not provide the update signature
func (cs ClientState) CheckHeaderAndUpdateState(
ctx sdk.Context, cdc codec.BinaryMarshaler, clientStore sdk.KVStore,
@ -37,7 +39,15 @@ func checkHeader(cdc codec.BinaryMarshaler, clientState *ClientState, header *He
if header.Sequence != clientState.Sequence {
return sdkerrors.Wrapf(
clienttypes.ErrInvalidHeader,
"sequence provided in the header does not match the client state sequence (%d != %d)", header.Sequence, clientState.Sequence,
"header sequence does not match the client state sequence (%d != %d)", header.Sequence, clientState.Sequence,
)
}
// assert update timestamp is not less than current consensus state timestamp
if header.Timestamp < clientState.ConsensusState.Timestamp {
return sdkerrors.Wrapf(
clienttypes.ErrInvalidHeader,
"header timestamp is less than to the consensus state timestamp (%d < %d)", header.Timestamp, clientState.ConsensusState.Timestamp,
)
}

View File

@ -54,6 +54,15 @@ func (suite *SoloMachineTestSuite) TestCheckHeaderAndUpdateState() {
},
false,
},
{
"invalid timestamp in header",
func() {
clientState = suite.solomachine.ClientState()
h := suite.solomachine.CreateHeader()
h.Timestamp--
header = h
}, false,
},
{
"signature uses wrong sequence",
func() {