Skip to content

Commit 64ea051

Browse files
committed
feat(requestid): use uuids for requestids
Ref: #278 Closes: #279 Closes: #281
1 parent 655606b commit 64ea051

26 files changed

+122
-110
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/ipfs/go-graphsync
33
go 1.16
44

55
require (
6+
github.com/google/uuid v1.3.0
67
github.com/hannahhoward/cbor-gen-for v0.0.0-20200817222906-ea96cece81f1
78
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e
89
github.com/ipfs/go-block-format v0.0.3

graphsync.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,29 @@ import (
55
"errors"
66
"fmt"
77

8+
"github.com/google/uuid"
89
"github.com/ipfs/go-cid"
910
"github.com/ipld/go-ipld-prime"
1011
"github.com/ipld/go-ipld-prime/traversal"
1112
"github.com/libp2p/go-libp2p-core/peer"
1213
)
1314

1415
// RequestID is a unique identifier for a GraphSync request.
15-
type RequestID int32
16+
type RequestID uuid.UUID
1617

1718
// Tag returns an easy way to identify this request id as a graphsync request (for libp2p connections)
1819
func (r RequestID) Tag() string {
19-
return fmt.Sprintf("graphsync-request-%d", r)
20+
return r.String()
21+
}
22+
23+
// String form of a RequestID (should be a well-formed UUIDv4 string)
24+
func (r RequestID) String() string {
25+
return uuid.UUID(r).String()
26+
}
27+
28+
// Create a new, random RequestID (should be a UUIDv4)
29+
func NewRequestID() RequestID {
30+
return RequestID(uuid.New())
2031
}
2132

2233
// Priority a priority for a GraphSync request.

impl/graphsync_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"io/ioutil"
1010
"math"
11-
"math/rand"
1211
"os"
1312
"path/filepath"
1413
"testing"
@@ -133,7 +132,7 @@ func TestSendResponseToIncomingRequest(t *testing.T) {
133132
blockChainLength := 100
134133
blockChain := testutil.SetupBlockChain(ctx, t, td.persistence2, 100, blockChainLength)
135134

136-
requestID := graphsync.RequestID(rand.Int31())
135+
requestID := graphsync.NewRequestID()
137136

138137
builder := gsmsg.NewBuilder(gsmsg.Topic(0))
139138
builder.AddRequest(gsmsg.NewRequest(requestID, blockChain.TipLink.(cidlink.Link).Cid, blockChain.Selector(), graphsync.Priority(math.MaxInt32), td.extension))

linktracker/linktracker_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package linktracker
22

33
import (
4-
"math/rand"
54
"testing"
65

76
"github.com/ipld/go-ipld-prime"
@@ -74,7 +73,7 @@ func TestBlockRefCount(t *testing.T) {
7473
linkTracker := New()
7574
link := testutil.NewTestLink()
7675
for _, rq := range data.requests {
77-
requestID := graphsync.RequestID(rand.Int31())
76+
requestID := graphsync.NewRequestID()
7877
for _, present := range rq.traversals {
7978
linkTracker.RecordLinkTraversal(requestID, link, present)
8079
}
@@ -116,7 +115,7 @@ func TestFinishRequest(t *testing.T) {
116115
for testCase, data := range testCases {
117116
t.Run(testCase, func(t *testing.T) {
118117
linkTracker := New()
119-
requestID := graphsync.RequestID(rand.Int31())
118+
requestID := graphsync.NewRequestID()
120119
for _, lt := range data.linksTraversed {
121120
linkTracker.RecordLinkTraversal(requestID, lt.link, lt.blockPresent)
122121
}
@@ -151,7 +150,7 @@ func TestIsKnownMissingLink(t *testing.T) {
151150
t.Run(testCase, func(t *testing.T) {
152151
linkTracker := New()
153152
link := testutil.NewTestLink()
154-
requestID := graphsync.RequestID(rand.Int31())
153+
requestID := graphsync.NewRequestID()
155154
for _, present := range data.traversals {
156155
linkTracker.RecordLinkTraversal(requestID, link, present)
157156
}

message/builder_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package message
22

33
import (
44
"fmt"
5-
"math/rand"
65
"testing"
76

87
"github.com/ipld/go-ipld-prime"
@@ -21,10 +20,10 @@ func TestMessageBuilding(t *testing.T) {
2120
for _, block := range blocks {
2221
links = append(links, cidlink.Link{Cid: block.Cid()})
2322
}
24-
requestID1 := graphsync.RequestID(rand.Int31())
25-
requestID2 := graphsync.RequestID(rand.Int31())
26-
requestID3 := graphsync.RequestID(rand.Int31())
27-
requestID4 := graphsync.RequestID(rand.Int31())
23+
requestID1 := graphsync.NewRequestID()
24+
requestID2 := graphsync.NewRequestID()
25+
requestID3 := graphsync.NewRequestID()
26+
requestID4 := graphsync.NewRequestID()
2827

2928
rb.AddLink(requestID1, links[0], true)
3029
rb.AddLink(requestID1, links[1], false)
@@ -133,8 +132,8 @@ func TestMessageBuilding(t *testing.T) {
133132

134133
func TestMessageBuildingExtensionOnly(t *testing.T) {
135134
rb := NewBuilder(Topic(0))
136-
requestID1 := graphsync.RequestID(rand.Int31())
137-
requestID2 := graphsync.RequestID(rand.Int31())
135+
requestID1 := graphsync.NewRequestID()
136+
requestID2 := graphsync.NewRequestID()
138137

139138
extensionData1 := testutil.RandomBytes(100)
140139
extensionName1 := graphsync.ExtensionName("AppleSauce/McGee")

message/message.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88

9+
"github.com/google/uuid"
910
blocks "github.com/ipfs/go-block-format"
1011
cid "github.com/ipfs/go-cid"
1112
"github.com/ipld/go-ipld-prime"
@@ -162,7 +163,12 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
162163
if exts == nil {
163164
exts = make(map[string][]byte)
164165
}
165-
requests[graphsync.RequestID(req.Id)] = newRequest(graphsync.RequestID(req.Id), root, selector, graphsync.Priority(req.Priority), req.Cancel, req.Update, exts)
166+
uid, err := uuid.FromBytes(req.Id)
167+
if err != nil {
168+
return GraphSyncMessage{}, err
169+
}
170+
id := graphsync.RequestID(uid)
171+
requests[id] = newRequest(id, root, selector, graphsync.Priority(req.Priority), req.Cancel, req.Update, exts)
166172
}
167173

168174
responses := make(map[graphsync.RequestID]GraphSyncResponse, len(pbm.GetResponses()))
@@ -174,7 +180,12 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
174180
if exts == nil {
175181
exts = make(map[string][]byte)
176182
}
177-
responses[graphsync.RequestID(res.Id)] = newResponse(graphsync.RequestID(res.Id), graphsync.ResponseStatusCode(res.Status), exts)
183+
uid, err := uuid.FromBytes(res.Id)
184+
if err != nil {
185+
return GraphSyncMessage{}, err
186+
}
187+
id := graphsync.RequestID(uid)
188+
responses[id] = newResponse(id, graphsync.ResponseStatusCode(res.Status), exts)
178189
}
179190

180191
blks := make(map[cid.Cid]blocks.Block, len(pbm.GetData()))
@@ -270,7 +281,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
270281
}
271282
}
272283
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
273-
Id: int32(request.id),
284+
Id: request.id[:],
274285
Root: request.root.Bytes(),
275286
Selector: selector,
276287
Priority: int32(request.priority),
@@ -283,7 +294,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
283294
pbm.Responses = make([]*pb.Message_Response, 0, len(gsm.responses))
284295
for _, response := range gsm.responses {
285296
pbm.Responses = append(pbm.Responses, &pb.Message_Response{
286-
Id: int32(response.requestID),
297+
Id: response.requestID[:],
287298
Status: int32(response.status),
288299
Extensions: response.extensions,
289300
})

message/message_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAppendingRequests(t *testing.T) {
2626
root := testutil.GenerateCids(1)[0]
2727
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
2828
selector := ssb.Matcher().Node()
29-
id := graphsync.RequestID(rand.Int31())
29+
id := graphsync.NewRequestID()
3030
priority := graphsync.Priority(rand.Int31())
3131

3232
builder := NewBuilder(Topic(0))
@@ -51,7 +51,7 @@ func TestAppendingRequests(t *testing.T) {
5151
require.NoError(t, err)
5252

5353
pbRequest := pbMessage.Requests[0]
54-
require.Equal(t, int32(id), pbRequest.Id)
54+
require.Equal(t, id[:], pbRequest.Id)
5555
require.Equal(t, int32(priority), pbRequest.Priority)
5656
require.False(t, pbRequest.Cancel)
5757
require.False(t, pbRequest.Update)
@@ -82,7 +82,7 @@ func TestAppendingResponses(t *testing.T) {
8282
Name: extensionName,
8383
Data: testutil.RandomBytes(100),
8484
}
85-
requestID := graphsync.RequestID(rand.Int31())
85+
requestID := graphsync.NewRequestID()
8686
status := graphsync.RequestAcknowledged
8787

8888
builder := NewBuilder(Topic(0))
@@ -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, int32(requestID), pbResponse.Id)
105+
require.Equal(t, requestID[:], pbResponse.Id)
106106
require.Equal(t, int32(status), pbResponse.Status)
107107
require.Equal(t, extension.Data, pbResponse.Extensions["graphsync/awesome"])
108108

@@ -154,7 +154,7 @@ func contains(strs []string, x string) bool {
154154
func TestRequestCancel(t *testing.T) {
155155
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
156156
selector := ssb.Matcher().Node()
157-
id := graphsync.RequestID(rand.Int31())
157+
id := graphsync.NewRequestID()
158158
priority := graphsync.Priority(rand.Int31())
159159
root := testutil.GenerateCids(1)[0]
160160

@@ -184,7 +184,7 @@ func TestRequestCancel(t *testing.T) {
184184

185185
func TestRequestUpdate(t *testing.T) {
186186

187-
id := graphsync.RequestID(rand.Int31())
187+
id := graphsync.NewRequestID()
188188
extensionName := graphsync.ExtensionName("graphsync/awesome")
189189
extension := graphsync.ExtensionData{
190190
Name: extensionName,
@@ -235,7 +235,7 @@ func TestToNetFromNetEquivalency(t *testing.T) {
235235
Name: extensionName,
236236
Data: testutil.RandomBytes(100),
237237
}
238-
id := graphsync.RequestID(rand.Int31())
238+
id := graphsync.NewRequestID()
239239
priority := graphsync.Priority(rand.Int31())
240240
status := graphsync.RequestAcknowledged
241241

@@ -325,7 +325,7 @@ func TestMergeExtensions(t *testing.T) {
325325
root := testutil.GenerateCids(1)[0]
326326
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
327327
selector := ssb.Matcher().Node()
328-
id := graphsync.RequestID(rand.Int31())
328+
id := graphsync.NewRequestID()
329329
priority := graphsync.Priority(rand.Int31())
330330
defaultRequest := NewRequest(id, root, selector, priority, initialExtensions...)
331331
t.Run("when merging into empty", func(t *testing.T) {

message/pb/message.pb.go

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

message/pb/message.proto

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ option go_package = ".;graphsync_message_pb";
77
message Message {
88

99
message Request {
10-
int32 id = 1; // unique id set on the requester side
10+
bytes id = 1; // unique id set on the requester side
1111
bytes root = 2; // a CID for the root node in the query
1212
bytes selector = 3; // ipld selector to retrieve
1313
map<string, bytes> extensions = 4; // aux information. useful for other protocols
@@ -17,7 +17,7 @@ message Message {
1717
}
1818

1919
message Response {
20-
int32 id = 1; // the request id
20+
bytes id = 1; // the request id
2121
int32 status = 2; // a status code.
2222
map<string, bytes> extensions = 3; // additional data
2323
}

messagequeue/messagequeue_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestStartupAndShutdown(t *testing.T) {
7373

7474
messageQueue := New(ctx, peer, messageNetwork, allocator, messageSendRetries, sendMessageTimeout)
7575
messageQueue.Startup()
76-
id := graphsync.RequestID(rand.Int31())
76+
id := graphsync.NewRequestID()
7777
priority := graphsync.Priority(rand.Int31())
7878
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
7979
selector := ssb.Matcher().Node()
@@ -111,7 +111,7 @@ func TestShutdownDuringMessageSend(t *testing.T) {
111111

112112
messageQueue := New(ctx, peer, messageNetwork, allocator, messageSendRetries, sendMessageTimeout)
113113
messageQueue.Startup()
114-
id := graphsync.RequestID(rand.Int31())
114+
id := graphsync.NewRequestID()
115115
priority := graphsync.Priority(rand.Int31())
116116
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
117117
selector := ssb.Matcher().Node()
@@ -162,7 +162,7 @@ func TestProcessingNotification(t *testing.T) {
162162
waitGroup.Add(1)
163163
blks := testutil.GenerateBlocksOfSize(3, 128)
164164

165-
responseID := graphsync.RequestID(rand.Int31())
165+
responseID := graphsync.NewRequestID()
166166
extensionName := graphsync.ExtensionName("graphsync/awesome")
167167
extension := graphsync.ExtensionData{
168168
Name: extensionName,
@@ -216,7 +216,7 @@ func TestDedupingMessages(t *testing.T) {
216216
messageQueue := New(ctx, peer, messageNetwork, allocator, messageSendRetries, sendMessageTimeout)
217217
messageQueue.Startup()
218218
waitGroup.Add(1)
219-
id := graphsync.RequestID(rand.Int31())
219+
id := graphsync.NewRequestID()
220220
priority := graphsync.Priority(rand.Int31())
221221
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
222222
selector := ssb.Matcher().Node()
@@ -227,11 +227,11 @@ func TestDedupingMessages(t *testing.T) {
227227
}, []notifications.Notifee{})
228228
// wait for send attempt
229229
waitGroup.Wait()
230-
id2 := graphsync.RequestID(rand.Int31())
230+
id2 := graphsync.NewRequestID()
231231
priority2 := graphsync.Priority(rand.Int31())
232232
selector2 := ssb.ExploreAll(ssb.Matcher()).Node()
233233
root2 := testutil.GenerateCids(1)[0]
234-
id3 := graphsync.RequestID(rand.Int31())
234+
id3 := graphsync.NewRequestID()
235235
priority3 := graphsync.Priority(rand.Int31())
236236
selector3 := ssb.ExploreIndex(0, ssb.Matcher()).Node()
237237
root3 := testutil.GenerateCids(1)[0]

0 commit comments

Comments
 (0)