From 63d6c72bc0eb19f033d36094b2dbeb41b01fcbe9 Mon Sep 17 00:00:00 2001 From: p4u Date: Mon, 25 Feb 2019 15:30:01 +0100 Subject: [PATCH] Testing raw --- cmd/pssTest/psstest.go | 8 +++++--- net/swarm/swarm.go | 35 ++++++----------------------------- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/cmd/pssTest/psstest.go b/cmd/pssTest/psstest.go index 3384b5d..b67b4c3 100644 --- a/cmd/pssTest/psstest.go +++ b/cmd/pssTest/psstest.go @@ -28,10 +28,12 @@ func main() { kind := os.Args[1] topic := "vocdoni_test" - var key string + key := "" - if kind == "sym" || kind == "asym" { - key = os.Args[2] + if kind == "sym" || kind == "asym" || kind == "raw" { + if kind != "raw" { + key = os.Args[2] + } sn.PssSub(kind, key, topic, "") defer sn.PssTopics[topic].Unregister() } else { diff --git a/net/swarm/swarm.go b/net/swarm/swarm.go index 8929a4b..031ed63 100644 --- a/net/swarm/swarm.go +++ b/net/swarm/swarm.go @@ -298,27 +298,21 @@ func (sn *SwarmNet) PssPub(subType, key, topic, msg, address string) error { if err != nil { return err } + // send symetric message err = sn.Pss.SendSym(symKeyId, strTopic(topic), hexutil.Bytes(msg)) } if subType == "raw" { err = sn.Pss.SendRaw(hexutil.Bytes(dstAddr), dstTopic, hexutil.Bytes(msg)) } if subType == "asym" { + // add 0x prefix if not present if hasHexPrefix := strings.HasPrefix(key, "0x"); !hasHexPrefix { key = "0x" + key } - topics, addresses, err := sn.Pss.GetPublickeyPeers(key) + + // check if topic+address is already set for a pubKey + _, err := sn.Pss.GetPeerAddress(key, dstTopic) if err != nil { - return err - } - topicFound := false - for i, t := range topics { - if dstTopic == t && fmt.Sprintf("%x", addresses[i]) == fmt.Sprintf("%x", dstAddr) { - topicFound = true - break - } - } - if !topicFound { pubKeyBytes, err := hexutil.Decode(key) if err != nil { return err @@ -329,25 +323,8 @@ func (sn *SwarmNet) PssPub(subType, key, topic, msg, address string) error { } } + // send asymetric message err = sn.Pss.SendAsym(key, dstTopic, hexutil.Bytes(msg)) } return err } - -func (sn *SwarmNet) Test() error { - sn.PssSub("sym", "vocdoni", "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", "vocdoni", "vocdoni_test", fmt.Sprintf("Hello world from %s", hostname), "") - log.Info("pss sent", "err", err) - time.Sleep(10 * time.Second) - } -}