Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GetNodeData tests for stream client, increase nodes and receipts cap #4519

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions p2p/stream/protocols/sync/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func checkGetReceiptsResult(b []byte, hs []common.Hash) error {
return nil
}

func checkGetNodeDataResult(b []byte, hs []common.Hash) error {
var msg = &syncpb.Message{}
if err := protobuf.Unmarshal(b, msg); err != nil {
return err
}
bhResp, err := msg.GetNodeDataResponse()
if err != nil {
return err
}
if len(hs) != len(bhResp.DataBytes) {
return errors.New("unexpected size")
}
return nil
}

func numberToHash(bn uint64) common.Hash {
var h common.Hash
binary.LittleEndian.PutUint64(h[:], bn)
Expand Down
4 changes: 2 additions & 2 deletions p2p/stream/protocols/sync/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const (

// GetNodeDataCap is the cap of request of single GetNodeData request
// This number has an effect on maxMsgBytes as 20MB defined in github.com/harmony-one/harmony/p2p/stream/types.
GetNodeDataCap = 10
GetNodeDataCap = 256
sophoah marked this conversation as resolved.
Show resolved Hide resolved

// GetReceiptsCap is the cap of request of single GetReceipts request
// This number has an effect on maxMsgBytes as 20MB defined in github.com/harmony-one/harmony/p2p/stream/types.
GetReceiptsCap = 10
GetReceiptsCap = 128
sophoah marked this conversation as resolved.
Show resolved Hide resolved

// MaxStreamFailures is the maximum allowed failures before stream gets removed
MaxStreamFailures = 5
Expand Down
16 changes: 16 additions & 0 deletions p2p/stream/protocols/sync/message/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ func (msg *Message) GetReceiptsResponse() (*GetReceiptsResponse, error) {
}
return grResp, nil
}

// GetNodeDataResponse parse the message to GetNodeDataResponse
func (msg *Message) GetNodeDataResponse() (*GetNodeDataResponse, error) {
resp := msg.GetResp()
if resp == nil {
return nil, errors.New("not response message")
}
if errResp := resp.GetErrorResponse(); errResp != nil {
return nil, &ResponseError{errResp.Error}
}
gnResp := resp.GetGetNodeDataResponse()
if gnResp == nil {
return nil, errors.New("not GetGetNodeDataResponse")
}
return gnResp, nil
}
33 changes: 32 additions & 1 deletion p2p/stream/protocols/sync/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ var (
}
testGetReceiptsRequest = syncpb.MakeGetReceiptsRequest(testGetReceipts)
testGetReceiptsRequestMsg = syncpb.MakeMessageFromRequest(testGetReceiptsRequest)

testGetNodeData = []common.Hash{
numberToHash(1),
numberToHash(2),
numberToHash(3),
numberToHash(4),
numberToHash(5),
}
testGetNodeDataRequest = syncpb.MakeGetNodeDataRequest(testGetNodeData)
testGetNodeDataRequestMsg = syncpb.MakeMessageFromRequest(testGetNodeDataRequest)
)

func TestSyncStream_HandleGetBlocksByRequest(t *testing.T) {
Expand Down Expand Up @@ -152,7 +162,28 @@ func TestSyncStream_HandleGetReceipts(t *testing.T) {
time.Sleep(200 * time.Millisecond)
receivedBytes, _ := remoteSt.ReadBytes()

if err := checkGetReceiptsResult(receivedBytes, testGetBlockByHashes); err != nil {
if err := checkGetReceiptsResult(receivedBytes, testGetReceipts); err != nil {
t.Fatal(err)
}
}

func TestSyncStream_HandleGetNodeData(t *testing.T) {
st, remoteSt := makeTestSyncStream()

go st.run()
defer close(st.closeC)

req := testGetNodeDataRequestMsg
b, _ := protobuf.Marshal(req)
err := remoteSt.WriteBytes(b)
if err != nil {
t.Fatal(err)
}

time.Sleep(200 * time.Millisecond)
receivedBytes, _ := remoteSt.ReadBytes()

if err := checkGetNodeDataResult(receivedBytes, testGetNodeData); err != nil {
t.Fatal(err)
}
}
Expand Down