Commit ab3f4f9d authored by hannahhoward's avatar hannahhoward

feat(GraphSyncMessage): use Cid for root

Use a Cid as a root instead of a node, so that only one round trip is neccesary
parent b361b8ec
docs/GraphSync.png

255 KB | W: | H:

docs/GraphSync.png

253 KB | W: | H:

docs/GraphSync.png
docs/GraphSync.png
docs/GraphSync.png
docs/GraphSync.png
  • 2-up
  • Swipe
  • Onion skin
This diff is collapsed.
......@@ -25,8 +25,8 @@ package "go-selector" {
interface "GetBlockFunc func(cid) block.Block" as GetBlockFunc
interface SelectorQuerier {
Select(Selector, root Node) SelectionTraverser
Validate(Selector, root Node, incomingResponses SelectionTraverser) SelectionTraverser
Select(Selector, root Cid) SelectionTraverser
Validate(Selector, root Cid, incomingResponses SelectionTraverser) SelectionTraverser
}
object "Package Public Functions" as goSelectorPF {
......@@ -45,7 +45,7 @@ package "go-graphsync" {
responseManager: ResponseManager
selectorQuerier: SelectorQuerier
Request(p peer.ID, selector Selector, root Node) SelectionTraverser
Request(p peer.ID, selector Selector, root Cid) SelectionTraverser
ReceiveMessage(ctx context.Context, sender peer.ID, incoming GraphSyncMessage)
ReceiveError(error)
}
......@@ -74,8 +74,7 @@ package "go-graphsync" {
object "Package Public Functions" as goGraphSyncNetworkPF {
NewLibP2PNetwork(host libp2pHost.Host,
decodeSelectionResponseFunc: DecodeSelectionResponseFunc,
decodeSelectorFunc: DecodeSelectorFunc,
decodeRootNodeFunc: DecodeRootNodeFunc) GraphSyncNetwork
decodeSelectorFunc: DecodeSelectorFunc) GraphSyncNetwork
}
goGraphSyncNetworkPF .. libP2PGraphSyncNetwork
}
......@@ -85,7 +84,7 @@ package "go-graphsync" {
network : GraphSyncNetwork
selectorQuerier: SelectorQuerier
SendRequest(p peer.ID, selector Selector, node rootNode) SelectionTraverser
SendRequest(p peer.ID, selector Selector, root Cid) SelectionTraverser
ProcessResponses(responses []GraphSyncResponse)
}
SelectorQuerier --* RequestManager
......@@ -118,19 +117,17 @@ package "go-graphsync" {
package message {
interface "DecodeSelectionResponseFunc func([]byte) SelectionResponse" as DecodeSelectionResponseFunc
interface "DecodeSelectorFunc func([]byte) Selector" as DecodeSelectorFunc
interface "DecodeRootNodeFunc func([]byte) Node" as DecodeRootNodeFunc
object "Package Public Functions" as goGraphSyncMessagePF {
func FromPBReader(pbr ggio.Reader,
decodeSelectionResponseFunc: DecodeSelectionResponseFunc,
decodeSelectorFunc: DecodeSelectorFunc,
decodeRootNodeFunc: DecodeRootNodeFunc) (GraphSyncMessage, error)
decodeSelectorFunc: DecodeSelectorFunc) (GraphSyncMessage, error)
}
goGraphSyncMessagePF .. libP2PGraphSyncNetwork
interface GraphSyncRequest {
Selector() Selector
Root() Node
Root() Cid
Priority() Priority
ID() int
IsCancel() bool
......@@ -169,7 +166,6 @@ package "go-filecoin" {
host: libp2pHost.Host
decodeSelectionResponseFunc: DecodeSelectionResponseFunc
decodeSelectorFunc: DecodeSelectorFunc
decodeRootNodeFunc: DecodeRootNodeFunc
}
"go-filecoin" *-- GraphSync
......
......@@ -4,9 +4,9 @@ import (
"fmt"
ggio "github.com/gogo/protobuf/io"
cid "github.com/ipfs/go-cid"
pb "github.com/ipfs/go-graphsync/message/pb"
gsselector "github.com/ipfs/go-graphsync/selector"
ipld "github.com/ipfs/go-ipld-format"
)
// GraphSyncRequestID is a unique identifier for a GraphSync request.
......@@ -64,7 +64,7 @@ const (
// GraphSyncMessage.
type GraphSyncRequest interface {
Selector() gsselector.Selector
Root() ipld.Node
Root() cid.Cid
Priority() GraphSyncPriority
ID() GraphSyncRequestID
IsCancel() bool
......@@ -87,7 +87,7 @@ type GraphSyncMessage interface {
AddRequest(id GraphSyncRequestID,
selector gsselector.Selector,
root ipld.Node,
root cid.Cid,
priority GraphSyncPriority)
Cancel(id GraphSyncRequestID)
......@@ -109,7 +109,7 @@ type Exportable interface {
type graphSyncRequest struct {
selector gsselector.Selector
root ipld.Node
root cid.Cid
priority GraphSyncPriority
id GraphSyncRequestID
isCancel bool
......@@ -130,10 +130,6 @@ type graphSyncMessage struct {
// the Selector interface from a raw byte array.
type DecodeSelectorFunc func([]byte) gsselector.Selector
// DecodeRootNodeFunc is a function that can build a type that satisfies
// the ipld.Node interface from a raw byte array.
type DecodeRootNodeFunc func([]byte) ipld.Node
// DecodeSelectionResponseFunc is a function that can build a type that satisfies
// the SelectionResponse interface from a raw byte array.
type DecodeSelectionResponseFunc func([]byte) gsselector.SelectionResponse
......@@ -151,13 +147,15 @@ func newMsg() *graphSyncMessage {
}
func newMessageFromProto(pbm pb.Message,
decodeRootNode DecodeRootNodeFunc,
decodeSelector DecodeSelectorFunc,
decodeSelectionResponse DecodeSelectionResponseFunc) (GraphSyncMessage, error) {
gsm := newMsg()
for _, req := range pbm.Reqlist {
selector := decodeSelector(req.Selector)
root := decodeRootNode(req.Root)
root, err := cid.Cast([]byte(req.Root))
if err != nil {
return nil, fmt.Errorf("incorrectly formatted cid in request queery: %s", err)
}
gsm.addRequest(GraphSyncRequestID(req.Id), selector, root, GraphSyncPriority(req.Priority), req.Cancel)
}
......@@ -187,12 +185,12 @@ func (gsm *graphSyncMessage) Responses() []GraphSyncResponse {
func (gsm *graphSyncMessage) Cancel(id GraphSyncRequestID) {
delete(gsm.requests, id)
gsm.addRequest(id, nil, nil, 0, true)
gsm.addRequest(id, nil, cid.Cid{}, 0, true)
}
func (gsm *graphSyncMessage) AddRequest(id GraphSyncRequestID,
selector gsselector.Selector,
root ipld.Node,
root cid.Cid,
priority GraphSyncPriority,
) {
gsm.addRequest(id, selector, root, priority, false)
......@@ -200,7 +198,7 @@ func (gsm *graphSyncMessage) AddRequest(id GraphSyncRequestID,
func (gsm *graphSyncMessage) addRequest(id GraphSyncRequestID,
selector gsselector.Selector,
root ipld.Node,
root cid.Cid,
priority GraphSyncPriority,
isCancel bool) {
gsm.requests[id] = &graphSyncRequest{
......@@ -224,7 +222,6 @@ func (gsm *graphSyncMessage) AddResponse(requestID GraphSyncRequestID,
// FromPBReader can deserialize a protobuf message into a GraphySyncMessage.
func FromPBReader(pbr ggio.Reader,
decodeRootNode DecodeRootNodeFunc,
decodeSelector DecodeSelectorFunc,
decodeSelectionResponse DecodeSelectionResponseFunc) (GraphSyncMessage, error) {
pb := new(pb.Message)
......@@ -232,7 +229,7 @@ func FromPBReader(pbr ggio.Reader,
return nil, err
}
return newMessageFromProto(*pb, decodeRootNode, decodeSelector, decodeSelectionResponse)
return newMessageFromProto(*pb, decodeSelector, decodeSelectionResponse)
}
func (gsm *graphSyncMessage) ToProto() *pb.Message {
......@@ -241,7 +238,7 @@ func (gsm *graphSyncMessage) ToProto() *pb.Message {
for _, request := range gsm.requests {
pbm.Reqlist = append(pbm.Reqlist, pb.Message_Request{
Id: int32(request.id),
Root: request.root.RawData(),
Root: request.root.Bytes(),
Selector: request.selector.RawData(),
Priority: int32(request.priority),
Cancel: request.isCancel,
......@@ -275,7 +272,7 @@ func (gsm *graphSyncMessage) Loggable() map[string]interface{} {
}
func (gsr *graphSyncRequest) ID() GraphSyncRequestID { return gsr.id }
func (gsr *graphSyncRequest) Root() ipld.Node { return gsr.root }
func (gsr *graphSyncRequest) Root() cid.Cid { return gsr.root }
func (gsr *graphSyncRequest) Selector() gsselector.Selector { return gsr.selector }
func (gsr *graphSyncRequest) Priority() GraphSyncPriority { return gsr.priority }
func (gsr *graphSyncRequest) IsCancel() bool { return gsr.isCancel }
......
......@@ -10,12 +10,12 @@ import (
func TestAppendingRequests(t *testing.T) {
selector := testselector.GenerateSelector()
rootNode := testselector.GenerateRootNode()
root := testselector.GenerateRootCid()
id := GraphSyncRequestID(rand.Int31())
priority := GraphSyncPriority(rand.Int31())
gsm := New()
gsm.AddRequest(id, selector, rootNode, priority)
gsm.AddRequest(id, selector, root, priority)
requests := gsm.Requests()
if len(requests) != 1 {
t.Fatal("Did not add request to message")
......@@ -24,7 +24,7 @@ func TestAppendingRequests(t *testing.T) {
if request.ID() != id ||
request.IsCancel() != false ||
request.Priority() != priority ||
!reflect.DeepEqual(request.Root(), rootNode) ||
!reflect.DeepEqual(request.Root(), root) ||
!reflect.DeepEqual(request.Selector(), selector) {
t.Fatal("Did not properly add request to message")
}
......@@ -34,13 +34,12 @@ func TestAppendingRequests(t *testing.T) {
if pbRequest.Id != int32(id) ||
pbRequest.Priority != int32(priority) ||
pbRequest.Cancel != false ||
!reflect.DeepEqual(pbRequest.Root, rootNode.RawData()) ||
!reflect.DeepEqual(pbRequest.Root, root.Bytes()) ||
!reflect.DeepEqual(pbRequest.Selector, selector.RawData()) {
t.Fatal("Did not properly serialize message to protobuf")
}
deserialized, err := newMessageFromProto(*pbMessage,
testselector.MockDecodeRootNodeFunc,
testselector.MockDecodeSelectorFunc,
testselector.MockDecodeSelectionResponseFunc,
)
......@@ -55,7 +54,7 @@ func TestAppendingRequests(t *testing.T) {
if deserializedRequest.ID() != id ||
deserializedRequest.IsCancel() != false ||
deserializedRequest.Priority() != priority ||
!reflect.DeepEqual(deserializedRequest.Root(), rootNode) ||
!reflect.DeepEqual(deserializedRequest.Root(), root) ||
!reflect.DeepEqual(deserializedRequest.Selector(), selector) {
t.Fatal("Did not properly deserialize protobuf messages so requests are equal")
}
......@@ -88,7 +87,6 @@ func TestAppendingResponses(t *testing.T) {
}
deserialized, err := newMessageFromProto(*pbMessage,
testselector.MockDecodeRootNodeFunc,
testselector.MockDecodeSelectorFunc,
testselector.MockDecodeSelectionResponseFunc,
)
......@@ -109,12 +107,12 @@ func TestAppendingResponses(t *testing.T) {
func TestRequestCancel(t *testing.T) {
selector := testselector.GenerateSelector()
rootNode := testselector.GenerateRootNode()
root := testselector.GenerateRootCid()
id := GraphSyncRequestID(rand.Int31())
priority := GraphSyncPriority(rand.Int31())
gsm := New()
gsm.AddRequest(id, selector, rootNode, priority)
gsm.AddRequest(id, selector, root, priority)
gsm.Cancel(id)
......
package selector
import (
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)
......@@ -24,6 +25,6 @@ type SelectionTraverser interface {
// SelectorQuerier can be used to make and validate selector queries.
type SelectorQuerier interface {
Select(Selector, root ipld.Node) SelectionTraverser
Validate(Selector, root ipld.Node, incomingResponses SelectionTraverser) SelectionTraverser
Select(Selector, root cid.Cid) SelectionTraverser
Validate(Selector, root cid.Cid, incomingResponses SelectionTraverser) SelectionTraverser
}
......@@ -126,9 +126,9 @@ func GenerateSelectionResponse() gsselector.SelectionResponse {
return node
}
// GenerateRootNode generates a new mock ipld Node to serve as a root node
func GenerateRootNode() ipld.Node {
// GenerateRootCid generates a new mock CID to serve as a root
func GenerateRootCid() cid.Cid {
node := newNode(randomBytes(blockSize, seedSeq))
seedSeq++
return node
return node.Cid()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment