From 0e867aef725ba4d5b1d367efc1ea851c9b4dfd7c Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 7 Feb 2025 00:39:02 -0300 Subject: [PATCH] Update grandpa authorities --- dot/state/grandpa.go | 12 ++++++------ dot/sync/service.go | 1 + dot/sync/state_sync.go | 33 ++++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/dot/state/grandpa.go b/dot/state/grandpa.go index e74d15968f..d0441c40fa 100644 --- a/dot/state/grandpa.go +++ b/dot/state/grandpa.go @@ -66,7 +66,7 @@ func NewGrandpaStateFromGenesis(db database.Database, bs *BlockState, return nil, fmt.Errorf("cannot set latest round: %w", err) } - if err := s.setAuthorities(genesisSetID, genesisAuthorities); err != nil { + if err := s.SetAuthorities(genesisSetID, genesisAuthorities); err != nil { return nil, fmt.Errorf("cannot set authorities: %w", err) } @@ -187,7 +187,7 @@ func (s *GrandpaState) ApplyScheduledChanges(finalizedHeader *types.Header) erro } grandpaVotersAuthorities := types.NewGrandpaVotersFromAuthorities(changeToApply.change.nextAuthorities) - err = s.setAuthorities(newSetID, grandpaVotersAuthorities) + err = s.SetAuthorities(newSetID, grandpaVotersAuthorities) if err != nil { return fmt.Errorf("cannot set authorities: %w", err) } @@ -259,7 +259,7 @@ func (s *GrandpaState) ApplyForcedChanges(importedBlockHeader *types.Header) err } grandpaVotersAuthorities := types.NewGrandpaVotersFromAuthorities(forcedChange.nextAuthorities) - err = s.setAuthorities(newSetID, grandpaVotersAuthorities) + err = s.SetAuthorities(newSetID, grandpaVotersAuthorities) if err != nil { return fmt.Errorf("cannot set authorities: %w", err) } @@ -334,8 +334,8 @@ func setIDChangeKey(setID uint64) []byte { return append(setIDChangePrefix, buf...) } -// setAuthorities sets the authorities for a given setID -func (s *GrandpaState) setAuthorities(setID uint64, authorities []types.GrandpaVoter) error { +// SetAuthorities sets the authorities for a given setID +func (s *GrandpaState) SetAuthorities(setID uint64, authorities []types.GrandpaVoter) error { enc, err := types.EncodeGrandpaVoters(authorities) if err != nil { return err @@ -407,7 +407,7 @@ func (s *GrandpaState) SetNextChange(authorities []types.GrandpaVoter, number ui } nextSetID := currSetID + 1 - err = s.setAuthorities(nextSetID, authorities) + err = s.SetAuthorities(nextSetID, authorities) if err != nil { return err } diff --git a/dot/sync/service.go b/dot/sync/service.go index 5b1227fa65..091fa2f8fc 100644 --- a/dot/sync/service.go +++ b/dot/sync/service.go @@ -50,6 +50,7 @@ const ( type GrandpaState interface { GetCurrentSetID() (uint64, error) GetAuthorities(uint64) ([]types.GrandpaVoter, error) + SetAuthorities(setID uint64, authorities []types.GrandpaVoter) error GetAuthoritiesChangesFromBlock(uint) ([]uint, error) } diff --git a/dot/sync/state_sync.go b/dot/sync/state_sync.go index 97a458772d..68ad9e2446 100644 --- a/dot/sync/state_sync.go +++ b/dot/sync/state_sync.go @@ -8,10 +8,13 @@ import ( "slices" "time" + "github.com/ChainSafe/gossamer/lib/crypto/ed25519" + "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/network/messages" "github.com/ChainSafe/gossamer/dot/peerset" "github.com/ChainSafe/gossamer/dot/types" + "github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa" "github.com/ChainSafe/gossamer/lib/blocktree" "github.com/ChainSafe/gossamer/lib/grandpa/warpsync" "github.com/ChainSafe/gossamer/lib/runtime/storage" @@ -35,6 +38,7 @@ type StateSyncStrategy struct { reqMaker network.RequestMaker blockReqMaker network.RequestMaker blockState BlockState + grandpaState GrandpaState storage StorageState stateRequestProvider *StateRequestProvider finalityGadget FinalityGadget @@ -52,6 +56,7 @@ type StateSyncStrategyConfig struct { Telemetry Telemetry BadBlocks []string BlockState BlockState + GrandpaState GrandpaState Peers *peerViewSet ReqMaker network.RequestMaker BlockReqMaker network.RequestMaker @@ -72,6 +77,7 @@ func NewStateSyncStrategy( peers: cfg.Peers, badBlocks: cfg.BadBlocks, blockState: cfg.BlockState, + grandpaState: cfg.GrandpaState, targetHeader: targetHeader, warpSyncResult: cfg.WarpSyncResult, reqMaker: cfg.ReqMaker, @@ -348,16 +354,33 @@ func (s *StateSyncStrategy) setBlockAsFullSyncStartingBlock() error { } /* - TODO: solve this later + TODO: solve this - we need to marshal encode the justification err = s.blockState.SetJustification(blockHeader.Hash(), *justification) if err != nil { return fmt.Errorf("setting justification for block number %d: %w", blockHeader.Number, err) - }*/ + } + */ + + err = s.grandpaState.SetAuthorities(uint64(s.warpSyncResult.SetId), + grandpaVotersFromAuthorities(s.warpSyncResult.AuthorityList)) + if err != nil { + return fmt.Errorf("setting new authorities set: %w", err) + } - logger.Infof("block finalized successfully %s", s.targetHeader.Hash()) - // TODO: - // update authorities set return nil } +func grandpaVotersFromAuthorities(authorities grandpa.AuthorityList) []types.GrandpaVoter { + voters := make([]types.GrandpaVoter, len(authorities)) + + for _, auth := range authorities { + voters = append(voters, types.GrandpaVoter{ + Key: ed25519.PublicKey(auth.AuthorityID[:]), + ID: uint64(auth.AuthorityWeight), + }) + } + + return voters +} + var _ Strategy = (*StateSyncStrategy)(nil)