Skip to content

Commit

Permalink
feat(consensus/grandpa): Add warp sync proof verification (#4240)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro authored Oct 21, 2024
1 parent 4938511 commit dc92001
Show file tree
Hide file tree
Showing 15 changed files with 740 additions and 317 deletions.
16 changes: 16 additions & 0 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.

15 changes: 15 additions & 0 deletions dot/network/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ 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"
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
// size is reached.
Generate(start common.Hash) (encodedProof []byte, err error)
Verify(
encodedProof []byte,
setId grandpa.SetID,
authorities primitives.AuthorityList,
) (*WarpSyncVerificationResult, error)
}

func (s *Service) handleWarpSyncRequest(req messages.WarpProofRequest) ([]byte, error) {
Expand Down
3 changes: 1 addition & 2 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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 @@ -350,7 +349,7 @@ func (nodeBuilder) createNetworkService(config *cfg.Config, stateSrvc *state.Ser
return nil, fmt.Errorf("failed to parse network log level: %w", err)
}

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

Expand Down
2 changes: 2 additions & 0 deletions dot/types/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,5 @@ func AuthoritiesRawToAuthorityAsAddress(authsRaw []AuthorityRaw, kt crypto.KeyTy
}
return auths, nil
}

type AuthorityList []Authority
4 changes: 2 additions & 2 deletions internal/client/consensus/grandpa/justification.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type decodeGrandpaJustification[
Hasher runtime.Hasher[Hash],
] GrandpaJustification[Hash, N]

func decodeJustification[
func DecodeJustification[
Hash runtime.Hash,
N runtime.Number,
Hasher runtime.Hasher[Hash],
Expand Down Expand Up @@ -100,7 +100,7 @@ func DecodeGrandpaJustificationVerifyFinalizes[
setID uint64,
voters grandpa.VoterSet[string],
) (GrandpaJustification[Hash, N], error) {
justification, err := decodeJustification[Hash, N, Hasher](encoded)
justification, err := DecodeJustification[Hash, N, Hasher](encoded)
if err != nil {
return GrandpaJustification[Hash, N]{}, fmt.Errorf("error decoding justification for header: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/client/consensus/grandpa/justification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestJustificationEncoding(t *testing.T) {
encodedJustification, err := scale.Marshal(expected)
require.NoError(t, err)

justification, err := decodeJustification[hash.H256, uint64, runtime.BlakeTwo256](encodedJustification)
justification, err := DecodeJustification[hash.H256, uint64, runtime.BlakeTwo256](encodedJustification)
require.NoError(t, err)
require.Equal(t, expected, justification.Justification)
}
Expand Down
6 changes: 0 additions & 6 deletions internal/client/consensus/grandpa/mocks_generate_test.go

This file was deleted.

180 changes: 0 additions & 180 deletions internal/client/consensus/grandpa/warp_sync.go

This file was deleted.

Loading

0 comments on commit dc92001

Please sign in to comment.