graphsync.go 14.2 KB
Newer Older
1 2 3 4 5
package graphsync

import (
	"context"

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
	logging "gitlab.dms3.io/dms3/go-log"
	"gitlab.dms3.io/dms3/go-peertaskqueue"
	ld "gitlab.dms3.io/ld/go-ld-prime"
	"gitlab.dms3.io/p2p/go-p2p-core/peer"

	"gitlab.dms3.io/dms3/go-graphsync"
	"gitlab.dms3.io/dms3/go-graphsync/allocator"
	"gitlab.dms3.io/dms3/go-graphsync/listeners"
	gsmsg "gitlab.dms3.io/dms3/go-graphsync/message"
	"gitlab.dms3.io/dms3/go-graphsync/messagequeue"
	gsnet "gitlab.dms3.io/dms3/go-graphsync/network"
	"gitlab.dms3.io/dms3/go-graphsync/peermanager"
	"gitlab.dms3.io/dms3/go-graphsync/requestmanager"
	"gitlab.dms3.io/dms3/go-graphsync/requestmanager/asyncloader"
	requestorhooks "gitlab.dms3.io/dms3/go-graphsync/requestmanager/hooks"
	"gitlab.dms3.io/dms3/go-graphsync/responsemanager"
	responderhooks "gitlab.dms3.io/dms3/go-graphsync/responsemanager/hooks"
	"gitlab.dms3.io/dms3/go-graphsync/responsemanager/persistenceoptions"
	"gitlab.dms3.io/dms3/go-graphsync/responsemanager/responseassembler"
	"gitlab.dms3.io/dms3/go-graphsync/selectorvalidator"
26 27 28 29
)

var log = logging.Logger("graphsync")

30
const maxRecursionDepth = 100
31 32
const defaultTotalMaxMemory = uint64(256 << 20)
const defaultMaxMemoryPerPeer = uint64(16 << 20)
33
const defaultMaxInProgressRequests = uint64(6)
34

35 36 37
// GraphSync is an instance of a GraphSync exchange that implements
// the graphsync protocol.
type GraphSync struct {
38
	network                     gsnet.GraphSyncNetwork
39
	linkSystem                  ld.LinkSystem
40 41 42
	requestManager              *requestmanager.RequestManager
	responseManager             *responsemanager.ResponseManager
	asyncLoader                 *asyncloader.AsyncLoader
43
	responseAssembler           *responseassembler.ResponseAssembler
44 45 46 47 48
	peerTaskQueue               *peertaskqueue.PeerTaskQueue
	peerManager                 *peermanager.PeerMessageManager
	incomingRequestHooks        *responderhooks.IncomingRequestHooks
	outgoingBlockHooks          *responderhooks.OutgoingBlockHooks
	requestUpdatedHooks         *responderhooks.RequestUpdatedHooks
49 50 51 52
	completedResponseListeners  *listeners.CompletedResponseListeners
	requestorCancelledListeners *listeners.RequestorCancelledListeners
	blockSentListeners          *listeners.BlockSentListeners
	networkErrorListeners       *listeners.NetworkErrorListeners
53
	receiverErrorListeners      *listeners.NetworkReceiverErrorListeners
54 55 56 57 58 59
	incomingResponseHooks       *requestorhooks.IncomingResponseHooks
	outgoingRequestHooks        *requestorhooks.OutgoingRequestHooks
	incomingBlockHooks          *requestorhooks.IncomingBlockHooks
	persistenceOptions          *persistenceoptions.PersistenceOptions
	ctx                         context.Context
	cancel                      context.CancelFunc
60
	allocator                   *allocator.Allocator
61 62 63 64 65 66 67
}

type graphsyncConfigOptions struct {
	totalMaxMemory           uint64
	maxMemoryPerPeer         uint64
	maxInProgressRequests    uint64
	registerDefaultValidator bool
68 69 70 71
}

// Option defines the functional option type that can be used to configure
// graphsync instances
72
type Option func(*graphsyncConfigOptions)
73 74 75 76

// RejectAllRequestsByDefault means that without hooks registered
// that perform their own request validation, all requests are rejected
func RejectAllRequestsByDefault() Option {
77 78
	return func(gs *graphsyncConfigOptions) {
		gs.registerDefaultValidator = false
79
	}
80 81
}

82 83
// MaxMemoryResponder defines the maximum amount of memory the responder
// may consume queueing up messages for a response in total
84
func MaxMemoryResponder(totalMaxMemory uint64) Option {
85
	return func(gs *graphsyncConfigOptions) {
86 87 88 89
		gs.totalMaxMemory = totalMaxMemory
	}
}

90 91
// MaxMemoryPerPeerResponder defines the maximum amount of memory a peer
// may consume queueing up messages for a response
92
func MaxMemoryPerPeerResponder(maxMemoryPerPeer uint64) Option {
93
	return func(gs *graphsyncConfigOptions) {
94 95 96 97
		gs.maxMemoryPerPeer = maxMemoryPerPeer
	}
}

98 99 100
// MaxInProgressRequests changes the maximum number of
// graphsync requests that are processed in parallel (default 6)
func MaxInProgressRequests(maxInProgressRequests uint64) Option {
101
	return func(gs *graphsyncConfigOptions) {
102 103 104 105
		gs.maxInProgressRequests = maxInProgressRequests
	}
}

106
// New creates a new GraphSync Exchange on the given network,
107
// and the given link loader+storer.
108
func New(parent context.Context, network gsnet.GraphSyncNetwork,
109
	linkSystem ld.LinkSystem, options ...Option) graphsync.GraphExchange {
110 111
	ctx, cancel := context.WithCancel(parent)

112 113 114 115 116 117 118 119
	gsConfig := &graphsyncConfigOptions{
		totalMaxMemory:           defaultTotalMaxMemory,
		maxMemoryPerPeer:         defaultMaxMemoryPerPeer,
		maxInProgressRequests:    defaultMaxInProgressRequests,
		registerDefaultValidator: true,
	}
	for _, option := range options {
		option(gsConfig)
120
	}
Hannah Howard's avatar
Hannah Howard committed
121 122
	incomingResponseHooks := requestorhooks.NewResponseHooks()
	outgoingRequestHooks := requestorhooks.NewRequestHooks()
Hannah Howard's avatar
Hannah Howard committed
123
	incomingBlockHooks := requestorhooks.NewBlockHooks()
124
	networkErrorListeners := listeners.NewNetworkErrorListeners()
125
	receiverErrorListeners := listeners.NewReceiverNetworkErrorListeners()
126
	persistenceOptions := persistenceoptions.New()
Hannah Howard's avatar
Hannah Howard committed
127 128 129
	incomingRequestHooks := responderhooks.NewRequestHooks(persistenceOptions)
	outgoingBlockHooks := responderhooks.NewBlockHooks()
	requestUpdatedHooks := responderhooks.NewUpdateHooks()
130 131 132
	completedResponseListeners := listeners.NewCompletedResponseListeners()
	requestorCancelledListeners := listeners.NewRequestorCancelledListeners()
	blockSentListeners := listeners.NewBlockSentListeners()
133 134 135 136 137 138 139 140
	if gsConfig.registerDefaultValidator {
		incomingRequestHooks.Register(selectorvalidator.SelectorValidator(maxRecursionDepth))
	}
	allocator := allocator.NewAllocator(gsConfig.totalMaxMemory, gsConfig.maxMemoryPerPeer)
	createMessageQueue := func(ctx context.Context, p peer.ID) peermanager.PeerQueue {
		return messagequeue.New(ctx, p, network, allocator)
	}
	peerManager := peermanager.NewMessageManager(ctx, createMessageQueue)
Hannah Howard's avatar
Hannah Howard committed
141
	asyncLoader := asyncloader.New(ctx, linkSystem)
142
	requestManager := requestmanager.New(ctx, asyncLoader, outgoingRequestHooks, incomingResponseHooks, incomingBlockHooks, networkErrorListeners)
143
	responseAssembler := responseassembler.New(ctx, peerManager)
144
	peerTaskQueue := peertaskqueue.New()
Hannah Howard's avatar
Hannah Howard committed
145
	responseManager := responsemanager.New(ctx, linkSystem, responseAssembler, peerTaskQueue, incomingRequestHooks, outgoingBlockHooks, requestUpdatedHooks, completedResponseListeners, requestorCancelledListeners, blockSentListeners, networkErrorListeners, gsConfig.maxInProgressRequests)
146
	graphSync := &GraphSync{
147
		network:                     network,
Hannah Howard's avatar
Hannah Howard committed
148
		linkSystem:                  linkSystem,
149
		requestManager:              requestManager,
150 151 152 153
		responseManager:             responseManager,
		asyncLoader:                 asyncLoader,
		responseAssembler:           responseAssembler,
		peerTaskQueue:               peerTaskQueue,
154 155 156 157
		peerManager:                 peerManager,
		incomingRequestHooks:        incomingRequestHooks,
		outgoingBlockHooks:          outgoingBlockHooks,
		requestUpdatedHooks:         requestUpdatedHooks,
158
		completedResponseListeners:  completedResponseListeners,
159
		requestorCancelledListeners: requestorCancelledListeners,
160 161
		blockSentListeners:          blockSentListeners,
		networkErrorListeners:       networkErrorListeners,
162
		receiverErrorListeners:      receiverErrorListeners,
163 164 165
		incomingResponseHooks:       incomingResponseHooks,
		outgoingRequestHooks:        outgoingRequestHooks,
		incomingBlockHooks:          incomingBlockHooks,
166
		persistenceOptions:          persistenceOptions,
167 168
		ctx:                         ctx,
		cancel:                      cancel,
169
		allocator:                   allocator,
170
	}
171 172 173 174 175 176 177 178 179 180

	asyncLoader.Startup()
	requestManager.SetDelegate(peerManager)
	requestManager.Startup()
	responseManager.Startup()
	network.SetDelegate((*graphSyncReceiver)(graphSync))
	return graphSync
}

// Request initiates a new GraphSync request to the given peer using the given selector spec.
181
func (gs *GraphSync) Request(ctx context.Context, p peer.ID, root ld.Link, selector ld.Node, extensions ...graphsync.ExtensionData) (<-chan graphsync.ResponseProgress, <-chan error) {
182
	return gs.requestManager.SendRequest(ctx, p, root, selector, extensions...)
183 184
}

185
// RegisterIncomingRequestHook adds a hook that runs when a request is received
186 187 188
// If overrideDefaultValidation is set to true, then if the hook does not error,
// it is considered to have "validated" the request -- and that validation supersedes
// the normal validation of requests Graphsync does (i.e. all selectors can be accepted)
189
func (gs *GraphSync) RegisterIncomingRequestHook(hook graphsync.OnIncomingRequestHook) graphsync.UnregisterHookFunc {
190
	return gs.incomingRequestHooks.Register(hook)
191 192
}

193 194
// RegisterIncomingResponseHook adds a hook that runs when a response is received
func (gs *GraphSync) RegisterIncomingResponseHook(hook graphsync.OnIncomingResponseHook) graphsync.UnregisterHookFunc {
Hannah Howard's avatar
Hannah Howard committed
195
	return gs.incomingResponseHooks.Register(hook)
196 197 198 199
}

// RegisterOutgoingRequestHook adds a hook that runs immediately prior to sending a new request
func (gs *GraphSync) RegisterOutgoingRequestHook(hook graphsync.OnOutgoingRequestHook) graphsync.UnregisterHookFunc {
Hannah Howard's avatar
Hannah Howard committed
200
	return gs.outgoingRequestHooks.Register(hook)
201 202 203
}

// RegisterPersistenceOption registers an alternate loader/storer combo that can be substituted for the default
204
func (gs *GraphSync) RegisterPersistenceOption(name string, lsys ld.LinkSystem) error {
Hannah Howard's avatar
Hannah Howard committed
205
	err := gs.asyncLoader.RegisterPersistenceOption(name, lsys)
206 207 208
	if err != nil {
		return err
	}
Hannah Howard's avatar
Hannah Howard committed
209
	return gs.persistenceOptions.Register(name, lsys)
210 211
}

212 213 214 215 216 217 218 219 220
// UnregisterPersistenceOption unregisters an alternate loader/storer combo
func (gs *GraphSync) UnregisterPersistenceOption(name string) error {
	err := gs.asyncLoader.UnregisterPersistenceOption(name)
	if err != nil {
		return err
	}
	return gs.persistenceOptions.Unregister(name)
}

221 222 223 224 225
// RegisterOutgoingBlockHook registers a hook that runs after each block is sent in a response
func (gs *GraphSync) RegisterOutgoingBlockHook(hook graphsync.OnOutgoingBlockHook) graphsync.UnregisterHookFunc {
	return gs.outgoingBlockHooks.Register(hook)
}

Hannah Howard's avatar
Hannah Howard committed
226 227 228 229 230
// RegisterRequestUpdatedHook registers a hook that runs when an update to a request is received
func (gs *GraphSync) RegisterRequestUpdatedHook(hook graphsync.OnRequestUpdatedHook) graphsync.UnregisterHookFunc {
	return gs.requestUpdatedHooks.Register(hook)
}

231 232 233
// RegisterCompletedResponseListener adds a listener on the responder for completed responses
func (gs *GraphSync) RegisterCompletedResponseListener(listener graphsync.OnResponseCompletedListener) graphsync.UnregisterHookFunc {
	return gs.completedResponseListeners.Register(listener)
234 235
}

Hannah Howard's avatar
Hannah Howard committed
236 237 238 239 240
// RegisterIncomingBlockHook adds a hook that runs when a block is received and validated (put in block store)
func (gs *GraphSync) RegisterIncomingBlockHook(hook graphsync.OnIncomingBlockHook) graphsync.UnregisterHookFunc {
	return gs.incomingBlockHooks.Register(hook)
}

241 242 243 244 245 246
// RegisterRequestorCancelledListener adds a listener on the responder for
// responses cancelled by the requestor
func (gs *GraphSync) RegisterRequestorCancelledListener(listener graphsync.OnRequestorCancelledListener) graphsync.UnregisterHookFunc {
	return gs.requestorCancelledListeners.Register(listener)
}

247 248 249 250 251 252 253 254 255 256
// RegisterBlockSentListener adds a listener for when blocks are actually sent over the wire
func (gs *GraphSync) RegisterBlockSentListener(listener graphsync.OnBlockSentListener) graphsync.UnregisterHookFunc {
	return gs.blockSentListeners.Register(listener)
}

// RegisterNetworkErrorListener adds a listener for when errors occur sending data over the wire
func (gs *GraphSync) RegisterNetworkErrorListener(listener graphsync.OnNetworkErrorListener) graphsync.UnregisterHookFunc {
	return gs.networkErrorListeners.Register(listener)
}

257 258 259 260 261
// RegisterReceiverNetworkErrorListener adds a listener for when errors occur receiving data over the wire
func (gs *GraphSync) RegisterReceiverNetworkErrorListener(listener graphsync.OnReceiverNetworkErrorListener) graphsync.UnregisterHookFunc {
	return gs.receiverErrorListeners.Register(listener)
}

262 263 264 265 266 267 268 269 270 271 272
// UnpauseRequest unpauses a request that was paused in a block hook based request ID
// Can also send extensions with unpause
func (gs *GraphSync) UnpauseRequest(requestID graphsync.RequestID, extensions ...graphsync.ExtensionData) error {
	return gs.requestManager.UnpauseRequest(requestID, extensions...)
}

// PauseRequest pauses an in progress request (may take 1 or more blocks to process)
func (gs *GraphSync) PauseRequest(requestID graphsync.RequestID) error {
	return gs.requestManager.PauseRequest(requestID)
}

273
// UnpauseResponse unpauses a response that was paused in a block hook based on peer ID and request ID
274 275 276 277 278 279 280 281 282 283 284 285
func (gs *GraphSync) UnpauseResponse(p peer.ID, requestID graphsync.RequestID, extensions ...graphsync.ExtensionData) error {
	return gs.responseManager.UnpauseResponse(p, requestID, extensions...)
}

// PauseResponse pauses an in progress response (may take 1 or more blocks to process)
func (gs *GraphSync) PauseResponse(p peer.ID, requestID graphsync.RequestID) error {
	return gs.responseManager.PauseResponse(p, requestID)
}

// CancelResponse cancels an in progress response
func (gs *GraphSync) CancelResponse(p peer.ID, requestID graphsync.RequestID) error {
	return gs.responseManager.CancelResponse(p, requestID)
286 287
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
type graphSyncReceiver GraphSync

func (gsr *graphSyncReceiver) graphSync() *GraphSync {
	return (*GraphSync)(gsr)
}

// ReceiveMessage is part of the networks Receiver interface and receives
// incoming messages from the network
func (gsr *graphSyncReceiver) ReceiveMessage(
	ctx context.Context,
	sender peer.ID,
	incoming gsmsg.GraphSyncMessage) {
	gsr.graphSync().responseManager.ProcessRequests(ctx, sender, incoming.Requests())
	gsr.graphSync().requestManager.ProcessResponses(sender, incoming.Responses(), incoming.Blocks())
}

// ReceiveError is part of the network's Receiver interface and handles incoming
// errors from the network.
306 307 308
func (gsr *graphSyncReceiver) ReceiveError(p peer.ID, err error) {
	log.Infof("Graphsync ReceiveError from %s: %s", p, err)
	gsr.receiverErrorListeners.NotifyNetworkErrorListeners(p, err)
309 310 311 312 313 314 315 316 317 318 319 320 321
}

// Connected is part of the networks 's Receiver interface and handles peers connecting
// on the network
func (gsr *graphSyncReceiver) Connected(p peer.ID) {
	gsr.graphSync().peerManager.Connected(p)
}

// Connected is part of the networks 's Receiver interface and handles peers connecting
// on the network
func (gsr *graphSyncReceiver) Disconnected(p peer.ID) {
	gsr.graphSync().peerManager.Disconnected(p)
}