Skip to content

Commit 72da44e

Browse files
committed
comment out pause/catch up logic, store highest setID/round in db
1 parent 2946d6b commit 72da44e

File tree

15 files changed

+142
-70
lines changed

15 files changed

+142
-70
lines changed

dot/core/interface.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type BlockState interface {
4141
GetSlotForBlock(common.Hash) (uint64, error)
4242
GetFinalisedHeader(uint64, uint64) (*types.Header, error)
4343
GetFinalisedHash(uint64, uint64) (common.Hash, error)
44-
SetFinalisedHash(common.Hash, uint64, uint64) error
4544
RegisterImportedChannel(ch chan<- *types.Block) (byte, error)
4645
UnregisterImportedChannel(id byte)
4746
RegisterFinalizedChannel(ch chan<- *types.FinalisationInfo) (byte, error)

dot/network/service.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (s *Service) sentBlockIntervalTelemetry() {
343343
}
344344
bestHash := best.Hash()
345345

346-
finalized, err := s.blockState.GetFinalisedHeader(0, 0) //nolint
346+
finalized, err := s.blockState.GetHighestFinalisedHeader() //nolint
347347
if err != nil {
348348
continue
349349
}
@@ -519,16 +519,21 @@ func (s *Service) GossipMessage(msg NotificationsMessage) {
519519
func (s *Service) SendMessage(to peer.ID, msg NotificationsMessage) error {
520520
s.notificationsMu.Lock()
521521
defer s.notificationsMu.Unlock()
522+
522523
for msgID, prtl := range s.notificationsProtocols {
523-
if msg.Type() != msgID || prtl == nil {
524+
if msg.Type() != msgID {
524525
continue
525526
}
527+
526528
hs, err := prtl.getHandshake()
527529
if err != nil {
528530
return err
529531
}
532+
530533
s.sendData(to, hs, prtl, msg)
534+
return nil
531535
}
536+
532537
return errors.New("message not supported by any notifications protocol")
533538
}
534539

dot/network/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type BlockState interface {
2929
BestBlockNumber() (*big.Int, error)
3030
GenesisHash() common.Hash
3131
HasBlockBody(common.Hash) (bool, error)
32-
GetFinalisedHeader(round, setID uint64) (*types.Header, error)
32+
GetHighestFinalisedHeader() (*types.Header, error)
3333
GetHashByNumber(num *big.Int) (common.Hash, error)
3434
}
3535

dot/network/sync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (q *syncQueue) benchmark() {
351351
goal := atomic.LoadInt64(&q.goal)
352352

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

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

817817
if errors.Is(err, chaindb.ErrKeyNotFound) || errors.Is(err, blocktree.ErrParentNotFound) {
818-
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0)
818+
finalised, err := q.s.blockState.GetHighestFinalisedHeader()
819819
if err != nil {
820820
panic(err)
821821
}

dot/state/block.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ func NewBlockStateFromGenesis(db chaindb.Database, header *types.Header) (*Block
123123

124124
bs.genesisHash = header.Hash()
125125

126+
if err := bs.db.Put(highestRoundAndSetIDKey, roundSetIDKey(0, 0)); err != nil {
127+
return nil, err
128+
}
129+
126130
// set the latest finalised head to the genesis header
127131
if err := bs.SetFinalisedHash(bs.genesisHash, 0, 0); err != nil {
128132
return nil, err

dot/state/block_finalisation.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
package state
1818

1919
import (
20+
"encoding/binary"
2021
"fmt"
2122
"math/big"
2223

2324
"github.com/ChainSafe/gossamer/dot/types"
2425
"github.com/ChainSafe/gossamer/lib/common"
2526
)
2627

28+
var highestRoundAndSetIDKey = []byte("hrs")
29+
2730
// finalisedHashKey = FinalizedBlockHashKey + round + setID (LE encoded)
2831
func finalisedHashKey(round, setID uint64) []byte {
2932
return append(common.FinalizedBlockHashKey, roundSetIDKey(round, setID)...)
@@ -69,8 +72,63 @@ func (bs *BlockState) GetFinalisedHash(round, setID uint64) (common.Hash, error)
6972
return common.NewHash(h), nil
7073
}
7174

75+
func roundSetIDNum(round, setID uint64) *big.Int {
76+
buf := make([]byte, 8)
77+
binary.BigEndian.PutUint64(buf, round)
78+
buf2 := make([]byte, 8)
79+
binary.BigEndian.PutUint64(buf2, setID)
80+
return big.NewInt(0).SetBytes(append(buf, buf2...))
81+
}
82+
83+
func (bs *BlockState) setHighestRoundAndSetID(round, setID uint64) error {
84+
currRound, currSetID, err := bs.getHighestRoundAndSetID()
85+
if err != nil {
86+
return err
87+
}
88+
89+
if roundSetIDNum(round, setID).Cmp(roundSetIDNum(currRound, currSetID)) <= 0 {
90+
return nil
91+
}
92+
93+
return bs.db.Put(highestRoundAndSetIDKey, roundSetIDKey(round, setID))
94+
}
95+
96+
func (bs *BlockState) getHighestRoundAndSetID() (uint64, uint64, error) {
97+
b, err := bs.db.Get(highestRoundAndSetIDKey)
98+
if err != nil {
99+
return 0, 0, err
100+
}
101+
102+
round := binary.LittleEndian.Uint64(b[:8])
103+
setID := binary.LittleEndian.Uint64(b[8:16])
104+
return round, setID, nil
105+
}
106+
107+
func (bs *BlockState) GetHighestFinalisedHash() (common.Hash, error) {
108+
round, setID, err := bs.getHighestRoundAndSetID()
109+
if err != nil {
110+
return common.Hash{}, err
111+
}
112+
113+
return bs.GetFinalisedHash(round, setID)
114+
}
115+
116+
// GetHighestFinalisedHeader returns the most recently finalised block header
117+
func (bs *BlockState) GetHighestFinalisedHeader() (*types.Header, error) {
118+
h, err := bs.GetHighestFinalisedHash()
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
header, err := bs.GetHeader(h)
124+
if err != nil {
125+
return nil, err
126+
}
127+
128+
return header, nil
129+
}
130+
72131
// SetFinalisedHash sets the latest finalised block header
73-
// Note that using round=0 and setID=0 would refer to the latest finalised hash
74132
func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) error {
75133
bs.Lock()
76134
defer bs.Unlock()
@@ -114,7 +172,12 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er
114172
}
115173

116174
bs.lastFinalised = hash
117-
return bs.db.Put(finalisedHashKey(round, setID), hash[:])
175+
176+
if err := bs.db.Put(finalisedHashKey(round, setID), hash[:]); err != nil {
177+
return err
178+
}
179+
180+
return bs.setHighestRoundAndSetID(round, setID)
118181
}
119182

120183
func (bs *BlockState) setFirstSlotOnFinalisation() error {

dot/sync/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type Verifier interface {
7878

7979
// FinalityGadget implements justification verification functionality
8080
type FinalityGadget interface {
81-
VerifyBlockJustification([]byte) error
81+
VerifyBlockJustification(common.Hash, []byte) error
8282
}
8383

8484
// BlockImportHandler is the interface for the handler of newly imported blocks

dot/sync/mocks/FinalityGadget.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/sync/mocks/finality_gadget.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

dot/sync/syncer.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,12 @@ func (s *Service) handleJustification(header *types.Header, justification []byte
366366
return
367367
}
368368

369-
err := s.finalityGadget.VerifyBlockJustification(justification)
369+
err := s.finalityGadget.VerifyBlockJustification(header.Hash(), justification)
370370
if err != nil {
371371
logger.Warn("failed to verify block justification", "hash", header.Hash(), "number", header.Number, "error", err)
372372
return
373373
}
374374

375-
err = s.blockState.SetFinalisedHash(header.Hash(), 0, 0)
376-
if err != nil {
377-
logger.Error("failed to set finalised hash", "error", err)
378-
return
379-
}
380-
381375
err = s.blockState.SetJustification(header.Hash(), justification)
382376
if err != nil {
383377
logger.Error("failed tostore justification", "error", err)

0 commit comments

Comments
 (0)