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 Jan 17, 2022
1 parent f330fe8 commit 480fcf5
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 108 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
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
15 changes: 13 additions & 2 deletions graphsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ 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

// Tag returns an easy way to identify this request id as a graphsync request (for libp2p connections)
func (r RequestID) Tag() string {
return fmt.Sprintf("graphsync-request-%d", r)
return r.String()
}

// String form of a RequestID (should be a well-formed UUIDv4 string)
func (r RequestID) String() string {
return uuid.UUID(r).String()
}

// Create a new, random RequestID (should be a UUIDv4)
func NewRequestID() RequestID {
return RequestID(uuid.New())
}

// Priority a priority for a GraphSync request.
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 @@ -136,7 +135,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()
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
9 changes: 4 additions & 5 deletions message/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package message
import (
"bytes"
"io"
"math/rand"
"testing"

"github.com/ipld/go-ipld-prime"
Expand Down Expand Up @@ -55,10 +54,10 @@ func TestMessageBuilding(t *testing.T) {
Name: extensionName2,
Data: extensionData2,
}
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()
closer := io.NopCloser(nil)
testCases := map[string]struct {
build func(*Builder)
Expand Down
5 changes: 3 additions & 2 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"sort"

"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 @@ -326,7 +327,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 @@ -339,7 +340,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.ID),
Id: response.ID[:],
Status: int32(response.Status),
Extensions: toProtoExtensions(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 @@ -28,7 +28,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()
Expand All @@ -53,7 +53,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 @@ -85,7 +85,7 @@ func TestAppendingResponses(t *testing.T) {
Name: extensionName,
Data: basicnode.NewBytes(extensionBytes),
}
requestID := graphsync.RequestID(rand.Int31())
requestID := graphsync.NewRequestID()
status := graphsync.RequestAcknowledged

builder := NewBuilder()
Expand All @@ -105,7 +105,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, extensionBytes, pbResponse.Extensions["graphsync/awesome"])

Expand Down Expand Up @@ -157,7 +157,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 @@ -187,7 +187,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 := NamedExtension{
Name: extensionName,
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestToNetFromNetEquivalency(t *testing.T) {
Name: extensionName,
Data: basicnode.NewBytes(testutil.RandomBytes(100)),
}
id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
priority := graphsync.Priority(rand.Int31())
status := graphsync.RequestAcknowledged

Expand Down Expand Up @@ -332,7 +332,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
16 changes: 8 additions & 8 deletions messagequeue/messagequeue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -77,7 +77,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 @@ -128,7 +128,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 @@ -199,7 +199,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 @@ -210,11 +210,11 @@ func TestDedupingMessages(t *testing.T) {
})
// 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 Expand Up @@ -385,8 +385,8 @@ func TestNetworkErrorClearResponses(t *testing.T) {
messagesSent := make(chan gsmsg.GraphSyncMessage)
resetChan := make(chan struct{}, 1)
fullClosedChan := make(chan struct{}, 1)
requestID1 := graphsync.RequestID(rand.Int31())
requestID2 := graphsync.RequestID(rand.Int31())
requestID1 := graphsync.NewRequestID()
requestID2 := graphsync.NewRequestID()
messageSender := &fakeMessageSender{nil, fullClosedChan, resetChan, messagesSent}
var waitGroup sync.WaitGroup
messageNetwork := &fakeMessageNetwork{nil, nil, messageSender, &waitGroup}
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
2 changes: 1 addition & 1 deletion peermanager/peermessagemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestSendingMessagesToPeers(t *testing.T) {

tp := testutil.GeneratePeers(5)

id := graphsync.RequestID(rand.Int31())
id := graphsync.NewRequestID()
priority := graphsync.Priority(rand.Int31())
root := testutil.GenerateCids(1)[0]
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
Expand Down
3 changes: 1 addition & 2 deletions peerstate/peerstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package peerstate_test

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

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

0 comments on commit 480fcf5

Please sign in to comment.