Skip to content

Commit

Permalink
fix: compile libstatus and load libwaku on mobile (#6249)
Browse files Browse the repository at this point in the history
* fix: compile libstatus and load libwaku on mobile
* fix: gowaku compat
  • Loading branch information
richard-ramos authored Jan 15, 2025
1 parent 54be635 commit 1348510
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 24 deletions.
6 changes: 5 additions & 1 deletion cmd/status-cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func start(p StartParams, logger *zap.SugaredLogger) (*StatusCLI, error) {
return nil, err
}
waku := backend.StatusNode().WakuV2Service()
telemetryClient := telemetry.NewClient(telemetryLogger, p.TelemetryURL, backend.SelectedAccountKeyID(), p.Name, "cli", telemetry.WithPeerID(waku.PeerID().String()))
peerID, err := waku.PeerID()
if err != nil {
return nil, err
}
telemetryClient := telemetry.NewClient(telemetryLogger, p.TelemetryURL, backend.SelectedAccountKeyID(), p.Name, "cli", telemetry.WithPeerID(peerID.String()))
telemetryClient.Start(context.Background())
backend.StatusNode().WakuV2Service().SetStatusTelemetryClient(telemetryClient)
}
Expand Down
6 changes: 3 additions & 3 deletions eth-node/bridge/geth/waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (w *GethWakuWrapper) Version() uint {
}

// Added for compatibility with waku V2
func (w *GethWakuWrapper) PeerCount() int {
return -1
func (w *GethWakuWrapper) PeerCount() (int, error) {
return -1, nil
}

// Added for compatibility with waku V2
Expand Down Expand Up @@ -307,7 +307,7 @@ func (w *wakuFilterWrapper) ID() string {
func (w *GethWakuWrapper) ConfirmMessageDelivered(hashes []common.Hash) {
}

func (w *GethWakuWrapper) PeerID() peer.ID {
func (w *GethWakuWrapper) PeerID() (peer.ID, error) {
panic("not available in WakuV1")
}

Expand Down
4 changes: 2 additions & 2 deletions eth-node/bridge/geth/wakuv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (w *gethWakuV2Wrapper) Version() uint {
return 2
}

func (w *gethWakuV2Wrapper) PeerCount() int {
func (w *gethWakuV2Wrapper) PeerCount() (int, error) {
return w.waku.PeerCount()
}

Expand Down Expand Up @@ -302,7 +302,7 @@ func (w *gethWakuV2Wrapper) ConfirmMessageDelivered(hashes []common.Hash) {
w.waku.ConfirmMessageDelivered(hashes)
}

func (w *gethWakuV2Wrapper) PeerID() peer.ID {
func (w *gethWakuV2Wrapper) PeerID() (peer.ID, error) {
return w.waku.PeerID()
}

Expand Down
4 changes: 2 additions & 2 deletions eth-node/types/waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type Waku interface {
Version() uint

// PeerCount
PeerCount() int
PeerCount() (int, error)

ListenAddresses() ([]multiaddr.Multiaddr, error)

Expand Down Expand Up @@ -195,7 +195,7 @@ type Waku interface {
ConfirmMessageDelivered(hash []common.Hash)

// PeerID returns node's PeerID
PeerID() peer.ID
PeerID() (peer.ID, error)

// GetActiveStorenode returns the AddrInfo of the currently active storenode. It will be empty if no storenode is active
GetActiveStorenode() peer.AddrInfo
Expand Down
12 changes: 11 additions & 1 deletion node/status_node_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"crypto/ecdsa"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -382,7 +383,16 @@ func (b *StatusNode) wakuV2Service(nodeConfig *params.NodeConfig) (*wakuv2.Waku,
}
}

w, err := wakuv2.New(nodeKey, nodeConfig.ClusterConfig.Fleet, cfg, logutils.ZapLogger(), b.appDB, b.timeSource(), signal.SendHistoricMessagesRequestFailed, signal.SendPeerStats)
nwakuCfg := &wakuv2.WakuConfig{
NodeKey: hex.EncodeToString(crypto.FromECDSA(nodeKey)),
Host: nodeConfig.WakuV2Config.Host,
TcpPort: nodeConfig.WakuV2Config.Port,
LogLevel: "DEBUG", // TODO-nwaku ?
ClusterID: nodeConfig.ClusterConfig.ClusterID,
Shards: []uint16{wakuv2.DefaultShardIndex, wakuv2.NonProtectedShardIndex},
}

w, err := wakuv2.New(nodeKey, nodeConfig.ClusterConfig.Fleet, cfg, nwakuCfg, logutils.ZapLogger(), b.appDB, b.timeSource(), signal.SendHistoricMessagesRequestFailed, signal.SendPeerStats)

if err != nil {
return nil, err
Expand Down
12 changes: 10 additions & 2 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ func NewMessenger(
if err != nil || wakuV2 == nil {
return nil, errors.Wrap(err, "failed to find Whisper and Waku V1/V2 services")
}
peerId = wakuV2.PeerID()
peerId, err = wakuV2.PeerID()
if err != nil {
return nil, err
}
transp, err = transport.NewTransport(
wakuV2,
identity,
Expand Down Expand Up @@ -1000,7 +1003,12 @@ func (m *Messenger) handleConnectionChange(online bool) {
func (m *Messenger) Online() bool {
switch m.transport.WakuVersion() {
case 2:
return m.transport.PeerCount() > 0
pc, err := m.transport.PeerCount()
if err != nil {
m.logger.Error("could not obtain number of peers", zap.Error(err))
return false
}
return pc > 0
default:
return m.node.PeersCount() > 0
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (t *Transport) WakuVersion() uint {
return t.waku.Version()
}

func (t *Transport) PeerCount() int {
func (t *Transport) PeerCount() (int, error) {
return t.waku.PeerCount()
}

Expand Down
52 changes: 46 additions & 6 deletions wakuv2/gowaku.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func newTTLCache() *ttlcache.Cache[gethcommon.Hash, *common.ReceivedMessage] {
}

// New creates a WakuV2 client ready to communicate through the LibP2P network.
func New(nodeKey *ecdsa.PrivateKey, fleet string, cfg *Config, logger *zap.Logger, appDB *sql.DB, ts *timesource.NTPTimeSource, onHistoricMessagesRequestFailed func([]byte, peer.AddrInfo, error), onPeerStats func(types.ConnStatus)) (*Waku, error) {
func New(nodeKey *ecdsa.PrivateKey, fleet string, cfg *Config, _ *WakuConfig, logger *zap.Logger, appDB *sql.DB, ts *timesource.NTPTimeSource, onHistoricMessagesRequestFailed func([]byte, peer.AddrInfo, error), onPeerStats func(types.ConnStatus)) (*Waku, error) {
var err error
if logger == nil {
logger, err = zap.NewDevelopment()
Expand Down Expand Up @@ -1261,7 +1261,8 @@ func (w *Waku) checkForConnectionChanges() {
func (w *Waku) reportPeerMetrics() {
if w.statusTelemetryClient != nil {
connFailures := FormatPeerConnFailures(w.node)
w.statusTelemetryClient.PushPeerCount(w.ctx, w.PeerCount())
pc, _ := w.PeerCount() // PeerCount's err will never be != nil
w.statusTelemetryClient.PushPeerCount(w.ctx, pc)
w.statusTelemetryClient.PushPeerConnFailures(w.ctx, connFailures)

peerCountByOrigin := make(map[wps.Origin]uint)
Expand Down Expand Up @@ -1608,8 +1609,8 @@ func (w *Waku) ClearEnvelopesCache() {
w.envelopeCache = newTTLCache()
}

func (w *Waku) PeerCount() int {
return w.node.PeerCount()
func (w *Waku) PeerCount() (int, error) {
return w.node.PeerCount(), nil
}

func (w *Waku) Peers() types.PeerStats {
Expand Down Expand Up @@ -1931,8 +1932,8 @@ func (w *Waku) Clean() error {
return nil
}

func (w *Waku) PeerID() peer.ID {
return w.node.Host().ID()
func (w *Waku) PeerID() (peer.ID, error) {
return w.node.Host().ID(), nil
}

func (w *Waku) Peerstore() peerstore.Peerstore {
Expand Down Expand Up @@ -2009,3 +2010,42 @@ func (w *Waku) ListPeersInMesh(pubsubTopic string) (int, error) {
listPeers := w.node.Relay().PubSub().ListPeers(pubsubTopic)
return len(listPeers), nil
}

// Added just for compatibility with nwaku

type WakuConfig struct {
Host string `json:"host,omitempty"`
NodeKey string `json:"nodekey,omitempty"`
EnableRelay bool `json:"relay"`
LogLevel string `json:"logLevel"`
DnsDiscovery bool `json:"dnsDiscovery,omitempty"`
DnsDiscoveryUrl string `json:"dnsDiscoveryUrl,omitempty"`
MaxMessageSize string `json:"maxMessageSize,omitempty"`
Staticnodes []string `json:"staticnodes,omitempty"`
Discv5BootstrapNodes []string `json:"discv5BootstrapNodes,omitempty"`
Discv5Discovery bool `json:"discv5Discovery,omitempty"`
Discv5UdpPort int `json:"discv5UdpPort,omitempty"`
ClusterID uint16 `json:"clusterId,omitempty"`
Shards []uint16 `json:"shards,omitempty"`
PeerExchange bool `json:"peerExchange,omitempty"`
PeerExchangeNode string `json:"peerExchangeNode,omitempty"`
Filter bool `json:"filter,omitempty"`
FilterMaxPeersToServe int `json:"filterMaxPeersToServe,omitempty"`
Lightpush bool `json:"lightpush,omitempty"`
TcpPort int `json:"tcpPort,omitempty"`
RateLimits RateLimitsConfig `json:"rateLimits,omitempty"`
}

type RateLimitsConfig struct {
Filter *RateLimit `json:"-"`
Lightpush *RateLimit `json:"-"`
PeerExchange *RateLimit `json:"-"`
}

type RateLimit struct {
Volume int
Period int
Unit RateLimitUnit
}

type RateLimitUnit string
39 changes: 34 additions & 5 deletions wakuv2/nwaku.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
package wakuv2

/*
#cgo LDFLAGS: -L../third_party/nwaku/build/ -lnegentropy -lwaku
#cgo LDFLAGS: -L../third_party/nwaku -Wl,-rpath,../third_party/nwaku/build/
#cgo !android,!ios LDFLAGS: -L${SRCDIR}/../third_party/nwaku/build/ -lwaku
#cgo !android,!ios LDFLAGS: -L${SRCDIR}/../third_party/nwaku -Wl,-rpath,../third_party/nwaku/build/
#cgo amd64,android LDFLAGS:-L${SRCDIR}/../third_party/nwaku/build/android/x86_64 -lwaku
#cgo arm64,android LDFLAGS:-L${SRCDIR}/../third_party/nwaku/build/android/arm64-v8a -lwaku
#cgo arm,android LDFLAGS:-L${SRCDIR}/../third_party/nwaku/build/android/armeabi-v7a -lwaku
#cgo 386,android LDFLAGS:-L${SRCDIR}/../third_party/nwaku/build/android/x86 -lwaku
#cgo darwin,arm64,ios LDFLAGS:-L${SRCDIR}/../third_party/nwaku/build/ios/iphone -lwaku
#cgo darwin,amd64,ios LDFLAGS:-L${SRCDIR}/../third_party/nwaku/build/ios/simulator -lwaku
#include "../third_party/nwaku/library/libwaku.h"
#include <stdio.h>
Expand Down Expand Up @@ -932,6 +941,15 @@ func (w *Waku) telemetryBandwidthStats(telemetryServerURL string) {
}
}
}
*/

func (w *Waku) StartDiscV5() error {
return w.node.StartDiscV5()
}

func (w *Waku) StopDiscV5() error {
return w.node.StopDiscV5()
}

func (w *Waku) GetStats() types.StatsSummary {
stats := w.bandwidthCounter.GetBandwidthTotals()
Expand All @@ -941,6 +959,7 @@ func (w *Waku) GetStats() types.StatsSummary {
}
}

/* TODO-nwaku
func (w *Waku) runPeerExchangeLoop() {
defer gocommon.LogOnPanic()
defer w.wg.Done()
Expand Down Expand Up @@ -1987,18 +2006,22 @@ func (w *Waku) Peers() types.PeerStats {
// return FormatPeerStats(w.node)
}

/* TODO-nwaku
func (w *Waku) RelayPeersByTopic(topic string) (*types.PeerList, error) {
if w.cfg.LightClient {
/* TODO-nwaku
return nil, errors.New("only available in relay mode")
}
return &types.PeerList{
FullMeshPeers: w.node.Relay().PubSub().MeshPeers(topic),
AllPeers: w.node.Relay().PubSub().ListPeers(topic),
}, nil
*/
return &types.PeerList{}, nil
}

func (w *Waku) ENR() (*enode.Node, error) {
return w.node.ENR()
}
*/

func (w *Waku) SubscribeToPubsubTopic(topic string, pubkey *ecdsa.PublicKey) error {
topic = w.GetPubsubTopic(topic)
Expand Down Expand Up @@ -2051,6 +2074,10 @@ func (w *Waku) RemovePubsubTopicKey(topic string) error {
return w.protectedTopicStore.Delete(topic)
}

func (w *Waku) ListenAddresses() ([]multiaddr.Multiaddr, error) {
return w.node.ListenAddresses()
}

func (w *Waku) handleNetworkChangeFromApp(state connection.State) {
// TODO-nwaku
/*
Expand Down Expand Up @@ -2414,6 +2441,8 @@ func wakuNew(nodeKey *ecdsa.PrivateKey,
ctx, cancel := context.WithCancel(context.Background())

if !cfg.LightClient {
nwakuCfg.Discv5Discovery = true
nwakuCfg.EnableRelay = true
nwakuCfg.Filter = true
nwakuCfg.FilterMaxPeersToServe = 20
nwakuCfg.Lightpush = true
Expand Down

0 comments on commit 1348510

Please sign in to comment.