mirror of https://github.com/status-im/go-waku.git
refactor(c-bindings): store waku state in single variable
This commit is contained in:
parent
8b5e22002c
commit
2c9df85c51
|
@ -33,9 +33,16 @@ import (
|
||||||
"github.com/waku-org/go-waku/waku/v2/utils"
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var wakuNode *node.WakuNode
|
type WakuState struct {
|
||||||
var wakuRelayTopics []string
|
ctx context.Context
|
||||||
var wakuStarted = false
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
node *node.WakuNode
|
||||||
|
|
||||||
|
relayTopics []string
|
||||||
|
}
|
||||||
|
|
||||||
|
var wakuState WakuState
|
||||||
|
|
||||||
var errWakuNodeNotReady = errors.New("go-waku not initialized")
|
var errWakuNodeNotReady = errors.New("go-waku not initialized")
|
||||||
|
|
||||||
|
@ -150,7 +157,7 @@ func getConfig(configJSON string) (wakuConfig, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNode(configJSON string) string {
|
func NewNode(configJSON string) string {
|
||||||
if wakuNode != nil {
|
if wakuState.node != nil {
|
||||||
return MakeJSONResponse(errors.New("go-waku already initialized. stop it first"))
|
return MakeJSONResponse(errors.New("go-waku already initialized. stop it first"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +234,7 @@ func NewNode(configJSON string) string {
|
||||||
opts = append(opts, node.WithDiscoveryV5(*config.DiscV5UDPPort, bootnodes, true))
|
opts = append(opts, node.WithDiscoveryV5(*config.DiscV5UDPPort, bootnodes, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
wakuRelayTopics = config.RelayTopics
|
wakuState.relayTopics = config.RelayTopics
|
||||||
|
|
||||||
lvl, err := zapcore.ParseLevel(*config.LogLevel)
|
lvl, err := zapcore.ParseLevel(*config.LogLevel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -241,75 +248,75 @@ func NewNode(configJSON string) string {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
wakuNode = w
|
wakuState.node = w
|
||||||
|
|
||||||
return MakeJSONResponse(nil)
|
return MakeJSONResponse(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start() string {
|
func Start() string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
wakuState.ctx, wakuState.cancel = context.WithCancel(context.Background())
|
||||||
if err := wakuNode.Start(ctx); err != nil {
|
|
||||||
|
if err := wakuState.node.Start(wakuState.ctx); err != nil {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if wakuNode.DiscV5() != nil {
|
if wakuState.node.DiscV5() != nil {
|
||||||
if err := wakuNode.DiscV5().Start(context.Background()); err != nil {
|
if err := wakuState.node.DiscV5().Start(context.Background()); err != nil {
|
||||||
wakuNode.Stop()
|
wakuState.node.Stop()
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, topic := range wakuRelayTopics {
|
for _, topic := range wakuState.relayTopics {
|
||||||
topic := topic
|
topic := topic
|
||||||
sub, err := wakuNode.Relay().SubscribeToTopic(ctx, topic)
|
sub, err := wakuState.node.Relay().SubscribeToTopic(wakuState.ctx, topic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wakuNode.Stop()
|
wakuState.node.Stop()
|
||||||
return MakeJSONResponse(fmt.Errorf("could not subscribe to topic: %s, %w", topic, err))
|
return MakeJSONResponse(fmt.Errorf("could not subscribe to topic: %s, %w", topic, err))
|
||||||
}
|
}
|
||||||
wakuNode.Broadcaster().Unregister(&topic, sub.C)
|
wakuState.node.Broadcaster().Unregister(&topic, sub.C)
|
||||||
}
|
}
|
||||||
|
|
||||||
wakuStarted = true
|
|
||||||
|
|
||||||
return MakeJSONResponse(nil)
|
return MakeJSONResponse(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Stop() string {
|
func Stop() string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
wakuNode.Stop()
|
wakuState.node.Stop()
|
||||||
|
|
||||||
wakuStarted = false
|
wakuState.cancel()
|
||||||
wakuNode = nil
|
|
||||||
|
wakuState.node = nil
|
||||||
|
|
||||||
return MakeJSONResponse(nil)
|
return MakeJSONResponse(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsStarted() string {
|
func IsStarted() string {
|
||||||
return PrepareJSONResponse(wakuStarted, nil)
|
return PrepareJSONResponse(wakuState.node != nil, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PeerID() string {
|
func PeerID() string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
return PrepareJSONResponse(wakuNode.ID(), nil)
|
return PrepareJSONResponse(wakuState.node.ID(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenAddresses() string {
|
func ListenAddresses() string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
var addresses []string
|
var addresses []string
|
||||||
for _, addr := range wakuNode.ListenAddresses() {
|
for _, addr := range wakuState.node.ListenAddresses() {
|
||||||
addresses = append(addresses, addr.String())
|
addresses = append(addresses, addr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,7 +324,7 @@ func ListenAddresses() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddPeer(address string, protocolID libp2pProtocol.ID) string {
|
func AddPeer(address string, protocolID libp2pProtocol.ID) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,12 +333,12 @@ func AddPeer(address string, protocolID libp2pProtocol.ID) string {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
peerID, err := wakuNode.AddPeer(ma, protocolID)
|
peerID, err := wakuState.node.AddPeer(ma, protocolID)
|
||||||
return PrepareJSONResponse(peerID, err)
|
return PrepareJSONResponse(peerID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Connect(address string, ms int) string {
|
func Connect(address string, ms int) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -345,12 +352,12 @@ func Connect(address string, ms int) string {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
|
||||||
err := wakuNode.DialPeer(ctx, address)
|
err := wakuState.node.DialPeer(ctx, address)
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConnectPeerID(peerID string, ms int) string {
|
func ConnectPeerID(peerID string, ms int) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,12 +376,12 @@ func ConnectPeerID(peerID string, ms int) string {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = wakuNode.DialPeerByID(ctx, pID)
|
err = wakuState.node.DialPeerByID(ctx, pID)
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Disconnect(peerID string) string {
|
func Disconnect(peerID string) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -383,16 +390,16 @@ func Disconnect(peerID string) string {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = wakuNode.ClosePeerById(pID)
|
err = wakuState.node.ClosePeerById(pID)
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PeerCnt() string {
|
func PeerCnt() string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
return PrepareJSONResponse(wakuNode.PeerCount(), nil)
|
return PrepareJSONResponse(wakuState.node.PeerCount(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContentTopic(applicationName string, applicationVersion int, contentTopicName string, encoding string) string {
|
func ContentTopic(applicationName string, applicationVersion int, contentTopicName string, encoding string) string {
|
||||||
|
@ -429,11 +436,11 @@ func toSubscriptionMessage(msg *protocol.Envelope) *subscriptionMsg {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Peers() string {
|
func Peers() string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
peers, err := wakuNode.Peers()
|
peers, err := wakuState.node.Peers()
|
||||||
return PrepareJSONResponse(peers, err)
|
return PrepareJSONResponse(peers, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ func FilterSubscribe(filterJSON string, peerID string, ms int) string {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ func FilterSubscribe(filterJSON string, peerID string, ms int) string {
|
||||||
fOptions = append(fOptions, filter.WithAutomaticPeerSelection())
|
fOptions = append(fOptions, filter.WithAutomaticPeerSelection())
|
||||||
}
|
}
|
||||||
|
|
||||||
_, f, err := wakuNode.Filter().Subscribe(ctx, cf, fOptions...)
|
_, f, err := wakuState.node.Filter().Subscribe(ctx, cf, fOptions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ func FilterUnsubscribe(filterJSON string, ms int) string {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ func FilterUnsubscribe(filterJSON string, ms int) string {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = wakuNode.Filter().UnsubscribeFilter(ctx, cf)
|
err = wakuState.node.Filter().UnsubscribeFilter(ctx, cf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func lightpushPublish(msg *pb.WakuMessage, pubsubTopic string, peerID string, ms int) (string, error) {
|
func lightpushPublish(msg *pb.WakuMessage, pubsubTopic string, peerID string, ms int) (string, error) {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return "", errWakuNodeNotReady
|
return "", errWakuNodeNotReady
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ func lightpushPublish(msg *pb.WakuMessage, pubsubTopic string, peerID string, ms
|
||||||
lpOptions = append(lpOptions, lightpush.WithAutomaticPeerSelection())
|
lpOptions = append(lpOptions, lightpush.WithAutomaticPeerSelection())
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, err := wakuNode.Lightpush().PublishToTopic(ctx, msg, pubsubTopic, lpOptions...)
|
hash, err := wakuState.node.Lightpush().PublishToTopic(ctx, msg, pubsubTopic, lpOptions...)
|
||||||
return hexutil.Encode(hash), err
|
return hexutil.Encode(hash), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ var relaySubscriptions map[string]*relay.Subscription = make(map[string]*relay.S
|
||||||
var relaySubsMutex sync.Mutex
|
var relaySubsMutex sync.Mutex
|
||||||
|
|
||||||
func RelayEnoughPeers(topic string) string {
|
func RelayEnoughPeers(topic string) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,11 +25,11 @@ func RelayEnoughPeers(topic string) string {
|
||||||
topicToCheck = topic
|
topicToCheck = topic
|
||||||
}
|
}
|
||||||
|
|
||||||
return PrepareJSONResponse(wakuNode.Relay().EnoughPeersToPublishToTopic(topicToCheck), nil)
|
return PrepareJSONResponse(wakuState.node.Relay().EnoughPeersToPublishToTopic(topicToCheck), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func relayPublish(msg *pb.WakuMessage, pubsubTopic string, ms int) (string, error) {
|
func relayPublish(msg *pb.WakuMessage, pubsubTopic string, ms int) (string, error) {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return "", errWakuNodeNotReady
|
return "", errWakuNodeNotReady
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ func relayPublish(msg *pb.WakuMessage, pubsubTopic string, ms int) (string, erro
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, err := wakuNode.Relay().PublishToTopic(ctx, msg, pubsubTopic)
|
hash, err := wakuState.node.Relay().PublishToTopic(ctx, msg, pubsubTopic)
|
||||||
return hexutil.Encode(hash), err
|
return hexutil.Encode(hash), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func RelayPublishEncodeSymmetric(messageJSON string, topic string, symmetricKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func RelaySubscribe(topic string) string {
|
func RelaySubscribe(topic string) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ func RelaySubscribe(topic string) string {
|
||||||
return MakeJSONResponse(nil)
|
return MakeJSONResponse(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
subscription, err := wakuNode.Relay().SubscribeToTopic(context.Background(), topicToSubscribe)
|
subscription, err := wakuState.node.Relay().SubscribeToTopic(context.Background(), topicToSubscribe)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ func RelaySubscribe(topic string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RelayUnsubscribe(topic string) string {
|
func RelayUnsubscribe(topic string) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +129,7 @@ func RelayUnsubscribe(topic string) string {
|
||||||
|
|
||||||
delete(relaySubscriptions, topicToUnsubscribe)
|
delete(relaySubscriptions, topicToUnsubscribe)
|
||||||
|
|
||||||
err := wakuNode.Relay().Unsubscribe(context.Background(), topicToUnsubscribe)
|
err := wakuState.node.Relay().Unsubscribe(context.Background(), topicToUnsubscribe)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MakeJSONResponse(err)
|
return MakeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func queryResponse(ctx context.Context, args storeMessagesArgs, options []store.
|
||||||
contentTopics = append(contentTopics, ct.ContentTopic)
|
contentTopics = append(contentTopics, ct.ContentTopic)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := wakuNode.Store().Query(
|
res, err := wakuState.node.Store().Query(
|
||||||
ctx,
|
ctx,
|
||||||
store.Query{
|
store.Query{
|
||||||
Topic: args.Topic,
|
Topic: args.Topic,
|
||||||
|
@ -69,7 +69,7 @@ func queryResponse(ctx context.Context, args storeMessagesArgs, options []store.
|
||||||
}
|
}
|
||||||
|
|
||||||
func StoreQuery(queryJSON string, peerID string, ms int) string {
|
func StoreQuery(queryJSON string, peerID string, ms int) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ func StoreQuery(queryJSON string, peerID string, ms int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func StoreLocalQuery(queryJSON string) string {
|
func StoreLocalQuery(queryJSON string) string {
|
||||||
if wakuNode == nil {
|
if wakuState.node == nil {
|
||||||
return MakeJSONResponse(errWakuNodeNotReady)
|
return MakeJSONResponse(errWakuNodeNotReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue