Categorygithub.com/ipfs/go-graphsync
modulepackage
0.17.0
Repository: https://github.com/ipfs/go-graphsync.git
Documentation: pkg.go.dev

# README

go-graphsync

Coverage Status Build Status

An implementation of the graphsync protocol in go!

Table of Contents

Background

GraphSync 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 on the remote peer's local IPLD graph.

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

Go-IPLD-Prime

go-graphsync relies on go-ipld-prime to traverse IPLD Selectors in an IPLD graph. go-ipld-prime implements the IPLD specification 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.

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

Install

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

Usage

Initializing a GraphSync Exchange

import (
  graphsync "github.com/ipfs/go-graphsync/impl"
  gsnet "github.com/ipfs/go-graphsync/network"
  ipld "github.com/ipld/go-ipld-prime"
)

var ctx context.Context
var host libp2p.Host
var lsys ipld.LinkSystem

network := gsnet.NewFromLibp2pHost(host)
exchange := graphsync.New(ctx, network, lsys)

Parameter Notes:

  1. context is just the parent context for all of GraphSync
  2. network is a network abstraction provided to Graphsync on top of libp2p. This allows graphsync to be tested without the actual network
  3. lsys is an go-ipld-prime LinkSystem, which provides mechanisms loading and constructing go-ipld-prime nodes from a link, and saving ipld prime nodes to serialized data

Using GraphSync With An IPFS BlockStore

GraphSync provides a convenience function in the storeutil package for integrating with BlockStore's from IPFS.

import (
  graphsync "github.com/ipfs/go-graphsync/impl"
  gsnet "github.com/ipfs/go-graphsync/network"
  storeutil "github.com/ipfs/go-graphsync/storeutil"
  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)
lsys := storeutil.LinkSystemForBlockstore(bs)

exchange := graphsync.New(ctx, network, lsys)

Calling Graphsync

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)

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

Response Type


type ResponseProgress struct {
  Node      ipld.Node // a node which matched the graphsync query
  Path      ipld.Path // the path of that node relative to the traversal start
	LastBlock struct {  // LastBlock stores the Path and Link of the last block edge we had to load. 
		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!

Before doing anything heavy, checkout the Graphsync Architecture

See our Contributing Guidelines for more info.

License

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

Copyright 2019. Protocol Labs, Inc.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Create a new, random RequestID (should be a UUIDv4).
Create a RequestID from a byte slice.

# Constants

AdditionalPeers means additional peers were found that may be able to satisfy the request and contained in the extra block of the response.
CompletingSend means we have processed a query and are waiting for data to go over the network.
ExtensionDeDupByKey tells the responding peer to only deduplicate block sending for requests that have the same key.
ExtensionDoNotSendCIDs tells the responding peer not to send certain blocks if they are encountered in a traversal and is documented at https://github.com/ipld/specs/blob/master/block-layer/graphsync/known_extensions.md.
ExtensionsDoNotSendFirstBlocks tells the responding peer not to wait till the given number of blocks have been traversed before it begins to send blocks over the wire.
LinkActionDuplicateDAGSkipped means the DAG with this link points toward has already been traversed entirely in the course of this request so I am skipping over it.
LinkActionDuplicateNotSent means the linked block was present on this machine, but I am not sending it (most likely duplicate).
LinkActionMissing means I did not have the linked block, so I skipped over this part of the traversal.
LinkActionPresent means the linked block was present on this machine, and is included a this message.
NotEnoughGas means fulfilling this request requires payment.
OtherProtocol means a different type of response than GraphSync is contained in extra.
PartialResponse may include blocks and metadata about the in progress response in extra.
Paused means a request is paused.
Queued means a request has been received and is queued for processing.
RequestAcknowledged means the request was received and is being worked on.
RequestCancelled means the responder was processing the request but decided to top, for whatever reason.
RequestCompletedFull means the entire fulfillment of the GraphSync request was sent back.
RequestCompletedPartial means the response is completed, and part of the GraphSync request was sent back, but not the complete request.
RequestFailedBusy means the node is too busy, try again later.
RequestFailedContentNotFound means the respondent does not have the content.
RequestFailedLegal means the request failed for legal reasons.
RequestFailedUnknown means the request failed for an unspecified reason.
RequestPaused indicates a request is paused and will not send any more data until unpaused.
RequestRejected means the node did not accept the incoming request.
RequestTypeCancel means cancel the request referenced by request ID.
RequestTypeNew means a new request.
RequestTypeUpdate means the extensions contain an update about this request.
Running means a request is actively sending or receiving data.

# Variables

ErrExtensionAlreadyRegistered means a user extension can be registered only once.
No description provided by the author

# Structs

ExtensionData is a name/data pair for a graphsync extension.
RemoteIncorrectResponseError indicates that the remote peer sent a response to a traversal that did not correspond with the expected next link in the selector traversal based on verification of data up to this point.
RemoteMissingBlockErr indicates that the remote peer was missing a block in the selector requested, and we also don't have it locally.
RequestCancelledErr is an error message received on the error channel that indicates the responder cancelled a request.
RequestClientCancelledErr is an error message received on the error channel when the request is cancelled on by the client code, either by closing the passed request context or calling CancelRequest.
RequestFailedBusyErr is an error message received on the error channel when the peer is busy.
RequestFailedContentNotFoundErr is an error message received on the error channel when the content is not found.
RequestFailedLegalErr is an error message received on the error channel when the request fails for legal reasons.
RequestFailedUnknownErr is an error message received on the error channel when the request fails for unknown reasons.
RequestID is a unique identifier for a GraphSync request.
RequestIDContextKey is used to the desired request id in context when initializing a request.
RequestNotFoundErr indicates that a request with a particular request ID was not found.
RequestStats offer statistics about request processing.
ResponseProgress is the fundamental unit of responses making progress in Graphsync.
ResponseStats offer statistics about memory allocations for responses.
Stats describes statistics about the Graphsync implementations current state.

# Interfaces

BlockData gives information about a block included in a graphsync response.
GraphExchange is a protocol that can exchange IPLD graphs based on a selector.
IncomingBlockHookActions are actions that incoming block hook can take to change the execution of a request.
IncomingRequestHookActions are actions that a request hook can take to change behavior for the response.
IncomingResponseHookActions are actions that incoming response hook can take to change the execution of a request.
LinkMetadata is used to access link metadata through an Iterator.
OutgoingBlockHookActions are actions that an outgoing block hook can take to change the execution of a request.
OutgoingRequestHookActions are actions that an outgoing request hook can take to change the execution of a request.
RequestData describes a received graphsync request.
RequestUpdatedHookActions are actions that can be taken in a request updated hook to change execution of the response.
ResponseData describes a received Graphsync response.

# Type aliases

ExtensionName is a name for a GraphSync extension.
LinkAction is a code that is used by message metadata to communicate the state and reason for blocks being included or not in a transfer.
LinkMetadataIterator is used to access individual link metadata through a LinkMetadata object.
OnBlockSentListener runs when a block is sent over the wire.
OnIncomingBlockHook is a hook that runs each time a new block is validated as part of the response, regardless of whether it came locally or over the network It receives that sent the response, the most recent response, a link for the block received, and the size of the block received The difference between BlockSize & BlockSizeOnWire can be used to determine where the block came from (Local vs remote) It receives an interface for customizing how we handle the ongoing execution of the request.
OnIncomingRequestHook is a hook that runs each time a new request is received.
OnIncomingResponseHook is a hook that runs each time a new response is received.
OnNetworkErrorListener runs when queued data is not able to be sent.
OnOutgoingBlockHook is a hook that runs immediately after a requestor sends a new block on a response It receives the peer we're sending a request to, all the data aobut the request, a link for the block sent, and the size of the block sent It receives an interface for taking further action on the response.
OnOutgoingRequestHook is a hook that runs immediately prior to sending a request It receives the peer we're sending a request to and all the data aobut the request It receives an interface for customizing how we handle executing this request.
OnReceiverNetworkErrorListener runs when errors occur receiving data over the wire.
OnRequestorCancelledListener provides a way to listen for responses the requestor canncels.
OnRequestProcessingListener is called when a request actually begins processing (reaches the top of the request queue).
OnRequestUpdatedHook is a hook that runs when an update to a request is received It receives the peer we're sending to, the original request, the request update It receives an interface to taking further action on the response.
OnResponseCompletedListener provides a way to listen for when responder has finished serving a response.
Priority a priority for a GraphSync request.
RequestState describes the current general state of a request.
RequestStates describe a set of request IDs and their current state.
No description provided by the author
ResponseStatusCode is a status returned for a GraphSync Request.
UnregisterHookFunc is a function call to unregister a hook that was previously registered.