client

package
v3.6.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: GPL-3.0, LGPL-3.0, LGPL-3.0-or-later Imports: 13 Imported by: 0

Documentation

Overview

simple abstraction for implementing pss functionality

the pss client library aims to simplify usage of the p2p.protocols package over pss

IO is performed using the ordinary p2p.MsgReadWriter interface, which transparently communicates with a pss node via RPC using websockets as transport layer, using methods in the PssAPI class in the swarm/pss package

Minimal-ish usage example (requires a running pss node with websocket RPC):

  import (
 	"context"
 	"fmt"
 	"os"
 	pss "github.com/FusionFoundation/efsn/swarm/pss/client"
 	"github.com/FusionFoundation/efsn/p2p/protocols"
 	"github.com/FusionFoundation/efsn/p2p"
 	"github.com/FusionFoundation/efsn/swarm/pot"
 	"github.com/FusionFoundation/efsn/swarm/log"
 )

 type FooMsg struct {
 	Bar int
 }

 func fooHandler (msg interface{}) error {
 	foomsg, ok := msg.(*FooMsg)
 	if ok {
 		log.Debug("Yay, just got a message", "msg", foomsg)
 	}
 	return errors.New(fmt.Sprintf("Unknown message"))
 }

 spec := &protocols.Spec{
 	Name: "foo",
 	Version: 1,
 	MaxMsgSize: 1024,
 	Messages: []interface{}{
 		FooMsg{},
 	},
 }

 proto := &p2p.Protocol{
 	Name: spec.Name,
 	Version: spec.Version,
 	Length: uint64(len(spec.Messages)),
 	Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
 		pp := protocols.NewPeer(p, rw, spec)
 		return pp.Run(fooHandler)
 	},
 }

 func implementation() {
     cfg := pss.NewClientConfig()
     psc := pss.NewClient(context.Background(), nil, cfg)
     err := psc.Start()
     if err != nil {
     	log.Crit("can't start pss client")
     	os.Exit(1)
     }

	log.Debug("connected to pss node", "bzz addr", psc.BaseAddr)

     err = psc.RunProtocol(proto)
     if err != nil {
     	log.Crit("can't start protocol on pss websocket")
     	os.Exit(1)
     }

     addr := pot.RandomAddress() // should be a real address, of course
     psc.AddPssPeer(addr, spec)

     // use the protocol for something

     psc.Stop()
 }

BUG(test): TestIncoming test times out due to deadlock issues in the swarm hive

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	BaseAddrHex string
	// contains filtered or unexported fields
}

The pss client provides devp2p emulation over pss RPC API, giving access to pss methods from a different process

func NewClient

func NewClient(rpcurl string) (*Client, error)

Custom constructor

Provides direct access to the rpc object

func NewClientWithRPC

func NewClientWithRPC(rpcclient *rpc.Client) (*Client, error)

Main constructor

The 'rpcclient' parameter allows passing a in-memory rpc client to act as the remote websocket RPC.

func (*Client) AddPssPeer

func (c *Client) AddPssPeer(pubkeyid string, addr []byte, spec *protocols.Spec) error

Add a pss peer (public key) and run the protocol on it

client.RunProtocol with matching topic must have been run prior to adding the peer, or this method will return an error.

The key must exist in the key store of the pss node before the peer is added. The method will return an error if it is not.

func (*Client) Close

func (c *Client) Close() error

Always call this to ensure that we exit cleanly

func (*Client) RemovePssPeer

func (c *Client) RemovePssPeer(pubkeyid string, spec *protocols.Spec)

Remove a pss peer

TODO: underlying cleanup

func (*Client) RunProtocol

func (c *Client) RunProtocol(ctx context.Context, proto *p2p.Protocol) error

Mounts a new devp2p protcool on the pss connection

the protocol is aliased as a "pss topic" uses normal devp2p send and incoming message handler routines from the p2p/protocols package

when an incoming message is received from a peer that is not yet known to the client, this peer object is instantiated, and the protocol is run on it.

Notes

Bugs

  • TestIncoming test times out due to deadlock issues in the swarm hive

Jump to

Keyboard shortcuts

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