Skip to content

Commit 0e867ae

Browse files
committed
Update grandpa authorities
1 parent 9498268 commit 0e867ae

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

dot/state/grandpa.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewGrandpaStateFromGenesis(db database.Database, bs *BlockState,
6666
return nil, fmt.Errorf("cannot set latest round: %w", err)
6767
}
6868

69-
if err := s.setAuthorities(genesisSetID, genesisAuthorities); err != nil {
69+
if err := s.SetAuthorities(genesisSetID, genesisAuthorities); err != nil {
7070
return nil, fmt.Errorf("cannot set authorities: %w", err)
7171
}
7272

@@ -187,7 +187,7 @@ func (s *GrandpaState) ApplyScheduledChanges(finalizedHeader *types.Header) erro
187187
}
188188

189189
grandpaVotersAuthorities := types.NewGrandpaVotersFromAuthorities(changeToApply.change.nextAuthorities)
190-
err = s.setAuthorities(newSetID, grandpaVotersAuthorities)
190+
err = s.SetAuthorities(newSetID, grandpaVotersAuthorities)
191191
if err != nil {
192192
return fmt.Errorf("cannot set authorities: %w", err)
193193
}
@@ -259,7 +259,7 @@ func (s *GrandpaState) ApplyForcedChanges(importedBlockHeader *types.Header) err
259259
}
260260

261261
grandpaVotersAuthorities := types.NewGrandpaVotersFromAuthorities(forcedChange.nextAuthorities)
262-
err = s.setAuthorities(newSetID, grandpaVotersAuthorities)
262+
err = s.SetAuthorities(newSetID, grandpaVotersAuthorities)
263263
if err != nil {
264264
return fmt.Errorf("cannot set authorities: %w", err)
265265
}
@@ -334,8 +334,8 @@ func setIDChangeKey(setID uint64) []byte {
334334
return append(setIDChangePrefix, buf...)
335335
}
336336

337-
// setAuthorities sets the authorities for a given setID
338-
func (s *GrandpaState) setAuthorities(setID uint64, authorities []types.GrandpaVoter) error {
337+
// SetAuthorities sets the authorities for a given setID
338+
func (s *GrandpaState) SetAuthorities(setID uint64, authorities []types.GrandpaVoter) error {
339339
enc, err := types.EncodeGrandpaVoters(authorities)
340340
if err != nil {
341341
return err
@@ -407,7 +407,7 @@ func (s *GrandpaState) SetNextChange(authorities []types.GrandpaVoter, number ui
407407
}
408408

409409
nextSetID := currSetID + 1
410-
err = s.setAuthorities(nextSetID, authorities)
410+
err = s.SetAuthorities(nextSetID, authorities)
411411
if err != nil {
412412
return err
413413
}

dot/sync/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const (
5050
type GrandpaState interface {
5151
GetCurrentSetID() (uint64, error)
5252
GetAuthorities(uint64) ([]types.GrandpaVoter, error)
53+
SetAuthorities(setID uint64, authorities []types.GrandpaVoter) error
5354
GetAuthoritiesChangesFromBlock(uint) ([]uint, error)
5455
}
5556

dot/sync/state_sync.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import (
88
"slices"
99
"time"
1010

11+
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
12+
1113
"github.com/ChainSafe/gossamer/dot/network"
1214
"github.com/ChainSafe/gossamer/dot/network/messages"
1315
"github.com/ChainSafe/gossamer/dot/peerset"
1416
"github.com/ChainSafe/gossamer/dot/types"
17+
"github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa"
1518
"github.com/ChainSafe/gossamer/lib/blocktree"
1619
"github.com/ChainSafe/gossamer/lib/grandpa/warpsync"
1720
"github.com/ChainSafe/gossamer/lib/runtime/storage"
@@ -35,6 +38,7 @@ type StateSyncStrategy struct {
3538
reqMaker network.RequestMaker
3639
blockReqMaker network.RequestMaker
3740
blockState BlockState
41+
grandpaState GrandpaState
3842
storage StorageState
3943
stateRequestProvider *StateRequestProvider
4044
finalityGadget FinalityGadget
@@ -52,6 +56,7 @@ type StateSyncStrategyConfig struct {
5256
Telemetry Telemetry
5357
BadBlocks []string
5458
BlockState BlockState
59+
GrandpaState GrandpaState
5560
Peers *peerViewSet
5661
ReqMaker network.RequestMaker
5762
BlockReqMaker network.RequestMaker
@@ -72,6 +77,7 @@ func NewStateSyncStrategy(
7277
peers: cfg.Peers,
7378
badBlocks: cfg.BadBlocks,
7479
blockState: cfg.BlockState,
80+
grandpaState: cfg.GrandpaState,
7581
targetHeader: targetHeader,
7682
warpSyncResult: cfg.WarpSyncResult,
7783
reqMaker: cfg.ReqMaker,
@@ -348,16 +354,33 @@ func (s *StateSyncStrategy) setBlockAsFullSyncStartingBlock() error {
348354
}
349355

350356
/*
351-
TODO: solve this later
357+
TODO: solve this - we need to marshal encode the justification
352358
err = s.blockState.SetJustification(blockHeader.Hash(), *justification)
353359
if err != nil {
354360
return fmt.Errorf("setting justification for block number %d: %w", blockHeader.Number, err)
355-
}*/
361+
}
362+
*/
363+
364+
err = s.grandpaState.SetAuthorities(uint64(s.warpSyncResult.SetId),
365+
grandpaVotersFromAuthorities(s.warpSyncResult.AuthorityList))
366+
if err != nil {
367+
return fmt.Errorf("setting new authorities set: %w", err)
368+
}
356369

357-
logger.Infof("block finalized successfully %s", s.targetHeader.Hash())
358-
// TODO:
359-
// update authorities set
360370
return nil
361371
}
362372

373+
func grandpaVotersFromAuthorities(authorities grandpa.AuthorityList) []types.GrandpaVoter {
374+
voters := make([]types.GrandpaVoter, len(authorities))
375+
376+
for _, auth := range authorities {
377+
voters = append(voters, types.GrandpaVoter{
378+
Key: ed25519.PublicKey(auth.AuthorityID[:]),
379+
ID: uint64(auth.AuthorityWeight),
380+
})
381+
}
382+
383+
return voters
384+
}
385+
363386
var _ Strategy = (*StateSyncStrategy)(nil)

0 commit comments

Comments
 (0)