diff --git a/dot/state/block_finalisation.go b/dot/state/block_finalisation.go index 9b8e1f5e0c..0666c72f9e 100644 --- a/dot/state/block_finalisation.go +++ b/dot/state/block_finalisation.go @@ -5,6 +5,7 @@ package state import ( "encoding/binary" + "errors" "fmt" "github.com/ChainSafe/gossamer/dot/telemetry" @@ -12,6 +13,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" ) +var errSetIDLowerThanHighest = errors.New("set id lower than highest") var highestRoundAndSetIDKey = []byte("hrs") // finalisedHashKey = FinalizedBlockHashKey + round + setID (LE encoded) @@ -63,14 +65,13 @@ func (bs *BlockState) GetFinalisedHash(round, setID uint64) (common.Hash, error) } func (bs *BlockState) setHighestRoundAndSetID(round, setID uint64) error { - currRound, currSetID, err := bs.GetHighestRoundAndSetID() + _, highestSetID, err := bs.GetHighestRoundAndSetID() if err != nil { return err } - // higher setID takes precedence over round - if setID < currSetID || setID == currSetID && round <= currRound { - return nil + if setID < highestSetID { + return fmt.Errorf("%w: %d should be greater or equal %d", errSetIDLowerThanHighest, setID, highestSetID) } return bs.db.Put(highestRoundAndSetIDKey, roundAndSetIDToBytes(round, setID)) @@ -206,16 +207,7 @@ func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error { return nil } - prev, err := bs.GetHighestFinalisedHash() - if err != nil { - return fmt.Errorf("failed to get highest finalised hash: %w", err) - } - - if prev == curr { - return nil - } - - subchain, err := bs.RangeInMemory(prev, curr) + subchain, err := bs.RangeInMemory(bs.lastFinalised, curr) if err != nil { return err } diff --git a/dot/state/block_finalisation_test.go b/dot/state/block_finalisation_test.go index aa62f591a4..0834529bf0 100644 --- a/dot/state/block_finalisation_test.go +++ b/dot/state/block_finalisation_test.go @@ -36,12 +36,14 @@ func TestHighestRoundAndSetID(t *testing.T) { require.Equal(t, uint64(10), round) require.Equal(t, uint64(0), setID) + // is possible to have a lower round number + // in the same set ID: https://github.com/ChainSafe/gossamer/issues/3150 err = bs.setHighestRoundAndSetID(9, 0) require.NoError(t, err) round, setID, err = bs.GetHighestRoundAndSetID() require.NoError(t, err) - require.Equal(t, uint64(10), round) + require.Equal(t, uint64(9), round) require.Equal(t, uint64(0), setID) err = bs.setHighestRoundAndSetID(0, 1) @@ -53,7 +55,9 @@ func TestHighestRoundAndSetID(t *testing.T) { require.Equal(t, uint64(1), setID) err = bs.setHighestRoundAndSetID(100000, 0) - require.NoError(t, err) + require.ErrorIs(t, err, errSetIDLowerThanHighest) + const expectedErrorMessage = "set id lower than highest: 0 should be greater or equal 1" + require.EqualError(t, err, expectedErrorMessage) round, setID, err = bs.GetHighestRoundAndSetID() require.NoError(t, err)