mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 11:27:42 +08:00
this is a major refactor of the entire codebase it changes the monolithic peer.Peer into using a peer.ID and a peer.Peerstore. Other changes: - removed handshake3. - testutil vastly simplified peer - secio bugfix + debugging logs - testutil: RandKeyPair - backpressure bugfix: w.o.w. - peer: added hex enc/dec - peer: added a PeerInfo struct PeerInfo is a small struct used to pass around a peer with a set of addresses and keys. This is not meant to be a complete view of the system, but rather to model updates to the peerstore. It is used by things like the routing system. - updated peer/queue + peerset - latency metrics - testutil: use crand for PeerID gen RandPeerID generates random "valid" peer IDs. it does not NEED to generate keys because it is as if we lost the key right away. fine to read some randomness and hash it. to generate proper keys and an ID, use: sk, pk, _ := testutil.RandKeyPair() id, _ := peer.IDFromPublicKey(pk) Also added RandPeerIDFatal helper - removed old spipe - updated seccat - core: cleanup initIdentity - removed old getFromPeerList
99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
// Package mocknet provides a mock net.Network to test with.
|
|
//
|
|
// - a Mocknet has many inet.Networks
|
|
// - a Mocknet has many Links
|
|
// - a Link joins two inet.Networks
|
|
// - inet.Conns and inet.Streams are created by inet.Networks
|
|
package mocknet
|
|
|
|
import (
|
|
"io"
|
|
"time"
|
|
|
|
ic "github.com/jbenet/go-ipfs/crypto"
|
|
inet "github.com/jbenet/go-ipfs/net"
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
|
|
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
|
)
|
|
|
|
type Mocknet interface {
|
|
|
|
// GenPeer generates a peer and its inet.Network in the Mocknet
|
|
GenPeer() (inet.Network, error)
|
|
|
|
// AddPeer adds an existing peer. we need both a privkey and addr.
|
|
// ID is derived from PrivKey
|
|
AddPeer(ic.PrivKey, ma.Multiaddr) (inet.Network, error)
|
|
|
|
// retrieve things
|
|
Peers() []peer.ID
|
|
Net(peer.ID) inet.Network
|
|
Nets() []inet.Network
|
|
Links() LinkMap
|
|
LinksBetweenPeers(a, b peer.ID) []Link
|
|
LinksBetweenNets(a, b inet.Network) []Link
|
|
|
|
// Links are the **ability to connect**.
|
|
// think of Links as the physical medium.
|
|
// For p1 and p2 to connect, a link must exist between them.
|
|
// (this makes it possible to test dial failures, and
|
|
// things like relaying traffic)
|
|
LinkPeers(peer.ID, peer.ID) (Link, error)
|
|
LinkNets(inet.Network, inet.Network) (Link, error)
|
|
Unlink(Link) error
|
|
UnlinkPeers(peer.ID, peer.ID) error
|
|
UnlinkNets(inet.Network, inet.Network) error
|
|
|
|
// LinkDefaults are the default options that govern links
|
|
// if they do not have thier own option set.
|
|
SetLinkDefaults(LinkOptions)
|
|
LinkDefaults() LinkOptions
|
|
|
|
// Connections are the usual. Connecting means Dialing.
|
|
// **to succeed, peers must be linked beforehand**
|
|
ConnectPeers(peer.ID, peer.ID) error
|
|
ConnectNets(inet.Network, inet.Network) error
|
|
DisconnectPeers(peer.ID, peer.ID) error
|
|
DisconnectNets(inet.Network, inet.Network) error
|
|
}
|
|
|
|
// LinkOptions are used to change aspects of the links.
|
|
// Sorry but they dont work yet :(
|
|
type LinkOptions struct {
|
|
Latency time.Duration
|
|
Bandwidth int // in bytes-per-second
|
|
// we can make these values distributions down the road.
|
|
}
|
|
|
|
// Link represents the **possibility** of a connection between
|
|
// two peers. Think of it like physical network links. Without
|
|
// them, the peers can try and try but they won't be able to
|
|
// connect. This allows constructing topologies where specific
|
|
// nodes cannot talk to each other directly. :)
|
|
type Link interface {
|
|
Networks() []inet.Network
|
|
Peers() []peer.ID
|
|
|
|
SetOptions(LinkOptions)
|
|
Options() LinkOptions
|
|
|
|
// Metrics() Metrics
|
|
}
|
|
|
|
// LinkMap is a 3D map to give us an easy way to track links.
|
|
// (wow, much map. so data structure. how compose. ahhh pointer)
|
|
type LinkMap map[string]map[string]map[Link]struct{}
|
|
|
|
// Printer lets you inspect things :)
|
|
type Printer interface {
|
|
// MocknetLinks shows the entire Mocknet's link table :)
|
|
MocknetLinks(mn Mocknet)
|
|
NetworkConns(ni inet.Network)
|
|
}
|
|
|
|
// PrinterTo returns a Printer ready to write to w.
|
|
func PrinterTo(w io.Writer) Printer {
|
|
return &printer{w}
|
|
}
|