chore: example update (#1012)

This commit is contained in:
Prem Chaitanya Prathi 2024-02-01 15:38:01 +05:30 committed by GitHub
parent b647314846
commit 0c27742f67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 1495 additions and 7 deletions

View File

@ -31,6 +31,7 @@ jobs:
- 'go.mod' - 'go.mod'
- 'go.sum' - 'go.sum'
- 'flake.nix' - 'flake.nix'
- 'examples/**'
v2: v2:
- 'waku/**' - 'waku/**'
@ -165,8 +166,3 @@ jobs:
run: | run: |
docker compose -f .github/docker-compose/ganache.yml up -d docker compose -f .github/docker-compose/ganache.yml up -d
make test-onchain${{ matrix.tests == 'test-with-race' && '-with-race' || '' }} make test-onchain${{ matrix.tests == 'test-with-race' && '-with-race' || '' }}
- name: "Run tests with postgres"
run: |
docker compose -f .github/docker-compose/postgres.yml up -d
make test-postgres${{ matrix.tests == 'test-with-race' && '-with-race' || '' }}

View File

@ -0,0 +1,6 @@
.PHONY: all build
build:
go build -o build/basic_light_client .
all: build

View File

@ -0,0 +1,35 @@
# Using the `basic_light_client` application
## Background
The `basic_light_client` application is a basic example app that demonstrates how to send/receive messages using Waku Filter and Lightpush.
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_light_client` application run the following from the project directory
```
./build/basic_light_client --maddr=<filterNode>
```
The app will send a "Hello world!" through the lightpush protocol 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_light_client --maddr=<filterNode> --cluster-id=<value of cluster-id> --shard=<shard number>
```
e.g: ./build/basic_light_client --maddr="/ip4/0.0.0.0/tcp/30304/p2p/16Uiu2HAmBu5zRFzBGAzzMAuGWhaxN2EwcbW7CzibELQELzisf192" --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.

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,137 @@
module basic2
go 1.20
replace github.com/waku-org/go-waku => ../..
replace github.com/ethereum/go-ethereum v1.10.26 => github.com/status-im/go-ethereum v1.10.25-status.4
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.26.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
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
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
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.32.2 // indirect
github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect
github.com/libp2p/go-libp2p-mplex v0.9.0 // indirect
github.com/libp2p/go-libp2p-pubsub v0.10.0 // indirect
github.com/libp2p/go-mplex v0.7.0 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.2.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/libp2p/go-yamux/v4 v4.0.1 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.56 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.12.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/ginkgo/v2 v2.13.0 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.4 // indirect
github.com/quic-go/quic-go v0.39.4 // indirect
github.com/quic-go/webtransport-go v0.6.0 // 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
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/waku-org/go-discover v0.0.0-20221209174356-61c833f34d98 // indirect
github.com/waku-org/go-libp2p-rendezvous v0.0.0-20240110193335-a67d1cc760a0 // indirect
github.com/waku-org/go-zerokit-rln v0.1.14-0.20240102145250-fa738c0bdf59 // indirect
github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230916172309-ee0ee61dde2b // indirect
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/dig v1.17.1 // indirect
go.uber.org/fx v1.20.1 // indirect
go.uber.org/mock v0.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
golang.org/x/tools v0.14.0 // 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
)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,273 @@
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
cli "github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/payload"
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
"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/lightpush"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
"github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
var log = utils.Logger().Named("basic-light-client")
var ClusterID = altsrc.NewUintFlag(&cli.UintFlag{
Name: "cluster-id",
Value: 1,
Usage: "Cluster id that the client is interested in connecting to.",
Destination: &clusterID,
})
var Shard = altsrc.NewUintFlag(&cli.UintFlag{
Name: "shard-type",
Value: 0,
Usage: "shard that the node is interested in publishing/receiving from.",
Destination: &shard,
})
var StaticNode = altsrc.NewStringFlag(&cli.StringFlag{
Name: "maddr",
Usage: "multiaddress of static node to connect to.",
Destination: &multiaddress,
Required: true,
})
var clusterID, shard uint
var pubsubTopicStr string
var multiaddress string
func main() {
cliFlags := []cli.Flag{
ClusterID,
Shard,
StaticNode,
}
app := &cli.App{
Name: "basic-light-client-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-light-client", "1", "test", "proto")
if err != nil {
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 err
}
prvKey, err := crypto.HexToECDSA(key)
if err != nil {
log.Error("Could not convert hex into ecdsa key", zap.Error(err))
return err
}
ctx := context.Background()
lightNode, err := node.New(
node.WithPrivateKey(prvKey),
node.WithHostAddress(hostAddr),
node.WithWakuFilterLightNode(),
node.WithClusterID(uint16(clusterID)),
node.WithLightPush(),
//node.WithLogLevel(zapcore.DebugLevel),
)
if err != nil {
log.Error("Error creating wakunode", zap.Error(err))
return err
}
err = lightNode.Start(ctx)
if err != nil {
log.Error("Error starting wakunode", zap.Error(err))
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()
}
maddr, err := multiaddr.NewMultiaddr(multiaddress)
if err != nil {
log.Info("Error decoding multiaddr ", zap.Error(err))
}
peerID, err := lightNode.AddPeer(maddr, wps.Static,
[]string{pubsubTopicStr}, filter.FilterSubscribeID_v20beta1, lightpush.LightPushID_v20beta1)
if err != nil {
log.Info("Error adding filter peer on light node ", zap.Error(err))
}
useFilterAndLightPush(lightNode, contentTopic, pubsubTopicStr, peerID)
// shut the node down
lightNode.Stop()
return nil
}
func useFilterAndLightPush(lightNode *node.WakuNode, contentTopic string, pubsubTopic string, filterNode peer.ID) {
// Send FilterRequest from light node to full node
cf := protocol.ContentFilter{
PubsubTopic: pubsubTopic,
ContentTopics: protocol.NewContentTopicSet(contentTopic),
}
time.Sleep(2 * time.Second)
log.Info("Subscribing to peer ", zap.String("peerId", filterNode.String()))
theFilter, err := lightNode.FilterLightnode().Subscribe(context.Background(), cf, filter.WithPeer(filterNode))
if err != nil {
panic(err)
}
go func() {
for env := range theFilter[0].C { //Safely picking first subscriptions since only 1 contentTopic is subscribed
log.Info("Light node received msg ", zap.String("message", string(env.Message().Payload)))
}
log.Info("Message channel closed!")
}()
time.Sleep(2 * time.Second)
msg := &pb.WakuMessage{ContentTopic: contentTopic, Version: proto.Uint32(1), Timestamp: proto.Int64(time.Now().Unix()), Payload: []byte("Hello World!")}
hash, err := lightNode.Lightpush().Publish(context.Background(), msg, lightpush.WithPeer(filterNode), lightpush.WithPubSubTopic(pubsubTopic))
if err != nil {
panic(err)
}
log.Info("published msg via lightpush with hash:", logging.HexBytes("hash", hash))
log.Info("Done sending msgs.......")
log.Info("Press Ctrl+C to exit safely")
// 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...")
log.Info("UnSubscribing to peer ", zap.String("filterNode", filterNode.String()))
result, err := lightNode.FilterLightnode().Unsubscribe(context.Background(), cf, filter.WithPeer(filterNode))
if err != nil {
log.Error("failed to unsubscribe due to ", zap.Error(err))
panic(err)
}
for _, err := range result.Errors() {
if err.Err != nil {
log.Error("failed to unsubscribe due to res err", zap.Error(err.Err))
panic(err)
}
}
}
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, contentTopic string, msgContent string) {
var version uint32 = 0
p := new(payload.Payload)
p.Data = []byte(wakuNode.ID() + ": " + msgContent)
p.Key = &payload.KeyInfo{Kind: payload.None}
payload, err := p.Encode(version)
if err != nil {
log.Error("Error encoding the payload", zap.Error(err))
return
}
msg := &pb.WakuMessage{
Payload: payload,
Version: proto.Uint32(version),
ContentTopic: contentTopic,
Timestamp: utils.GetUnixEpoch(wakuNode.Timesource()),
}
_, 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) {
for {
time.Sleep(2 * time.Second)
write(ctx, wakuNode, contentTopic, "Hello world!")
}
}
func readLoop(ctx context.Context, wakuNode *node.WakuNode, contentTopic string) {
sub, err := wakuNode.Relay().Subscribe(ctx, protocol.NewContentFilter(pubsubTopicStr, contentTopic))
if err != nil {
log.Error("Could not subscribe", zap.Error(err))
return
}
for envelope := range sub[0].Ch {
if envelope.Message().ContentTopic != contentTopic {
continue
}
payload, err := payload.DecodePayload(envelope.Message(), &payload.KeyInfo{Kind: payload.None})
if err != nil {
log.Error("Error decoding payload", zap.Error(err))
continue
}
log.Info("Received msg, ", zap.String("data", string(payload.Data)))
}
}

View File

@ -13,15 +13,19 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/multiformats/go-multiaddr"
cli "github.com/urfave/cli/v2" cli "github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc" "github.com/urfave/cli/v2/altsrc"
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/v2/node" "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/payload"
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
"github.com/waku-org/go-waku/waku/v2/protocol" "github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/relay" "github.com/waku-org/go-waku/waku/v2/protocol/relay"
"github.com/waku-org/go-waku/waku/v2/utils" "github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
@ -41,14 +45,22 @@ var Shard = altsrc.NewUintFlag(&cli.UintFlag{
Destination: &shard, Destination: &shard,
}) })
var StaticNode = altsrc.NewStringFlag(&cli.StringFlag{
Name: "maddr",
Usage: "multiaddress of static node to connect to.",
Destination: &multiaddress,
})
var clusterID, shard uint var clusterID, shard uint
var pubsubTopicStr string var pubsubTopicStr string
var multiaddress string
func main() { func main() {
cliFlags := []cli.Flag{ cliFlags := []cli.Flag{
ClusterID, ClusterID,
Shard, Shard,
StaticNode,
} }
app := &cli.App{ app := &cli.App{
@ -103,6 +115,7 @@ func Execute() error {
node.WithNTP(), node.WithNTP(),
node.WithWakuRelay(), node.WithWakuRelay(),
node.WithClusterID(uint16(clusterID)), node.WithClusterID(uint16(clusterID)),
node.WithLogLevel(zapcore.DebugLevel),
) )
if err != nil { if err != nil {
log.Error("Error creating wakunode", zap.Error(err)) log.Error("Error creating wakunode", zap.Error(err))
@ -120,6 +133,18 @@ func Execute() error {
pubsubTopicStr = pubsubTopic.String() pubsubTopicStr = pubsubTopic.String()
} }
if multiaddress != "" {
maddr, err := multiaddr.NewMultiaddr(multiaddress)
if err != nil {
log.Info("Error decoding multiaddr ", zap.Error(err))
}
_, err = wakuNode.AddPeer(maddr, wps.Static,
[]string{pubsubTopicStr}, relay.WakuRelayID_v200)
if err != nil {
log.Info("Error adding filter peer on light node ", zap.Error(err))
}
}
go writeLoop(ctx, wakuNode, contentTopic) go writeLoop(ctx, wakuNode, contentTopic)
go readLoop(ctx, wakuNode, contentTopic) go readLoop(ctx, wakuNode, contentTopic)
@ -162,11 +187,11 @@ func write(ctx context.Context, wakuNode *node.WakuNode, contentTopic string, ms
Timestamp: utils.GetUnixEpoch(wakuNode.Timesource()), Timestamp: utils.GetUnixEpoch(wakuNode.Timesource()),
} }
_, err = wakuNode.Relay().Publish(ctx, msg, relay.WithPubSubTopic(pubsubTopicStr)) hash, err := wakuNode.Relay().Publish(ctx, msg, relay.WithPubSubTopic(pubsubTopicStr))
if err != nil { if err != nil {
log.Error("Error sending a message", zap.Error(err)) log.Error("Error sending a message", zap.Error(err))
} }
log.Info("Published msg,", zap.String("data", string(msg.Payload))) log.Info("Published msg,", zap.String("data", string(msg.Payload)), logging.HexBytes("hash", hash))
} }
func writeLoop(ctx context.Context, wakuNode *node.WakuNode, contentTopic string) { func writeLoop(ctx context.Context, wakuNode *node.WakuNode, contentTopic string) {