wadoku/waku/lightpush/lightpush.go

161 lines
3.7 KiB
Go
Raw Normal View History

2023-01-11 12:27:40 +05:30
package main
import (
"context"
"flag"
"fmt"
"net"
2023-01-14 01:10:43 +05:30
"bytes"
2023-01-14 20:31:59 +05:30
//"math/rand"
"strconv"
2023-01-14 01:10:43 +05:30
"encoding/binary"
2023-01-11 12:27:40 +05:30
"os"
"time"
logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multiaddr"
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
"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/pb"
"github.com/waku-org/go-waku/waku/v2/utils"
2023-01-14 20:31:59 +05:30
"github.com/logos-co/wadoku/waku/common"
2023-01-11 12:27:40 +05:30
//"crypto/rand"
//"encoding/hex"
//"github.com/ethereum/go-ethereum/crypto"
//"github.com/waku-org/go-waku/waku/v2/protocol/filter"
//"github.com/waku-org/go-waku/waku/v2/protocol"
2023-01-11 15:07:25 +05:30
//"github.com/wadoku/wadoku/utils"
2023-01-11 12:27:40 +05:30
)
var log = logging.Logger("lightpush")
2023-01-14 01:10:43 +05:30
var seqNumber int32 = 0
2023-01-14 20:31:59 +05:30
var conf = common.Config{}
2023-01-11 12:27:40 +05:30
func init() {
// args
fmt.Println("Populating CLI params...")
2023-01-14 20:31:59 +05:30
common.ArgInit(&conf)
2023-01-11 12:27:40 +05:30
}
2023-01-14 20:31:59 +05:30
2023-01-11 12:27:40 +05:30
func main() {
flag.Parse()
// setup the log
2023-01-14 01:10:43 +05:30
lvl, err := logging.LevelFromString(conf.LogLevel)
2023-01-11 12:27:40 +05:30
if err != nil {
panic(err)
}
logging.SetAllLoggers(lvl)
2023-01-14 20:31:59 +05:30
tcpEndPoint := "0.0.0.0:" + strconv.Itoa(common.StartPort + common.RandInt(0, common.Offset))
2023-01-11 12:27:40 +05:30
// create the waku node
2023-01-14 20:31:59 +05:30
hostAddr, _ := net.ResolveTCPAddr("tcp", tcpEndPoint)
2023-01-11 12:27:40 +05:30
ctx := context.Background()
lightNode, err := node.New(ctx,
node.WithWakuRelay(),
node.WithHostAddress(hostAddr),
node.WithWakuFilter(false),
node.WithLightPush(),
)
if err != nil {
panic(err)
}
2023-01-14 01:10:43 +05:30
log.Info("config: ", conf)
2023-01-11 12:27:40 +05:30
// find the list of full node fleet peers
2023-01-14 20:31:59 +05:30
log.Info("attempting DNS discovery with: ", common.DnsDiscoveryUrl)
nodes, err := dnsdisc.RetrieveNodes(ctx, common.DnsDiscoveryUrl, dnsdisc.WithNameserver(common.NameServer))
2023-01-11 12:27:40 +05:30
if err != nil {
panic(err.Error())
}
// connect to the first peer
var nodeList []multiaddr.Multiaddr
for _, n := range nodes {
nodeList = append(nodeList, n.Addresses...)
}
2023-01-14 01:10:43 +05:30
log.Info("Discovered and connecting to: ", nodeList[0])
2023-01-11 12:27:40 +05:30
peerID, err := nodeList[0].ValueForProtocol(multiaddr.P_P2P)
if err != nil {
2023-01-14 01:10:43 +05:30
log.Error("could not get peerID: ", err)
2023-01-11 12:27:40 +05:30
panic(err)
}
err = lightNode.DialPeerWithMultiAddress(ctx, nodeList[0])
if err != nil {
2023-01-14 01:10:43 +05:30
log.Error("could not connect to ", peerID, err)
2023-01-11 12:27:40 +05:30
panic(err)
}
2023-01-14 01:10:43 +05:30
log.Info("STARTING THE LIGHTPUSH NODE ", conf.ContentTopic)
2023-01-11 12:27:40 +05:30
// start the light node
err = lightNode.Start()
if err != nil {
2023-01-14 01:10:43 +05:30
log.Error("COULD NOT START THE LIGHTPUSH ", peerID, err)
2023-01-11 12:27:40 +05:30
panic(err)
}
go writeLoop(ctx, &conf, lightNode)
<-time.After(conf.Duration)
// shut the nodes down
lightNode.Stop()
}
2023-01-14 20:31:59 +05:30
func writeLoop(ctx context.Context, conf *common.Config, wakuNode *node.WakuNode) {
2023-01-14 01:10:43 +05:30
log.Info("STARTING THE WRITELOOP ", conf.ContentTopic)
2023-01-11 12:27:40 +05:30
f, err := os.OpenFile(conf.Ofname, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
2023-01-14 01:10:43 +05:30
log.Error("Could not open file: ", err)
2023-01-11 12:27:40 +05:30
panic(err)
}
defer f.Close()
for {
time.Sleep(conf.Iat)
2023-01-14 01:10:43 +05:30
seqNumber++
2023-01-11 12:27:40 +05:30
2023-01-14 01:10:43 +05:30
// build the message & seq number
2023-01-11 12:27:40 +05:30
p := new(payload.Payload)
2023-01-14 01:10:43 +05:30
wbuf := new(bytes.Buffer)
err := binary.Write(wbuf, binary.LittleEndian, seqNumber)
if err != nil {
log.Error("binary.Write failed:", err)
panic(err)
}
p.Data = wbuf.Bytes()
2023-01-11 12:27:40 +05:30
var version uint32 = 0
payload, err := p.Encode(version)
if err != nil {
log.Error("Could not Encode: ", err)
2023-01-14 01:10:43 +05:30
panic(err)
2023-01-11 12:27:40 +05:30
}
msg := &pb.WakuMessage{
Payload: payload,
Version: version,
ContentTopic: conf.ContentTopic,
Timestamp: utils.GetUnixEpochFrom(wakuNode.Timesource().Now()),
}
// publish the message
_, err = wakuNode.Lightpush().Publish(ctx, msg)
if err != nil {
log.Error("Could not publish: ", err)
return
}
str := fmt.Sprintf("MSG: %s\n", msg)
if _, err = f.WriteString(str); err != nil {
panic(err)
}
2023-01-14 01:10:43 +05:30
log.Info("PUBLISHED/PUSHED... ", seqNumber, msg)
2023-01-11 12:27:40 +05:30
}
}