bitswap

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0, MIT Imports: 18 Imported by: 0

Documentation

Overview

Package bitswap is Lantern's Bitswap client.

Phase 10 (V1.2 swarm-native fetch) replaced the original Phase 2 stub with a real boxo/bitswap client wired against the Lantern libp2p host. The package still exposes the Stub type for backward compatibility with older Phase 2 demos and unit tests in net/combined.

Production usage:

hc, _ := llibp2p.New(ctx, llibp2p.HostConfig{...})
bs, _ := bitswap.New(ctx, bitswap.Config{
    Host:           hc.H,
    ProviderFinder: hc.DHT(),       // optional; when nil broadcasts to connected peers
    PreferredPeers: beaconPeers,    // optional; warm-pool of beacon multiaddrs
})
// bs satisfies state/hamt.BlockGetter:
rawBlock, err := bs.Get(ctx, someCID)

Trust model: every block returned is hashed by net/combined.Fetcher before being trusted. Peers cannot lie undetected; the worst they can do is refuse to serve.

No CGo; depends only on github.com/ipfs/boxo and github.com/libp2p/go-libp2p.

Index

Constants

This section is empty.

Variables

View Source
var ErrStub = errors.New("net/bitswap: stub (no Filecoin Bitswap peers wired yet, see B8)")

ErrStub is the canonical "Bitswap path not yet wired" error.

Functions

This section is empty.

Types

type Client

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

Client is Lantern's Bitswap client. It satisfies state/hamt.BlockGetter (the .Get(ctx, cid) ([]byte, error) shape).

func New

func New(ctx context.Context, cfg Config) (*Client, error)

New constructs a Bitswap client and starts dialing PreferredPeers.

func (*Client) Close

func (c *Client) Close() error

Close releases the Bitswap client.

func (*Client) Get

func (c *Client) Get(ctx context.Context, k cid.Cid) ([]byte, error)

Get fetches the block by CID using a two-stage deadline: try preferred peers fast, escalate to full swarm, then give up. Returns the raw block bytes. The caller is responsible for verifying CID == hash(bytes); net/combined.Fetcher does this for production callers.

func (*Client) GetMany

func (c *Client) GetMany(ctx context.Context, ks []cid.Cid) (map[cid.Cid][]byte, error)

GetMany is a convenience batch lookup. It opens a single session so the HAMT walk-style "N sequential CIDs" pattern shares a warm peer pool.

func (*Client) NewSession

func (c *Client) NewSession(ctx context.Context) exchange.Fetcher

NewSession returns a Bitswap session — useful for HAMT-walk style fetch patterns where successive lookups share a warm peer pool.

func (*Client) Stats

func (c *Client) Stats() Stats

Stats snapshot.

type Config

type Config struct {
	// Host is the libp2p host Bitswap rides on top of. Required.
	Host host.Host
	// ProviderFinder optionally feeds Bitswap with provider hints. When
	// nil (no DHT wired), Bitswap broadcasts WANT-HAVE to its connected
	// peer set, which is sufficient when preferred peers are explicitly
	// connected.
	ProviderFinder routing.ContentDiscovery
	// PreferredPeers is the list of always-keep-connected peers tried
	// first. Beacon nodes (Phase 10 Part C) live here.
	PreferredPeers []peer.AddrInfo
	// FastDeadline is how long to wait for preferred peers before
	// escalating to the full swarm. Default 1.5s.
	FastDeadline time.Duration
	// FullDeadline is the total deadline applied when no per-call
	// timeout is set. Default 5s.
	FullDeadline time.Duration
	// ProtocolPrefix namespaces the bitswap libp2p protocol IDs. Filecoin
	// nodes (Lotus/Forest) serve bitswap under "/chain" (i.e.
	// /chain/ipfs/bitswap/1.2.0), NOT the boxo default "/ipfs/bitswap/...".
	// Set this to build.BitswapProtocolPrefix() ("/chain") to exchange
	// blocks with the Filecoin swarm. Empty => boxo default (IPFS), which
	// will NOT find blocks on the Filecoin network. lantern#50.
	ProtocolPrefix string
}

Config controls Bitswap wiring. All fields optional except Host.

type Stats

type Stats struct {
	GotBlocks uint64
	Misses    uint64
	Errors    uint64
	BytesIn   uint64
}

Stats is the snapshot shape Client.Stats() returns.

type Stub

type Stub struct{}

Stub satisfies state/hamt.BlockGetter. Always returns an error so the combined fetcher falls through to the next source.

func (Stub) Get

func (Stub) Get(_ context.Context, _ cid.Cid) ([]byte, error)

Get always returns ErrStub. This is intentional: see package doc.

Jump to

Keyboard shortcuts

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