Skip to content

Commit d7da6d6

Browse files
committed
add option funcs to WakuNode, add extraOptions to pass arbitrary config to nwaku
1 parent d838ad1 commit d7da6d6

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

waku/common/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package common
22

3+
type ExtraOptions map[string]interface{}
4+
35
type WakuConfig struct {
46
Host string `json:"host,omitempty"`
57
Nodekey string `json:"nodekey,omitempty"`

waku/nwaku.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,26 +346,47 @@ func GoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) {
346346
type WakuNode struct {
347347
wakuCtx unsafe.Pointer
348348
config *common.WakuConfig
349+
extraOptions common.ExtraOptions
349350
MsgChan chan common.Envelope
350351
TopicHealthChan chan topicHealth
351352
ConnectionChangeChan chan connectionChange
352353
nodeName string
353354
}
354355

355-
func NewWakuNode(config *common.WakuConfig, nodeName string) (*WakuNode, error) {
356+
func NewWakuNode(config *common.WakuConfig, nodeName string, opts ...WakuNodeOption) (*WakuNode, error) {
356357
Debug("Creating new WakuNode: %v", nodeName)
357358
n := &WakuNode{
358359
config: config,
359360
nodeName: nodeName,
360361
}
361362

363+
for _, o := range opts {
364+
o(n)
365+
}
366+
362367
wg := sync.WaitGroup{}
363368

364369
jsonConfig, err := json.Marshal(config)
365370
if err != nil {
366371
return nil, err
367372
}
368373

374+
if len(n.extraOptions) > 0 {
375+
configMap := make(common.ExtraOptions)
376+
err = json.Unmarshal(jsonConfig, &configMap)
377+
if err != nil {
378+
return nil, err
379+
}
380+
381+
for k, v := range n.extraOptions {
382+
configMap[k] = v
383+
}
384+
jsonConfig, err = json.Marshal(configMap)
385+
if err != nil {
386+
return nil, err
387+
}
388+
}
389+
369390
var cJsonConfig = C.CString(string(jsonConfig))
370391
var resp = C.allocResp(unsafe.Pointer(&wg))
371392

@@ -1314,7 +1335,7 @@ func GetFreePortIfNeeded(tcpPort int, discV5UDPPort int) (int, int, error) {
13141335
}
13151336

13161337
// Create & start node
1317-
func StartWakuNode(nodeName string, customCfg *common.WakuConfig) (*WakuNode, error) {
1338+
func StartWakuNode(nodeName string, customCfg *common.WakuConfig, opts ...WakuNodeOption) (*WakuNode, error) {
13181339

13191340
Debug("Initializing %s", nodeName)
13201341

@@ -1339,7 +1360,7 @@ func StartWakuNode(nodeName string, customCfg *common.WakuConfig) (*WakuNode, er
13391360
}
13401361

13411362
Debug("Creating %s", nodeName)
1342-
node, err := NewWakuNode(&nodeCfg, nodeName)
1363+
node, err := NewWakuNode(&nodeCfg, nodeName, opts...)
13431364
if err != nil {
13441365
Error("Failed to create %s: %v", nodeName, err)
13451366
return nil, err

waku/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package waku
2+
3+
import "github.com/waku-org/waku-go-bindings/waku/common"
4+
5+
type WakuNodeOption func(*WakuNode)
6+
7+
// 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
8+
// It is mostly for development and experimental purposes and will be removed in the future.
9+
func WithExtraOptions(extraOptions common.ExtraOptions) WakuNodeOption {
10+
return func(wn *WakuNode) {
11+
wn.extraOptions = extraOptions
12+
}
13+
}

0 commit comments

Comments
 (0)