p2p

package
v0.0.0-...-b1bb88e Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2021 License: LGPL-2.1 Imports: 66 Imported by: 0

Documentation

Overview

Provides subscibing and publishng to events. Event do not have a clear peer as target, but propagate through the whole netork and are accepted from everyone who is subscribed to it. The propagation happen through the already existing connections.

LocalServer

Identification.go

Swarm: main functions

Index

Constants

View Source
const (
	AUTH_NONE      = AUTH_STATE(0)
	AUTH_READONLY  = AUTH_STATE(1)
	AUTH_READWRITE = AUTH_STATE(2)
)

possible states

View Source
const (
	STATE_EVENT_PEER_ACTIVE        = replica.EVENT_PEER_ADDED
	STATE_EVENT_PEER_INACTIVE      = replica.EVENT_PEER_REMOVED
	STATE_EVENT_MAJORITY_AVAILABLE = replica.EVENT_MAJORITY_AVAILABLE
	STATE_EVENT_MAJORITY_LOST      = replica.EVENT_MAJORITY_LOST
)
View Source
const Error_Arguments = "arguments_wrong" // arguments are invalid (e.g. wrong number, wrong type)
View Source
const Error_Authorisation = "authorisation_invalid" // error due to wrong authorisation
View Source
const Error_Data = "data_share_failed" // For all kind of internal proccesses
View Source
const Error_Invalid_Data = "data_invalid" // For all kind of wrong data errors
View Source
const Error_Operation_Invalid = "operation_invalid" // User operation not possible with current application state
View Source
const Error_Process = "process_failed" // For all kind of proccesses or operations
View Source
const Error_Setup = "setup_invalid" // The datastructures are not setup correctly
View Source
const Error_Unavailable = "node_unavailable" // error due to not being able to reach node

Variables

View Source
var InvalidPeer = PeerID("")

Functions

func AuthStateToString

func AuthStateToString(state AUTH_STATE) string

func MakeTemporaryTwoHostNetwork

func MakeTemporaryTwoHostNetwork(path string) (*Host, *Host, error)

func NewDAGService

func NewDAGService(owner string, session exchange.Interface, ownerstore datastore.TxnDatastore, blockstore blockstore.Blockstore) ipld.DAGService

func RepeatableData

func RepeatableData(size int) []byte

Types

type AUTH_STATE

type AUTH_STATE uint

A type describing a state of authorisation

func AuthStateFromString

func AuthStateFromString(state string) (AUTH_STATE, error)

type Adder

type Adder struct {
	CidBuilder cid.Builder
	// contains filtered or unexported fields
}

Adder builds merkle trees from files and adds the to bitswap

func NewAdder

func NewAdder(ctx context.Context, ds ipld.DAGService) (*Adder, error)

NewAdder Returns a new Adder used for a file add operation.

func (*Adder) Add

func (self *Adder) Add(file files.Node) (ipld.Node, error)

Add adds the given request's files. It creates a DAG structure which fits the file and returns the root node

type BootstrapConfig

type BootstrapConfig struct {
	// MinPeerThreshold governs whether to bootstrap more connections. If the
	// node has less open connections than this number, it will open connections
	// to the bootstrap nodes. From there, the routing system should be able
	// to use the connections to the bootstrap nodes to connect to even more
	// peers. Routing systems like the IpfsDHT do so in their own Bootstrap
	// process, which issues random queries to find more peers.
	MinPeerThreshold int

	// Period governs the periodic interval at which the node will
	// attempt to bootstrap. The bootstrap process is not very expensive, so
	// this threshold can afford to be small (<=30s).
	Period time.Duration

	// ConnectionTimeout determines how long to wait for a bootstrap
	// connection attempt before cancelling it.
	ConnectionTimeout time.Duration

	// BootstrapPeers is a function that returns a set of bootstrap peers
	// for the bootstrap process to use. This makes it possible for clients
	// to control the peers the process uses at any moment.
	BootstrapPeers func() []peer.AddrInfo

	// The logger used to log all bootstrap messages
	Logger hclog.Logger
}

BootstrapConfig specifies parameters used in an IpfsNode's network bootstrapping process.

func GetDefaultBootstrapConfig

func GetDefaultBootstrapConfig(log hclog.Logger) BootstrapConfig

type DataService

type DataService interface {
	Add(ctx context.Context, path string) (utils.Cid, error)              //adds a file or directory
	AddData(ctx context.Context, data []byte) (utils.Cid, error)          //adds a Blockifyer
	AddAsync(path string) (utils.Cid, error)                              //adds a file or directory, but returns when the local operation is done
	Drop(ctx context.Context, id utils.Cid) error                         //removes a file or directory
	Fetch(ctx context.Context, id utils.Cid) error                        //Fetches the given data
	FetchAsync(id utils.Cid) error                                        //Fetches the given data async
	Get(ctx context.Context, id utils.Cid) (io.Reader, error)             //gets the file described by the id (fetches if needed)
	Write(ctx context.Context, id utils.Cid, path string) (string, error) //writes the file or directory to the given path (fetches if needed)
	ReadChannel(ctx context.Context, id utils.Cid) (chan []byte, error)   //reads the data in individual binary blocks (does not work for directory)
	HasLocal(id utils.Cid) bool                                           //checks if the given id is available locally
	Close()
}

the data service interface!

func NewDataService

func NewDataService(host *Host) (DataService, error)

type Event

type Event struct {
	Arguments []interface{}
	Source    PeerID
	Topic     string
}

small custom wrapper for message to expose custom Event type

type Host

type Host struct {

	//serivces the host provides
	Rpc   *hostRpcService
	Data  DataService
	Event *hostEventService
	// contains filtered or unexported fields
}

func MakeTemporaryTestingHost

func MakeTemporaryTestingHost(path string) (*Host, error)

for external modules that need a host for tessting

func NewHost

func NewHost(router *connection.Router, logger hclog.Logger) *Host

Host creates p2p host which manages all peer connections

func (*Host) Addresses

func (h *Host) Addresses(peer PeerID) ([]ma.Multiaddr, error)

func (*Host) CloseConnection

func (h *Host) CloseConnection(peer PeerID, force bool) error

Closes the connection to given peer. If force is true, it will be closed. Otherwise it stays open and the host is free to close it later in case of too many open connections

func (*Host) Connect

func (h *Host) Connect(ctx context.Context, peer PeerID, keep_open bool) error

Tries to connect to given peer. If keep_open is true the connection stays always open, otherwise it could be closed later in case too many connections are open

func (*Host) CreateSwarm

func (h *Host) CreateSwarm(ctx context.Context, states []State) (*Swarm, error)

func (*Host) CreateSwarmWithID

func (h *Host) CreateSwarmWithID(ctx context.Context, id SwarmID, states []State) (*Swarm, error)

func (*Host) FindProviders

func (h *Host) FindProviders(ctx context.Context, cid utils.Cid, num int) ([]PeerID, error)

find peers that provide the given cid. The returned slice can have less than num entries, depending on the find results

func (*Host) FindProvidersAsync

func (h *Host) FindProvidersAsync(ctx context.Context, cid utils.Cid, num int) (chan PeerID, error)

find peers that provide the given cid

func (*Host) FindSwarmMember

func (h *Host) FindSwarmMember(ctx context.Context, id SwarmID) (PeerID, error)

Finds a peer active in current swarm

func (*Host) GetPath

func (h *Host) GetPath() string

func (*Host) GetSwarm

func (h *Host) GetSwarm(id SwarmID) (*Swarm, error)

func (*Host) ID

func (h *Host) ID() PeerID

func (*Host) IsConnected

func (h *Host) IsConnected(peer PeerID) bool

func (*Host) JoinSwarm

func (h *Host) JoinSwarm(ctx context.Context, id SwarmID, states []State, knownPeers []PeerID) (*Swarm, error)

func (*Host) Keys

func (h *Host) Keys() (crypto.PrivKey, crypto.PubKey)

func (*Host) OwnAddresses

func (h *Host) OwnAddresses() []ma.Multiaddr

func (*Host) Peers

func (h *Host) Peers(connectedOnly bool) []PeerID

returns all known peers

func (*Host) Provide

func (h *Host) Provide(ctx context.Context, cid utils.Cid) error

provides for 24h, afterwards gets deletet if not provided again

func (*Host) Reachability

func (h *Host) Reachability() string

func (*Host) Routing

func (h *Host) Routing() *kaddht.IpfsDHT

func (*Host) SetAdress

func (h *Host) SetAdress(peer PeerID, addr ma.Multiaddr) error

func (*Host) SetMultipleAdress

func (self *Host) SetMultipleAdress(peer PeerID, addrs []ma.Multiaddr) error

func (*Host) Start

func (h *Host) Start(shouldBootstrap bool) error

Starts the listening for connections and the bootstrap prozess

func (*Host) Stop

func (h *Host) Stop(ctx context.Context) error

func (*Host) Swarms

func (h *Host) Swarms() []*Swarm

type HostRPCApi

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

RPC Api of the host

func (HostRPCApi) HasSwarm

func (self HostRPCApi) HasSwarm(ctx context.Context, id SwarmID, has *bool) error

type OwnerAwareBlockService

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

implementation of the IPFS Blockservice interface. reason to have our own is to allow ownership of blocks for our swarm implementation it works together with OwnerAwareRouting

func NewOwnerAwareBlockService

func NewOwnerAwareBlockService(owner string, ds datastore.TxnDatastore, bs blockstore.Blockstore, service exchange.Interface) *OwnerAwareBlockService

creates a blockservice with a datastore for ownership data and a blockstore for storing the block data. Note: Could be the same datastore for both

func (*OwnerAwareBlockService) AddBlock

func (self *OwnerAwareBlockService) AddBlock(o blocks.Block) error

AddBlock puts a given block to the underlying datastore

func (*OwnerAwareBlockService) AddBlocks

func (self *OwnerAwareBlockService) AddBlocks(bs []blocks.Block) error

AddBlocks adds a slice of blocks at the same time using batching capabilities of the underlying datastore whenever possible.

func (*OwnerAwareBlockService) Blockstore

func (self *OwnerAwareBlockService) Blockstore() blockstore.Blockstore

Blockstore returns a reference to the underlying blockstore

func (*OwnerAwareBlockService) Close

func (self *OwnerAwareBlockService) Close() error

func (*OwnerAwareBlockService) DeleteBlock

func (self *OwnerAwareBlockService) DeleteBlock(c cid.Cid) error

DeleteBlock deletes the given block from the blockservice.

func (*OwnerAwareBlockService) Exchange

func (self *OwnerAwareBlockService) Exchange() exchange.Interface

Exchange returns a reference to the underlying exchange (usually bitswap)

func (*OwnerAwareBlockService) GetBlock

func (self *OwnerAwareBlockService) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error)

GetBlock gets the requested block.

func (*OwnerAwareBlockService) GetBlocks

func (self *OwnerAwareBlockService) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block

GetBlocks does a batch request for the given cids, returning blocks as they are found, in no particular order.

It may not be able to find all requested blocks (or the context may be canceled). In that case, it will close the channel early. It is up to the consumer to detect this situation and keep track which blocks it has received and which it hasn't.

type OwnerAwareRouting

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

the routing type used by bitswap network

func NewOwnerAwareRouting

func NewOwnerAwareRouting(host *Host, ownerStore datastore.Datastore) (OwnerAwareRouting, error)

func (OwnerAwareRouting) FindProvidersAsync

func (self OwnerAwareRouting) FindProvidersAsync(ctx context.Context, id cid.Cid, num int) <-chan peerstore.PeerInfo

Search for peers who are able to provide a given key

func (OwnerAwareRouting) Provide

func (self OwnerAwareRouting) Provide(ctx context.Context, id cid.Cid, announce bool) error

Provide adds the given cid to the content routing system. If 'true' is passed, it also announces it, otherwise it is just kept in the local accounting of which objects are being provided.

type PeerID

type PeerID = peer.ID

func NoPeers

func NoPeers() []PeerID

func PeerIDFromPublicKey

func PeerIDFromPublicKey(pk crypto.PubKey) (PeerID, error)

func PeerIDFromPublicKeyFile

func PeerIDFromPublicKeyFile(file string) (PeerID, error)

func PeerIDFromString

func PeerIDFromString(id string) (PeerID, error)

func SwarmPeers

func SwarmPeers(peers ...PeerID) []PeerID

type PeerSet

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

A concurency safe store for peers

func NewPeerSet

func NewPeerSet() *PeerSet

func (*PeerSet) Add

func (ps *PeerSet) Add(p PeerID)

func (*PeerSet) Contains

func (ps *PeerSet) Contains(p PeerID) bool

func (*PeerSet) Peers

func (ps *PeerSet) Peers() []PeerID

func (*PeerSet) Remove

func (ps *PeerSet) Remove(p PeerID)

func (*PeerSet) Size

func (ps *PeerSet) Size() int

type ReplicaAPI

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

func (*ReplicaAPI) AddCommand

func (self *ReplicaAPI) AddCommand(ctx context.Context, op replica.Operation, ret *bool) error

type ReplicaReadAPI

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

Api callable by Read Only auth

func (*ReplicaReadAPI) GetLeader

func (self *ReplicaReadAPI) GetLeader(ctx context.Context, inp struct{}, ret *peer.ID) error

func (*ReplicaReadAPI) Join

func (self *ReplicaReadAPI) Join(ctx context.Context, peer PeerID, ret *AUTH_STATE) error

join is ReadAPI as also read only peers need to call it for themself. If joining is allowed will be checked in this function

func (*ReplicaReadAPI) Leave

func (self *ReplicaReadAPI) Leave(ctx context.Context, peer PeerID, ret *struct{}) error

leav is ReadAPI as also read only peers need to call it for themself.

type State

type State = replica.State

func NoStates

func NoStates() []State

func SwarmStates

func SwarmStates(states ...State) []State

little helper to add swarms to the states

type StateEvent

type StateEvent = replica.Event

type StateObserver

type StateObserver = replica.Observer

type State_Event_Type

type State_Event_Type = replica.Event_Type

type Subscription

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

a small wrapper for subscription type supports custom message type

func (Subscription) Cancel

func (self Subscription) Cancel()

Cancels the subscription. Next will return with an error and no more events will be catched

func (Subscription) Next

func (self Subscription) Next(ctx context.Context) (*Event, error)

blocks till a event arrives, the context is canceled or the subscription itself is canceld

func (Subscription) Topic

func (self Subscription) Topic() string

type SubscriptionFilter

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

Filters subscriptions based on P2P authorisation

func (SubscriptionFilter) CanSubscribe

func (self SubscriptionFilter) CanSubscribe(topic string) bool

CanSubscribe returns true if the topic is of interest and we can subscribe to it

func (SubscriptionFilter) FilterIncomingSubscriptions

func (self SubscriptionFilter) FilterIncomingSubscriptions(sender peer.ID, subopts []*pb.RPC_SubOpts) ([]*pb.RPC_SubOpts, error)

we filter every subscription that is not authorized

type Swarm

type Swarm struct {
	//provided services
	Event *swarmEventService
	Rpc   *swarmRpcService
	Data  DataService
	State *sharedStateService

	ID SwarmID
	// contains filtered or unexported fields
}

Type that represents a collection if peers which are connected together and form a swarm. It allows to share data between all peers, as well as have common events and provide rpc calls.

A swarm does always allow reading for its pears! It is not possible to make it secure, don't use it if the shared information is not public. Even if a peer is not added it is easily possible to catch the information for attacking nodes.

It does allow for a certain Authorisation sheme: ReadOnly or ReadWrite. It has the folloing impact on the swarm services:

  • RPC: -- ReadOnly Peer can be adressed for all RPC calls it offers (may fail dependend on its on AUTH info) -- ReadOnly Peer is allowed to call all registered ReadOnly RPCs of this swarm -- ReadOnly Peer is not allowed to call all registered ReadWrtie RPCs of this swarm. Those calls will fail. -- ReadWrite Peer is additionaly allowed to call ReadWrite RPC's of this swarm
  • Event: -- ReadOnly Peer will receive all events send from this swarm (may fail dependend on its on AUTH info) -- ReadOnly Peer can publish ReadOnly events to this swarm -- ReadOnly Peer can not publish ReadWrite event to this swarm -- ReadWrite Peer can additionally publish ReadWrite events to this swarm
  • Data: -- ReadOnly Peer will receive all shared file information and the files itself -- ReadOnly Peer will not be able to add new files to the swarm -- ReadWrite Peer will be able to add new files to the swarm

func (*Swarm) AddPeer

func (s *Swarm) AddPeer(ctx context.Context, pid PeerID, state AUTH_STATE) error

Peer is added and hence allowed to use all swarm functionality.

func (*Swarm) ChangePeer

func (s *Swarm) ChangePeer(ctx context.Context, peer PeerID, auth AUTH_STATE) error

func (*Swarm) Close

func (s *Swarm) Close(ctx context.Context)

func (*Swarm) GetHost

func (s *Swarm) GetHost() *Host

func (*Swarm) GetPath

func (self *Swarm) GetPath() string

func (*Swarm) GetPeers

func (s *Swarm) GetPeers(state AUTH_STATE) []PeerID

returns all peers with the given or higher auth state. E.g. AUTH_READONLY returns all peers with read only as well as read write auth If you want all peers use AUTH_NONE

func (*Swarm) HasPeer

func (s *Swarm) HasPeer(peer PeerID) bool

func (*Swarm) PeerAuth

func (self *Swarm) PeerAuth(peer PeerID) AUTH_STATE

func (*Swarm) RemovePeer

func (s *Swarm) RemovePeer(ctx context.Context, peer PeerID) error

type SwarmConfOp

type SwarmConfOp struct {
	Remove bool
	Peer   PeerID
	Auth   AUTH_STATE
}

func (SwarmConfOp) ToBytes

func (self SwarmConfOp) ToBytes() []byte

type SwarmConfiguration

type SwarmConfiguration struct {
	Peer map[PeerID]AUTH_STATE
	// contains filtered or unexported fields
}

this is a replica state

func (*SwarmConfiguration) Apply

func (self *SwarmConfiguration) Apply(data []byte) interface{}

state interface

func (*SwarmConfiguration) GetPeers

func (self *SwarmConfiguration) GetPeers(state AUTH_STATE) []PeerID

func (*SwarmConfiguration) HasPeer

func (self *SwarmConfiguration) HasPeer(peer PeerID) bool

func (*SwarmConfiguration) LoadSnapshot

func (self *SwarmConfiguration) LoadSnapshot(data []byte) error

state interface

func (*SwarmConfiguration) PeerAuth

func (self *SwarmConfiguration) PeerAuth(peer PeerID) AUTH_STATE

func (*SwarmConfiguration) Snapshot

func (self *SwarmConfiguration) Snapshot() ([]byte, error)

state interface

type SwarmID

type SwarmID string

func (SwarmID) Cid

func (id SwarmID) Cid() cid.Cid

create a cid from the swarm ID to be used in the dht

func (SwarmID) Pretty

func (id SwarmID) Pretty() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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