Skip to content

Commit

Permalink
fix(dot/babe): use bs.latestFinalised instead of using `round/set i…
Browse files Browse the repository at this point in the history
…d` (#3167)
  • Loading branch information
EclesioMeloJunior committed Mar 28, 2023
1 parent e028a1b commit 46c0ef7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
20 changes: 6 additions & 14 deletions dot/state/block_finalisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package state

import (
"encoding/binary"
"errors"
"fmt"

"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
"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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions dot/state/block_finalisation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 46c0ef7

Please sign in to comment.