Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not merge] msg refactor in progress #9

Open
wants to merge 7 commits into
base: issue-1703
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pss/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Pss provides devp2p functionality for swarm nodes without the need for a direct tcp connection between them.

Messages are encapsulated in a devp2p message structure `PssMsg`. These capsules are forwarded from node to node using ordinary tcp devp2p until they reach their destination: The node or nodes who can successfully decrypt the message.
Messages are encapsulated in a devp2p message structure `message.Message`. These capsules are forwarded from node to node using ordinary tcp devp2p until they reach their destination: The node or nodes who can successfully decrypt the message.

| Layer | Contents |
|-----------|-----------------|
Expand Down
29 changes: 15 additions & 14 deletions pss/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethersphere/swarm/log"
"github.com/ethersphere/swarm/pss/message"
)

// Wrapper for receiving pss messages when using the pss API
Expand All @@ -50,7 +51,7 @@ func NewAPI(ps *Pss) *API {
//
// All incoming messages to the node matching this topic will be encapsulated in the APIMsg
// struct and sent to the subscriber
func (pssapi *API) Receive(ctx context.Context, topic Topic, raw bool, prox bool) (*rpc.Subscription, error) {
func (pssapi *API) Receive(ctx context.Context, topic message.Topic, raw bool, prox bool) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return nil, fmt.Errorf("Subscribe not supported")
Expand Down Expand Up @@ -90,7 +91,7 @@ func (pssapi *API) Receive(ctx context.Context, topic Topic, raw bool, prox bool
return psssub, nil
}

func (pssapi *API) GetAddress(topic Topic, asymmetric bool, key string) (PssAddress, error) {
func (pssapi *API) GetAddress(topic message.Topic, asymmetric bool, key string) (PssAddress, error) {
var addr PssAddress
if asymmetric {
peer, ok := pssapi.Pss.pubKeyPool[key][topic]
Expand All @@ -117,13 +118,13 @@ func (pssapi *API) BaseAddr() (PssAddress, error) {
// Retrieves the node's public key in hex form
func (pssapi *API) GetPublicKey() (keybytes hexutil.Bytes) {
key := pssapi.Pss.PublicKey()
keybytes = pssapi.Pss.Crypto.FromECDSAPub(key)
keybytes = pssapi.Pss.Crypto.SerializePublicKey(key)
return keybytes
}

// Set Public key to associate with a particular Pss peer
func (pssapi *API) SetPeerPublicKey(pubkey hexutil.Bytes, topic Topic, addr PssAddress) error {
pk, err := pssapi.Pss.Crypto.UnmarshalPubkey(pubkey)
func (pssapi *API) SetPeerPublicKey(pubkey hexutil.Bytes, topic message.Topic, addr PssAddress) error {
pk, err := pssapi.Pss.Crypto.UnmarshalPublicKey(pubkey)
if err != nil {
return fmt.Errorf("Cannot unmarshal pubkey: %x", pubkey)
}
Expand All @@ -139,50 +140,50 @@ func (pssapi *API) GetSymmetricKey(symkeyid string) (hexutil.Bytes, error) {
return hexutil.Bytes(symkey), err
}

func (pssapi *API) GetSymmetricAddressHint(topic Topic, symkeyid string) (PssAddress, error) {
func (pssapi *API) GetSymmetricAddressHint(topic message.Topic, symkeyid string) (PssAddress, error) {
return pssapi.Pss.symKeyPool[symkeyid][topic].address, nil
}

func (pssapi *API) GetAsymmetricAddressHint(topic Topic, pubkeyid string) (PssAddress, error) {
func (pssapi *API) GetAsymmetricAddressHint(topic message.Topic, pubkeyid string) (PssAddress, error) {
return pssapi.Pss.pubKeyPool[pubkeyid][topic].address, nil
}

func (pssapi *API) StringToTopic(topicstring string) (Topic, error) {
topicbytes := BytesToTopic([]byte(topicstring))
func (pssapi *API) StringToTopic(topicstring string) (message.Topic, error) {
topicbytes := message.NewTopic([]byte(topicstring))
if topicbytes == rawTopic {
return rawTopic, errors.New("Topic string hashes to 0x00000000 and cannot be used")
}
return topicbytes, nil
}

func (pssapi *API) SendAsym(pubkeyhex string, topic Topic, msg hexutil.Bytes) error {
func (pssapi *API) SendAsym(pubkeyhex string, topic message.Topic, msg hexutil.Bytes) error {
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendAsym(pubkeyhex, topic, msg[:])
}

func (pssapi *API) SendSym(symkeyhex string, topic Topic, msg hexutil.Bytes) error {
func (pssapi *API) SendSym(symkeyhex string, topic message.Topic, msg hexutil.Bytes) error {
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendSym(symkeyhex, topic, msg[:])
}

func (pssapi *API) SendRaw(addr hexutil.Bytes, topic Topic, msg hexutil.Bytes) error {
func (pssapi *API) SendRaw(addr hexutil.Bytes, topic message.Topic, msg hexutil.Bytes) error {
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendRaw(PssAddress(addr), topic, msg[:])
}

func (pssapi *API) GetPeerTopics(pubkeyhex string) ([]Topic, error) {
func (pssapi *API) GetPeerTopics(pubkeyhex string) ([]message.Topic, error) {
topics, _, err := pssapi.Pss.GetPublickeyPeers(pubkeyhex)
return topics, err

}

func (pssapi *API) GetPeerAddress(pubkeyhex string, topic Topic) (PssAddress, error) {
func (pssapi *API) GetPeerAddress(pubkeyhex string, topic message.Topic) (PssAddress, error) {
return pssapi.Pss.getPeerAddress(pubkeyhex, topic)
}

Expand Down
13 changes: 7 additions & 6 deletions pss/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethersphere/swarm/log"
"github.com/ethersphere/swarm/p2p/protocols"
"github.com/ethersphere/swarm/pss"
"github.com/ethersphere/swarm/pss/message"
)

const (
Expand All @@ -46,8 +47,8 @@ type Client struct {
BaseAddrHex string

// peers
peerPool map[pss.Topic]map[string]*pssRPCRW
protos map[pss.Topic]*p2p.Protocol
peerPool map[message.Topic]map[string]*pssRPCRW
protos map[message.Topic]*p2p.Protocol

// rpc connections
rpc *rpc.Client
Expand All @@ -71,7 +72,7 @@ type pssRPCRW struct {
closed bool
}

func (c *Client) newpssRPCRW(pubkeyid string, addr pss.PssAddress, topicobj pss.Topic) (*pssRPCRW, error) {
func (c *Client) newpssRPCRW(pubkeyid string, addr pss.PssAddress, topicobj message.Topic) (*pssRPCRW, error) {
topic := topicobj.String()
err := c.rpc.Call(nil, "pss_setPeerPublicKey", pubkeyid, topic, hexutil.Encode(addr[:]))
if err != nil {
Expand Down Expand Up @@ -218,8 +219,8 @@ func NewClientWithRPC(rpcclient *rpc.Client) (*Client, error) {
func newClient() (client *Client) {
client = &Client{
quitC: make(chan struct{}),
peerPool: make(map[pss.Topic]map[string]*pssRPCRW),
protos: make(map[pss.Topic]*p2p.Protocol),
peerPool: make(map[message.Topic]map[string]*pssRPCRW),
protos: make(map[message.Topic]*p2p.Protocol),
}
return
}
Expand All @@ -232,7 +233,7 @@ func newClient() (client *Client) {
// 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.
func (c *Client) RunProtocol(ctx context.Context, proto *p2p.Protocol) error {
topicobj := pss.BytesToTopic([]byte(fmt.Sprintf("%s:%d", proto.Name, proto.Version)))
topicobj := message.NewTopic([]byte(fmt.Sprintf("%s:%d", proto.Name, proto.Version)))
topichex := topicobj.String()
msgC := make(chan pss.APIMsg)
c.peerPool[topicobj] = make(map[string]*pssRPCRW)
Expand Down
12 changes: 2 additions & 10 deletions pss/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
Expand All @@ -45,7 +46,6 @@ type protoCtrl struct {
}

var (
cryptoUtils pss.CryptoUtils
// custom logging
psslogmain log.Logger
pssprotocols map[string]*protoCtrl
Expand All @@ -62,8 +62,6 @@ func init() {

psslogmain = log.New("psslog", "*")

cryptoUtils = pss.NewCryptoUtils()

pssprotocols = make(map[string]*protoCtrl)
}

Expand Down Expand Up @@ -231,13 +229,7 @@ func newServices() adapters.Services {
}
return adapters.Services{
"pss": func(ctx *adapters.ServiceContext) (node.Service, error) {
ctxlocal, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
keys, err := cryptoUtils.NewKeyPair(ctxlocal)
if err != nil {
return nil, err
}
privkey, err := cryptoUtils.GetPrivateKey(keys)
privkey, err := ethCrypto.GenerateKey()
if err != nil {
return nil, err
}
Expand Down
104 changes: 0 additions & 104 deletions pss/crypto.go

This file was deleted.

Loading