Skip to content

Commit

Permalink
Add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Sep 18, 2024
1 parent 6204490 commit c513695
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions dot/network/messages/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/ChainSafe/gossamer/pkg/scale"
)

// WarpProofRequest is a struct for p2p warp proof request
type WarpProofRequest struct {
Begin common.Hash
}

// Decode decodes the message into a WarpProofRequest
func (wsr *WarpProofRequest) Decode(in []byte) error {
reader := bytes.NewReader(in)
sd := scale.NewDecoder(reader)
Expand All @@ -26,6 +28,7 @@ func (wsr *WarpProofRequest) Decode(in []byte) error {
return nil
}

// Encode encodes the warp sync request
func (wsr *WarpProofRequest) Encode() ([]byte, error) {
buffer := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buffer)
Expand All @@ -36,6 +39,7 @@ func (wsr *WarpProofRequest) Encode() ([]byte, error) {
return buffer.Bytes(), nil
}

// String returns the string representation of a WarpProofRequest
func (wsr *WarpProofRequest) String() string {
if wsr == nil {
return "WarpProofRequest=nil"
Expand Down
1 change: 1 addition & 0 deletions dot/network/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
)

// 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.
Expand Down
13 changes: 11 additions & 2 deletions dot/network/warp_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ import (
func TestDecodeWarpSyncMessage(t *testing.T) {
t.Parallel()

// Basic WarpProofRequest
testWarpReqMessage := &messages.WarpProofRequest{
Begin: common.MustBlake2bHash([]byte("test")),
}

testPeer := peer.ID("me")
// Test encoding
reqEnc, err := testWarpReqMessage.Encode()
require.NoError(t, err)

// Test decoding
testPeer := peer.ID("me")
msg, err := decodeWarpSyncMessage(reqEnc, testPeer, true)
require.NoError(t, err)

Expand All @@ -33,6 +36,7 @@ func TestDecodeWarpSyncMessage(t *testing.T) {
require.Equal(t, testWarpReqMessage, req)
}

// createServiceWithWarpSyncHelper creates a basic service with warp sync handler support
func createServiceWithWarpSyncHelper(t *testing.T, warpSyncProvider WarpSyncProvider) *Service {
t.Helper()

Expand All @@ -55,15 +59,17 @@ func createServiceWithWarpSyncHelper(t *testing.T, warpSyncProvider WarpSyncProv
func TestHandleWarpSyncRequestOk(t *testing.T) {
t.Parallel()

// Creates warp sync provider mock to generate proofs with the expected result
expectedProof := []byte{0x01}

ctrl := gomock.NewController(t)

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

// Initiate service using the warp sync provider mock
srvc := createServiceWithWarpSyncHelper(t, warpSyncProvider)

// Handle request and check resulting proof
req := messages.WarpProofRequest{
Begin: common.EmptyHash,
}
Expand All @@ -76,14 +82,17 @@ func TestHandleWarpSyncRequestOk(t *testing.T) {
func TestHandleWarpSyncRequestError(t *testing.T) {
t.Parallel()

// Creates warp sync provider mock to generate proofs with the expected erro
expectedError := fmt.Errorf("error generating proof")
ctrl := gomock.NewController(t)

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

// Initiate service using the warp sync provider mock
srvc := createServiceWithWarpSyncHelper(t, warpSyncProvider)

// Handle request and check resulting error
req := messages.WarpProofRequest{
Begin: common.EmptyHash,
}
Expand Down

0 comments on commit c513695

Please sign in to comment.