diff --git a/.gitignore b/.gitignore index c1161ea7..c8e4a809 100644 --- a/.gitignore +++ b/.gitignore @@ -26,8 +26,8 @@ pkg/* # output binaries go-waku -examples/basic2/basic2 -examples/filter2/filter2 +examples/basic-relay/build/basic-relay +examples/filter2/build/filter2 examples/noise/build/ examples/noise/noise diff --git a/Makefile b/Makefile index 71caed28..554461bd 100644 --- a/Makefile +++ b/Makefile @@ -112,8 +112,8 @@ docker-image: docker build --tag $(DOCKER_IMAGE_NAME) \ --build-arg="GIT_COMMIT=$(shell git rev-parse HEAD)" . -build-example-basic2: - cd examples/basic2 && $(MAKE) +build-example-basic-relay: + cd examples/basic-relay && $(MAKE) build-example-chat-2: cd examples/chat2 && $(MAKE) @@ -130,7 +130,7 @@ build-example-noise: build-example-rln: cd examples/rln && $(MAKE) -build-example: build-example-basic2 build-example-chat-2 build-example-filter2 build-example-c-bindings build-example-noise build-example-rln +build-example: build-example-basic-relay build-example-chat-2 build-example-filter2 build-example-c-bindings build-example-noise build-example-rln static-library: @echo "Building static library..." diff --git a/examples/basic-relay/Makefile b/examples/basic-relay/Makefile new file mode 100644 index 00000000..2c0fc33a --- /dev/null +++ b/examples/basic-relay/Makefile @@ -0,0 +1,6 @@ +.PHONY: all build + +build: + go build -o build/basic_relay . + +all: build \ No newline at end of file diff --git a/examples/basic-relay/README.md b/examples/basic-relay/README.md new file mode 100644 index 00000000..1ce7ade2 --- /dev/null +++ b/examples/basic-relay/README.md @@ -0,0 +1,35 @@ +# Using the `basic_relay` application + +## Background + +The `basic_relay` application is a basic example app that demonstrates how to subscribe to and publish messages using Waku relay. + +There are 2 ways of running the example.: +1. To work with the public Waku network in which case it uses the autosharding feature.This is the default way to run this. +2. To work with a custom Waku network which using static sharding. In this case a clusterID has to be specified. + +## Preparation +``` +make +``` + +## Basic application usage + +To start the `basic_relay` application run the following from the project directory + +``` +./build/basic_relay +``` + +The app will send a "Hello world!" through the wakurelay protocol every 2 seconds and display it on the terminal as soon as it receives the message. + +In order to run it with you own static sharded network, then run it as below + +``` +./build/basic_relay --cluster-id= --shard= +``` +e.g: ./build/basic_relay --cluster-id=2 --shard=1 // If you want to run with clusterID 2 and shard as 1 + +Cluster-id is a unique identifier for your own network and shard number is a segment/shard identifier of your network. + +Note that clusterID's 1 & 16 are reserved for the public Waku Network and Status repectively. \ No newline at end of file diff --git a/examples/basic2/build/.gitignore b/examples/basic-relay/build/.gitignore similarity index 100% rename from examples/basic2/build/.gitignore rename to examples/basic-relay/build/.gitignore diff --git a/examples/basic2/go.mod b/examples/basic-relay/go.mod similarity index 95% rename from examples/basic2/go.mod rename to examples/basic-relay/go.mod index 23d3f42a..85985130 100644 --- a/examples/basic2/go.mod +++ b/examples/basic-relay/go.mod @@ -8,12 +8,14 @@ replace github.com/ethereum/go-ethereum v1.10.26 => github.com/status-im/go-ethe require ( github.com/ethereum/go-ethereum v1.10.26 + github.com/urfave/cli/v2 v2.24.4 github.com/waku-org/go-waku v0.2.3-0.20221109195301-b2a5a68d28ba go.uber.org/zap v1.24.0 google.golang.org/protobuf v1.31.0 ) require ( + github.com/BurntSushi/toml v1.2.1 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/avast/retry-go/v4 v4.5.1 // indirect github.com/beevik/ntp v0.3.0 // indirect @@ -26,6 +28,7 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/containerd/cgroups v1.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/cruxic/go-hmac-drbg v0.0.0-20170206035330-84c46983886d // indirect github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect github.com/deckarep/golang-set v1.8.0 // indirect @@ -101,6 +104,7 @@ require ( github.com/quic-go/webtransport-go v0.5.3 // indirect github.com/raulk/go-watchdog v1.3.0 // indirect github.com/rjeczalik/notify v0.9.3 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/status-im/status-go/extkeys v1.1.2 // indirect @@ -114,6 +118,7 @@ require ( github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230916171929-1dd9494ff065 // indirect github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230916171518-2a77c3734dd1 // indirect github.com/wk8/go-ordered-map v1.0.0 // indirect + github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.uber.org/atomic v1.11.0 // indirect go.uber.org/dig v1.17.0 // indirect go.uber.org/fx v1.20.0 // indirect @@ -128,5 +133,6 @@ require ( golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect golang.org/x/tools v0.12.1-0.20230818130535-1517d1a3ba60 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/blake3 v1.2.1 // indirect ) diff --git a/examples/basic2/go.sum b/examples/basic-relay/go.sum similarity index 99% rename from examples/basic2/go.sum rename to examples/basic-relay/go.sum index 044c13a3..4881ad7b 100644 --- a/examples/basic2/go.sum +++ b/examples/basic-relay/go.sum @@ -39,6 +39,8 @@ github.com/Azure/go-autorest/autorest/mocks v0.3.0/go.mod h1:a8FDP3DYzQ4RYfVAxAN github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc= github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= +github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= @@ -119,6 +121,7 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cruxic/go-hmac-drbg v0.0.0-20170206035330-84c46983886d h1:bE1UyBQ5aE6FjhNY4lbPtMqh7VDldoVkvZMtFEbd+CE= github.com/cruxic/go-hmac-drbg v0.0.0-20170206035330-84c46983886d/go.mod h1:HAe1wsCrwH2uFnFaCC2vlcyEohnxs8KeShAFqGIHvmM= @@ -561,10 +564,10 @@ github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4Ug github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= -github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= @@ -633,10 +636,10 @@ github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefld github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs= -github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= github.com/urfave/cli/v2 v2.24.4 h1:0gyJJEBYtCV87zI/x2nZCPyDxD51K6xM8SkwjHFCNEU= +github.com/urfave/cli/v2 v2.24.4/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= @@ -659,6 +662,7 @@ github.com/wk8/go-ordered-map v1.0.0 h1:BV7z+2PaK8LTSd/mWgY12HyMAo5CEgkHqbkVq2th github.com/wk8/go-ordered-map v1.0.0/go.mod h1:9ZIbRunKbuvfPKyBP1SIKLcXNlv74YCOZ3t3VTS6gRk= github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= diff --git a/examples/basic2/main.go b/examples/basic-relay/main.go similarity index 64% rename from examples/basic2/main.go rename to examples/basic-relay/main.go index 5907de36..7821dcf9 100644 --- a/examples/basic2/main.go +++ b/examples/basic-relay/main.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "encoding/hex" + "errors" "fmt" "net" "os" @@ -12,6 +13,8 @@ import ( "time" "github.com/ethereum/go-ethereum/crypto" + cli "github.com/urfave/cli/v2" + "github.com/urfave/cli/v2/altsrc" "github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/payload" "github.com/waku-org/go-waku/waku/v2/protocol" @@ -22,25 +25,74 @@ import ( "google.golang.org/protobuf/proto" ) -var log = utils.Logger().Named("basic2") +var log = utils.Logger().Named("basic-relay") + +var ClusterID = altsrc.NewUintFlag(&cli.UintFlag{ + Name: "cluster-id", + Value: 1, + Usage: "Cluster id that the node is running in. Node in a different cluster id is disconnected.", + Destination: &clusterID, +}) + +var Shard = altsrc.NewUintFlag(&cli.UintFlag{ + Name: "shard", + Value: 0, + Usage: "shard that the node wants to subscribe and publish to.", + Destination: &shard, +}) + +var clusterID, shard uint +var pubsubTopicStr string func main() { - var cTopic, err = protocol.NewContentTopic("basic2", "1", "test", "proto") + + cliFlags := []cli.Flag{ + ClusterID, + Shard, + } + + app := &cli.App{ + Name: "basic-relay-example", + Flags: cliFlags, + Action: func(c *cli.Context) error { + err := Execute() + if err != nil { + utils.Logger().Error("failure while executing wakunode", zap.Error(err)) + switch e := err.(type) { + case cli.ExitCoder: + return e + case error: + return cli.Exit(err.Error(), 1) + } + } + return nil + }, + } + err := app.Run(os.Args) + if err != nil { + panic(err) + } + +} + +func Execute() error { + + var cTopic, err = protocol.NewContentTopic("basic-relay", "1", "test", "proto") if err != nil { fmt.Println("Invalid contentTopic") - return + return errors.New("invalid contentTopic") } contentTopic := cTopic.String() hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0") key, err := randomHex(32) if err != nil { log.Error("Could not generate random key", zap.Error(err)) - return + return err } prvKey, err := crypto.HexToECDSA(key) if err != nil { log.Error("Could not convert hex into ecdsa key", zap.Error(err)) - return + return err } ctx := context.Background() @@ -50,15 +102,22 @@ func main() { node.WithHostAddress(hostAddr), node.WithNTP(), node.WithWakuRelay(), + node.WithClusterID(uint16(clusterID)), ) if err != nil { log.Error("Error creating wakunode", zap.Error(err)) - return + return err } if err := wakuNode.Start(ctx); err != nil { log.Error("Error starting wakunode", zap.Error(err)) - return + return err + } + + //Populate pubsubTopic if shard is specified. Otherwise it is derived via autosharing algorithm + if shard != 0 { + pubsubTopic := protocol.NewStaticShardingPubsubTopic(uint16(clusterID), uint16(shard)) + pubsubTopicStr = pubsubTopic.String() } go writeLoop(ctx, wakuNode, contentTopic) @@ -72,7 +131,7 @@ func main() { // shut the node down wakuNode.Stop() - + return nil } func randomHex(n int) (string, error) { @@ -103,10 +162,11 @@ func write(ctx context.Context, wakuNode *node.WakuNode, contentTopic string, ms Timestamp: utils.GetUnixEpoch(wakuNode.Timesource()), } - _, err = wakuNode.Relay().Publish(ctx, msg, relay.WithDefaultPubsubTopic()) + _, err = wakuNode.Relay().Publish(ctx, msg, relay.WithPubSubTopic(pubsubTopicStr)) if err != nil { log.Error("Error sending a message", zap.Error(err)) } + log.Info("Published msg,", zap.String("data", string(msg.Payload))) } func writeLoop(ctx context.Context, wakuNode *node.WakuNode, contentTopic string) { @@ -117,7 +177,7 @@ func writeLoop(ctx context.Context, wakuNode *node.WakuNode, contentTopic string } func readLoop(ctx context.Context, wakuNode *node.WakuNode, contentTopic string) { - sub, err := wakuNode.Relay().Subscribe(ctx, protocol.NewContentFilter(relay.DefaultWakuTopic)) + sub, err := wakuNode.Relay().Subscribe(ctx, protocol.NewContentFilter(pubsubTopicStr, contentTopic)) if err != nil { log.Error("Could not subscribe", zap.Error(err)) return diff --git a/examples/basic2/Makefile b/examples/basic2/Makefile deleted file mode 100644 index 9d2eba0b..00000000 --- a/examples/basic2/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -.PHONY: all build - -build: - go build -o build/basic2 . - -all: build \ No newline at end of file diff --git a/examples/basic2/README.md b/examples/basic2/README.md deleted file mode 100644 index 6014b7dd..00000000 --- a/examples/basic2/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# Using the `basic2` application - -## Background - -The `basic2` application is a basic example app that demonstrates how to subscribe to and publish messages - -## Preparation -``` -make -``` - -## Basic application usage - -To start the `basic2` application run the following from the project directory - -``` -./build/basic2 -``` - -The app will send a "Hello world!" through the wakurelay protocol every 2 seconds and display it on the terminal as soon as it receives the message diff --git a/waku/v2/protocol/relay/waku_relay.go b/waku/v2/protocol/relay/waku_relay.go index 4492b3ca..060f161c 100644 --- a/waku/v2/protocol/relay/waku_relay.go +++ b/waku/v2/protocol/relay/waku_relay.go @@ -250,7 +250,7 @@ func (w *WakuRelay) subscribeToPubsubTopic(topic string) (*pubsubTopicSubscripti return result, nil } -// PublishToTopic is used to broadcast a WakuMessage to a pubsub topic. The pubsubTopic is derived from contentTopic +// Publish is used to broadcast a WakuMessage to a pubsub topic. The pubsubTopic is derived from contentTopic // specified in the message via autosharding. To publish to a specific pubsubTopic, the `WithPubSubTopic` option should // be provided func (w *WakuRelay) Publish(ctx context.Context, message *pb.WakuMessage, opts ...PublishOption) ([]byte, error) {