Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update of IPLD Prime #66

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/go-graphsync.puml
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ package "go-filecoin" {
"go-filecoin" .. goGraphSyncNetworkPF
"go-filecoin" *-- RawLoader
"go-filecoin" *-- MulticodecDecodeTable
"go-filecoin" *-- NodeBuilderChooser
"go-filecoin" *-- LinkTargetNodeStyleChooser

}

Expand Down
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/ipfs/go-graphsync
go 1.12

require (
github.com/filecoin-project/go-data-transfer v0.0.0-20200408061858-82c58b423ca6
github.com/filecoin-project/go-fil-markets v0.0.0-20200408062434-d92f329a6428
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.3.2 // indirect
github.com/gopherjs/gopherjs v0.0.0-20190812055157-5d271430af9f // indirect
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-blockservice v0.1.3
github.com/ipfs/go-cid v0.0.5
Expand All @@ -16,18 +16,26 @@ require (
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.8
github.com/ipfs/go-ipfs-util v0.0.1
github.com/ipfs/go-ipld-cbor v0.0.4 // indirect
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-log v1.0.2
github.com/ipfs/go-merkledag v0.3.1
github.com/ipfs/go-peertaskqueue v0.2.0
github.com/ipfs/go-unixfs v0.2.4
github.com/ipld/go-ipld-prime v0.0.2-0.20200229094926-eb71617f4aeb
github.com/ipld/go-ipld-prime-proto v0.0.0-20200409003434-8cf97d9cb362
github.com/ipld/go-ipld-prime v0.0.2-0.20200428162820-8b59dc292b8e
github.com/ipld/go-ipld-prime-proto v0.0.0-20200428191222-c1ffdadc01e1
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/libp2p/go-libp2p v0.6.0
github.com/libp2p/go-libp2p-core v0.5.0
github.com/libp2p/go-libp2p-peer v0.2.0
github.com/libp2p/go-libp2p-record v0.1.1 // indirect
github.com/multiformats/go-multiaddr v0.2.1
github.com/multiformats/go-multihash v0.0.13
github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a // indirect
github.com/smartystreets/assertions v1.0.1 // indirect
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
github.com/stretchr/testify v1.4.0
github.com/whyrusleeping/cbor-gen v0.0.0-20200402171437-3d27c146c105 // indirect
go.uber.org/multierr v1.4.0 // indirect
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361 // indirect
)
86 changes: 15 additions & 71 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions graphsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type BlockData interface {
type IncomingRequestHookActions interface {
SendExtensionData(ExtensionData)
UsePersistenceOption(name string)
UseNodeBuilderChooser(traversal.NodeBuilderChooser)
UseLinkTargetNodeStyleChooser(traversal.LinkTargetNodeStyleChooser)
TerminateWithError(error)
ValidateRequest()
}
Expand All @@ -172,7 +172,7 @@ type OutgoingBlockHookActions interface {
// to change the execution of a request
type OutgoingRequestHookActions interface {
UsePersistenceOption(name string)
UseNodeBuilderChooser(traversal.NodeBuilderChooser)
UseLinkTargetNodeStyleChooser(traversal.LinkTargetNodeStyleChooser)
}

// IncomingResponseHookActions are actions that incoming response hook can take
Expand Down
8 changes: 4 additions & 4 deletions impl/graphsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

ipldfree "github.com/ipld/go-ipld-prime/impl/free"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/stretchr/testify/require"

cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Expand Down Expand Up @@ -381,14 +381,14 @@ func TestGraphsyncRoundTripAlternatePersistenceAndNodes(t *testing.T) {
requestor.RegisterOutgoingRequestHook(func(p peer.ID, requestData graphsync.RequestData, hookActions graphsync.OutgoingRequestHookActions) {
_, has := requestData.Extension(extensionName)
if has {
hookActions.UseNodeBuilderChooser(blockChain.Chooser)
hookActions.UseLinkTargetNodeStyleChooser(blockChain.Chooser)
hookActions.UsePersistenceOption("chainstore")
}
})
responder.RegisterIncomingRequestHook(func(p peer.ID, requestData graphsync.RequestData, hookActions graphsync.IncomingRequestHookActions) {
_, has := requestData.Extension(extensionName)
if has {
hookActions.UseNodeBuilderChooser(blockChain.Chooser)
hookActions.UseLinkTargetNodeStyleChooser(blockChain.Chooser)
hookActions.UsePersistenceOption("chainstore")
}
})
Expand Down Expand Up @@ -557,7 +557,7 @@ func TestUnixFSFetch(t *testing.T) {
clink := cidlink.Link{Cid: nd.Cid()}

// create a selector for the whole UnixFS dag
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)

allSelector := ssb.ExploreRecursive(ipldselector.RecursionLimitNone(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()
Expand Down
29 changes: 17 additions & 12 deletions ipldutil/ipldutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

ipld "github.com/ipld/go-ipld-prime"
dagpb "github.com/ipld/go-ipld-prime-proto"
"github.com/ipld/go-ipld-prime/encoding/dagcbor"
free "github.com/ipld/go-ipld-prime/impl/free"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/traversal"
ipldtraversal "github.com/ipld/go-ipld-prime/traversal"
"github.com/ipld/go-ipld-prime/traversal/selector"
Expand All @@ -23,28 +23,30 @@ func ErrDoNotFollow() error {
}

var (
defaultChooser traversal.NodeBuilderChooser = dagpb.AddDagPBSupportToChooser(func(ipld.Link, ipld.LinkContext) (ipld.NodeBuilder, error) {
return free.NodeBuilder(), nil
defaultChooser traversal.LinkTargetNodeStyleChooser = dagpb.AddDagPBSupportToChooser(func(ipld.Link, ipld.LinkContext) (ipld.NodeStyle, error) {
return basicnode.Style.Any, nil
})
)

func Traverse(ctx context.Context, loader ipld.Loader, chooser traversal.NodeBuilderChooser, root ipld.Link, s selector.Selector, fn traversal.AdvVisitFn) error {
func Traverse(ctx context.Context, loader ipld.Loader, chooser traversal.LinkTargetNodeStyleChooser, root ipld.Link, s selector.Selector, fn traversal.AdvVisitFn) error {
if chooser == nil {
chooser = defaultChooser
}
builder, err := chooser(root, ipld.LinkContext{})
ns, err := chooser(root, ipld.LinkContext{})
if err != nil {
return err
}
node, err := root.Load(ctx, ipld.LinkContext{}, builder, loader)
nb := ns.NewBuilder()
err = root.Load(ctx, ipld.LinkContext{}, nb, loader)
if err != nil {
return err
}
node := nb.Build()
return traversal.Progress{
Cfg: &traversal.Config{
Ctx: ctx,
LinkLoader: loader,
LinkNodeBuilderChooser: chooser,
Ctx: ctx,
LinkLoader: loader,
LinkTargetNodeStyleChooser: chooser,
},
}.WalkAdv(node, s, fn)
}
Expand All @@ -63,8 +65,11 @@ func EncodeNode(node ipld.Node) ([]byte, error) {
}

func DecodeNode(encoded []byte) (ipld.Node, error) {
reader := bytes.NewReader(encoded)
return dagcbor.Decoder(free.NodeBuilder(), reader)
nb := basicnode.Style.Any.NewBuilder()
if err := dagcbor.Decoder(nb, bytes.NewReader(encoded)); err != nil {
return nil, err
}
return nb.Build(), nil
}

func ParseSelector(selector ipld.Node) (selector.Selector, error) {
Expand Down
16 changes: 9 additions & 7 deletions ipldutil/traverser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type TraversalBuilder struct {
Root ipld.Link
Selector ipld.Node
Visitor traversal.AdvVisitFn
Chooser traversal.NodeBuilderChooser
Chooser traversal.LinkTargetNodeStyleChooser
}

// Traverser is an interface for performing a selector traversal that operates iteratively --
Expand Down Expand Up @@ -75,7 +75,7 @@ type traverser struct {
root ipld.Link
selector ipld.Node
visitor traversal.AdvVisitFn
chooser traversal.NodeBuilderChooser
chooser traversal.LinkTargetNodeStyleChooser
currentLink ipld.Link
currentContext ipld.LinkContext
isDone bool
Expand Down Expand Up @@ -129,16 +129,18 @@ func (t *traverser) start() {
return response.input, response.err
}
}
nb, err := t.chooser(t.root, ipld.LinkContext{})
ns, err := t.chooser(t.root, ipld.LinkContext{})
if err != nil {
t.writeDone(err)
return
}
nd, err := t.root.Load(t.ctx, ipld.LinkContext{}, nb, loader)
nb := ns.NewBuilder()
err = t.root.Load(t.ctx, ipld.LinkContext{}, nb, loader)
if err != nil {
t.writeDone(err)
return
}
nd := nb.Build()

sel, err := selector.ParseSelector(t.selector)
if err != nil {
Expand All @@ -147,9 +149,9 @@ func (t *traverser) start() {
}
err = traversal.Progress{
Cfg: &traversal.Config{
Ctx: t.ctx,
LinkLoader: loader,
LinkNodeBuilderChooser: t.chooser,
Ctx: t.ctx,
LinkLoader: loader,
LinkTargetNodeStyleChooser: t.chooser,
},
}.WalkAdv(nd, sel, t.visitor)
t.writeDone(err)
Expand Down
4 changes: 2 additions & 2 deletions ipldutil/traverser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/testutil"
ipld "github.com/ipld/go-ipld-prime"
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/traversal"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
Expand All @@ -22,7 +22,7 @@ func TestTraverser(t *testing.T) {

t.Run("traverses correctly, simple struct", func(t *testing.T) {
testdata := testutil.NewTestIPLDTree()
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
sel := ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()
traverser := TraversalBuilder{
Root: testdata.RootNodeLnk,
Expand Down
8 changes: 4 additions & 4 deletions message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-graphsync"
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
"github.com/stretchr/testify/require"

Expand All @@ -23,7 +23,7 @@ func TestAppendingRequests(t *testing.T) {
Data: testutil.RandomBytes(100),
}
root := testutil.GenerateCids(1)[0]
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
selector := ssb.Matcher().Node()
id := graphsync.RequestID(rand.Int31())
priority := graphsync.Priority(rand.Int31())
Expand Down Expand Up @@ -144,7 +144,7 @@ func contains(strs []string, x string) bool {
}

func TestRequestCancel(t *testing.T) {
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
selector := ssb.Matcher().Node()
id := graphsync.RequestID(rand.Int31())
priority := graphsync.Priority(rand.Int31())
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestRequestUpdate(t *testing.T) {

func TestToNetFromNetEquivalency(t *testing.T) {
root := testutil.GenerateCids(1)[0]
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
selector := ssb.Matcher().Node()
extensionName := graphsync.ExtensionName("graphsync/awesome")
extension := graphsync.ExtensionData{
Expand Down
8 changes: 4 additions & 4 deletions messagequeue/messagequeue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

gsmsg "github.com/ipfs/go-graphsync/message"
gsnet "github.com/ipfs/go-graphsync/network"
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/libp2p/go-libp2p-core/peer"
)

Expand Down Expand Up @@ -69,7 +69,7 @@ func TestStartupAndShutdown(t *testing.T) {
messageQueue.Startup()
id := graphsync.RequestID(rand.Int31())
priority := graphsync.Priority(rand.Int31())
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
selector := ssb.Matcher().Node()
root := testutil.GenerateCids(1)[0]

Expand Down Expand Up @@ -104,7 +104,7 @@ func TestShutdownDuringMessageSend(t *testing.T) {
messageQueue.Startup()
id := graphsync.RequestID(rand.Int31())
priority := graphsync.Priority(rand.Int31())
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
selector := ssb.Matcher().Node()
root := testutil.GenerateCids(1)[0]

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestDedupingMessages(t *testing.T) {
waitGroup.Add(1)
id := graphsync.RequestID(rand.Int31())
priority := graphsync.Priority(rand.Int31())
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
selector := ssb.Matcher().Node()
root := testutil.GenerateCids(1)[0]

Expand Down
63 changes: 34 additions & 29 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/ipfs/go-graphsync/ipldutil"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)

// Item is a single link traversed in a repsonse
Expand All @@ -24,42 +24,47 @@ func DecodeMetadata(data []byte) (Metadata, error) {
if err != nil {
return nil, err
}
var decodedData interface{}
err = fluent.Recover(func() {
simpleNode := fluent.WrapNode(node)
iterator := simpleNode.ListIterator()
var metadata Metadata
if simpleNode.Length() != -1 {
metadata = make(Metadata, 0, simpleNode.Length())
}
iterator := node.ListIterator()
var metadata Metadata
if node.Length() != -1 {
metadata = make(Metadata, 0, node.Length())
}

for !iterator.Done() {
_, item := iterator.Next()
link := item.LookupString("link").AsLink()
blockPresent := item.LookupString("blockPresent").AsBool()
metadata = append(metadata, Item{link, blockPresent})
for !iterator.Done() {
_, item, err := iterator.Next()
if err != nil {
return nil, err
}
decodedData = metadata
})
if err != nil {
return nil, err
linkNode, err := item.LookupString("link")
if err != nil {
return nil, err
}
link, err := linkNode.AsLink()
if err != nil {
return nil, err
}
blockPresentNode, err := item.LookupString("blockPresent")
if err != nil {
return nil, err
}
blockPresent, err := blockPresentNode.AsBool()
if err != nil {
return nil, err
}
metadata = append(metadata, Item{link, blockPresent})
}
return decodedData.(Metadata), err
return metadata, err
}

// EncodeMetadata encodes metadata to an IPLD node then serializes to raw bytes
func EncodeMetadata(entries Metadata) ([]byte, error) {
var node ipld.Node
err := fluent.Recover(func() {
nb := fluent.WrapNodeBuilder(ipldfree.NodeBuilder())
node = nb.CreateList(func(lb fluent.ListBuilder, nb fluent.NodeBuilder) {
node, err := fluent.Build(basicnode.Style.List, func(na fluent.NodeAssembler) {
na.CreateList(len(entries), func(na fluent.ListAssembler) {
for _, item := range entries {
lb.Append(
nb.CreateMap(func(mb fluent.MapBuilder, knb fluent.NodeBuilder, vnb fluent.NodeBuilder) {
mb.Insert(knb.CreateString("link"), vnb.CreateLink(item.Link))
mb.Insert(knb.CreateString("blockPresent"), vnb.CreateBool(item.BlockPresent))
}),
)
na.AssembleValue().CreateMap(2, func(na fluent.MapAssembler) {
na.AssembleEntry("link").AssignLink(item.Link)
na.AssembleEntry("blockPresent").AssignBool(item.BlockPresent)
})
}
})
})
Expand Down
Loading