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 ¶
- Variables
- func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork, ...) exchange.Interface
- type Bitswap
- func (bs *Bitswap) CancelWants(cids []*cid.Cid, ses uint64)
- func (bs *Bitswap) Close() error
- func (bs *Bitswap) GetBlock(parent context.Context, k *cid.Cid) (blocks.Block, error)
- func (bs *Bitswap) GetBlocks(ctx context.Context, keys []*cid.Cid) (<-chan blocks.Block, error)
- func (bs *Bitswap) GetWantlist() []*cid.Cid
- func (bs *Bitswap) HasBlock(blk blocks.Block) error
- func (bs *Bitswap) IsOnline() bool
- func (bs *Bitswap) LedgerForPeer(p peer.ID) *decision.Receipt
- func (bs *Bitswap) NewSession(ctx context.Context) *Session
- func (bs *Bitswap) PeerConnected(p peer.ID)
- func (bs *Bitswap) PeerDisconnected(p peer.ID)
- func (bs *Bitswap) ReceiveError(err error)
- func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage)
- func (bs *Bitswap) SessionsForBlock(c *cid.Cid) []*Session
- func (bs *Bitswap) Stat() (*Stat, error)
- func (bs *Bitswap) WantlistForPeer(p peer.ID) []*cid.Cid
- type Instance
- type Session
- type SessionGenerator
- type Stat
- type WantManager
- func (pm *WantManager) CancelWants(ctx context.Context, ks []*cid.Cid, peers []peer.ID, ses uint64)
- func (pm *WantManager) Connected(p peer.ID)
- func (pm *WantManager) ConnectedPeers() []peer.ID
- func (pm *WantManager) Disconnected(p peer.ID)
- func (pm *WantManager) Run()
- func (pm *WantManager) SendBlock(ctx context.Context, env *engine.Envelope)
- func (pm *WantManager) WantBlocks(ctx context.Context, ks []*cid.Cid, peers []peer.ID, ses uint64)
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyHaveBlock = errors.New("already have block")
var (
HasBlockBufferSize = 256
)
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 ¶ added in v0.3.2
type Bitswap struct {
// contains filtered or unexported fields
}
Bitswap instances implement the bitswap protocol.
func (*Bitswap) CancelWants ¶ added in v0.3.8
CancelWant removes a given key from the wantlist
func (*Bitswap) GetBlock ¶ added in v0.3.2
GetBlock attempts to retrieve a particular block from peers within the deadline enforced by the context.
func (*Bitswap) GetBlocks ¶ added in v0.3.2
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 ¶ added in v0.3.2
func (*Bitswap) HasBlock ¶ added in v0.3.2
HasBlock announces the existance of a block to this bitswap service. The service will potentially notify its peers.
func (*Bitswap) LedgerForPeer ¶ added in v0.4.5
func (*Bitswap) NewSession ¶ added in v0.4.11
NewSession creates a new bitswap session whose lifetime is bounded by the given context
func (*Bitswap) PeerConnected ¶ added in v0.3.2
Connected/Disconnected warns bitswap about peer connections
func (*Bitswap) PeerDisconnected ¶ added in v0.3.2
Connected/Disconnected warns bitswap about peer connections
func (*Bitswap) ReceiveError ¶ added in v0.3.2
func (*Bitswap) ReceiveMessage ¶ added in v0.3.2
func (*Bitswap) SessionsForBlock ¶ added in v0.4.11
SessionsForBlock returns a slice of all sessions that may be interested in the given cid
type Instance ¶
func MkSession ¶ added in v0.4.11
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
type Session ¶ added in v0.3.6
type Session struct {
// contains filtered or unexported fields
}
Session holds state for an individual bitswap transfer operation. This allows bitswap to make smarter decisions about who to send wantlist info to, and who to request blocks from
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 WantManager ¶ added in v0.3.5
type WantManager struct {
// contains filtered or unexported fields
}
func NewWantManager ¶ added in v0.3.5
func NewWantManager(ctx context.Context, network bsnet.BitSwapNetwork) *WantManager
func (*WantManager) CancelWants ¶ added in v0.3.5
CancelWants removes the given cids from the wantlist, tracked by the given session
func (*WantManager) Connected ¶ added in v0.3.5
func (pm *WantManager) Connected(p peer.ID)
func (*WantManager) ConnectedPeers ¶ added in v0.4.0
func (pm *WantManager) ConnectedPeers() []peer.ID
func (*WantManager) Disconnected ¶ added in v0.3.5
func (pm *WantManager) Disconnected(p peer.ID)
func (*WantManager) Run ¶ added in v0.3.5
func (pm *WantManager) Run()
TODO: use goprocess here once i trust it
func (*WantManager) SendBlock ¶ added in v0.3.5
func (pm *WantManager) SendBlock(ctx context.Context, env *engine.Envelope)
func (*WantManager) WantBlocks ¶ added in v0.3.5
WantBlocks adds the given cids to the wantlist, tracked by the given session
Source Files ¶
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. |