2021-06-10 12:59:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2022-11-02 10:39:13 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2021-06-10 12:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("filter2")
|
|
|
|
|
2021-11-10 14:28:45 +00:00
|
|
|
var pubSubTopic = protocol.DefaultPubsubTopic()
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
const contentTopic = "test"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
lvl, err := logging.LevelFromString("info")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
logging.SetAllLoggers(lvl)
|
|
|
|
|
2021-06-16 10:14:22 +00:00
|
|
|
hostAddr1, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:60000"))
|
|
|
|
hostAddr2, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:60001"))
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
key1, err := randomHex(32)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not generate random key")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prvKey1, err := crypto.HexToECDSA(key1)
|
2021-11-06 13:20:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Invalid key")
|
|
|
|
return
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
key2, err := randomHex(32)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not generate random key")
|
|
|
|
return
|
|
|
|
}
|
2021-11-06 13:20:01 +00:00
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
prvKey2, err := crypto.HexToECDSA(key2)
|
2021-11-06 13:20:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Invalid key")
|
|
|
|
return
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
fullNode, err := node.New(ctx,
|
|
|
|
node.WithPrivateKey(prvKey1),
|
2021-11-17 16:19:42 +00:00
|
|
|
node.WithHostAddress(hostAddr1),
|
2021-06-10 12:59:51 +00:00
|
|
|
node.WithWakuRelay(),
|
2021-10-30 14:29:34 +00:00
|
|
|
node.WithWakuFilter(true),
|
2021-06-10 12:59:51 +00:00
|
|
|
)
|
|
|
|
|
2021-10-06 15:34:39 +00:00
|
|
|
err = fullNode.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
lightNode, err := node.New(ctx,
|
|
|
|
node.WithPrivateKey(prvKey2),
|
2021-11-17 16:19:42 +00:00
|
|
|
node.WithHostAddress(hostAddr2),
|
2021-10-30 14:29:34 +00:00
|
|
|
node.WithWakuFilter(false),
|
2021-06-10 12:59:51 +00:00
|
|
|
)
|
2021-11-06 13:20:01 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
_, err = lightNode.AddPeer(fullNode.ListenAddresses()[0], string(filter.FilterID_v20beta1))
|
2021-06-10 12:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Info("Error adding filter peer on light node ", err)
|
|
|
|
}
|
2021-10-05 02:13:54 +00:00
|
|
|
|
|
|
|
err = lightNode.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
//
|
|
|
|
// Setup filter
|
|
|
|
//
|
|
|
|
|
|
|
|
// Send FilterRequest from light node to full node
|
2021-10-18 12:25:55 +00:00
|
|
|
cf := filter.ContentFilter{
|
2021-11-10 14:28:45 +00:00
|
|
|
Topic: pubSubTopic.String(),
|
2021-10-18 12:25:55 +00:00
|
|
|
ContentTopics: []string{contentTopic},
|
|
|
|
}
|
|
|
|
|
2021-11-10 14:28:45 +00:00
|
|
|
_, theFilter, err := lightNode.Filter().Subscribe(ctx, cf)
|
2021-10-11 22:45:54 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-11-10 14:28:45 +00:00
|
|
|
for env := range theFilter.Chan {
|
2021-06-28 14:14:28 +00:00
|
|
|
log.Info("Light node received msg, ", string(env.Message().Payload))
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
2021-10-11 22:45:54 +00:00
|
|
|
log.Info("Message channel closed!")
|
2021-06-10 12:59:51 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go writeLoop(ctx, fullNode)
|
2021-10-10 22:44:07 +00:00
|
|
|
go readLoop(ctx, fullNode)
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-06-14 12:54:50 +00:00
|
|
|
go func() {
|
|
|
|
// Unsubscribe filter after 5 seconds
|
|
|
|
time.Sleep(5 * time.Second)
|
2021-11-10 14:28:45 +00:00
|
|
|
lightNode.Filter().UnsubscribeFilter(ctx, cf)
|
2021-06-14 12:54:50 +00:00
|
|
|
}()
|
2021-06-10 12:59:51 +00:00
|
|
|
// Wait for a SIGINT or SIGTERM signal
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
|
|
|
fmt.Println("\n\n\nReceived signal, shutting down...")
|
|
|
|
|
|
|
|
// shut the nodes down
|
|
|
|
fullNode.Stop()
|
|
|
|
lightNode.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomHex(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) {
|
|
|
|
var version uint32 = 0
|
2022-12-09 03:08:04 +00:00
|
|
|
var timestamp int64 = utils.GetUnixEpoch(wakuNode.Timesource())
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
p := new(node.Payload)
|
|
|
|
p.Data = []byte(wakuNode.ID() + ": " + msgContent)
|
|
|
|
p.Key = &node.KeyInfo{Kind: node.None}
|
|
|
|
|
2021-10-10 22:44:07 +00:00
|
|
|
payload, _ := p.Encode(version)
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
msg := &pb.WakuMessage{
|
|
|
|
Payload: payload,
|
|
|
|
Version: version,
|
|
|
|
ContentTopic: contentTopic,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
|
|
|
|
2021-11-19 20:01:52 +00:00
|
|
|
_, err := wakuNode.Relay().Publish(ctx, msg)
|
2021-06-10 12:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error sending a message: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeLoop(ctx context.Context, wakuNode *node.WakuNode) {
|
|
|
|
for {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
write(ctx, wakuNode, "Hello world!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 22:44:07 +00:00
|
|
|
func readLoop(ctx context.Context, wakuNode *node.WakuNode) {
|
2021-11-19 16:19:48 +00:00
|
|
|
pubsubTopic := pubSubTopic.String()
|
2021-11-20 00:03:05 +00:00
|
|
|
sub, err := wakuNode.Relay().SubscribeToTopic(ctx, pubsubTopic)
|
2021-06-10 12:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not subscribe: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for value := range sub.C {
|
|
|
|
payload, err := node.DecodePayload(value.Message(), &node.KeyInfo{Kind: node.None})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Received msg, ", string(payload.Data))
|
|
|
|
}
|
|
|
|
}
|