Skip to content

Commit 480fcf5

Browse files
committed
feat(requestid): use uuids for requestids
Ref: #278 Closes: #279 Closes: #281
1 parent f330fe8 commit 480fcf5

26 files changed

+111
-108
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.16
44

55
require (
66
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
7+
github.com/google/uuid v1.3.0
78
github.com/hannahhoward/cbor-gen-for v0.0.0-20200817222906-ea96cece81f1
89
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e
910
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"
@@ -136,7 +135,7 @@ func TestSendResponseToIncomingRequest(t *testing.T) {
136135
blockChainLength := 100
137136
blockChain := testutil.SetupBlockChain(ctx, t, td.persistence2, 100, blockChainLength)
138137

139-
requestID := graphsync.RequestID(rand.Int31())
138+
requestID := graphsync.NewRequestID()
140139

141140
builder := gsmsg.NewBuilder()
142141
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

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package message
33
import (
44
"bytes"
55
"io"
6-
"math/rand"
76
"testing"
87

98
"github.com/ipld/go-ipld-prime"
@@ -55,10 +54,10 @@ func TestMessageBuilding(t *testing.T) {
5554
Name: extensionName2,
5655
Data: extensionData2,
5756
}
58-
requestID1 := graphsync.RequestID(rand.Int31())
59-
requestID2 := graphsync.RequestID(rand.Int31())
60-
requestID3 := graphsync.RequestID(rand.Int31())
61-
requestID4 := graphsync.RequestID(rand.Int31())
57+
requestID1 := graphsync.NewRequestID()
58+
requestID2 := graphsync.NewRequestID()
59+
requestID3 := graphsync.NewRequestID()
60+
requestID4 := graphsync.NewRequestID()
6261
closer := io.NopCloser(nil)
6362
testCases := map[string]struct {
6463
build func(*Builder)

message/message.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"sort"
99

10+
"github.com/google/uuid"
1011
blocks "github.com/ipfs/go-block-format"
1112
cid "github.com/ipfs/go-cid"
1213
"github.com/ipld/go-ipld-prime"
@@ -326,7 +327,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
326327
}
327328
}
328329
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
329-
Id: int32(request.ID),
330+
Id: request.ID[:],
330331
Root: request.Root.Bytes(),
331332
Selector: selector,
332333
Priority: int32(request.Priority),
@@ -339,7 +340,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
339340
pbm.Responses = make([]*pb.Message_Response, 0, len(gsm.Responses))
340341
for _, response := range gsm.Responses {
341342
pbm.Responses = append(pbm.Responses, &pb.Message_Response{
342-
Id: int32(response.ID),
343+
Id: response.ID[:],
343344
Status: int32(response.Status),
344345
Extensions: toProtoExtensions(response.Extensions),
345346
})

message/message_test.go

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

3434
builder := NewBuilder()
@@ -53,7 +53,7 @@ func TestAppendingRequests(t *testing.T) {
5353
require.NoError(t, err)
5454

5555
pbRequest := pbMessage.Requests[0]
56-
require.Equal(t, int32(id), pbRequest.Id)
56+
require.Equal(t, id[:], pbRequest.Id)
5757
require.Equal(t, int32(priority), pbRequest.Priority)
5858
require.False(t, pbRequest.Cancel)
5959
require.False(t, pbRequest.Update)
@@ -85,7 +85,7 @@ func TestAppendingResponses(t *testing.T) {
8585
Name: extensionName,
8686
Data: basicnode.NewBytes(extensionBytes),
8787
}
88-
requestID := graphsync.RequestID(rand.Int31())
88+
requestID := graphsync.NewRequestID()
8989
status := graphsync.RequestAcknowledged
9090

9191
builder := NewBuilder()
@@ -105,7 +105,7 @@ func TestAppendingResponses(t *testing.T) {
105105
pbMessage, err := gsm.ToProto()
106106
require.NoError(t, err, "serialize to protobuf errored")
107107
pbResponse := pbMessage.Responses[0]
108-
require.Equal(t, int32(requestID), pbResponse.Id)
108+
require.Equal(t, requestID[:], pbResponse.Id)
109109
require.Equal(t, int32(status), pbResponse.Status)
110110
require.Equal(t, extensionBytes, pbResponse.Extensions["graphsync/awesome"])
111111

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

@@ -187,7 +187,7 @@ func TestRequestCancel(t *testing.T) {
187187

188188
func TestRequestUpdate(t *testing.T) {
189189

190-
id := graphsync.RequestID(rand.Int31())
190+
id := graphsync.NewRequestID()
191191
extensionName := graphsync.ExtensionName("graphsync/awesome")
192192
extension := NamedExtension{
193193
Name: extensionName,
@@ -238,7 +238,7 @@ func TestToNetFromNetEquivalency(t *testing.T) {
238238
Name: extensionName,
239239
Data: basicnode.NewBytes(testutil.RandomBytes(100)),
240240
}
241-
id := graphsync.RequestID(rand.Int31())
241+
id := graphsync.NewRequestID()
242242
priority := graphsync.Priority(rand.Int31())
243243
status := graphsync.RequestAcknowledged
244244

@@ -332,7 +332,7 @@ func TestMergeExtensions(t *testing.T) {
332332
root := testutil.GenerateCids(1)[0]
333333
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
334334
selector := ssb.Matcher().Node()
335-
id := graphsync.RequestID(rand.Int31())
335+
id := graphsync.NewRequestID()
336336
priority := graphsync.Priority(rand.Int31())
337337
defaultRequest := NewRequest(id, root, selector, priority, initialExtensions...)
338338
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

+8-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestStartupAndShutdown(t *testing.T) {
3939

4040
messageQueue := New(ctx, peer, messageNetwork, allocator, messageSendRetries, sendMessageTimeout)
4141
messageQueue.Startup()
42-
id := graphsync.RequestID(rand.Int31())
42+
id := graphsync.NewRequestID()
4343
priority := graphsync.Priority(rand.Int31())
4444
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
4545
selector := ssb.Matcher().Node()
@@ -77,7 +77,7 @@ func TestShutdownDuringMessageSend(t *testing.T) {
7777

7878
messageQueue := New(ctx, peer, messageNetwork, allocator, messageSendRetries, sendMessageTimeout)
7979
messageQueue.Startup()
80-
id := graphsync.RequestID(rand.Int31())
80+
id := graphsync.NewRequestID()
8181
priority := graphsync.Priority(rand.Int31())
8282
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
8383
selector := ssb.Matcher().Node()
@@ -128,7 +128,7 @@ func TestProcessingNotification(t *testing.T) {
128128
waitGroup.Add(1)
129129
blks := testutil.GenerateBlocksOfSize(3, 128)
130130

131-
responseID := graphsync.RequestID(rand.Int31())
131+
responseID := graphsync.NewRequestID()
132132
extensionName := graphsync.ExtensionName("graphsync/awesome")
133133
extension := graphsync.ExtensionData{
134134
Name: extensionName,
@@ -199,7 +199,7 @@ func TestDedupingMessages(t *testing.T) {
199199
messageQueue := New(ctx, peer, messageNetwork, allocator, messageSendRetries, sendMessageTimeout)
200200
messageQueue.Startup()
201201
waitGroup.Add(1)
202-
id := graphsync.RequestID(rand.Int31())
202+
id := graphsync.NewRequestID()
203203
priority := graphsync.Priority(rand.Int31())
204204
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
205205
selector := ssb.Matcher().Node()
@@ -210,11 +210,11 @@ func TestDedupingMessages(t *testing.T) {
210210
})
211211
// wait for send attempt
212212
waitGroup.Wait()
213-
id2 := graphsync.RequestID(rand.Int31())
213+
id2 := graphsync.NewRequestID()
214214
priority2 := graphsync.Priority(rand.Int31())
215215
selector2 := ssb.ExploreAll(ssb.Matcher()).Node()
216216
root2 := testutil.GenerateCids(1)[0]
217-
id3 := graphsync.RequestID(rand.Int31())
217+
id3 := graphsync.NewRequestID()
218218
priority3 := graphsync.Priority(rand.Int31())
219219
selector3 := ssb.ExploreIndex(0, ssb.Matcher()).Node()
220220
root3 := testutil.GenerateCids(1)[0]
@@ -385,8 +385,8 @@ func TestNetworkErrorClearResponses(t *testing.T) {
385385
messagesSent := make(chan gsmsg.GraphSyncMessage)
386386
resetChan := make(chan struct{}, 1)
387387
fullClosedChan := make(chan struct{}, 1)
388-
requestID1 := graphsync.RequestID(rand.Int31())
389-
requestID2 := graphsync.RequestID(rand.Int31())
388+
requestID1 := graphsync.NewRequestID()
389+
requestID2 := graphsync.NewRequestID()
390390
messageSender := &fakeMessageSender{nil, fullClosedChan, resetChan, messagesSent}
391391
var waitGroup sync.WaitGroup
392392
messageNetwork := &fakeMessageNetwork{nil, nil, messageSender, &waitGroup}

network/libp2p_impl_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestMessageSendAndReceive(t *testing.T) {
7777
Name: extensionName,
7878
Data: testutil.RandomBytes(100),
7979
}
80-
id := graphsync.RequestID(rand.Int31())
80+
id := graphsync.NewRequestID()
8181
priority := graphsync.Priority(rand.Int31())
8282
status := graphsync.RequestAcknowledged
8383

peermanager/peermessagemanager_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestSendingMessagesToPeers(t *testing.T) {
6767

6868
tp := testutil.GeneratePeers(5)
6969

70-
id := graphsync.RequestID(rand.Int31())
70+
id := graphsync.NewRequestID()
7171
priority := graphsync.Priority(rand.Int31())
7272
root := testutil.GenerateCids(1)[0]
7373
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)

peerstate/peerstate_test.go

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

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

87
"github.com/stretchr/testify/require"
@@ -14,7 +13,7 @@ import (
1413
func TestDiagnostics(t *testing.T) {
1514
requestIDs := make([]graphsync.RequestID, 0, 5)
1615
for i := 0; i < 5; i++ {
17-
requestIDs = append(requestIDs, graphsync.RequestID(rand.Int31()))
16+
requestIDs = append(requestIDs, graphsync.NewRequestID())
1817
}
1918
testCases := map[string]struct {
2019
requestStates graphsync.RequestStates

0 commit comments

Comments
 (0)