sim

package
v0.0.0-...-20ef9fc Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0, MIT Imports: 15 Imported by: 0

Documentation

Overview

Package sim provides simple implementations of the principal Kademlia interfaces that allow the construction of simulations and test scenarios. The implementations are not suitable for use with real Kademlia networks.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotNetworkedEndpoint = errors.New("endpoint is not a NetworkedEndpoint")
	ErrUnknownMessageFormat = errors.New("unknown message format")
	ErrInvalidResponseType  = errors.New("invalid response type, expected MinKadResponseMessage")
)

Functions

func AddSchedulers

func AddSchedulers(s Simulator, schedulers ...event.AwareScheduler)

AddSchedulers adds a set of schedulers to a simulator

func RemoveSchedulers

func RemoveSchedulers(s Simulator, schedulers ...event.AwareScheduler)

RemoveSchedulers removes a set of schedulers from a simulator

Types

type Endpoint

type Endpoint[K kad.Key[K], A kad.Address[A]] struct {
	// contains filtered or unexported fields
}

Endpoint is a single threaded endpoint implementation simulating a network. It simulates a network and handles message exchanges between multiple peers in a simulation.

func NewEndpoint

func NewEndpoint[K kad.Key[K], A kad.Address[A]](self kad.NodeID[K], sched event.Scheduler, router *Router[K, A]) *Endpoint[K, A]

func (*Endpoint[K, A]) AddRequestHandler

func (e *Endpoint[K, A]) AddRequestHandler(protoID address.ProtocolID,
	req kad.Message, reqHandler endpoint.RequestHandlerFn[K],
) error

func (*Endpoint[K, A]) Connectedness

func (e *Endpoint[K, A]) Connectedness(id kad.NodeID[K]) (endpoint.Connectedness, error)

Peerstore functions

func (*Endpoint[K, A]) DialPeer

func (e *Endpoint[K, A]) DialPeer(ctx context.Context, id kad.NodeID[K]) error

func (*Endpoint[K, A]) HandleMessage

func (e *Endpoint[K, A]) HandleMessage(ctx context.Context, id kad.NodeID[K],
	protoID address.ProtocolID, sid endpoint.StreamID, msg kad.Message,
)

func (*Endpoint[K, A]) Key

func (e *Endpoint[K, A]) Key() K

func (*Endpoint[K, A]) MaybeAddToPeerstore

func (e *Endpoint[K, A]) MaybeAddToPeerstore(ctx context.Context, id kad.NodeInfo[K, A], ttl time.Duration) error

MaybeAddToPeerstore adds the given address to the peerstore. Endpoint doesn't take into account the ttl.

func (*Endpoint[K, A]) NetworkAddress

func (e *Endpoint[K, A]) NetworkAddress(id kad.NodeID[K]) (kad.NodeInfo[K, A], error)

func (*Endpoint[K, A]) RemoveRequestHandler

func (e *Endpoint[K, A]) RemoveRequestHandler(protoID address.ProtocolID)

func (*Endpoint[K, A]) SendRequestHandleResponse

func (e *Endpoint[K, A]) SendRequestHandleResponse(ctx context.Context,
	protoID address.ProtocolID, id kad.NodeID[K], req kad.Message,
	resp kad.Message, timeout time.Duration,
	handleResp endpoint.ResponseHandlerFn[K, A],
) error

type LiteSimulator

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

func NewLiteSimulator

func NewLiteSimulator(clk *clock.Mock) *LiteSimulator

func (*LiteSimulator) Add

func (s *LiteSimulator) Add(sched event.AwareScheduler)

func (*LiteSimulator) Clock

func (s *LiteSimulator) Clock() *clock.Mock

func (*LiteSimulator) Remove

func (s *LiteSimulator) Remove(sched event.AwareScheduler)

func (*LiteSimulator) Run

func (s *LiteSimulator) Run(ctx context.Context)

type Message

type Message[K kad.Key[K], A kad.Address[A]] struct {
	// contains filtered or unexported fields
}

Message is a simple implementation of `Request` and `Response`. It only contains the minimal fields that are required by Kademlia to operate.

func NewRequest

func NewRequest[K kad.Key[K], A kad.Address[A]](target K) *Message[K, A]

func NewResponse

func NewResponse[K kad.Key[K], A kad.Address[A]](closerPeers []kad.NodeInfo[K, A]) *Message[K, A]

func (*Message[K, A]) CloserNodes

func (m *Message[K, A]) CloserNodes() []kad.NodeInfo[K, A]

func (*Message[K, A]) EmptyResponse

func (m *Message[K, A]) EmptyResponse() kad.Response[K, A]

func (*Message[K, A]) Target

func (m *Message[K, A]) Target() K

type Router

type Router[K kad.Key[K], A kad.Address[A]] struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter[K kad.Key[K], A kad.Address[A]]() *Router[K, A]

func (*Router[K, A]) AddPeer

func (r *Router[K, A]) AddPeer(id kad.NodeID[K], peer SimEndpoint[K, A], sched event.Scheduler)

func (*Router[K, A]) RemovePeer

func (r *Router[K, A]) RemovePeer(id kad.NodeID[K])

func (*Router[K, A]) SendMessage

func (r *Router[K, A]) SendMessage(ctx context.Context, from, to kad.NodeID[K],
	protoID address.ProtocolID, sid endpoint.StreamID,
	msg kad.Message,
) (endpoint.StreamID, error)

type Server

type Server[K kad.Key[K], A kad.Address[A]] struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer[K kad.Key[K], A kad.Address[A]](rt kad.RoutingTable[K, kad.NodeID[K]], endpoint endpoint.Endpoint[K, A], cfg *ServerConfig) *Server[K, A]

func (*Server[K, A]) HandleFindNodeRequest

func (s *Server[K, A]) HandleFindNodeRequest(ctx context.Context,
	rpeer kad.NodeID[K], msg kad.Message,
) (kad.Message, error)

func (*Server[K, A]) HandleRequest

func (s *Server[K, A]) HandleRequest(ctx context.Context, rpeer kad.NodeID[K],
	msg kad.Message,
) (kad.Message, error)

type ServerConfig

type ServerConfig struct {
	PeerstoreTTL            time.Duration
	NumberUsefulCloserPeers int
}

func DefaultServerConfig

func DefaultServerConfig() *ServerConfig

type SimEndpoint

type SimEndpoint[K kad.Key[K], A kad.Address[A]] interface {
	endpoint.ServerEndpoint[K, A]
	// HandleMessage handles a message from the given peer.
	HandleMessage(context.Context, kad.NodeID[K], address.ProtocolID,
		endpoint.StreamID, kad.Message)
}

SimEndpoint is a simulated endpoint that doesn't operate on real network

type Simulator

type Simulator interface {
	// Add adds a scheduler to the simulator
	Add(event.AwareScheduler)
	// Remove removes a scheduler from the simulator
	Remove(event.AwareScheduler)
	// Run runs the simulator until there are no more Actions to run
	Run(context.Context)
}

Simulator is an interface for simulating a set of schedulers.

Jump to

Keyboard shortcuts

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