refactor(c-bindings): store waku state in single variable

This commit is contained in:
Richard Ramos 2023-02-16 18:09:21 -04:00 committed by RichΛrd
parent 8b5e22002c
commit 2c9df85c51
5 changed files with 63 additions and 56 deletions

View File

@ -33,9 +33,16 @@ import (
"github.com/waku-org/go-waku/waku/v2/utils"
)
var wakuNode *node.WakuNode
var wakuRelayTopics []string
var wakuStarted = false
type WakuState struct {
ctx context.Context
cancel context.CancelFunc
node *node.WakuNode
relayTopics []string
}
var wakuState WakuState
var errWakuNodeNotReady = errors.New("go-waku not initialized")
@ -150,7 +157,7 @@ func getConfig(configJSON string) (wakuConfig, error) {
}
func NewNode(configJSON string) string {
if wakuNode != nil {
if wakuState.node != nil {
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))
}
wakuRelayTopics = config.RelayTopics
wakuState.relayTopics = config.RelayTopics
lvl, err := zapcore.ParseLevel(*config.LogLevel)
if err != nil {
@ -241,75 +248,75 @@ func NewNode(configJSON string) string {
return MakeJSONResponse(err)
}
wakuNode = w
wakuState.node = w
return MakeJSONResponse(nil)
}
func Start() string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
ctx := context.Background()
if err := wakuNode.Start(ctx); err != nil {
wakuState.ctx, wakuState.cancel = context.WithCancel(context.Background())
if err := wakuState.node.Start(wakuState.ctx); err != nil {
return MakeJSONResponse(err)
}
if wakuNode.DiscV5() != nil {
if err := wakuNode.DiscV5().Start(context.Background()); err != nil {
wakuNode.Stop()
if wakuState.node.DiscV5() != nil {
if err := wakuState.node.DiscV5().Start(context.Background()); err != nil {
wakuState.node.Stop()
return MakeJSONResponse(err)
}
}
for _, topic := range wakuRelayTopics {
for _, topic := range wakuState.relayTopics {
topic := topic
sub, err := wakuNode.Relay().SubscribeToTopic(ctx, topic)
sub, err := wakuState.node.Relay().SubscribeToTopic(wakuState.ctx, topic)
if err != nil {
wakuNode.Stop()
wakuState.node.Stop()
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)
}
func Stop() string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
wakuNode.Stop()
wakuState.node.Stop()
wakuStarted = false
wakuNode = nil
wakuState.cancel()
wakuState.node = nil
return MakeJSONResponse(nil)
}
func IsStarted() string {
return PrepareJSONResponse(wakuStarted, nil)
return PrepareJSONResponse(wakuState.node != nil, nil)
}
func PeerID() string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
return PrepareJSONResponse(wakuNode.ID(), nil)
return PrepareJSONResponse(wakuState.node.ID(), nil)
}
func ListenAddresses() string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
var addresses []string
for _, addr := range wakuNode.ListenAddresses() {
for _, addr := range wakuState.node.ListenAddresses() {
addresses = append(addresses, addr.String())
}
@ -317,7 +324,7 @@ func ListenAddresses() string {
}
func AddPeer(address string, protocolID libp2pProtocol.ID) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -326,12 +333,12 @@ func AddPeer(address string, protocolID libp2pProtocol.ID) string {
return MakeJSONResponse(err)
}
peerID, err := wakuNode.AddPeer(ma, protocolID)
peerID, err := wakuState.node.AddPeer(ma, protocolID)
return PrepareJSONResponse(peerID, err)
}
func Connect(address string, ms int) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -345,12 +352,12 @@ func Connect(address string, ms int) string {
ctx = context.Background()
}
err := wakuNode.DialPeer(ctx, address)
err := wakuState.node.DialPeer(ctx, address)
return MakeJSONResponse(err)
}
func ConnectPeerID(peerID string, ms int) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -369,12 +376,12 @@ func ConnectPeerID(peerID string, ms int) string {
ctx = context.Background()
}
err = wakuNode.DialPeerByID(ctx, pID)
err = wakuState.node.DialPeerByID(ctx, pID)
return MakeJSONResponse(err)
}
func Disconnect(peerID string) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -383,16 +390,16 @@ func Disconnect(peerID string) string {
return MakeJSONResponse(err)
}
err = wakuNode.ClosePeerById(pID)
err = wakuState.node.ClosePeerById(pID)
return MakeJSONResponse(err)
}
func PeerCnt() string {
if wakuNode == nil {
if wakuState.node == nil {
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 {
@ -429,11 +436,11 @@ func toSubscriptionMessage(msg *protocol.Envelope) *subscriptionMsg {
}
func Peers() string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
peers, err := wakuNode.Peers()
peers, err := wakuState.node.Peers()
return PrepareJSONResponse(peers, err)
}

View File

@ -38,7 +38,7 @@ func FilterSubscribe(filterJSON string, peerID string, ms int) string {
return MakeJSONResponse(err)
}
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -63,7 +63,7 @@ func FilterSubscribe(filterJSON string, peerID string, ms int) string {
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 {
return MakeJSONResponse(err)
}
@ -83,7 +83,7 @@ func FilterUnsubscribe(filterJSON string, ms int) string {
return MakeJSONResponse(err)
}
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -97,7 +97,7 @@ func FilterUnsubscribe(filterJSON string, ms int) string {
ctx = context.Background()
}
err = wakuNode.Filter().UnsubscribeFilter(ctx, cf)
err = wakuState.node.Filter().UnsubscribeFilter(ctx, cf)
if err != nil {
return MakeJSONResponse(err)
}

View File

@ -12,7 +12,7 @@ import (
)
func lightpushPublish(msg *pb.WakuMessage, pubsubTopic string, peerID string, ms int) (string, error) {
if wakuNode == nil {
if wakuState.node == nil {
return "", errWakuNodeNotReady
}
@ -37,7 +37,7 @@ func lightpushPublish(msg *pb.WakuMessage, pubsubTopic string, peerID string, ms
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
}

View File

@ -16,7 +16,7 @@ var relaySubscriptions map[string]*relay.Subscription = make(map[string]*relay.S
var relaySubsMutex sync.Mutex
func RelayEnoughPeers(topic string) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -25,11 +25,11 @@ func RelayEnoughPeers(topic string) string {
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) {
if wakuNode == nil {
if wakuState.node == nil {
return "", errWakuNodeNotReady
}
@ -43,7 +43,7 @@ func relayPublish(msg *pb.WakuMessage, pubsubTopic string, ms int) (string, erro
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
}
@ -80,7 +80,7 @@ func RelayPublishEncodeSymmetric(messageJSON string, topic string, symmetricKey
}
func RelaySubscribe(topic string) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -94,7 +94,7 @@ func RelaySubscribe(topic string) string {
return MakeJSONResponse(nil)
}
subscription, err := wakuNode.Relay().SubscribeToTopic(context.Background(), topicToSubscribe)
subscription, err := wakuState.node.Relay().SubscribeToTopic(context.Background(), topicToSubscribe)
if err != nil {
return MakeJSONResponse(err)
}
@ -111,7 +111,7 @@ func RelaySubscribe(topic string) string {
}
func RelayUnsubscribe(topic string) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -129,7 +129,7 @@ func RelayUnsubscribe(topic string) string {
delete(relaySubscriptions, topicToUnsubscribe)
err := wakuNode.Relay().Unsubscribe(context.Background(), topicToUnsubscribe)
err := wakuState.node.Relay().Unsubscribe(context.Background(), topicToUnsubscribe)
if err != nil {
return MakeJSONResponse(err)
}

View File

@ -41,7 +41,7 @@ func queryResponse(ctx context.Context, args storeMessagesArgs, options []store.
contentTopics = append(contentTopics, ct.ContentTopic)
}
res, err := wakuNode.Store().Query(
res, err := wakuState.node.Store().Query(
ctx,
store.Query{
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 {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}
@ -109,7 +109,7 @@ func StoreQuery(queryJSON string, peerID string, ms int) string {
}
func StoreLocalQuery(queryJSON string) string {
if wakuNode == nil {
if wakuState.node == nil {
return MakeJSONResponse(errWakuNodeNotReady)
}