graphsync-get.go 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9
package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"os"

tavit ohanian's avatar
tavit ohanian committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	"gitlab.dms3.io/dms3/go-blockservice"
	"gitlab.dms3.io/dms3/go-cid"
	"gitlab.dms3.io/dms3/go-datastore"
	dssync "gitlab.dms3.io/dms3/go-datastore/sync"
	blockstore "gitlab.dms3.io/dms3/go-dms3-blockstore"
	offline "gitlab.dms3.io/dms3/go-dms3-exchange-offline"
	"gitlab.dms3.io/dms3/go-graphsync"
	gsimpl "gitlab.dms3.io/dms3/go-graphsync/impl"
	"gitlab.dms3.io/dms3/go-graphsync/network"
	"gitlab.dms3.io/dms3/go-graphsync/storeutil"
	"gitlab.dms3.io/dms3/go-merkledag"
	uio "gitlab.dms3.io/dms3/go-unixfs/io"
	"gitlab.dms3.io/dms3/ld/go-ld-prime"
	cidlink "gitlab.dms3.io/dms3/ld/go-ld-prime/linking/cid"
	basicnode "gitlab.dms3.io/dms3/ld/go-ld-prime/node/basic"
	ldselector "gitlab.dms3.io/dms3/ld/go-ld-prime/traversal/selector"
	"gitlab.dms3.io/dms3/ld/go-ld-prime/traversal/selector/builder"
	"gitlab.dms3.io/mf/go-multiaddr"
	"gitlab.dms3.io/p2p/go-p2p"
	"gitlab.dms3.io/p2p/go-p2p-core/host"
	"gitlab.dms3.io/p2p/go-p2p-core/peer"
31 32 33
)

func newGraphsync(ctx context.Context, p2p host.Host, bs blockstore.Blockstore) (graphsync.GraphExchange, error) {
tavit ohanian's avatar
tavit ohanian committed
34
	network := network.NewFromP2pHost(p2p)
35
	return gsimpl.New(ctx,
36
		network,
37 38 39 40 41
		storeutil.LoaderForBlockstore(bs),
		storeutil.StorerForBlockstore(bs),
	), nil
}

tavit ohanian's avatar
tavit ohanian committed
42
var selectAll ld.Node = func() ld.Node {
43
	ssb := builder.NewSelectorSpecBuilder(basicnode.Style.Any)
44
	return ssb.ExploreRecursive(
tavit ohanian's avatar
tavit ohanian committed
45
		ldselector.RecursionLimitDepth(100), // default max
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
		ssb.ExploreAll(ssb.ExploreRecursiveEdge()),
	).Node()
}()

func fetch(ctx context.Context, gs graphsync.GraphExchange, p peer.ID, c cid.Cid) error {
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	resps, errs := gs.Request(ctx, p, cidlink.Link{Cid: c}, selectAll)
	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case _, ok := <-resps:
			if !ok {
				resps = nil
			}
		case err, ok := <-errs:
			if !ok {
				// done.
				return nil
			}
			if err != nil {
				return fmt.Errorf("got an unexpected error: %s", err)
			}
		}
	}
}

func main() {
	if len(os.Args) != 3 {
		log.Fatalf("expected a multiaddr and a CID, got %d args", len(os.Args)-1)
	}
	addr, err := multiaddr.NewMultiaddr(os.Args[1])
	if err != nil {
		log.Fatalf("failed to multiaddr '%q': %s", os.Args[1], err)
	}
	ai, err := peer.AddrInfoFromP2pAddr(addr)
	if err != nil {
		log.Fatal(err)
	}

	target, err := cid.Decode(os.Args[2])
	if err != nil {
		log.Fatalf("failed to decode CID '%q': %s", os.Args[2], err)
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

tavit ohanian's avatar
tavit ohanian committed
96
	p2p, err := p2p.New(ctx, p2p.NoListenAddrs)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	if err != nil {
		log.Fatal(err)
	}
	err = p2p.Connect(ctx, *ai)
	if err != nil {
		log.Fatal(err)
	}

	bs := blockstore.NewBlockstore(dssync.MutexWrap(datastore.NewMapDatastore()))
	gs, err := newGraphsync(ctx, p2p, bs)
	if err != nil {
		log.Fatal("failed to start", err)
	}
	err = fetch(ctx, gs, ai.ID, target)
	if err != nil {
		log.Fatal(err)
	}

	dag := merkledag.NewDAGService(blockservice.New(bs, offline.Exchange(bs)))
	root, err := dag.Get(ctx, target)
	if err != nil {
		log.Fatal(err)
	}
	reader, err := uio.NewDagReader(ctx, root, dag)
	if err != nil {
		log.Fatal(err)
	}
	_, err = io.Copy(os.Stdout, reader)
	if err != nil {
		log.Fatal(err)
	}
}