Skip to content

Commit

Permalink
Update grandpa authorities
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Feb 7, 2025
1 parent 9498268 commit 0e867ae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
12 changes: 6 additions & 6 deletions dot/state/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions dot/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
33 changes: 28 additions & 5 deletions dot/sync/state_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,6 +38,7 @@ type StateSyncStrategy struct {
reqMaker network.RequestMaker
blockReqMaker network.RequestMaker
blockState BlockState
grandpaState GrandpaState
storage StorageState
stateRequestProvider *StateRequestProvider
finalityGadget FinalityGadget
Expand All @@ -52,6 +56,7 @@ type StateSyncStrategyConfig struct {
Telemetry Telemetry
BadBlocks []string
BlockState BlockState
GrandpaState GrandpaState
Peers *peerViewSet
ReqMaker network.RequestMaker
BlockReqMaker network.RequestMaker
Expand All @@ -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,
Expand Down Expand Up @@ -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)

0 comments on commit 0e867ae

Please sign in to comment.