Skip to content

Commit

Permalink
Move types to warpsync package
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Dec 5, 2024
1 parent 5733f30 commit 7294e7d
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 107 deletions.
36 changes: 0 additions & 36 deletions dot/network/messages/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ package messages
import (
"fmt"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/client/consensus/grandpa"
"github.com/ChainSafe/gossamer/internal/primitives/core/hash"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
)
Expand Down Expand Up @@ -46,37 +43,4 @@ func (wpr *WarpProofRequest) String() string {
return fmt.Sprintf("WarpProofRequest begin=%v", wpr.Begin)
}

type WarpSyncFragment struct {
Header types.Header
Justification grandpa.GrandpaJustification[hash.H256, uint32]
}

type WarpSyncProof struct {
Proofs []WarpSyncFragment
// indicates whether the warp sync has been completed
IsFinished bool
proofsLength int
}

func (wsp *WarpSyncProof) Decode(in []byte) error {
return scale.Unmarshal(in, wsp)
}

func (wsp *WarpSyncProof) Encode() ([]byte, error) {
if wsp == nil {
return nil, fmt.Errorf("cannot encode nil WarpSyncProof")
}
return scale.Marshal(*wsp)
}

func (wsp *WarpSyncProof) String() string {
if wsp == nil {
return "WarpSyncProof=nil"
}

return fmt.Sprintf("WarpSyncProof proofs=%v isFinished=%v proofsLength=%v",
wsp.Proofs, wsp.IsFinished, wsp.proofsLength)
}

var _ P2PMessage = (*WarpSyncProof)(nil)
var _ P2PMessage = (*WarpProofRequest)(nil)
43 changes: 0 additions & 43 deletions dot/network/messages/warp_sync_test.go

This file was deleted.

5 changes: 3 additions & 2 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.

11 changes: 2 additions & 9 deletions dot/network/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ import (
"fmt"

"github.com/ChainSafe/gossamer/dot/network/messages"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa"
primitives "github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa/warpsync"
libp2pnetwork "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
)

const MaxAllowedSameRequestPerPeer = 5

type WarpSyncVerificationResult struct {
SetId grandpa.SetID
AuthorityList primitives.AuthorityList
Header types.Header
Completed bool
}

// WarpSyncProvider is an interface for generating warp sync proofs
type WarpSyncProvider interface {
// Generate proof starting at given block hash. The proof is accumulated until maximum proof
Expand All @@ -34,7 +27,7 @@ type WarpSyncProvider interface {
encodedProof []byte,
setId grandpa.SetID,
authorities primitives.AuthorityList,
) (*WarpSyncVerificationResult, error)
) (*warpsync.WarpSyncVerificationResult, error)
}

func (s *Service) handleWarpSyncRequest(req messages.WarpProofRequest) ([]byte, error) {
Expand Down
5 changes: 3 additions & 2 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/lib/grandpa/warpsync"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
Expand Down Expand Up @@ -349,7 +350,7 @@ func (nodeBuilder) createNetworkService(config *cfg.Config, stateSrvc *state.Ser
return nil, fmt.Errorf("failed to parse network log level: %w", err)
}

warpSyncProvider := grandpa.NewWarpSyncProofProvider(
warpSyncProvider := warpsync.NewWarpSyncProofProvider(
stateSrvc.Block, stateSrvc.Grandpa,
)

Expand Down Expand Up @@ -524,7 +525,7 @@ func (nodeBuilder) newSyncService(config *cfg.Config, st *state.Service, fg sync
var warpSyncStrategy sync.Strategy

if config.Core.Sync == "warp" {
warpSyncProvider := grandpa.NewWarpSyncProofProvider(st.Block, st.Grandpa)
warpSyncProvider := warpsync.NewWarpSyncProofProvider(st.Block, st.Grandpa)

warpSyncCfg := &sync.WarpSyncConfig{
Telemetry: telemetryMailer,
Expand Down
5 changes: 3 additions & 2 deletions dot/sync/mocks_test.go

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

15 changes: 8 additions & 7 deletions dot/sync/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/types"
primitives "github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa"
"github.com/ChainSafe/gossamer/lib/grandpa/warpsync"
"github.com/libp2p/go-libp2p/core/peer"
)

Expand All @@ -27,7 +28,7 @@ const (
type WarpSyncProofProvider interface {
CurrentAuthorities() (primitives.AuthorityList, error)
Verify(encodedProof []byte, setId primitives.SetID, authorities primitives.AuthorityList) (
*network.WarpSyncVerificationResult, error)
*warpsync.WarpSyncVerificationResult, error)
}

type WarpSyncStrategy struct {
Expand Down Expand Up @@ -141,7 +142,7 @@ func (w *WarpSyncStrategy) NextActions() ([]*SyncTask, error) {
case WarpProof:
task = SyncTask{
request: messages.NewWarpProofRequest(lastBlock.Hash()),
response: &messages.WarpSyncProof{},
response: &warpsync.WarpSyncProof{},
requestMaker: w.warpSyncReqMaker,
}
case TargetBlock:
Expand Down Expand Up @@ -172,7 +173,7 @@ func (w *WarpSyncStrategy) Process(results []*SyncTaskResult) (
case WarpProof:
logger.Debug("processing warp sync proof results")

var warpProofResult *network.WarpSyncVerificationResult
var warpProofResult *warpsync.WarpSyncVerificationResult

repChanges, bans, warpProofResult = w.validateWarpSyncResults(results)

Expand Down Expand Up @@ -208,16 +209,16 @@ func (w *WarpSyncStrategy) Process(results []*SyncTaskResult) (
}

func (w *WarpSyncStrategy) validateWarpSyncResults(results []*SyncTaskResult) (
repChanges []Change, peersToBlock []peer.ID, result *network.WarpSyncVerificationResult) {
repChanges []Change, peersToBlock []peer.ID, result *warpsync.WarpSyncVerificationResult) {

repChanges = make([]Change, 0)
peersToBlock = make([]peer.ID, 0)
bestProof := &messages.WarpSyncProof{}
var bestResult *network.WarpSyncVerificationResult
bestProof := &warpsync.WarpSyncProof{}
var bestResult *warpsync.WarpSyncVerificationResult

for _, result := range results {
switch response := result.response.(type) {
case *messages.WarpSyncProof:
case *warpsync.WarpSyncProof:
if !result.completed {
continue
}
Expand Down
3 changes: 2 additions & 1 deletion dot/sync/warp_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa/warpsync"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -143,7 +144,7 @@ func TestWarpSyncNextActions(t *testing.T) {
"warp_sync_phase": {
phase: WarpProof,
expectedRequestType: &messages.WarpProofRequest{},
expectedResponseType: &messages.WarpSyncProof{},
expectedResponseType: &warpsync.WarpSyncProof{},
},
"target_block_phase": {
phase: TargetBlock,
Expand Down
6 changes: 6 additions & 0 deletions lib/grandpa/warpsync/mocks_generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package warpsync

//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . BlockState,GrandpaState
Loading

0 comments on commit 7294e7d

Please sign in to comment.