Skip to content

Commit

Permalink
feat(requestid): use uuids for requestids
Browse files Browse the repository at this point in the history
Ref: #278
Closes: #279
Closes: #281
  • Loading branch information
rvagg committed Dec 15, 2021
1 parent 655606b commit 52d93a4
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 108 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ipfs/go-graphsync
go 1.16

require (
github.com/google/uuid v1.3.0
github.com/hannahhoward/cbor-gen-for v0.0.0-20200817222906-ea96cece81f1
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e
github.com/ipfs/go-block-format v0.0.3
Expand Down
11 changes: 10 additions & 1 deletion graphsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import (
"errors"
"fmt"

"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/traversal"
"github.com/libp2p/go-libp2p-core/peer"
)

// RequestID is a unique identifier for a GraphSync request.
type RequestID int32
type RequestID uuid.UUID

func (rid RequestID) String() string {
return uuid.UUID(rid).String()
}

func NewRequestID() RequestID {
return RequestID(uuid.New())
}

// Tag returns an easy way to identify this request id as a graphsync request (for libp2p connections)
func (r RequestID) Tag() string {
Expand Down
3 changes: 1 addition & 2 deletions impl/graphsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"io/ioutil"
"math"
"math/rand"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -133,7 +132,7 @@ func TestSendResponseToIncomingRequest(t *testing.T) {
blockChainLength := 100
blockChain := testutil.SetupBlockChain(ctx, t, td.persistence2, 100, blockChainLength)

requestID := graphsync.RequestID(rand.Int31())
requestID := graphsync.NewRequestID()

builder := gsmsg.NewBuilder(gsmsg.Topic(0))
builder.AddRequest(gsmsg.NewRequest(requestID, blockChain.TipLink.(cidlink.Link).Cid, blockChain.Selector(), graphsync.Priority(math.MaxInt32), td.extension))
Expand Down
7 changes: 3 additions & 4 deletions linktracker/linktracker_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package linktracker

import (
"math/rand"
"testing"

"github.com/ipld/go-ipld-prime"
Expand Down Expand Up @@ -74,7 +73,7 @@ func TestBlockRefCount(t *testing.T) {
linkTracker := New()
link := testutil.NewTestLink()
for _, rq := range data.requests {
requestID := graphsync.RequestID(rand.Int31())
requestID := graphsync.NewRequestID()
for _, present := range rq.traversals {
linkTracker.RecordLinkTraversal(requestID, link, present)
}
Expand Down Expand Up @@ -116,7 +115,7 @@ func TestFinishRequest(t *testing.T) {
for testCase, data := range testCases {
t.Run(testCase, func(t *testing.T) {
linkTracker := New()
requestID := graphsync.RequestID(rand.Int31())
requestID := graphsync.NewRequestID()
for _, lt := range data.linksTraversed {
linkTracker.RecordLinkTraversal(requestID, lt.link, lt.blockPresent)
}
Expand Down Expand Up @@ -151,7 +150,7 @@ func TestIsKnownMissingLink(t *testing.T) {
t.Run(testCase, func(t *testing.T) {
linkTracker := New()
link := testutil.NewTestLink()
requestID := graphsync.RequestID(rand.Int31())
requestID := graphsync.NewRequestID()
for _, present := range data.traversals {
linkTracker.RecordLinkTraversal(requestID, link, present)
}
Expand Down
13 changes: 6 additions & 7 deletions message/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package message

import (
"fmt"
"math/rand"
"testing"

"github.com/ipld/go-ipld-prime"
Expand All @@ -21,10 +20,10 @@ func TestMessageBuilding(t *testing.T) {
for _, block := range blocks {
links = append(links, cidlink.Link{Cid: block.Cid()})
}
requestID1 := graphsync.RequestID(rand.Int31())
requestID2 := graphsync.RequestID(rand.Int31())
requestID3 := graphsync.RequestID(rand.Int31())
requestID4 := graphsync.RequestID(rand.Int31())
requestID1 := graphsync.NewRequestID()
requestID2 := graphsync.NewRequestID()
requestID3 := graphsync.NewRequestID()
requestID4 := graphsync.NewRequestID()

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

func TestMessageBuildingExtensionOnly(t *testing.T) {
rb := NewBuilder(Topic(0))
requestID1 := graphsync.RequestID(rand.Int31())
requestID2 := graphsync.RequestID(rand.Int31())
requestID1 := graphsync.NewRequestID()
requestID2 := graphsync.NewRequestID()

extensionData1 := testutil.RandomBytes(100)
extensionName1 := graphsync.ExtensionName("AppleSauce/McGee")
Expand Down
19 changes: 15 additions & 4 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"

"github.com/google/uuid"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
Expand Down Expand Up @@ -162,7 +163,12 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
if exts == nil {
exts = make(map[string][]byte)
}
requests[graphsync.RequestID(req.Id)] = newRequest(graphsync.RequestID(req.Id), root, selector, graphsync.Priority(req.Priority), req.Cancel, req.Update, exts)
uid, err := uuid.FromBytes(req.Id)
if err != nil {
return GraphSyncMessage{}, err
}
id := graphsync.RequestID(uid)
requests[id] = newRequest(id, root, selector, graphsync.Priority(req.Priority), req.Cancel, req.Update, exts)
}

responses := make(map[graphsync.RequestID]GraphSyncResponse, len(pbm.GetResponses()))
Expand All @@ -174,7 +180,12 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
if exts == nil {
exts = make(map[string][]byte)
}
responses[graphsync.RequestID(res.Id)] = newResponse(graphsync.RequestID(res.Id), graphsync.ResponseStatusCode(res.Status), exts)
uid, err := uuid.FromBytes(res.Id)
if err != nil {
return GraphSyncMessage{}, err
}
id := graphsync.RequestID(uid)
responses[id] = newResponse(id, graphsync.ResponseStatusCode(res.Status), exts)
}

blks := make(map[cid.Cid]blocks.Block, len(pbm.GetData()))
Expand Down Expand Up @@ -270,7 +281,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
}
}
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
Id: int32(request.id),
Id: request.id[:],
Root: request.root.Bytes(),
Selector: selector,
Priority: int32(request.priority),
Expand All @@ -283,7 +294,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
pbm.Responses = make([]*pb.Message_Response, 0, len(gsm.responses))
for _, response := range gsm.responses {
pbm.Responses = append(pbm.Responses, &pb.Message_Response{
Id: int32(response.requestID),
Id: response.requestID[:],
Status: int32(response.status),
Extensions: response.extensions,
})
Expand Down
16 changes: 8 additions & 8 deletions message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAppendingRequests(t *testing.T) {
root := testutil.GenerateCids(1)[0]
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
selector := ssb.Matcher().Node()
id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
priority := graphsync.Priority(rand.Int31())

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

pbRequest := pbMessage.Requests[0]
require.Equal(t, int32(id), pbRequest.Id)
require.Equal(t, id[:], pbRequest.Id)
require.Equal(t, int32(priority), pbRequest.Priority)
require.False(t, pbRequest.Cancel)
require.False(t, pbRequest.Update)
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestAppendingResponses(t *testing.T) {
Name: extensionName,
Data: testutil.RandomBytes(100),
}
requestID := graphsync.RequestID(rand.Int31())
requestID := graphsync.NewRequestID()
status := graphsync.RequestAcknowledged

builder := NewBuilder(Topic(0))
Expand All @@ -102,7 +102,7 @@ func TestAppendingResponses(t *testing.T) {
pbMessage, err := gsm.ToProto()
require.NoError(t, err, "serialize to protobuf errored")
pbResponse := pbMessage.Responses[0]
require.Equal(t, int32(requestID), pbResponse.Id)
require.Equal(t, requestID[:], pbResponse.Id)
require.Equal(t, int32(status), pbResponse.Status)
require.Equal(t, extension.Data, pbResponse.Extensions["graphsync/awesome"])

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

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

func TestRequestUpdate(t *testing.T) {

id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
extensionName := graphsync.ExtensionName("graphsync/awesome")
extension := graphsync.ExtensionData{
Name: extensionName,
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestToNetFromNetEquivalency(t *testing.T) {
Name: extensionName,
Data: testutil.RandomBytes(100),
}
id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
priority := graphsync.Priority(rand.Int31())
status := graphsync.RequestAcknowledged

Expand Down Expand Up @@ -325,7 +325,7 @@ func TestMergeExtensions(t *testing.T) {
root := testutil.GenerateCids(1)[0]
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
selector := ssb.Matcher().Node()
id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
priority := graphsync.Priority(rand.Int31())
defaultRequest := NewRequest(id, root, selector, priority, initialExtensions...)
t.Run("when merging into empty", func(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions message/pb/message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions message/pb/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ option go_package = ".;graphsync_message_pb";
message Message {

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

message Response {
int32 id = 1; // the request id
bytes id = 1; // the request id
int32 status = 2; // a status code.
map<string, bytes> extensions = 3; // additional data
}
Expand Down
12 changes: 6 additions & 6 deletions messagequeue/messagequeue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestStartupAndShutdown(t *testing.T) {

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

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

responseID := graphsync.RequestID(rand.Int31())
responseID := graphsync.NewRequestID()
extensionName := graphsync.ExtensionName("graphsync/awesome")
extension := graphsync.ExtensionData{
Name: extensionName,
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestDedupingMessages(t *testing.T) {
messageQueue := New(ctx, peer, messageNetwork, allocator, messageSendRetries, sendMessageTimeout)
messageQueue.Startup()
waitGroup.Add(1)
id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
priority := graphsync.Priority(rand.Int31())
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
selector := ssb.Matcher().Node()
Expand All @@ -227,11 +227,11 @@ func TestDedupingMessages(t *testing.T) {
}, []notifications.Notifee{})
// wait for send attempt
waitGroup.Wait()
id2 := graphsync.RequestID(rand.Int31())
id2 := graphsync.NewRequestID()
priority2 := graphsync.Priority(rand.Int31())
selector2 := ssb.ExploreAll(ssb.Matcher()).Node()
root2 := testutil.GenerateCids(1)[0]
id3 := graphsync.RequestID(rand.Int31())
id3 := graphsync.NewRequestID()
priority3 := graphsync.Priority(rand.Int31())
selector3 := ssb.ExploreIndex(0, ssb.Matcher()).Node()
root3 := testutil.GenerateCids(1)[0]
Expand Down
2 changes: 1 addition & 1 deletion network/libp2p_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestMessageSendAndReceive(t *testing.T) {
Name: extensionName,
Data: testutil.RandomBytes(100),
}
id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
priority := graphsync.Priority(rand.Int31())
status := graphsync.RequestAcknowledged

Expand Down
Loading

0 comments on commit 52d93a4

Please sign in to comment.