README.md 8.35 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1 2
# go-graphsync

3 4
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
Hannah Howard's avatar
Hannah Howard committed
5 6 7 8 9
[![Matrix](https://img.shields.io/badge/matrix-%23ipfs%3Amatrix.org-blue.svg?style=flat-square)](https://matrix.to/#/#ipfs:matrix.org)
[![IRC](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Discord](https://img.shields.io/discord/475789330380488707?color=blueviolet&label=discord&style=flat-square)](https://discord.gg/24fmuwR)
[![Coverage Status](https://codecov.io/gh/ipfs/go-graphsync/branch/master/graph/badge.svg)](https://codecov.io/gh/ipfs/go-graphsync/branch/master)
[![Build Status](https://circleci.com/gh/ipfs/go-bitswap.svg?style=svg)](https://circleci.com/gh/ipfs/go-graphsync)
10

Dominic Della Valle's avatar
Dominic Della Valle committed
11
> An implementation of the [graphsync protocol](https://github.com/ipld/specs/blob/master/block-layer/graphsync/graphsync.md) in go!
12 13 14 15 16 17

## Table of Contents

- [Background](#background)
- [Install](#install)
- [Usage](#usage)
18
- [Architecture](#architecture)
19 20 21 22 23
- [Contribute](#contribute)
- [License](#license)

## Background

24
[GraphSync](https://github.com/ipld/specs/blob/master/block-layer/graphsync/graphsync.md) is a protocol for synchronizing IPLD graphs among peers. It allows a host to make a single request to a remote peer for all of the results of traversing an [IPLD selector](https://github.com/ipld/specs/blob/master/block-layer/selectors/selectors.md) on the remote peer's local IPLD graph.
25 26 27 28 29

`go-graphsync` provides an implementation of the Graphsync protocol in go.

### Go-IPLD-Prime

30
`go-graphsync` relies on `go-ipld-prime` to traverse IPLD Selectors in an IPLD graph. `go-ipld-prime` implements the [IPLD specification](https://github.com/ipld/specs) in go and is an alternative to older implementations such as `go-ipld-format` and `go-ipld-cbor`. In order to use `go-graphsync`, some understanding and use of `go-ipld-prime` concepts is necessary.
31

32
If your existing library (i.e. `go-ipfs` or `go-filecoin`) uses these other older libraries, you can largely use go-graphsync without switching to `go-ipld-prime` across your codebase, but it will require some translations
33 34 35 36 37 38 39 40 41 42 43

## Install

`go-graphsync` requires Go >= 1.11 and can be installed using Go modules

## Usage

### Initializing a GraphSync Exchange

```golang
import (
44
  graphsync "github.com/ipfs/go-graphsync/impl"
45
  gsnet "github.com/ipfs/go-graphsync/network"
46
  ipld "github.com/ipld/go-ipld-prime"
47 48 49
)

var ctx context.Context
hannahhoward's avatar
hannahhoward committed
50
var host libp2p.Host
51
var loader ipld.Loader
52
var storer ipld.Storer
53

54
network := gsnet.NewFromLibp2pHost(host)
55
exchange := graphsync.New(ctx, network, loader, storer)
56 57 58 59 60
```

Parameter Notes:

1. `context` is just the parent context for all of GraphSync
61 62
2. `network` is a network abstraction provided to Graphsync on top
of libp2p. This allows graphsync to be tested without the actual network
63 64
3. `loader` is used to load blocks from content ids from the local block store. It's used when RESPONDING to requests from other clients. It should conform to the IPLD loader interface: https://github.com/ipld/go-ipld-prime/blob/master/linking.go
4. `storer` is used to store incoming blocks to the local block store. It's used when REQUESTING a graphsync query, to store blocks locally once they are validated as part of the correct response. It should conform to the IPLD storer interface: https://github.com/ipld/go-ipld-prime/blob/master/linking.go
65

66 67 68 69 70 71 72 73 74
### Using GraphSync With An IPFS BlockStore

GraphSync provides two convenience functions in the `storeutil` package for
integrating with BlockStore's from IPFS.

```golang
import (
  graphsync "github.com/ipfs/go-graphsync/impl"
  gsnet "github.com/ipfs/go-graphsync/network"
75
  storeutil "github.com/ipfs/go-graphsync/storeutil"
76 77 78 79 80 81 82 83 84 85 86 87
  ipld "github.com/ipld/go-ipld-prime"
  blockstore "github.com/ipfs/go-ipfs-blockstore"
)

var ctx context.Context
var host libp2p.Host
var bs blockstore.Blockstore

network := gsnet.NewFromLibp2pHost(host)
loader := storeutil.LoaderForBlockstore(bs)
storer := storeutil.StorerForBlockstore(bs)

88
exchange := graphsync.New(ctx, network, loader, storer)
89 90
```

91
### Write A Loader An IPFS BlockStore
92

93 94 95 96 97 98 99 100 101
If you are using a traditional go-ipfs-blockstore, your link loading function looks like this:

```golang
type BlockStore interface {
	Get(lnk cid.Cid) (blocks.Block, error)
}
```

or, more generally:
102

hannahhoward's avatar
hannahhoward committed
103
```golang
104 105 106 107 108
type Cid2BlockFn func (lnk cid.Cid) (blocks.Block, error)
```

in `go-ipld-prime`, the signature for a link loader is as follows:

hannahhoward's avatar
hannahhoward committed
109
```golang
110 111 112
type Loader func(lnk Link, lnkCtx LinkContext) (io.Reader, error)
```

113
`go-ipld-prime` intentionally keeps its interfaces as abstract as possible to limit dependencies on other ipfs/filecoin specific packages. An IPLD Link is an abstraction for a CID, and IPLD expects io.Reader's rather than an actual block. IPLD provides a `cidLink` package for working with Links that use CIDs as the underlying data, and it's safe to assume that's the type in use if your code deals only with CIDs. A conversion would look something like this:
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

```golang
import (
   ipld "github.com/ipld/go-ipld-prime"
   cidLink "github.com/ipld/go-ipld-prime/linking/cid"
)

func LoaderFromCid2BlockFn(cid2BlockFn Cid2BlockFn) ipld.Loader {
	return func(lnk ipld.Link, lnkCtx ipld.LinkContext) (io.Reader, error) {
		asCidLink, ok := lnk.(cidlink.Link)
		if !ok {
			return nil, fmt.Errorf("Unsupported Link Type")
		}
		block, err := cid2BlockFn(asCidLink.Cid)
		if err != nil {
			return nil, err
		}
		return bytes.NewReader(block.RawData()), nil
	}
}
```

136 137 138
### Write A Storer From An IPFS BlockStore

If you are using a traditional go-ipfs-blockstore, your storage function looks like this:
139 140

```golang
141 142 143
type BlockStore interface {
	Put(blocks.Block) error
}
144 145
```

146
or, more generally:
147 148

```golang
149
type BlockStoreFn func (blocks.Block) (error)
150 151
```

152
in `go-ipld-prime`, the signature for a link storer is a bit different:
153

154 155 156 157
```golang
type StoreCommitter func(Link) error
type Storer func(lnkCtx LinkContext) (io.Writer, StoreCommitter, error)
```
158

159
`go-ipld-prime` stores in two parts to support streaming -- the storer is called and returns an IO.Writer and a function to commit changes when finished. Here's how you can write a storer from a traditional block storing signature.
160 161 162

```golang
import (
163 164 165
	blocks "github.com/ipfs/go-block-format"
  ipld "github.com/ipld/go-ipld-prime"
  cidLink "github.com/ipld/go-ipld-prime/linking/cid"
166 167
)

168 169 170 171 172 173 174 175 176 177 178 179
func StorerFromBlockStoreFn(blockStoreFn BlockStoreFn) ipld.Storer {
	return func(lnkCtx ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
		var buffer bytes.Buffer
		committer := func(lnk ipld.Link) error {
			asCidLink, ok := lnk.(cidlink.Link)
			if !ok {
				return fmt.Errorf("Unsupported Link Type")
			}
			block := blocks.NewBlockWithCid(buffer.Bytes(), asCidLink.Cid)
			return blockStoreFn(block)
		}
		return &buffer, committer, nil
180 181 182 183
	}
}
```

184
### Calling Graphsync
185 186

```golang
187 188 189 190 191 192 193 194 195 196
var exchange graphsync.GraphSync
var ctx context.Context
var p peer.ID
var selector ipld.Node
var rootLink ipld.Link

var responseProgress <-chan graphsync.ResponseProgress
var errors <-chan error

responseProgress, errors = exchange.Request(ctx context.Context, p peer.ID, root ipld.Link, selector ipld.Node)
197 198
```

199 200 201 202 203 204
Paramater Notes:
1. `ctx` is the context for this request. To cancel an in progress request, cancel the context.
2. `p` is the peer you will send this request to
3. `link` is an IPLD Link, i.e. a CID (cidLink.Link{Cid})
4. `selector` is an IPLD selector node. Recommend using selector builders from go-ipld-prime to construct these

205 206 207 208 209
### Response Type

```golang

type ResponseProgress struct {
hannahhoward's avatar
hannahhoward committed
210
  Node      ipld.Node // a node which matched the graphsync query
211
  Path      ipld.Path // the path of that node relative to the traversal start
212
	LastBlock struct {  // LastBlock stores the Path and Link of the last block edge we had to load.
213 214 215 216 217 218 219 220 221 222 223 224 225
		ipld.Path
		ipld.Link
	}
}

```

The above provides both immediate and relevant metadata for matching nodes in a traversal, and is very similar to the information provided by a local IPLD selector traversal in `go-ipld-prime`

## Contribute

PRs are welcome!

226 227
Before doing anything heavy, checkout the [Graphsync Architecture](docs/architecture.md)

Hannah Howard's avatar
Hannah Howard committed
228
See our [Contributing Guidelines](https://github.com/ipfs/go-graphsync/blob/master/CONTRIBUTING.md) for more info.
229 230 231

## License

232 233 234
This library is dual-licensed under Apache 2.0 and MIT terms.

Copyright 2019. Protocol Labs, Inc.