2023-01-11 06:57:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2023-01-13 19:40:43 +00:00
|
|
|
"bytes"
|
2023-01-14 15:01:59 +00:00
|
|
|
//"math/rand"
|
|
|
|
"strconv"
|
2023-01-13 19:40:43 +00:00
|
|
|
"encoding/binary"
|
2023-01-11 06:57:40 +00:00
|
|
|
"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 15:01:59 +00:00
|
|
|
"github.com/logos-co/wadoku/waku/common"
|
|
|
|
|
2023-01-11 06:57:40 +00:00
|
|
|
//"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 09:37:25 +00:00
|
|
|
//"github.com/wadoku/wadoku/utils"
|
2023-01-11 06:57:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("lightpush")
|
2023-01-13 19:40:43 +00:00
|
|
|
var seqNumber int32 = 0
|
2023-01-14 15:01:59 +00:00
|
|
|
var conf = common.Config{}
|
2023-01-18 11:57:05 +00:00
|
|
|
var nodeType = "lightpush"
|
2023-01-11 06:57:40 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
// args
|
|
|
|
fmt.Println("Populating CLI params...")
|
2023-01-14 15:01:59 +00:00
|
|
|
common.ArgInit(&conf)
|
2023-01-11 06:57:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 15:01:59 +00:00
|
|
|
|
2023-01-11 06:57:40 +00:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// setup the log
|
2023-01-13 19:40:43 +00:00
|
|
|
lvl, err := logging.LevelFromString(conf.LogLevel)
|
2023-01-11 06:57:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
logging.SetAllLoggers(lvl)
|
|
|
|
|
2023-01-18 11:57:05 +00:00
|
|
|
tcpEndPoint := common.LocalHost +
|
|
|
|
":" +
|
|
|
|
strconv.Itoa(common.StartPort + common.RandInt(0, common.PortRange))
|
2023-01-11 06:57:40 +00:00
|
|
|
// create the waku node
|
2023-01-14 15:01:59 +00:00
|
|
|
hostAddr, _ := net.ResolveTCPAddr("tcp", tcpEndPoint)
|
2023-01-11 06:57:40 +00:00
|
|
|
ctx := context.Background()
|
2023-01-18 11:57:05 +00:00
|
|
|
lightNode, err := node.New(ctx,
|
2023-01-11 06:57:40 +00:00
|
|
|
node.WithHostAddress(hostAddr),
|
2023-01-18 11:57:05 +00:00
|
|
|
//node.WithNTP(), // don't use NTP, fails at msec granularity
|
|
|
|
node.WithWakuRelay(),
|
2023-01-20 14:51:02 +00:00
|
|
|
//node.WithLightPush(), // no need to add lightpush to be a lightpush client!
|
2023-01-11 06:57:40 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:40:43 +00:00
|
|
|
log.Info("config: ", conf)
|
2023-01-11 06:57:40 +00:00
|
|
|
// find the list of full node fleet peers
|
2023-01-14 15:01:59 +00:00
|
|
|
log.Info("attempting DNS discovery with: ", common.DnsDiscoveryUrl)
|
|
|
|
nodes, err := dnsdisc.RetrieveNodes(ctx, common.DnsDiscoveryUrl, dnsdisc.WithNameserver(common.NameServer))
|
2023-01-11 06:57:40 +00:00
|
|
|
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-13 19:40:43 +00:00
|
|
|
log.Info("Discovered and connecting to: ", nodeList[0])
|
2023-01-11 06:57:40 +00:00
|
|
|
peerID, err := nodeList[0].ValueForProtocol(multiaddr.P_P2P)
|
|
|
|
if err != nil {
|
2023-01-13 19:40:43 +00:00
|
|
|
log.Error("could not get peerID: ", err)
|
2023-01-11 06:57:40 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = lightNode.DialPeerWithMultiAddress(ctx, nodeList[0])
|
|
|
|
if err != nil {
|
2023-01-13 19:40:43 +00:00
|
|
|
log.Error("could not connect to ", peerID, err)
|
2023-01-11 06:57:40 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:57:05 +00:00
|
|
|
log.Info("Starting the ", nodeType, " node ", conf.ContentTopic)
|
2023-01-11 06:57:40 +00:00
|
|
|
// start the light node
|
|
|
|
err = lightNode.Start()
|
|
|
|
if err != nil {
|
2023-01-18 11:57:05 +00:00
|
|
|
log.Error("Could not start the", nodeType, " node ", conf.ContentTopic)
|
2023-01-11 06:57:40 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:57:05 +00:00
|
|
|
go func() {
|
|
|
|
log.Info("IN THE WRITELOOP ", conf.ContentTopic)
|
|
|
|
|
|
|
|
f, err := os.OpenFile(conf.Ofname,
|
|
|
|
os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not open file: ", err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
prevTStamp := lightNode.Timesource().Now()
|
|
|
|
for {
|
|
|
|
time.Sleep(conf.Iat)
|
|
|
|
seqNumber++
|
|
|
|
|
|
|
|
// build the message with seq number //TODO: message size
|
|
|
|
p := new(payload.Payload)
|
|
|
|
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()
|
|
|
|
|
|
|
|
var version uint32 = 0
|
|
|
|
payload, err := p.Encode(version)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not Encode: ", err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
msg := &pb.WakuMessage{
|
|
|
|
Payload: payload,
|
|
|
|
Version: version,
|
|
|
|
ContentTopic: conf.ContentTopic,
|
|
|
|
Timestamp: utils.GetUnixEpochFrom(lightNode.Timesource().Now()),
|
|
|
|
}
|
|
|
|
|
|
|
|
// publish the message
|
|
|
|
_, err = lightNode.Lightpush().Publish(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not publish: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
iat := time.Since(prevTStamp)
|
|
|
|
str := fmt.Sprintf("SENT: %d %s %d\n", seqNumber, msg, iat.Milliseconds())
|
|
|
|
if _, err = f.WriteString(str); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Info(str)
|
|
|
|
prevTStamp = time.Unix(0, msg.Timestamp)
|
|
|
|
}
|
|
|
|
log.Error("Out of the Write loop: Message channel closed - timeout")
|
|
|
|
}()
|
2023-01-11 06:57:40 +00:00
|
|
|
|
|
|
|
<-time.After(conf.Duration)
|
2023-01-18 11:57:05 +00:00
|
|
|
log.Error(conf.Duration, " elapsed, stopping the " + nodeType + " node!");
|
2023-01-11 06:57:40 +00:00
|
|
|
|
|
|
|
// shut the nodes down
|
|
|
|
lightNode.Stop()
|
|
|
|
}
|