Skip to content

Commit

Permalink
chore: fix tests and normalize log info
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Sep 20, 2024
1 parent 413d7bc commit 70c9246
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 27 deletions.
11 changes: 11 additions & 0 deletions dot/network/messages/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ const (
Descending
)

func (s SyncDirection) String() string {
switch s {
case Ascending:
return "ascending"
case Descending:
return "descending"
default:
return "undefined direction"
}
}

// The following defines the fields that will needs to be
// in the response message
const (
Expand Down
44 changes: 23 additions & 21 deletions dot/sync/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sync
import (
"bytes"
"fmt"
"slices"

"github.com/ChainSafe/gossamer/dot/network/messages"
"github.com/ChainSafe/gossamer/dot/peerset"
Expand All @@ -21,6 +22,10 @@ func (s *Service) CreateBlockResponse(from peer.ID, req *messages.BlockRequestMe
*messages.BlockResponseMessage, error) {
logger.Debugf("sync request from %s: %s", from, req.String())

if req.RequestedData == 0 {
return nil, fmt.Errorf("%w: invalid requested data %v", ErrInvalidBlockRequest, req.RequestedData)
}

encodedRequest, err := req.Encode()
if err != nil {
return nil, fmt.Errorf("encoding request: %w", err)
Expand Down Expand Up @@ -188,15 +193,15 @@ func (s *Service) handleDescendingRequest(req *messages.BlockRequestMessage) (*m
}

if startHash == nil || endHash == nil {
logger.Debugf("handling BlockRequestMessage with direction %s "+
"from start block with number %d to end block with number %d",
req.Direction, startNumber, endNumber)
logger.Infof("handling block request message with direction %s "+
"from number %d to number %d\n",
req.Direction.String(), startNumber, endNumber)
return s.handleDescendingByNumber(startNumber, endNumber, req.RequestedData)
}

logger.Debugf("handling block request message with direction %s "+
"from start block with hash %s to end block with hash %s",
req.Direction, *startHash, *endHash)
logger.Infof("handling block request message with direction %s "+
"from hash %s to end block with hash %s",
req.Direction.String(), *startHash, *endHash)
return s.handleChainByHash(*endHash, *startHash, max, req.RequestedData, req.Direction)
}

Expand Down Expand Up @@ -301,19 +306,20 @@ func (s *Service) handleAscendingByNumber(start, end uint,
func (s *Service) handleDescendingByNumber(start, end uint,
requestedData byte) (*messages.BlockResponseMessage, error) {
var err error
data := make([]*types.BlockData, (start-end)+1)

response := &messages.BlockResponseMessage{
BlockData: make([]*types.BlockData, (start-end)+1),
}

for i := uint(0); start-i >= end; i++ {
blockNumber := start - i
data[i], err = s.getBlockDataByNumber(blockNumber, requestedData)
response.BlockData[i], err = s.getBlockDataByNumber(blockNumber, requestedData)
if err != nil {
return nil, err
}
}

return &messages.BlockResponseMessage{
BlockData: data,
}, nil
return response, nil
}

func (s *Service) handleChainByHash(ancestor, descendant common.Hash,
Expand All @@ -334,23 +340,23 @@ func (s *Service) handleChainByHash(ancestor, descendant common.Hash,
}
}

data := make([]*types.BlockData, len(subchain))
response := &messages.BlockResponseMessage{
BlockData: make([]*types.BlockData, len(subchain)),
}

for i, hash := range subchain {
data[i], err = s.getBlockData(hash, requestedData)
response.BlockData[i], err = s.getBlockData(hash, requestedData)
if err != nil {
return nil, err
}
}

// reverse BlockData, if descending request
if direction == messages.Descending {
reverseBlockData(data)
slices.Reverse(response.BlockData)
}

return &messages.BlockResponseMessage{
BlockData: data,
}, nil
return response, nil
}

func (s *Service) getBlockDataByNumber(num uint, requestedData byte) (*types.BlockData, error) {
Expand All @@ -368,10 +374,6 @@ func (s *Service) getBlockData(hash common.Hash, requestedData byte) (*types.Blo
Hash: hash,
}

if requestedData == 0 {
return blockData, nil
}

if (requestedData & messages.RequestedDataHeader) == 1 {
blockData.Header, err = s.blockState.GetHeader(hash)
if err != nil {
Expand Down
62 changes: 56 additions & 6 deletions dot/sync/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sync

import (
"errors"
"fmt"
"testing"

"github.com/ChainSafe/gossamer/dot/network/messages"
Expand Down Expand Up @@ -33,22 +34,28 @@ func TestService_CreateBlockResponse(t *testing.T) {
mockBlockState := NewMockBlockState(ctrl)
return mockBlockState
},
args: args{req: &messages.BlockRequestMessage{}},
err: ErrInvalidBlockRequest,
args: args{req: &messages.BlockRequestMessage{
StartingBlock: *messages.NewFromBlock(uint(0)),
}},
err: fmt.Errorf("%w: invalid requested data %v", ErrInvalidBlockRequest, 0),
},
"ascending_request_nil_startHash": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
dummyHeader := &types.Header{Number: 2}
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil)
mockBlockState.EXPECT().GetHashByNumber(uint(1)).Return(common.Hash{1, 2}, nil)
mockBlockState.EXPECT().GetHeader(common.Hash{1, 2}).Return(dummyHeader, nil)
return mockBlockState
},
args: args{req: &messages.BlockRequestMessage{
RequestedData: messages.RequestedDataHeader,
StartingBlock: *messages.NewFromBlock(uint(0)),
Direction: messages.Ascending,
}},
want: &messages.BlockResponseMessage{BlockData: []*types.BlockData{{
Hash: common.Hash{1, 2},
Hash: common.Hash{1, 2},
Header: &types.Header{Number: 2},
}}},
},
"ascending_request_start_number_higher": {
Expand All @@ -58,6 +65,7 @@ func TestService_CreateBlockResponse(t *testing.T) {
return mockBlockState
},
args: args{req: &messages.BlockRequestMessage{
RequestedData: messages.BootstrapRequestData,
StartingBlock: *messages.NewFromBlock(uint(2)),
Direction: messages.Ascending,
}},
Expand All @@ -71,29 +79,48 @@ func TestService_CreateBlockResponse(t *testing.T) {
return mockBlockState
},
args: args{req: &messages.BlockRequestMessage{
RequestedData: messages.BootstrapRequestData,
StartingBlock: *messages.NewFromBlock(uint(0)),
Direction: messages.Descending,
}},
want: &messages.BlockResponseMessage{BlockData: []*types.BlockData{}},
},
"descending_request_start_number_higher": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
dummyBody := types.NewBody([]types.Extrinsic{
{0, 1, 2, 3},
{5, 5, 5, 5},
})

mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil)
mockBlockState.EXPECT().GetHashByNumber(uint(1)).Return(common.Hash{1, 2}, nil)
mockBlockState.EXPECT().
GetBlockBody(common.Hash{1, 2}).
Return(dummyBody, nil)
return mockBlockState
},
args: args{req: &messages.BlockRequestMessage{
RequestedData: messages.RequestedDataBody,
StartingBlock: *messages.NewFromBlock(uint(2)),
Direction: messages.Descending,
}},
err: nil,
want: &messages.BlockResponseMessage{BlockData: []*types.BlockData{{
Hash: common.Hash{1, 2},
Body: types.NewBody([]types.Extrinsic{
{0, 1, 2, 3},
{5, 5, 5, 5},
}),
}}},
},
"ascending_request_startHash": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
dummyBody := types.NewBody([]types.Extrinsic{
{0, 1, 2, 3},
{5, 5, 5, 5},
})

mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetHeader(common.Hash{}).Return(&types.Header{
Number: 1,
Expand All @@ -102,21 +129,34 @@ func TestService_CreateBlockResponse(t *testing.T) {
mockBlockState.EXPECT().GetHashByNumber(uint(2)).Return(common.Hash{1, 2, 3}, nil)
mockBlockState.EXPECT().IsDescendantOf(common.Hash{}, common.Hash{1, 2, 3}).Return(true,
nil)
mockBlockState.EXPECT().Range(common.Hash{}, common.Hash{1, 2, 3}).Return([]common.Hash{{1,
2}},
nil)
mockBlockState.EXPECT().
Range(common.Hash{}, common.Hash{1, 2, 3}).
Return([]common.Hash{{1, 2}}, nil)
mockBlockState.EXPECT().
GetBlockBody(common.Hash{1, 2}).
Return(dummyBody, nil)
return mockBlockState
},
args: args{req: &messages.BlockRequestMessage{
RequestedData: messages.RequestedDataBody,
StartingBlock: *messages.NewFromBlock(common.Hash{}),
Direction: messages.Ascending,
}},
want: &messages.BlockResponseMessage{BlockData: []*types.BlockData{{
Hash: common.Hash{1, 2},
Body: types.NewBody([]types.Extrinsic{
{0, 1, 2, 3},
{5, 5, 5, 5},
}),
}}},
},
"descending_request_startHash": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
dummyBody := types.NewBody([]types.Extrinsic{
{0, 1, 2, 3},
{5, 5, 5, 5},
})

mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetHeader(common.Hash{}).Return(&types.Header{
Number: 1,
Expand All @@ -127,14 +167,23 @@ func TestService_CreateBlockResponse(t *testing.T) {
mockBlockState.EXPECT().Range(common.MustHexToHash(
"0x6443a0b46e0412e626363028115a9f2cf963eeed526b8b33e5316f08b50d0dc3"),
common.Hash{}).Return([]common.Hash{{1, 2}}, nil)

mockBlockState.EXPECT().
GetBlockBody(common.Hash{1, 2}).
Return(dummyBody, nil)
return mockBlockState
},
args: args{req: &messages.BlockRequestMessage{
RequestedData: messages.RequestedDataBody,
StartingBlock: *messages.NewFromBlock(common.Hash{}),
Direction: messages.Descending,
}},
want: &messages.BlockResponseMessage{BlockData: []*types.BlockData{{
Hash: common.Hash{1, 2},
Body: types.NewBody([]types.Extrinsic{
{0, 1, 2, 3},
{5, 5, 5, 5},
}),
}}},
},
"invalid_direction": {
Expand All @@ -143,6 +192,7 @@ func TestService_CreateBlockResponse(t *testing.T) {
},
args: args{
req: &messages.BlockRequestMessage{
RequestedData: messages.BootstrapRequestData,
StartingBlock: *messages.NewFromBlock(common.Hash{}),
Direction: messages.SyncDirection(3),
}},
Expand Down

0 comments on commit 70c9246

Please sign in to comment.