Skip to content

Commit dc6cd57

Browse files
committed
fix(requestid): use string as base type for RequestId
1 parent 3ea3e42 commit dc6cd57

5 files changed

Lines changed: 26 additions & 26 deletions

File tree

graphsync.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// RequestID is a unique identifier for a GraphSync request.
16-
type RequestID uuid.UUID
16+
type RequestID string
1717

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

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

2828
// Create a new, random RequestID (should be a UUIDv4)
2929
func NewRequestID() RequestID {
30-
return RequestID(uuid.New())
30+
u := uuid.New()
31+
return RequestID(u[:])
3132
}
3233

3334
// Priority a priority for a GraphSync request.

message/message.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package message
33
import (
44
"encoding/binary"
55
"errors"
6-
"fmt"
76
"io"
87

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

@@ -184,7 +183,7 @@ func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
184183
if err != nil {
185184
return GraphSyncMessage{}, err
186185
}
187-
id := graphsync.RequestID(uid)
186+
id := graphsync.RequestID(uid[:])
188187
responses[id] = newResponse(id, graphsync.ResponseStatusCode(res.Status), exts)
189188
}
190189

@@ -281,7 +280,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
281280
}
282281
}
283282
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
284-
Id: request.id[:],
283+
Id: []byte(request.id),
285284
Root: request.root.Bytes(),
286285
Selector: selector,
287286
Priority: int32(request.priority),
@@ -294,7 +293,7 @@ func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
294293
pbm.Responses = make([]*pb.Message_Response, 0, len(gsm.responses))
295294
for _, response := range gsm.responses {
296295
pbm.Responses = append(pbm.Responses, &pb.Message_Response{
297-
Id: response.requestID[:],
296+
Id: []byte(response.requestID),
298297
Status: int32(response.status),
299298
Extensions: response.extensions,
300299
})
@@ -333,11 +332,11 @@ func (gsm GraphSyncMessage) ToNet(w io.Writer) error {
333332
func (gsm GraphSyncMessage) Loggable() map[string]interface{} {
334333
requests := make([]string, 0, len(gsm.requests))
335334
for _, request := range gsm.requests {
336-
requests = append(requests, fmt.Sprintf("%d", request.id))
335+
requests = append(requests, request.id.String())
337336
}
338337
responses := make([]string, 0, len(gsm.responses))
339338
for _, response := range gsm.responses {
340-
responses = append(responses, fmt.Sprintf("%d", response.requestID))
339+
responses = append(responses, response.requestID.String())
341340
}
342341
return map[string]interface{}{
343342
"requests": requests,

message/message_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestAppendingRequests(t *testing.T) {
5151
require.NoError(t, err)
5252

5353
pbRequest := pbMessage.Requests[0]
54-
require.Equal(t, id[:], pbRequest.Id)
54+
require.Equal(t, []byte(id), pbRequest.Id)
5555
require.Equal(t, int32(priority), pbRequest.Priority)
5656
require.False(t, pbRequest.Cancel)
5757
require.False(t, pbRequest.Update)
@@ -102,7 +102,7 @@ func TestAppendingResponses(t *testing.T) {
102102
pbMessage, err := gsm.ToProto()
103103
require.NoError(t, err, "serialize to protobuf errored")
104104
pbResponse := pbMessage.Responses[0]
105-
require.Equal(t, requestID[:], pbResponse.Id)
105+
require.Equal(t, []byte(requestID), pbResponse.Id)
106106
require.Equal(t, int32(status), pbResponse.Status)
107107
require.Equal(t, extension.Data, pbResponse.Extensions["graphsync/awesome"])
108108

peerstate/peerstate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,32 @@ func (ps PeerState) Diagnostics() map[graphsync.RequestID][]string {
3030
if ok {
3131
matchedActiveQueue[id] = struct{}{}
3232
if status != graphsync.Running {
33-
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))
33+
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))
3434
}
3535
} else {
36-
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in active task queue but appears to have no tracked state", id))
36+
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in active task queue but appears to have no tracked state", id.String()))
3737
}
3838
}
3939
for _, id := range ps.TaskQueueState.Pending {
4040
status, ok := ps.RequestStates[id]
4141
if ok {
4242
matchedPendingQueue[id] = struct{}{}
4343
if status != graphsync.Queued {
44-
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))
44+
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))
4545
}
4646
} else {
47-
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in pending task queue but appears to have no tracked state", id))
47+
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in pending task queue but appears to have no tracked state", id.String()))
4848
}
4949
}
5050
for id, state := range ps.RequestStates {
5151
if state == graphsync.Running {
5252
if _, ok := matchedActiveQueue[id]; !ok {
53-
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in running state is not in the active task queue", id))
53+
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in running state is not in the active task queue", id.String()))
5454
}
5555
}
5656
if state == graphsync.Queued {
5757
if _, ok := matchedPendingQueue[id]; !ok {
58-
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %d in queued state is not in the pending task queue", id))
58+
diagnostics[id] = append(diagnostics[id], fmt.Sprintf("request with id %s in queued state is not in the pending task queue", id.String()))
5959
}
6060
}
6161
}

peerstate/peerstate_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func TestDiagnostics(t *testing.T) {
4747
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
4848
},
4949
expectedDiagnostics: map[graphsync.RequestID][]string{
50-
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])},
51-
requestIDs[4]: {fmt.Sprintf("expected request with id %d in active task queue to be in running state, but was paused", requestIDs[4])},
50+
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())},
51+
requestIDs[4]: {fmt.Sprintf("expected request with id %s in active task queue to be in running state, but was paused", requestIDs[4].String())},
5252
},
5353
},
5454
"active task with no state": {
@@ -63,7 +63,7 @@ func TestDiagnostics(t *testing.T) {
6363
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
6464
},
6565
expectedDiagnostics: map[graphsync.RequestID][]string{
66-
requestIDs[1]: {fmt.Sprintf("request with id %d in active task queue but appears to have no tracked state", requestIDs[1])},
66+
requestIDs[1]: {fmt.Sprintf("request with id %s in active task queue but appears to have no tracked state", requestIDs[1].String())},
6767
},
6868
},
6969
"pending task with with incorrect state": {
@@ -79,8 +79,8 @@ func TestDiagnostics(t *testing.T) {
7979
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3], requestIDs[4]},
8080
},
8181
expectedDiagnostics: map[graphsync.RequestID][]string{
82-
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])},
83-
requestIDs[4]: {fmt.Sprintf("expected request with id %d in pending task queue to be in queued state, but was paused", requestIDs[4])},
82+
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())},
83+
requestIDs[4]: {fmt.Sprintf("expected request with id %s in pending task queue to be in queued state, but was paused", requestIDs[4].String())},
8484
},
8585
},
8686
"pending task with no state": {
@@ -95,7 +95,7 @@ func TestDiagnostics(t *testing.T) {
9595
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
9696
},
9797
expectedDiagnostics: map[graphsync.RequestID][]string{
98-
requestIDs[3]: {fmt.Sprintf("request with id %d in pending task queue but appears to have no tracked state", requestIDs[3])},
98+
requestIDs[3]: {fmt.Sprintf("request with id %s in pending task queue but appears to have no tracked state", requestIDs[3].String())},
9999
},
100100
},
101101
"request state running with no active task": {
@@ -111,7 +111,7 @@ func TestDiagnostics(t *testing.T) {
111111
Pending: []graphsync.RequestID{requestIDs[2], requestIDs[3]},
112112
},
113113
expectedDiagnostics: map[graphsync.RequestID][]string{
114-
requestIDs[1]: {fmt.Sprintf("request with id %d in running state is not in the active task queue", requestIDs[1])},
114+
requestIDs[1]: {fmt.Sprintf("request with id %s in running state is not in the active task queue", requestIDs[1].String())},
115115
},
116116
},
117117
"request state queued with no pending task": {
@@ -127,7 +127,7 @@ func TestDiagnostics(t *testing.T) {
127127
Pending: []graphsync.RequestID{requestIDs[2]},
128128
},
129129
expectedDiagnostics: map[graphsync.RequestID][]string{
130-
requestIDs[3]: {fmt.Sprintf("request with id %d in queued state is not in the pending task queue", requestIDs[3])},
130+
requestIDs[3]: {fmt.Sprintf("request with id %s in queued state is not in the pending task queue", requestIDs[3].String())},
131131
},
132132
},
133133
}

0 commit comments

Comments
 (0)