Add first asym implementation Move test code to pssTest.go Minor changes
p4u p4u@dabax.net
Fri, 22 Feb 2019 21:37:35 +0100
2 files changed,
37 insertions(+),
6 deletions(-)
M
cmd/pssTest/psstest.go
→
cmd/pssTest/psstest.go
@@ -2,7 +2,10 @@ package main
import ( "fmt" + "os" + "time" + "github.com/ethereum/go-ethereum/log" swarm "github.com/vocdoni/go-dvote/net/swarm" )@@ -13,5 +16,20 @@ if err != nil {
fmt.Printf("%v\n", err) return } - sn.Test() + key := "randomkey0" + sn.PssSub("sym", key, "vocdoni_test", "") + + go func() { + for { + msg := <-sn.PssTopics["vocdoni_test"].Delivery + fmt.Printf("Pss received: %s\n", msg) + } + }() + + hostname, _ := os.Hostname() + for { + err := sn.PssPub("sym", key, "vocdoni_test", fmt.Sprintf("Hello world from %s", hostname), "") + log.Info("pss sent", "err", err) + time.Sleep(10 * time.Second) + } }
M
net/swarm/swarm.go
→
net/swarm/swarm.go
@@ -11,6 +11,7 @@
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/log"@@ -89,7 +90,6 @@ log.Crit("cannot crate swarm node")
} // register swarm service to the node var swarmService node.ServiceConstructor = func(ctx *node.ServiceContext) (node.Service, error) { - //return swarm.NewSwarm(swarmCfg, nil) return swarmNode, nil } return swarmNode, swarmCfg, swarmService@@ -124,6 +124,7 @@ EnodeID string
Datadir string Key *ecdsa.PrivateKey Pss *pss.API + PssPubKey string PssAddr pss.PssAddress PssTopics map[string]*pssSub Hive *network.Hive@@ -204,6 +205,7 @@ node, _ := enode.ParseV4(url)
sn.Node.Server().AddPeer(node) } + // wait to connect to the p2p network _, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() time.Sleep(time.Second * 5)@@ -221,15 +223,19 @@
// Create topics map sn.PssTopics = make(map[string]*pssSub) - // Set the enode ID and the pss Address, fail if not available + // Set some extra data sn.EnodeID = sn.Node.Server().NodeInfo().Enode - + sn.PssPubKey = hexutil.Encode(crypto.FromECDSAPub(sn.Pss.PublicKey())) sn.PssAddr, err = sn.Pss.BaseAddr() if err != nil { return fmt.Errorf("pss API fail %v", err) } + + // Print some information + log.Info(fmt.Sprintf("My PSS pubkey is %s", sn.PssPubKey)) log.Info(fmt.Sprintf("My PSS address is %x", sn.PssAddr)) + // Run statistics goroutine sn.PrintStats() return nil@@ -278,7 +284,7 @@ log.Info(fmt.Sprintf("Subscribed to topic %s", pssTopic.String()))
return nil } -func (sn *SwarmNet) PssSend(subType, key, topic, msg, address string) error { +func (sn *SwarmNet) PssPub(subType, key, topic, msg, address string) error { var err error dstAddr := strAddress(address) dstTopic := strTopic(topic)@@ -292,6 +298,13 @@ }
if subType == "raw" { err = sn.Pss.SendRaw(hexutil.Bytes(dstAddr), dstTopic, hexutil.Bytes(msg)) } + if subType == "asym" { + err = sn.Pss.SetPeerPublicKey(hexutil.Bytes(key), dstTopic, dstAddr) + if err != nil { + return err + } + err = sn.Pss.SendAsym(key, dstTopic, hexutil.Bytes(msg)) + } return err }@@ -307,7 +320,7 @@ }()
hostname, _ := os.Hostname() for { - err := sn.PssSend("sym", "vocdoni", "vocdoni_test", fmt.Sprintf("Hello world from %s", hostname), "") + err := sn.PssPub("sym", "vocdoni", "vocdoni_test", fmt.Sprintf("Hello world from %s", hostname), "") log.Info("pss sent", "err", err) time.Sleep(10 * time.Second) }