mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 03:17:43 +08:00
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package bitswap
|
|
|
|
import (
|
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
swarm "github.com/jbenet/go-ipfs/swarm"
|
|
u "github.com/jbenet/go-ipfs/util"
|
|
|
|
ds "github.com/jbenet/datastore.go"
|
|
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// PartnerWantListMax is the bound for the number of keys we'll store per
|
|
// partner. These are usually taken from the top of the Partner's WantList
|
|
// advertisements. WantLists are sorted in terms of priority.
|
|
const PartnerWantListMax = 10
|
|
|
|
// KeySet is just a convenient alias for maps of keys, where we only care
|
|
// access/lookups.
|
|
type KeySet map[u.Key]struct{}
|
|
|
|
// BitSwap instances implement the bitswap protocol.
|
|
type BitSwap struct {
|
|
// peer is the identity of this (local) node.
|
|
peer *peer.Peer
|
|
|
|
// net holds the connections to all peers.
|
|
net swarm.Network
|
|
|
|
// datastore is the local database
|
|
// Ledgers of known
|
|
datastore ds.Datastore
|
|
|
|
// partners is a map of currently active bitswap relationships.
|
|
// The Ledger has the peer.ID, and the peer connection works through net.
|
|
// Ledgers of known relationships (active or inactive) stored in datastore.
|
|
// Changes to the Ledger should be committed to the datastore.
|
|
partners map[u.Key]*Ledger
|
|
|
|
// haveList is the set of keys we have values for. a map for fast lookups.
|
|
// haveList KeySet -- not needed. all values in datastore?
|
|
|
|
// wantList is the set of keys we want values for. a map for fast lookups.
|
|
wantList KeySet
|
|
}
|
|
|
|
// NewBitSwap creates a new BitSwap instance. It does not check its parameters.
|
|
func NewBitSwap(p *peer.Peer, net swarm.Network, d ds.Datastore) *BitSwap {
|
|
return &BitSwap{
|
|
peer: p,
|
|
net: net,
|
|
datastore: d,
|
|
partners: LedgerMap{},
|
|
wantList: KeySet{},
|
|
}
|
|
}
|
|
|
|
// GetBlock attempts to retrieve a particular block from peers, within timeout.
|
|
func (s *BitSwap) GetBlock(k u.Key, timeout time.Time) (
|
|
*blocks.Block, error) {
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
// HaveBlock announces the existance of a block to BitSwap, potentially sending
|
|
// it to peers (Partners) whose WantLists include it.
|
|
func (s *BitSwap) HaveBlock(k u.Key) (*blocks.Block, error) {
|
|
return nil, errors.New("not implemented")
|
|
}
|