bitswap

package
v0.0.0-...-795e242 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2015 License: MIT Imports: 26 Imported by: 0

README

Bitswap

Protocol

Bitswap is the data trading module for ipfs, it manages requesting and sending blocks to and from other peers in the network. Bitswap has two main jobs, the first is to acquire blocks requested by the client from the network. The second is to judiciously send blocks in its posession to other peers who want them.

Bitswap is a message based protocol, as opposed to response-reply. All messages contain wantlists, or blocks. Upon receiving a wantlist, a node should consider sending out wanted blocks if they have them. Upon receiving blocks, the node should send out a notification called a 'Cancel' signifying that they no longer want the block. At a protocol level, bitswap is very simple.

go-ipfs Implementation

Internally, when a message with a wantlist is received, it is sent to the decision engine to be considered, and blocks that we have that are wanted are placed into the peer request queue. Any block we possess that is wanted by another peer has a task in the peer request queue created for it. The peer request queue is a priority queue that sorts available tasks by some metric, currently, that metric is very simple and aims to fairly address the tasks of each other peer. More advanced decision logic will be implemented in the future. Task workers pull tasks to be done off of the queue, retreive the block to be sent, and send it off. The number of task workers is limited by a constant factor.

Client requests for new blocks are handled by the want manager, for every new block (or set of blocks) wanted, the 'WantBlocks' method is invoked. The want manager then ensures that connected peers are notified of the new block that we want by sending the new entries to a message queue for each peer. The message queue will loop while there is work available and do the following: 1) Ensure it has a connection to its peer, 2) grab the message to be sent, and 3) send it. If new messages are added while the loop is in steps 1 or 3, the messages are combined into one to avoid having to keep an actual queue and send multiple messages. The same process occurs when the client receives a block and sends a cancel message for it.

Documentation

Overview

package bitswap implements the IPFS Exchange interface with the BitSwap bilateral exchange protocol.

Index

Constants

View Source
const (
	HasBlockBufferSize = 256
)

Variables

View Source
var ErrAlreadyHaveBlock = errors.New("already have block")
View Source
var TaskWorkerCount = 8

Functions

func New

func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
	bstore blockstore.Blockstore, nice bool) exchange.Interface

New initializes a BitSwap instance that communicates over the provided BitSwapNetwork. This function registers the returned instance as the network delegate. Runs until context is cancelled.

Types

type Bitswap

type Bitswap struct {
	// contains filtered or unexported fields
}

Bitswap instances implement the bitswap protocol.

func (*Bitswap) CancelWants

func (bs *Bitswap) CancelWants(ks []key.Key)

CancelWant removes a given key from the wantlist

func (*Bitswap) Close

func (bs *Bitswap) Close() error

func (*Bitswap) GetBlock

func (bs *Bitswap) GetBlock(parent context.Context, k key.Key) (*blocks.Block, error)

GetBlock attempts to retrieve a particular block from peers within the deadline enforced by the context.

func (*Bitswap) GetBlocks

func (bs *Bitswap) GetBlocks(ctx context.Context, keys []key.Key) (<-chan *blocks.Block, error)

GetBlocks returns a channel where the caller may receive blocks that correspond to the provided |keys|. Returns an error if BitSwap is unable to begin this request within the deadline enforced by the context.

NB: Your request remains open until the context expires. To conserve resources, provide a context with a reasonably short deadline (ie. not one that lasts throughout the lifetime of the server)

func (*Bitswap) GetWantlist

func (bs *Bitswap) GetWantlist() []key.Key

func (*Bitswap) HasBlock

func (bs *Bitswap) HasBlock(blk *blocks.Block) error

HasBlock announces the existance of a block to this bitswap service. The service will potentially notify its peers.

func (*Bitswap) PeerConnected

func (bs *Bitswap) PeerConnected(p peer.ID)

Connected/Disconnected warns bitswap about peer connections

func (*Bitswap) PeerDisconnected

func (bs *Bitswap) PeerDisconnected(p peer.ID)

Connected/Disconnected warns bitswap about peer connections

func (*Bitswap) ReceiveError

func (bs *Bitswap) ReceiveError(err error)

func (*Bitswap) ReceiveMessage

func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage)

func (*Bitswap) Stat

func (bs *Bitswap) Stat() (*Stat, error)

func (*Bitswap) WantlistForPeer

func (bs *Bitswap) WantlistForPeer(p peer.ID) []key.Key

type Instance

type Instance struct {
	Peer     peer.ID
	Exchange *Bitswap
	// contains filtered or unexported fields
}

func Session

func Session(ctx context.Context, net tn.Network, p testutil.Identity) Instance

session creates a test bitswap session.

NB: It's easy make mistakes by providing the same peer ID to two different sessions. To safeguard, use the SessionGenerator to generate sessions. It's just a much better idea.

func (*Instance) Blockstore

func (i *Instance) Blockstore() blockstore.Blockstore

func (*Instance) SetBlockstoreLatency

func (i *Instance) SetBlockstoreLatency(t time.Duration) time.Duration

type SessionGenerator

type SessionGenerator struct {
	// contains filtered or unexported fields
}

TODO move this SessionGenerator to the core package and export it as the core generator

func NewTestSessionGenerator

func NewTestSessionGenerator(
	net tn.Network) SessionGenerator

WARNING: this uses RandTestBogusIdentity DO NOT USE for NON TESTS!

func (*SessionGenerator) Close

func (g *SessionGenerator) Close() error

func (*SessionGenerator) Instances

func (g *SessionGenerator) Instances(n int) []Instance

func (*SessionGenerator) Next

func (g *SessionGenerator) Next() Instance

type Stat

type Stat struct {
	ProvideBufLen   int
	Wantlist        []key.Key
	Peers           []string
	BlocksReceived  int
	DupBlksReceived int
	DupDataReceived uint64
}

type WantManager

type WantManager struct {
	// contains filtered or unexported fields
}

func NewWantManager

func NewWantManager(ctx context.Context, network bsnet.BitSwapNetwork) *WantManager

func (*WantManager) CancelWants

func (pm *WantManager) CancelWants(ks []key.Key)

func (*WantManager) Connected

func (pm *WantManager) Connected(p peer.ID)

func (*WantManager) Disconnected

func (pm *WantManager) Disconnected(p peer.ID)

func (*WantManager) Run

func (pm *WantManager) Run()

TODO: use goprocess here once i trust it

func (*WantManager) SendBlock

func (pm *WantManager) SendBlock(ctx context.Context, env *engine.Envelope)

func (*WantManager) WantBlocks

func (pm *WantManager) WantBlocks(ks []key.Key)

Directories

Path Synopsis
package decision implements the decision engine for the bitswap service.
package decision implements the decision engine for the bitswap service.
pb
Package bitswap_message_pb is a generated protocol buffer package.
Package bitswap_message_pb is a generated protocol buffer package.
package wantlist implements an object for bitswap that contains the keys that a given peer wants.
package wantlist implements an object for bitswap that contains the keys that a given peer wants.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL