Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dot/babe): use bs.latestFinalised instead of using round/set id #3167

Merged
merged 6 commits into from
Mar 28, 2023
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 {
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
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