pastry

package module
v0.0.0-...-cb0b922 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2019 License: GPL-3.0 Imports: 15 Imported by: 0

README

Pastry GoDoc

The Pastry DHT written in Go. Written specifically for Pastry Search.

Status

Under development.

Example

package main

import (
	"context"
	"crypto/ed25519"
	"crypto/rand"
	"io"
	"log"
	
	"github.com/uhthomas/pastry"
	"golang.org/x/sync/errgroup"
)

func main() {
	// Generate key for node
	var seed [ed25519.SeedSize]byte
	if _, err := io.ReadFull(rand.Reader, seed[:]); err != nil {
		log.Fatal(err)
	}
	
	n, err := pastry.New(
		// Pass logger to node
		pastry.DebugLogger,
		// Pass ed25519 seed to node
		pastry.Seed(seed[:]),
		// Use a forwarding func to log forwarded requests or modify next
		pastry.Forward(pastry.ForwarderFunc(func(ctx context.Context, next, key []byte, r io.Reader) error {
			// message <key> with <b> is being forwarded to <next>
			return nil
		})),
		// Handle received messages
		pastry.Deliver(pastry.DelivererFunc(func(ctx context.Context, key []byte, r io.Reader) error {
		        return nil
		})),
	)
	if err != nil {
		log.Fatal(err)
	}
	
	g, ctx := errgroup.WithContext(context.Background())
	
	// Connect to another node -- bootstrap 
	g.Go(func() error { return n.DialAndAccept(ctx, "localhost:1234") })
	
	// Listen for other nodes
	g.Go(func() error { return n.ListenAndServe(ctx, "localhost") })
	
	if err := g.Wait(); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

View Source
const B = 4

Variables

This section is empty.

Functions

func DebugLogger

func DebugLogger(n *Node) error

func DiscardLogger

func DiscardLogger(n *Node) error

func RandomSeed

func RandomSeed(n *Node) error

Types

type Deliverer

type Deliverer interface {
	Deliver(ctx context.Context, key []byte, r io.Reader) error
}

Deliverer will be called when the node is the closest the message can be routed to.

type DelivererFunc

type DelivererFunc func(ctx context.Context, key []byte, r io.Reader) error

DelivererFunc is an adapter to allow the use of ordinary functions as Deliverers.

func (DelivererFunc) Deliver

func (f DelivererFunc) Deliver(ctx context.Context, key []byte, r io.Reader) error

Deliver calls f(key, b)

type Forwarder

type Forwarder interface {
	Forward(ctx context.Context, next, key []byte, r io.Reader) error
}

Forwarder can modify the contents of redirect the message elsewhere or set next to nil to deliver to itself.

type ForwarderFunc

type ForwarderFunc func(ctx context.Context, next, key []byte, r io.Reader) error

ForwarderFunc is an adapter to allow the use of ordinary functions as Forwarders.

func (ForwarderFunc) Forward

func (f ForwarderFunc) Forward(ctx context.Context, next, key []byte, r io.Reader) error

Forward calls f(key, b, next)

type InvalidSignatureError

type InvalidSignatureError struct {
	PublicKey *[32]byte
	Signature [ed25519.SignatureSize]byte
}

func (InvalidSignatureError) Error

func (err InvalidSignatureError) Error() string

type LeafSet

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

func NewLeafSet

func NewLeafSet(n *Node) LeafSet

func (*LeafSet) Close

func (l *LeafSet) Close() error

func (*LeafSet) Closest

func (l *LeafSet) Closest(k []byte) *Peer

Closest will return the closest peer to the given key. If the key is equal to the parent then nil is returned.

func (*LeafSet) Insert

func (l *LeafSet) Insert(p *Peer) (ok bool)

func (*LeafSet) Remove

func (l *LeafSet) Remove(p *Peer) (ok bool)

type Message

type Message struct{ Key, Data []byte }

func NewMessage

func NewMessage(k, b []byte) *Message

type Node

type Node struct {
	Leafset LeafSet
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) (*Node, error)

func (*Node) Accept

func (n *Node) Accept(conn quic.Session, stream quic.Stream) (err error)

Accept takes the session and a pre-opened stream since we need to do the initial handshake. The only way to do that agnostically is to have a pre-opened stream.

func (*Node) Apply

func (n *Node) Apply(opts ...Option) error

func (*Node) Close

func (n *Node) Close() error

func (*Node) DialAndAccept

func (n *Node) DialAndAccept(ctx context.Context, address string) error

func (*Node) Handshake

func (n *Node) Handshake(
	w io.Writer,
	r, rand io.Reader,
) (
	publicKey [ed25519.PublicKeySize]byte,
	sharedKey [32]byte,
	err error,
)

Handshake will generate an ephemeral key-pair and will then send our handshake to w. We will then read the peer's handshake from r, verifying the signature and then generating the shared secret.

The handshake looks as follows: <-> [

[32] public key,
[32] ephemeral key public key,
[64] signature(private key, ephemeral public key),

]

func (*Node) ListenAndServe

func (n *Node) ListenAndServe(ctx context.Context, address string) error

func (*Node) PublicKey

func (n *Node) PublicKey() ed25519.PublicKey

func (*Node) Route

func (n *Node) Route(ctx context.Context, key []byte, r io.Reader) error

Send data to the node closest to the key.

func (*Node) Serve

func (n *Node) Serve(l quic.Listener) error

type Option

type Option func(*Node) error

func Deliver

func Deliver(d Deliverer) Option

func Forward

func Forward(f Forwarder) Option

func Logger

func Logger(l *log.Logger) Option

func Seed

func Seed(seed []byte) Option

type Peer

type Peer struct {
	PublicKey ed25519.PublicKey
	Node      *Node
	quic.Session
}

type Table

type Table [][15]uint8

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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