Skip to content

Commit

Permalink
comment out pause/catch up logic, store highest setID/round in db
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Jul 27, 2021
1 parent 2946d6b commit 72da44e
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 70 deletions.
1 change: 0 additions & 1 deletion dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type BlockState interface {
GetSlotForBlock(common.Hash) (uint64, error)
GetFinalisedHeader(uint64, uint64) (*types.Header, error)
GetFinalisedHash(uint64, uint64) (common.Hash, error)
SetFinalisedHash(common.Hash, uint64, uint64) error
RegisterImportedChannel(ch chan<- *types.Block) (byte, error)
UnregisterImportedChannel(id byte)
RegisterFinalizedChannel(ch chan<- *types.FinalisationInfo) (byte, error)
Expand Down
9 changes: 7 additions & 2 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (s *Service) sentBlockIntervalTelemetry() {
}
bestHash := best.Hash()

finalized, err := s.blockState.GetFinalisedHeader(0, 0) //nolint
finalized, err := s.blockState.GetHighestFinalisedHeader() //nolint
if err != nil {
continue
}
Expand Down Expand Up @@ -519,16 +519,21 @@ func (s *Service) GossipMessage(msg NotificationsMessage) {
func (s *Service) SendMessage(to peer.ID, msg NotificationsMessage) error {
s.notificationsMu.Lock()
defer s.notificationsMu.Unlock()

for msgID, prtl := range s.notificationsProtocols {
if msg.Type() != msgID || prtl == nil {
if msg.Type() != msgID {
continue
}

hs, err := prtl.getHandshake()
if err != nil {
return err
}

s.sendData(to, hs, prtl, msg)
return nil
}

return errors.New("message not supported by any notifications protocol")
}

Expand Down
2 changes: 1 addition & 1 deletion dot/network/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type BlockState interface {
BestBlockNumber() (*big.Int, error)
GenesisHash() common.Hash
HasBlockBody(common.Hash) (bool, error)
GetFinalisedHeader(round, setID uint64) (*types.Header, error)
GetHighestFinalisedHeader() (*types.Header, error)
GetHashByNumber(num *big.Int) (common.Hash, error)
}

Expand Down
6 changes: 3 additions & 3 deletions dot/network/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (q *syncQueue) benchmark() {
goal := atomic.LoadInt64(&q.goal)

if before.Number.Int64() >= goal {
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0) //nolint
finalised, err := q.s.blockState.GetHighestFinalisedHeader() //nolint
if err != nil {
continue
}
Expand Down Expand Up @@ -767,7 +767,7 @@ func (q *syncQueue) handleBlockJustification(data []*types.BlockData) {
}

func (q *syncQueue) handleBlockData(data []*types.BlockData) {
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0)
finalised, err := q.s.blockState.GetHighestFinalisedHeader()
if err != nil {
panic(err) // this should never happen
}
Expand Down Expand Up @@ -815,7 +815,7 @@ func (q *syncQueue) handleBlockDataFailure(idx int, err error, data []*types.Blo
logger.Warn("failed to handle block data", "failed on block", q.currStart+int64(idx), "error", err)

if errors.Is(err, chaindb.ErrKeyNotFound) || errors.Is(err, blocktree.ErrParentNotFound) {
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0)
finalised, err := q.s.blockState.GetHighestFinalisedHeader()
if err != nil {
panic(err)
}
Expand Down
4 changes: 4 additions & 0 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func NewBlockStateFromGenesis(db chaindb.Database, header *types.Header) (*Block

bs.genesisHash = header.Hash()

if err := bs.db.Put(highestRoundAndSetIDKey, roundSetIDKey(0, 0)); err != nil {
return nil, err
}

// set the latest finalised head to the genesis header
if err := bs.SetFinalisedHash(bs.genesisHash, 0, 0); err != nil {
return nil, err
Expand Down
67 changes: 65 additions & 2 deletions dot/state/block_finalisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package state

import (
"encoding/binary"
"fmt"
"math/big"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
)

var highestRoundAndSetIDKey = []byte("hrs")

// finalisedHashKey = FinalizedBlockHashKey + round + setID (LE encoded)
func finalisedHashKey(round, setID uint64) []byte {
return append(common.FinalizedBlockHashKey, roundSetIDKey(round, setID)...)
Expand Down Expand Up @@ -69,8 +72,63 @@ func (bs *BlockState) GetFinalisedHash(round, setID uint64) (common.Hash, error)
return common.NewHash(h), nil
}

func roundSetIDNum(round, setID uint64) *big.Int {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, round)
buf2 := make([]byte, 8)
binary.BigEndian.PutUint64(buf2, setID)
return big.NewInt(0).SetBytes(append(buf, buf2...))
}

func (bs *BlockState) setHighestRoundAndSetID(round, setID uint64) error {
currRound, currSetID, err := bs.getHighestRoundAndSetID()
if err != nil {
return err
}

if roundSetIDNum(round, setID).Cmp(roundSetIDNum(currRound, currSetID)) <= 0 {
return nil
}

return bs.db.Put(highestRoundAndSetIDKey, roundSetIDKey(round, setID))
}

func (bs *BlockState) getHighestRoundAndSetID() (uint64, uint64, error) {
b, err := bs.db.Get(highestRoundAndSetIDKey)
if err != nil {
return 0, 0, err
}

round := binary.LittleEndian.Uint64(b[:8])
setID := binary.LittleEndian.Uint64(b[8:16])
return round, setID, nil
}

func (bs *BlockState) GetHighestFinalisedHash() (common.Hash, error) {
round, setID, err := bs.getHighestRoundAndSetID()
if err != nil {
return common.Hash{}, err
}

return bs.GetFinalisedHash(round, setID)
}

// GetHighestFinalisedHeader returns the most recently finalised block header
func (bs *BlockState) GetHighestFinalisedHeader() (*types.Header, error) {
h, err := bs.GetHighestFinalisedHash()
if err != nil {
return nil, err
}

header, err := bs.GetHeader(h)
if err != nil {
return nil, err
}

return header, nil
}

// SetFinalisedHash sets the latest finalised block header
// Note that using round=0 and setID=0 would refer to the latest finalised hash
func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) error {
bs.Lock()
defer bs.Unlock()
Expand Down Expand Up @@ -114,7 +172,12 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er
}

bs.lastFinalised = hash
return bs.db.Put(finalisedHashKey(round, setID), hash[:])

if err := bs.db.Put(finalisedHashKey(round, setID), hash[:]); err != nil {
return err
}

return bs.setHighestRoundAndSetID(round, setID)
}

func (bs *BlockState) setFirstSlotOnFinalisation() error {
Expand Down
2 changes: 1 addition & 1 deletion dot/sync/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Verifier interface {

// FinalityGadget implements justification verification functionality
type FinalityGadget interface {
VerifyBlockJustification([]byte) error
VerifyBlockJustification(common.Hash, []byte) error
}

// BlockImportHandler is the interface for the handler of newly imported blocks
Expand Down
27 changes: 27 additions & 0 deletions dot/sync/mocks/FinalityGadget.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions dot/sync/mocks/finality_gadget.go

This file was deleted.

8 changes: 1 addition & 7 deletions dot/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,12 @@ func (s *Service) handleJustification(header *types.Header, justification []byte
return
}

err := s.finalityGadget.VerifyBlockJustification(justification)
err := s.finalityGadget.VerifyBlockJustification(header.Hash(), justification)
if err != nil {
logger.Warn("failed to verify block justification", "hash", header.Hash(), "number", header.Number, "error", err)
return
}

err = s.blockState.SetFinalisedHash(header.Hash(), 0, 0)
if err != nil {
logger.Error("failed to set finalised hash", "error", err)
return
}

err = s.blockState.SetJustification(header.Hash(), justification)
if err != nil {
logger.Error("failed tostore justification", "error", err)
Expand Down
4 changes: 2 additions & 2 deletions dot/sync/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import (
)

// NewMockFinalityGadget create and return sync FinalityGadget interface mock
func NewMockFinalityGadget() *syncmocks.MockFinalityGadget {
m := new(syncmocks.MockFinalityGadget)
func NewMockFinalityGadget() *syncmocks.FinalityGadget {
m := new(syncmocks.FinalityGadget)
// using []uint8 instead of []byte: https://github.com/stretchr/testify/pull/969
m.On("VerifyBlockJustification", mock.AnythingOfType("[]uint8")).Return(nil)
return m
Expand Down
9 changes: 6 additions & 3 deletions lib/grandpa/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ package grandpa

import (
"errors"
"fmt"

"github.com/ChainSafe/gossamer/lib/blocktree"
)

// errRoundMismatch is returned when trying to validate a vote message that isn't for the current round
func errRoundMismatch(got, want uint64) error {
return fmt.Errorf("rounds do not match: got %d, want %d", got, want)
}

//nolint
var (
ErrNilBlockState = errors.New("cannot have nil BlockState")
Expand All @@ -39,9 +45,6 @@ var (
// ErrSetIDMismatch is returned when trying to validate a vote message with an invalid voter set ID, or when receiving a catch up message with a different set ID
ErrSetIDMismatch = errors.New("set IDs do not match")

// ErrRoundMismatch is returned when trying to validate a vote message that isn't for the current round
ErrRoundMismatch = errors.New("rounds do not match")

// ErrEquivocation is returned when trying to validate a vote for that is equivocatory
ErrEquivocation = errors.New("vote is equivocatory")

Expand Down
16 changes: 7 additions & 9 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ func (s *Service) initiate() error {
}

if err != nil {
return err
logger.Warn("play grandpa round error", "error", err)
continue
}

if s.ctx.Err() != nil {
Expand Down Expand Up @@ -802,12 +803,7 @@ func (s *Service) finalise() error {
return err
}

if err = s.grandpaState.SetLatestRound(s.state.round); err != nil {
return err
}

// set latest finalised head in db
return s.blockState.SetFinalisedHash(bfc.Hash, 0, 0)
return s.grandpaState.SetLatestRound(s.state.round)
}

// createJustification collects the signed precommits received for this round and turns them into
Expand Down Expand Up @@ -1015,8 +1011,10 @@ func (s *Service) getPreVotedBlock() (Vote, error) {
func (s *Service) getGrandpaGHOST() (Vote, error) {
threshold := s.state.threshold()

var blocks map[common.Hash]uint32
var err error
var (
blocks map[common.Hash]uint32
err error
)

for {
blocks, err = s.getPossibleSelectedBlocks(prevote, threshold)
Expand Down
Loading

0 comments on commit 72da44e

Please sign in to comment.