Skip to content

Commit

Permalink
feat(dot/network): Initialize warp sync provider (#4214)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro authored Oct 2, 2024
1 parent 6a833fd commit 6e7a351
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
12 changes: 6 additions & 6 deletions dot/network/mock_warp_sync_provider_test.go

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

4 changes: 2 additions & 2 deletions dot/network/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
type WarpSyncProvider interface {
// Generate proof starting at given block hash. The proof is accumulated until maximum proof
// size is reached.
generate(start common.Hash) (encodedProof []byte, err error)
Generate(start common.Hash) (encodedProof []byte, err error)
}

func (s *Service) handleWarpSyncRequest(req messages.WarpProofRequest) ([]byte, error) {
// use the backend to generate the warp proof
proof, err := s.warpSyncProvider.generate(req.Begin)
proof, err := s.warpSyncProvider.Generate(req.Begin)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions dot/network/warp_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestHandleWarpSyncRequestOk(t *testing.T) {

ctrl := gomock.NewController(t)
warpSyncProvider := NewMockWarpSyncProvider(ctrl)
warpSyncProvider.EXPECT().generate(common.EmptyHash).Return(expectedProof, nil).Times(1)
warpSyncProvider.EXPECT().Generate(common.EmptyHash).Return(expectedProof, nil).Times(1)

// Initiate service using the warp sync provider mock
srvc := createServiceWithWarpSyncHelper(t, warpSyncProvider)
Expand All @@ -94,7 +94,7 @@ func TestHandleWarpSyncRequestError(t *testing.T) {
ctrl := gomock.NewController(t)

warpSyncProvider := NewMockWarpSyncProvider(ctrl)
warpSyncProvider.EXPECT().generate(common.EmptyHash).Return(nil, expectedError).Times(1)
warpSyncProvider.EXPECT().Generate(common.EmptyHash).Return(nil, expectedError).Times(1)

// Initiate service using the warp sync provider mock
srvc := createServiceWithWarpSyncHelper(t, warpSyncProvider)
Expand Down
7 changes: 7 additions & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ChainSafe/gossamer/dot/sync"
"github.com/ChainSafe/gossamer/dot/system"
"github.com/ChainSafe/gossamer/dot/types"
consensus_grandpa "github.com/ChainSafe/gossamer/internal/client/consensus/grandpa"
"github.com/ChainSafe/gossamer/internal/database"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/internal/metrics"
Expand Down Expand Up @@ -348,6 +349,11 @@ func (nodeBuilder) createNetworkService(config *cfg.Config, stateSrvc *state.Ser
if err != nil {
return nil, fmt.Errorf("failed to parse network log level: %w", err)
}

warpSyncProvider := consensus_grandpa.NewWarpSyncProofProvider(
stateSrvc.Block, stateSrvc.Grandpa,
)

// network service configuation
networkConfig := network.Config{
LogLvl: networkLogLevel,
Expand All @@ -370,6 +376,7 @@ func (nodeBuilder) createNetworkService(config *cfg.Config, stateSrvc *state.Ser
Metrics: metrics.NewIntervalConfig(config.PrometheusExternal),
NodeKey: config.Network.NodeKey,
ListenAddress: config.Network.ListenAddress,
WarpSyncProvider: warpSyncProvider,
}

networkSrvc, err := network.NewService(&networkConfig)
Expand Down
7 changes: 7 additions & 0 deletions internal/client/consensus/grandpa/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ type WarpSyncProofProvider struct {
grandpaState GrandpaState
}

func NewWarpSyncProofProvider(blockState BlockState, grandpaState GrandpaState) *WarpSyncProofProvider {
return &WarpSyncProofProvider{
blockState: blockState,
grandpaState: grandpaState,
}
}

// Generate build a warp sync encoded proof starting from the given block hash
func (np *WarpSyncProofProvider) Generate(start common.Hash) ([]byte, error) {
// Get and traverse all GRANDPA authorities changes from the given block hash
Expand Down

0 comments on commit 6e7a351

Please sign in to comment.