Skip to content

Commit 89fc820

Browse files
committed
chore(requestid): wrap requestid string in a struct
1 parent e728d6a commit 89fc820

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

graphsync.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// RequestID is a unique identifier for a GraphSync request.
16-
type RequestID string
16+
type RequestID struct{ string }
1717

1818
// Tag returns an easy way to identify this request id as a graphsync request (for libp2p connections)
1919
func (r RequestID) Tag() string {
@@ -22,13 +22,27 @@ func (r RequestID) Tag() string {
2222

2323
// String form of a RequestID (should be a well-formed UUIDv4 string)
2424
func (r RequestID) String() string {
25-
return uuid.Must(uuid.FromBytes([]byte(r))).String()
25+
return uuid.Must(uuid.FromBytes([]byte(r.string))).String()
26+
}
27+
28+
// Byte form of a RequestID
29+
func (r RequestID) Bytes() []byte {
30+
return []byte(r.string)
2631
}
2732

2833
// Create a new, random RequestID (should be a UUIDv4)
2934
func NewRequestID() RequestID {
3035
u := uuid.New()
31-
return RequestID(u[:])
36+
return RequestID{string(u[:])}
37+
}
38+
39+
// Create a RequestID from a byte slice
40+
func ParseRequestID(b []byte) (RequestID, error) {
41+
_, err := uuid.FromBytes(b)
42+
if err != nil {
43+
return RequestID{}, err
44+
}
45+
return RequestID{string(b)}, nil
3246
}
3347

3448
// Priority a priority for a GraphSync request.

message/message.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"io"
77

8-
"github.com/google/uuid"
98
blocks "github.com/ipfs/go-block-format"
109
cid "github.com/ipfs/go-cid"
1110
"github.com/ipld/go-ipld-prime"
@@ -162,11 +161,10 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
162161
if exts == nil {
163162
exts = make(map[string][]byte)
164163
}
165-
uid, err := uuid.FromBytes(req.Id)
164+
id, err := graphsync.ParseRequestID(req.Id)
166165
if err != nil {
167166
return GraphSyncMessage{}, err
168167
}
169-
id := graphsync.RequestID(uid[:])
170168
requests[id] = newRequest(id, root, selector, graphsync.Priority(req.Priority), req.Cancel, req.Update, exts)
171169
}
172170

@@ -179,11 +177,10 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
179177
if exts == nil {
180178
exts = make(map[string][]byte)
181179
}
182-
uid, err := uuid.FromBytes(res.Id)
180+
id, err := graphsync.ParseRequestID(res.Id)
183181
if err != nil {
184182
return GraphSyncMessage{}, err
185183
}
186-
id := graphsync.RequestID(uid[:])
187184
responses[id] = newResponse(id, graphsync.ResponseStatusCode(res.Status), exts)
188185
}
189186

@@ -288,7 +285,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
288285
}
289286
}
290287
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
291-
Id: []byte(request.id),
288+
Id: request.id.Bytes(),
292289
Root: request.root.Bytes(),
293290
Selector: selector,
294291
Priority: int32(request.priority),
@@ -301,7 +298,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
301298
pbm.Responses = make([]*pb.Message_Response, 0, len(gsm.responses))
302299
for _, response := range gsm.responses {
303300
pbm.Responses = append(pbm.Responses, &pb.Message_Response{
304-
Id: []byte(response.requestID),
301+
Id: response.requestID.Bytes(),
305302
Status: int32(response.status),
306303
Extensions: response.extensions,
307304
})

message/message_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestAppendingRequests(t *testing.T) {
5151
require.NoError(t, err)
5252

5353
pbRequest := pbMessage.Requests[0]
54-
require.Equal(t, []byte(id), pbRequest.Id)
54+
require.Equal(t, id.Bytes(), pbRequest.Id)
5555
require.Equal(t, int32(priority), pbRequest.Priority)
5656
require.False(t, pbRequest.Cancel)
5757
require.False(t, pbRequest.Update)
@@ -102,7 +102,7 @@ func TestAppendingResponses(t *testing.T) {
102102
pbMessage, err := gsm.ToProto()
103103
require.NoError(t, err, "serialize to protobuf errored")
104104
pbResponse := pbMessage.Responses[0]
105-
require.Equal(t, []byte(requestID), pbResponse.Id)
105+
require.Equal(t, requestID.Bytes(), pbResponse.Id)
106106
require.Equal(t, int32(status), pbResponse.Status)
107107
require.Equal(t, extension.Data, pbResponse.Extensions["graphsync/awesome"])
108108

responsemanager/responsemanager_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package responsemanager
33
import (
44
"context"
55
"errors"
6+
"math/rand"
67
"sync"
78
"testing"
89
"time"
@@ -1293,7 +1294,7 @@ func (td *testData) notifyStatusMessagesSent() {
12931294

12941295
func (td *testData) notifyBlockSendsSent() {
12951296
td.transactionLk.Lock()
1296-
td.notifeePublisher.PublishEvents(notifications.Topic(rand.Int31), []notifications.Event{
1297+
td.notifeePublisher.PublishEvents(notifications.Topic(graphsync.NewRequestID), []notifications.Event{
12971298
messagequeue.Event{Name: messagequeue.Sent, Metadata: messagequeue.Metadata{BlockData: td.blkNotifications}},
12981299
})
12991300
td.blkNotifications = make(map[graphsync.RequestID][]graphsync.BlockData)

0 commit comments

Comments
 (0)