Skip to content

Commit

Permalink
fix(requestid): use string as base type for RequestId
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jan 5, 2022
1 parent e00144d commit 9e94ae5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
7 changes: 4 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 uuid.UUID
type RequestID 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,12 +22,13 @@ func (r RequestID) Tag() string {

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

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

// Priority a priority for a GraphSync request.
Expand Down
13 changes: 6 additions & 7 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package message
import (
"encoding/binary"
"errors"
"fmt"
"io"

"github.com/google/uuid"
Expand Down Expand Up @@ -167,7 +166,7 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
if err != nil {
return GraphSyncMessage{}, err
}
id := graphsync.RequestID(uid)
id := graphsync.RequestID(uid[:])
requests[id] = newRequest(id, root, selector, graphsync.Priority(req.Priority), req.Cancel, req.Update, exts)
}

Expand All @@ -184,7 +183,7 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
if err != nil {
return GraphSyncMessage{}, err
}
id := graphsync.RequestID(uid)
id := graphsync.RequestID(uid[:])
responses[id] = newResponse(id, graphsync.ResponseStatusCode(res.Status), exts)
}

Expand Down Expand Up @@ -289,7 +288,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
}
}
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
Id: request.id[:],
Id: []byte(request.id),
Root: request.root.Bytes(),
Selector: selector,
Priority: int32(request.priority),
Expand All @@ -302,7 +301,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: response.requestID[:],
Id: []byte(response.requestID),
Status: int32(response.status),
Extensions: response.extensions,
})
Expand Down Expand Up @@ -341,11 +340,11 @@ func (gsm GraphSyncMessage) ToNet(w io.Writer) error {
func (gsm GraphSyncMessage) Loggable() map[string]interface{} {
requests := make([]string, 0, len(gsm.requests))
for _, request := range gsm.requests {
requests = append(requests, fmt.Sprintf("%d", request.id))
requests = append(requests, request.id.String())
}
responses := make([]string, 0, len(gsm.responses))
for _, response := range gsm.responses {
responses = append(responses, fmt.Sprintf("%d", response.requestID))
responses = append(responses, response.requestID.String())
}
return map[string]interface{}{
"requests": requests,
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, id[:], pbRequest.Id)
require.Equal(t, []byte(id), 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, requestID[:], pbResponse.Id)
require.Equal(t, []byte(requestID), pbResponse.Id)
require.Equal(t, int32(status), pbResponse.Status)
require.Equal(t, extension.Data, pbResponse.Extensions["graphsync/awesome"])

Expand Down
12 changes: 6 additions & 6 deletions peerstate/peerstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@ func (ps PeerState) Diagnostics() map[graphsync.RequestID][]string {
if ok {
matchedActiveQueue[id] = struct{}{}
if status != graphsync.Running {
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("expected request with id %d in active task queue to be in running state, but was %s", id, status))
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("expected request with id %s in active task queue to be in running state, but was %s", id.String(), status))
}
} else {
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in active task queue but appears to have no tracked state", id))
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in active task queue but appears to have no tracked state", id.String()))
}
}
for _, id := range ps.TaskQueueState.Pending {
status, ok := ps.RequestStates[id]
if ok {
matchedPendingQueue[id] = struct{}{}
if status != graphsync.Queued {
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("expected request with id %d in pending task queue to be in queued state, but was %s", id, status))
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("expected request with id %s in pending task queue to be in queued state, but was %s", id.String(), status))
}
} else {
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in pending task queue but appears to have no tracked state", id))
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in pending task queue but appears to have no tracked state", id.String()))
}
}
for id, state := range ps.RequestStates {
if state == graphsync.Running {
if _, ok := matchedActiveQueue[id]; !ok {
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in running state is not in the active task queue", id))
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in running state is not in the active task queue", id.String()))
}
}
if state == graphsync.Queued {
if _, ok := matchedPendingQueue[id]; !ok {
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in queued state is not in the pending task queue", id))
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in queued state is not in the pending task queue", id.String()))
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions peerstate/peerstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestDiagnostics(t *testing.T) {
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
},
expectedDiagnostics: map[graphsync.RequestID][]string{
requestIDs[1]: {fmt.Sprintf("expected request with id %d in active task queue to be in running state, but was queued", requestIDs[1]), fmt.Sprintf("request with id %d in queued state is not in the pending task queue", requestIDs[1])},
requestIDs[4]: {fmt.Sprintf("expected request with id %d in active task queue to be in running state, but was paused", requestIDs[4])},
requestIDs[1]: {fmt.Sprintf("expected request with id %s in active task queue to be in running state, but was queued", requestIDs[1].String()), fmt.Sprintf("request with id %s in queued state is not in the pending task queue", requestIDs[1].String())},
requestIDs[4]: {fmt.Sprintf("expected request with id %s in active task queue to be in running state, but was paused", requestIDs[4].String())},
},
},
"active task with no state": {
Expand All @@ -63,7 +63,7 @@ func TestDiagnostics(t *testing.T) {
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
},
expectedDiagnostics: map[graphsync.RequestID][]string{
requestIDs[1]: {fmt.Sprintf("request with id %d in active task queue but appears to have no tracked state", requestIDs[1])},
requestIDs[1]: {fmt.Sprintf("request with id %s in active task queue but appears to have no tracked state", requestIDs[1].String())},
},
},
"pending task with with incorrect state": {
Expand All @@ -79,8 +79,8 @@ func TestDiagnostics(t *testing.T) {
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3], requestIDs[4]},
},
expectedDiagnostics: map[graphsync.RequestID][]string{
requestIDs[3]: {fmt.Sprintf("expected request with id %d in pending task queue to be in queued state, but was running", requestIDs[3]), fmt.Sprintf("request with id %d in running state is not in the active task queue", requestIDs[3])},
requestIDs[4]: {fmt.Sprintf("expected request with id %d in pending task queue to be in queued state, but was paused", requestIDs[4])},
requestIDs[3]: {fmt.Sprintf("expected request with id %s in pending task queue to be in queued state, but was running", requestIDs[3].String()), fmt.Sprintf("request with id %s in running state is not in the active task queue", requestIDs[3].String())},
requestIDs[4]: {fmt.Sprintf("expected request with id %s in pending task queue to be in queued state, but was paused", requestIDs[4].String())},
},
},
"pending task with no state": {
Expand All @@ -95,7 +95,7 @@ func TestDiagnostics(t *testing.T) {
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
},
expectedDiagnostics: map[graphsync.RequestID][]string{
requestIDs[3]: {fmt.Sprintf("request with id %d in pending task queue but appears to have no tracked state", requestIDs[3])},
requestIDs[3]: {fmt.Sprintf("request with id %s in pending task queue but appears to have no tracked state", requestIDs[3].String())},
},
},
"request state running with no active task": {
Expand All @@ -111,7 +111,7 @@ func TestDiagnostics(t *testing.T) {
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
},
expectedDiagnostics: map[graphsync.RequestID][]string{
requestIDs[1]: {fmt.Sprintf("request with id %d in running state is not in the active task queue", requestIDs[1])},
requestIDs[1]: {fmt.Sprintf("request with id %s in running state is not in the active task queue", requestIDs[1].String())},
},
},
"request state queued with no pending task": {
Expand All @@ -127,7 +127,7 @@ func TestDiagnostics(t *testing.T) {
Pending: []graphsync.RequestID{requestIDs[2]},
},
expectedDiagnostics: map[graphsync.RequestID][]string{
requestIDs[3]: {fmt.Sprintf("request with id %d in queued state is not in the pending task queue", requestIDs[3])},
requestIDs[3]: {fmt.Sprintf("request with id %s in queued state is not in the pending task queue", requestIDs[3].String())},
},
},
}
Expand Down

0 comments on commit 9e94ae5

Please sign in to comment.