add option funcs to WakuNode, add extraOptions to pass arbitrary config to nwaku

This commit is contained in:
Václav Pavlín 2025-03-11 13:39:27 +01:00
parent d838ad1492
commit d7da6d60d2
No known key found for this signature in database
GPG Key ID: B378FB31BB6D89A5
3 changed files with 39 additions and 3 deletions

View File

@ -1,5 +1,7 @@
package common
type ExtraOptions map[string]interface{}
type WakuConfig struct {
Host string `json:"host,omitempty"`
Nodekey string `json:"nodekey,omitempty"`

View File

@ -346,19 +346,24 @@ func GoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) {
type WakuNode struct {
wakuCtx unsafe.Pointer
config *common.WakuConfig
extraOptions common.ExtraOptions
MsgChan chan common.Envelope
TopicHealthChan chan topicHealth
ConnectionChangeChan chan connectionChange
nodeName string
}
func NewWakuNode(config *common.WakuConfig, nodeName string) (*WakuNode, error) {
func NewWakuNode(config *common.WakuConfig, nodeName string, opts ...WakuNodeOption) (*WakuNode, error) {
Debug("Creating new WakuNode: %v", nodeName)
n := &WakuNode{
config: config,
nodeName: nodeName,
}
for _, o := range opts {
o(n)
}
wg := sync.WaitGroup{}
jsonConfig, err := json.Marshal(config)
@ -366,6 +371,22 @@ func NewWakuNode(config *common.WakuConfig, nodeName string) (*WakuNode, error)
return nil, err
}
if len(n.extraOptions) > 0 {
configMap := make(common.ExtraOptions)
err = json.Unmarshal(jsonConfig, &configMap)
if err != nil {
return nil, err
}
for k, v := range n.extraOptions {
configMap[k] = v
}
jsonConfig, err = json.Marshal(configMap)
if err != nil {
return nil, err
}
}
var cJsonConfig = C.CString(string(jsonConfig))
var resp = C.allocResp(unsafe.Pointer(&wg))
@ -1314,7 +1335,7 @@ func GetFreePortIfNeeded(tcpPort int, discV5UDPPort int) (int, int, error) {
}
// Create & start node
func StartWakuNode(nodeName string, customCfg *common.WakuConfig) (*WakuNode, error) {
func StartWakuNode(nodeName string, customCfg *common.WakuConfig, opts ...WakuNodeOption) (*WakuNode, error) {
Debug("Initializing %s", nodeName)
@ -1339,7 +1360,7 @@ func StartWakuNode(nodeName string, customCfg *common.WakuConfig) (*WakuNode, er
}
Debug("Creating %s", nodeName)
node, err := NewWakuNode(&nodeCfg, nodeName)
node, err := NewWakuNode(&nodeCfg, nodeName, opts...)
if err != nil {
Error("Failed to create %s: %v", nodeName, err)
return nil, err

13
waku/options.go Normal file
View File

@ -0,0 +1,13 @@
package waku
import "github.com/waku-org/waku-go-bindings/waku/common"
type WakuNodeOption func(*WakuNode)
// This allows you to pass arbitrary valid config options to Nwaku baed on https://github.com/waku-org/nwaku/blob/master/waku/factory/external_config.nim
// It is mostly for development and experimental purposes and will be removed in the future.
func WithExtraOptions(extraOptions common.ExtraOptions) WakuNodeOption {
return func(wn *WakuNode) {
wn.extraOptions = extraOptions
}
}