polity

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPeerExists = errors.New("peer exists")

Functions

This section is empty.

Types

type Address

type Address interface {
	net.Addr
	json.Marshaler
	json.Unmarshaler
	Equal(Address) bool
}

type AddressConnectMailer

type AddressConnectMailer interface {
	AddressConnector
	Mailer
}

type AddressConnector

type AddressConnector interface {
	Addresser
	Connector
	New() AddressConnector
}

An AddressConnector is an Addresser and Connector. It allows a node to accept and issue requests over the network.

type Addresser

type Addresser interface {
	net.Addr
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	Addr() net.Addr
}

An Addresser provides a network address and a way to serialize/deserialize it

type Connector

type Connector interface {
	Initialize()
	Connection() (net.PacketConn, error)    // persistent connection
	NewConnection() (net.PacketConn, error) // for ephemeral one-off connections
	Close() error
}

A Connector provides one persistent and one ad hoc packet connection. The ephemeral connection should be closed after each use. The persistent connection should be closed at shutdown

type Envelope

type Envelope[A Addresser] struct {
	ID        *MessageId      `json:"id" msgpack:"id"`
	Thread    *MessageId      `json:"thread" msgpack:"thread"`
	Sender    *Peer[A]        `json:"sender" msgpack:"sender"`
	Recipient *Peer[A]        `json:"recipient" msgpack:"recipient"`
	Message   *delphi.Message `json:"msg" msgpack:"msg"`
}

An Envelope wraps a delphi.Message with information essential for addressing and organizing.

func NewEnvelope

func NewEnvelope[A Addresser]() *Envelope[A]

NewEnvelope creates a new Envelope, ensuring there are no nil pointers

func (*Envelope[A]) Clone

func (e *Envelope[A]) Clone() *Envelope[A]

func (*Envelope[A]) Deserialize

func (e *Envelope[A]) Deserialize(data []byte) error

func (*Envelope[A]) IsSigned

func (e *Envelope[A]) IsSigned() bool

func (*Envelope[A]) Reply

func (e *Envelope[A]) Reply() *Envelope[A]

Reply crafts an Envelope whose recipient is the sender, and whose threadId points back to the original

func (*Envelope[A]) Serialize

func (e *Envelope[A]) Serialize() ([]byte, error)

func (*Envelope[A]) SetRecipient

func (e *Envelope[A]) SetRecipient(p *Peer[A])

func (*Envelope[A]) String

func (e *Envelope[A]) String() string

func (*Envelope[A]) Subject

func (e *Envelope[A]) Subject(str subj.Subject)

Subject sets the subject of the embedded delphi.Message, and uppercases it.

type Mailer

type Mailer interface {
	Send(envelope Envelope[Addresser]) error
	SendEphemeral(envelope Envelope[Addresser]) error
	Receive() chan Envelope[Addresser]
}

A Mailer can route [Envelope]s to a destination and receive them too.

type MessageId

type MessageId uuid.UUID

func NewMessageId

func NewMessageId() *MessageId

func (MessageId) String

func (m MessageId) String() string

type Peer

type Peer[A Addresser] struct {
	*goracle.Peer `json:"goracle"`
	Addr          A `json:"net"`
}

A Peer is a public key, some arbitrary key-value pairs, and an address on network N

func NewPeer

func NewPeer[A Addresser]() *Peer[A]

func PeerFromString

func PeerFromString[A Addresser](h string, addr A) (*Peer[A], error)

func (*Peer[A]) Decompose

func (p *Peer[A]) Decompose() (delphi.Key, PeerInfo[A])

func (*Peer[A]) MarshalBinary

func (p *Peer[A]) MarshalBinary() ([]byte, error)

func (*Peer[A]) MarshalJSON

func (p *Peer[A]) MarshalJSON() ([]byte, error)

func (*Peer[A]) MarshalPEM

func (p *Peer[A]) MarshalPEM() (*pem.Block, error)

func (*Peer[A]) PublicKey

func (p *Peer[A]) PublicKey() delphi.Key

func (*Peer[A]) String

func (p *Peer[A]) String() string

func (*Peer[A]) UnmarshalBinary

func (p *Peer[A]) UnmarshalBinary(data []byte) error

func (*Peer[A]) UnmarshalJSON

func (p *Peer[A]) UnmarshalJSON(data []byte) error

type PeerInfo

type PeerInfo[A Addresser] struct {
	IsAlive   bool
	Closeness int
	Trust     int
	Addr      A
	Props     *stablemap.StableMap[string, string]
}

PeerInfo contains everything that can be known about a Peer except for its pubKey, which is kept elsewhere.

func NewPeerInfo

func NewPeerInfo[A Addresser]() PeerInfo[A]

func (*PeerInfo[A]) ToPeer

func (info *PeerInfo[A]) ToPeer(pub delphi.Key) *Peer[A]

type PeerMap

type PeerMap[A Addresser] map[delphi.Key]*Peer[A]

type Principal

type Principal[A AddressConnector] struct {
	*goracle.Principal
	Net A

	Peers   *stablemap.ActiveMap[delphi.Key, PeerInfo[A]]
	Slogger *slog.Logger
	Logger  *log.Logger
	// contains filtered or unexported fields
}

A Principal is an entity (node) in the graph (cluster) that can: - send messages - listen for messages - encrypt, decrypt, sign, and verify signatures - keep track of [Peer]s, which represent "friends" - read and write to a [KnowledgeBase] containing knowledge of the graph

func NewPrincipal

func NewPrincipal[A AddressConnector](rand io.Reader, network A, opts ...PrincipalOption[A]) (*Principal[A], error)

func PrincipalFromFile

func PrincipalFromFile[A AddressConnector](s string, network A) (*Principal[A], error)

func PrincipalFromPEM

func PrincipalFromPEM[A AddressConnector](data []byte, network A) (*Principal[A], error)

func PrincipalFromPEMBlock

func PrincipalFromPEMBlock[A AddressConnector](block *pem.Block, network A) (*Principal[A], error)

func (*Principal[A]) AddPeer

func (p *Principal[A]) AddPeer(peer *Peer[A]) error

func (*Principal[A]) AllPeers

func (p *Principal[A]) AllPeers() []*Peer[A]

func (*Principal[A]) AsPeer

func (p *Principal[A]) AsPeer() *Peer[A]

func (*Principal[A]) Broadcast

func (p *Principal[A]) Broadcast(e *Envelope[A])

func (*Principal[A]) BroadcastParallel

func (p *Principal[A]) BroadcastParallel(e *Envelope[A])

func (*Principal[A]) Compose

func (p *Principal[A]) Compose(body []byte, recipient *Peer[A], thread *MessageId) *Envelope[A]

Compose a delphi.Message, wrapped in an Envelope, addressed to a particular Peer.

func (*Principal[A]) Connect

func (p *Principal[A]) Connect() error

Connect acquires an address and starts listening on it. After doing so, a node will probably want to advertise itself. It will probably also want to process incoming data using [Inbox].

func (*Principal[A]) Disconnect

func (p *Principal[A]) Disconnect() error

func (*Principal[A]) EachPeer

func (p *Principal[A]) EachPeer() iter.Seq[*Peer[A]]

func (*Principal[A]) Inbox

func (p *Principal[A]) Inbox() chan Envelope[A]

func (*Principal[A]) MarshalPEM

func (p *Principal[A]) MarshalPEM() (*pem.Block, error)

func (*Principal[A]) Send

func (p *Principal[A]) Send(e *Envelope[A]) (int, error)

func (*Principal[A]) SetPeerAliveness

func (p *Principal[A]) SetPeerAliveness(peer *Peer[A], val bool) error

func (*Principal[A]) Shutdown

func (p *Principal[A]) Shutdown()

func (*Principal[A]) UnmarshalPEM

func (p *Principal[A]) UnmarshalPEM(data []byte, network A) error

func (*Principal[A]) UnmarshalPEMBlock

func (p *Principal[A]) UnmarshalPEMBlock(block *pem.Block, network A) error

func (*Principal[AddressConnector]) With

func (p *Principal[AddressConnector]) With(fn PrincipalOption[AddressConnector])

type PrincipalOption

type PrincipalOption[A AddressConnector] func(principal *Principal[A])

func WithLogger

func WithLogger[A AddressConnector](log *log.Logger) PrincipalOption[A]

func WithSlogger

func WithSlogger[A AddressConnector](slog *slog.Logger) PrincipalOption[A]

type UDPAddr

type UDPAddr struct {
	*net.UDPAddr
}

func (*UDPAddr) Equal

func (u *UDPAddr) Equal(x Address) bool

func (*UDPAddr) MarshalJSON

func (u *UDPAddr) MarshalJSON() ([]byte, error)

func (*UDPAddr) UnmarshalJSON

func (u *UDPAddr) UnmarshalJSON(data []byte) error

Directories

Path Synopsis
cmd
polity command
polityctl command
polityd command