Skip to content

Commit

Permalink
chore(requestid): wrap requestid string in a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jan 13, 2022
1 parent 52b33da commit d354eff
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
20 changes: 17 additions & 3 deletions graphsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// RequestID is a unique identifier for a GraphSync request.
type RequestID string
type RequestID struct{ string }

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

// String form of a RequestID (should be a well-formed UUIDv4 string)
func (r RequestID) String() string {
return uuid.Must(uuid.FromBytes([]byte(r))).String()
return uuid.Must(uuid.FromBytes([]byte(r.string))).String()
}

// Byte form of a RequestID
func (r RequestID) Bytes() []byte {
return []byte(r.string)
}

// Create a new, random RequestID (should be a UUIDv4)
func NewRequestID() RequestID {
u := uuid.New()
return RequestID(u[:])
return RequestID{string(u[:])}
}

// Create a RequestID from a byte slice
func ParseRequestID(b []byte) (RequestID, error) {
_, err := uuid.FromBytes(b)
if err != nil {
return RequestID{}, err
}
return RequestID{string(b)}, nil
}

// Priority a priority for a GraphSync request.
Expand Down
11 changes: 4 additions & 7 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"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,11 +161,10 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
if exts == nil {
exts = make(map[string][]byte)
}
uid, err := uuid.FromBytes(req.Id)
id, err := graphsync.ParseRequestID(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)
}

Expand All @@ -179,11 +177,10 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
if exts == nil {
exts = make(map[string][]byte)
}
uid, err := uuid.FromBytes(res.Id)
id, err := graphsync.ParseRequestID(res.Id)
if err != nil {
return GraphSyncMessage{}, err
}
id := graphsync.RequestID(uid[:])
responses[id] = newResponse(id, graphsync.ResponseStatusCode(res.Status), exts)
}

Expand Down Expand Up @@ -288,7 +285,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
}
}
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
Id: []byte(request.id),
Id: request.id.Bytes(),
Root: request.root.Bytes(),
Selector: selector,
Priority: int32(request.priority),
Expand All @@ -301,7 +298,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: []byte(response.requestID),
Id: response.requestID.Bytes(),
Status: int32(response.status),
Extensions: response.extensions,
})
Expand Down
4 changes: 2 additions & 2 deletions message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAppendingRequests(t *testing.T) {
require.NoError(t, err)

pbRequest := pbMessage.Requests[0]
require.Equal(t, []byte(id), pbRequest.Id)
require.Equal(t, id.Bytes(), pbRequest.Id)
require.Equal(t, int32(priority), pbRequest.Priority)
require.False(t, pbRequest.Cancel)
require.False(t, pbRequest.Update)
Expand Down Expand Up @@ -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, []byte(requestID), pbResponse.Id)
require.Equal(t, requestID.Bytes(), pbResponse.Id)
require.Equal(t, int32(status), pbResponse.Status)
require.Equal(t, extension.Data, pbResponse.Extensions["graphsync/awesome"])

Expand Down
2 changes: 1 addition & 1 deletion responsemanager/responsemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ func (td *testData) notifyStatusMessagesSent() {

func (td *testData) notifyBlockSendsSent() {
td.transactionLk.Lock()
td.notifeePublisher.PublishEvents(notifications.Topic(rand.Int31), []notifications.Event{
td.notifeePublisher.PublishEvents(notifications.Topic(graphsync.NewRequestID), []notifications.Event{
messagequeue.Event{Name: messagequeue.Sent, Metadata: messagequeue.Metadata{BlockData: td.blkNotifications}},
})
td.blkNotifications = make(map[graphsync.RequestID][]graphsync.BlockData)
Expand Down

0 comments on commit d354eff

Please sign in to comment.