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"
|
2022-12-21 18:47:43 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/payload"
|
2023-08-16 12:26:57 +00:00
|
|
|
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
2024-05-16 22:06:39 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2023-10-30 16:30:25 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2023-11-07 19:48:43 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-06-10 12:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("filter2")
|
|
|
|
|
2023-10-30 14:56:26 +00:00
|
|
|
var pubSubTopic = protocol.DefaultPubsubTopic{}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2023-09-20 05:56:55 +00:00
|
|
|
const contentTopic = "/filter2test/1/testTopic/proto"
|
2021-06-10 12:59:51 +00:00
|
|
|
|
|
|
|
func main() {
|
2023-01-06 22:37:57 +00:00
|
|
|
hostAddr1, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:60000")
|
|
|
|
hostAddr2, _ := net.ResolveTCPAddr("tcp", "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()
|
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
fullNode, err := node.New(
|
2021-06-10 12:59:51 +00:00
|
|
|
node.WithPrivateKey(prvKey1),
|
2021-11-17 16:19:42 +00:00
|
|
|
node.WithHostAddress(hostAddr1),
|
2021-06-10 12:59:51 +00:00
|
|
|
node.WithWakuRelay(),
|
2023-04-11 14:38:16 +00:00
|
|
|
node.WithWakuFilterFullNode(),
|
2024-05-16 22:06:39 +00:00
|
|
|
node.WithLightPush(lightpush.WithRateLimiter(1, 1)),
|
2021-06-10 12:59:51 +00:00
|
|
|
)
|
2023-01-06 22:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
err = fullNode.Start(ctx)
|
2021-10-06 15:34:39 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
lightNode, err := node.New(
|
2021-06-10 12:59:51 +00:00
|
|
|
node.WithPrivateKey(prvKey2),
|
2021-11-17 16:19:42 +00:00
|
|
|
node.WithHostAddress(hostAddr2),
|
2023-04-11 14:38:16 +00:00
|
|
|
node.WithWakuFilterLightNode(),
|
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
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
err = lightNode.Start(ctx)
|
2021-10-05 02:13:54 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
//
|
|
|
|
// Setup filter
|
|
|
|
//
|
|
|
|
|
2023-09-14 15:00:06 +00:00
|
|
|
_, err = lightNode.AddPeer(fullNode.ListenAddresses()[0], wps.Static,
|
|
|
|
[]string{pubSubTopic.String()}, filter.FilterSubscribeID_v20beta1)
|
2023-05-08 18:12:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Info("Error adding filter peer on light node ", err)
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:59:51 +00:00
|
|
|
// Send FilterRequest from light node to full node
|
2023-09-29 05:13:25 +00:00
|
|
|
cf := protocol.ContentFilter{
|
2023-10-30 16:30:25 +00:00
|
|
|
PubsubTopic: relay.DefaultWakuTopic,
|
2023-09-29 05:13:25 +00:00
|
|
|
ContentTopics: protocol.NewContentTopicSet(contentTopic),
|
2021-10-18 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:16 +00:00
|
|
|
theFilter, err := lightNode.FilterLightnode().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() {
|
2023-09-20 05:56:55 +00:00
|
|
|
for env := range theFilter[0].C { //Safely picking first subscriptions since only 1 contentTopic is subscribed
|
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)
|
2024-05-16 22:06:39 +00:00
|
|
|
go writeLightpushLoop(ctx, lightNode)
|
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)
|
2023-04-11 14:38:16 +00:00
|
|
|
lightNode.FilterLightnode().Unsubscribe(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) {
|
2024-05-16 22:06:39 +00:00
|
|
|
msg := createMessage(wakuNode, msgContent)
|
|
|
|
|
|
|
|
_, err := wakuNode.Relay().Publish(ctx, msg, relay.WithPubSubTopic(pubSubTopic.String()))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error sending a message: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeLightpush(ctx context.Context, wakuNode *node.WakuNode, msgContent string) {
|
|
|
|
msg := createMessage(wakuNode, msgContent)
|
|
|
|
|
|
|
|
_, err := wakuNode.Lightpush().Publish(ctx, msg, lightpush.WithPubSubTopic(pubSubTopic.String()))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error sending a LP message: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMessage(wakuNode *node.WakuNode, msgContent string) *pb.WakuMessage {
|
2021-06-10 12:59:51 +00:00
|
|
|
var version uint32 = 0
|
|
|
|
|
2022-12-21 18:47:43 +00:00
|
|
|
p := new(payload.Payload)
|
2021-06-10 12:59:51 +00:00
|
|
|
p.Data = []byte(wakuNode.ID() + ": " + msgContent)
|
2022-12-21 18:47:43 +00:00
|
|
|
p.Key = &payload.KeyInfo{Kind: payload.None}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
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,
|
2023-11-07 19:48:43 +00:00
|
|
|
Version: proto.Uint32(version),
|
2021-06-10 12:59:51 +00:00
|
|
|
ContentTopic: contentTopic,
|
2023-11-07 19:48:43 +00:00
|
|
|
Timestamp: utils.GetUnixEpoch(wakuNode.Timesource()),
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 22:06:39 +00:00
|
|
|
return msg
|
2021-06-10 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeLoop(ctx context.Context, wakuNode *node.WakuNode) {
|
|
|
|
for {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
write(ctx, wakuNode, "Hello world!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 22:06:39 +00:00
|
|
|
func writeLightpushLoop(ctx context.Context, wakuNode *node.WakuNode) {
|
|
|
|
for {
|
|
|
|
//Change this sleep value to lower than 1 second to get Lightpush rate limiting kick in
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
writeLightpush(ctx, wakuNode, "Hello World via Lightpush!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2023-10-20 19:56:18 +00:00
|
|
|
sub, err := wakuNode.Relay().Subscribe(ctx, protocol.NewContentFilter(pubsubTopic))
|
2021-06-10 12:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not subscribe: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-20 19:56:18 +00:00
|
|
|
for value := range sub[0].Ch {
|
2022-12-21 18:47:43 +00:00
|
|
|
payload, err := payload.DecodePayload(value.Message(), &payload.KeyInfo{Kind: payload.None})
|
2021-06-10 12:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Received msg, ", string(payload.Data))
|
|
|
|
}
|
|
|
|
}
|