Skip to content

Commit 3c4a863

Browse files
feat(c-bindings): custom gossipsub params (#489)
1 parent 8826e2d commit 3c4a863

File tree

4 files changed

+514
-103
lines changed

4 files changed

+514
-103
lines changed

library/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ interface JsonConfig {
272272
keepAliveInterval?: number;
273273
relay?: boolean;
274274
relayTopics?: Array<string>;
275+
gossipsubParameters?: GossipSubParameters;
275276
minPeersToPublish?: number;
276277
filter?: boolean;
277278
discV5?: boolean;
@@ -306,6 +307,7 @@ If a key is `undefined`, or `null`, a default value will be set.
306307
Default `true`.
307308
- `relayTopics`: Array of pubsub topics that WakuRelay will automatically subscribe to when the node starts
308309
Default `[]`
310+
- `gossipSubParameters`: custom gossipsub parameters. See `GossipSubParameters` section for defaults
309311
- `minPeersToPublish`: The minimum number of peers required on a topic to allow broadcasting a message.
310312
Default `0`.
311313
- `filter`: Enable filter protocol.
@@ -338,6 +340,106 @@ For example:
338340
}
339341
```
340342

343+
344+
### `GossipsubParameters` type
345+
346+
Type holding custom gossipsub configuration:
347+
348+
```ts
349+
interface GossipSubParameters {
350+
D?: number;
351+
D_low?: number;
352+
D_high?: number;
353+
D_score?: number;
354+
D_out?: number;
355+
HistoryLength?: number;
356+
HistoryGossip?: number;
357+
D_lazy?: number;
358+
GossipFactor?: number;
359+
GossipRetransmission?: number;
360+
HeartbeatInitialDelayMs?: number;
361+
HeartbeatIntervalSeconds?: number;
362+
SlowHeartbeatWarning?: number;
363+
FanoutTTLSeconds?: number;
364+
PrunePeers?: number;
365+
PruneBackoffSeconds?: number;
366+
UnsubscribeBackoffSeconds?: number;
367+
Connectors?: number;
368+
MaxPendingConnections?: number;
369+
ConnectionTimeoutSeconds?: number;
370+
DirectConnectTicks?: number;
371+
DirectConnectInitialDelaySeconds?: number;
372+
OpportunisticGraftTicks?: number;
373+
OpportunisticGraftPeers?: number;
374+
GraftFloodThresholdSeconds?: number;
375+
MaxIHaveLength?: number;
376+
MaxIHaveMessages?: number;
377+
IWantFollowupTimeSeconds?: number;
378+
}
379+
```
380+
381+
Fields:
382+
383+
All fields are optional.
384+
If a key is `undefined`, or `null`, a default value will be set.
385+
386+
- `d`: optimal degree for a GossipSub topic mesh.
387+
Default `6`
388+
- `dLow`: lower bound on the number of peers we keep in a GossipSub topic mesh
389+
Default `5`
390+
- `dHigh`: upper bound on the number of peers we keep in a GossipSub topic mesh.
391+
Default `12`
392+
- `dScore`: affects how peers are selected when pruning a mesh due to over subscription.
393+
Default `4`
394+
- `dOut`: sets the quota for the number of outbound connections to maintain in a topic mesh.
395+
Default `2`
396+
- `historyLength`: controls the size of the message cache used for gossip.
397+
Default `5`
398+
- `historyGossip`: controls how many cached message ids we will advertise in IHAVE gossip messages.
399+
Default `3`
400+
- `dLazy`: affects how many peers we will emit gossip to at each heartbeat.
401+
Default `6`
402+
- `gossipFactor`: affects how many peers we will emit gossip to at each heartbeat.
403+
Default `0.25`
404+
- `gossipRetransmission`: controls how many times we will allow a peer to request the same message id through IWANT gossip before we start ignoring them.
405+
Default `3`
406+
- `heartbeatInitialDelayMs`: short delay in milliseconds before the heartbeat timer begins after the router is initialized.
407+
Default `100` milliseconds
408+
- `heartbeatIntervalSeconds`: controls the time between heartbeats.
409+
Default `1` second
410+
- `slowHeartbeatWarning`: duration threshold for heartbeat processing before emitting a warning.
411+
Default `0.1`
412+
- `fanoutTTLSeconds`: controls how long we keep track of the fanout state.
413+
Default `60` seconds
414+
- `prunePeers`: controls the number of peers to include in prune Peer eXchange.
415+
Default `16`
416+
- `pruneBackoffSeconds`: controls the backoff time for pruned peers.
417+
Default `60` seconds
418+
- `unsubscribeBackoffSeconds`: controls the backoff time to use when unsuscribing from a topic.
419+
Default `10` seconds
420+
- `connectors`: number of active connection attempts for peers obtained through PX.
421+
Default `8`
422+
- `maxPendingConnections`: maximum number of pending connections for peers attempted through px.
423+
Default `128`
424+
- `connectionTimeoutSeconds`: timeout in seconds for connection attempts.
425+
Default `30` seconds
426+
- `directConnectTicks`: the number of heartbeat ticks for attempting to reconnect direct peers that are not currently connected.
427+
Default `300`
428+
- `directConnectInitialDelaySeconds`: initial delay before opening connections to direct peers.
429+
Default `1` second
430+
- `opportunisticGraftTicks`: number of heartbeat ticks for attempting to improve the mesh with opportunistic grafting.
431+
Default `60`
432+
- `opportunisticGraftPeers`: the number of peers to opportunistically graft.
433+
Default `2`
434+
- `graftFloodThresholdSeconds`: If a GRAFT comes before GraftFloodThresholdSeconds has elapsed since the last PRUNE, then there is an extra score penalty applied to the peer through P7.
435+
Default `10` seconds
436+
- `maxIHaveLength`: max number of messages to include in an IHAVE message, also controls the max number of IHAVE ids we will accept and request with IWANT from a peer within a heartbeat.
437+
Default `5000`
438+
- `maxIHaveMessages`: max number of IHAVE messages to accept from a peer within a heartbeat.
439+
Default `10`
440+
- `iWantFollowupTimeSeconds`: Time to wait for a message requested through IWANT following an IHAVE advertisement.
441+
Default `3` seconds
442+
341443
### `extern char* waku_new(char* jsonConfig)`
342444

343445
Instantiates a Waku node.

library/api.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,36 @@ func main() {}
2828
// - keepAliveInterval: interval in seconds to ping all peers
2929
// - relay: Enable WakuRelay. Default `true`
3030
// - relayTopics: Array of pubsub topics that WakuRelay will automatically subscribe to when the node starts
31+
// - gossipsubParams: an object containing custom gossipsub parameters. All attributes are optional, and if not specified, it will use default values.
32+
// - d: optimal degree for a GossipSub topic mesh. Default `6`
33+
// - dLow: lower bound on the number of peers we keep in a GossipSub topic mesh. Default `5`
34+
// - dHigh: upper bound on the number of peers we keep in a GossipSub topic mesh. Default `12`
35+
// - dScore: affects how peers are selected when pruning a mesh due to over subscription. Default `4`
36+
// - dOut: sets the quota for the number of outbound connections to maintain in a topic mesh. Default `2`
37+
// - historyLength: controls the size of the message cache used for gossip. Default `5`
38+
// - historyGossip: controls how many cached message ids we will advertise in IHAVE gossip messages. Default `3`
39+
// - dLazy: affects how many peers we will emit gossip to at each heartbeat. Default `6`
40+
// - gossipFactor: affects how many peers we will emit gossip to at each heartbeat. Default `0.25`
41+
// - gossipRetransmission: controls how many times we will allow a peer to request the same message id through IWANT gossip before we start ignoring them. Default `3`
42+
// - heartbeatInitialDelayMs: short delay in milliseconds before the heartbeat timer begins after the router is initialized. Default `100` milliseconds
43+
// - heartbeatIntervalSeconds: controls the time between heartbeats. Default `1` second
44+
// - slowHeartbeatWarning: duration threshold for heartbeat processing before emitting a warning. Default `0.1`
45+
// - fanoutTTLSeconds: controls how long we keep track of the fanout state. Default `60` seconds
46+
// - prunePeers: controls the number of peers to include in prune Peer eXchange. Default `16`
47+
// - pruneBackoffSeconds: controls the backoff time for pruned peers. Default `60` seconds
48+
// - unsubscribeBackoffSeconds: controls the backoff time to use when unsuscribing from a topic. Default `10` seconds
49+
// - connectors: number of active connection attempts for peers obtained through PX. Default `8`
50+
// - maxPendingConnections: maximum number of pending connections for peers attempted through px. Default `128`
51+
// - connectionTimeoutSeconds: timeout in seconds for connection attempts. Default `30` seconds
52+
// - directConnectTicks: the number of heartbeat ticks for attempting to reconnect direct peers that are not currently connected. Default `300`
53+
// - directConnectInitialDelaySeconds: initial delay before opening connections to direct peers. Default `1` second
54+
// - opportunisticGraftTicks: number of heartbeat ticks for attempting to improve the mesh with opportunistic grafting. Default `60`
55+
// - opportunisticGraftPeers: the number of peers to opportunistically graft. Default `2`
56+
// - graftFloodThresholdSeconds: If a GRAFT comes before GraftFloodThresholdSeconds has elapsed since the last PRUNE, then there is an extra score penalty applied to the peer through P7. Default `10` seconds
57+
// - maxIHaveLength: max number of messages to include in an IHAVE message, also controls the max number of IHAVE ids we will accept and request with IWANT from a peer within a heartbeat. Default `5000`
58+
// - maxIHaveMessages: max number of IHAVE messages to accept from a peer within a heartbeat. Default `10`
59+
// - iWantFollowupTimeSeconds: Time to wait for a message requested through IWANT following an IHAVE advertisement. Default `3` seconds
60+
//
3161
// - minPeersToPublish: The minimum number of peers required on a topic to allow broadcasting a message. Default `0`
3262
// - filter: Enable Filter. Default `false`
3363
// - discV5: Enable DiscoveryV5. Default `false`

mobile/api.go

Lines changed: 8 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/ethereum/go-ethereum/crypto"
2222
"github.com/ethereum/go-ethereum/crypto/secp256k1"
2323
"github.com/ethereum/go-ethereum/p2p/enode"
24+
pubsub "github.com/libp2p/go-libp2p-pubsub"
2425
"github.com/libp2p/go-libp2p/core/peer"
2526
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
2627
"github.com/multiformats/go-multiaddr"
@@ -54,108 +55,6 @@ func randomHex(n int) (string, error) {
5455
return hex.EncodeToString(bytes), nil
5556
}
5657

57-
type wakuConfig struct {
58-
Host *string `json:"host,omitempty"`
59-
Port *int `json:"port,omitempty"`
60-
AdvertiseAddress *string `json:"advertiseAddr,omitempty"`
61-
NodeKey *string `json:"nodeKey,omitempty"`
62-
LogLevel *string `json:"logLevel,omitempty"`
63-
KeepAliveInterval *int `json:"keepAliveInterval,omitempty"`
64-
EnableRelay *bool `json:"relay"`
65-
RelayTopics []string `json:"relayTopics,omitempty"`
66-
EnableFilter *bool `json:"filter,omitempty"`
67-
MinPeersToPublish *int `json:"minPeersToPublish,omitempty"`
68-
EnableDiscV5 *bool `json:"discV5,omitempty"`
69-
DiscV5BootstrapNodes []string `json:"discV5BootstrapNodes,omitempty"`
70-
DiscV5UDPPort *uint `json:"discV5UDPPort,omitempty"`
71-
EnableStore *bool `json:"store,omitempty"`
72-
DatabaseURL *string `json:"databaseURL,omitempty"`
73-
RetentionMaxMessages *int `json:"storeRetentionMaxMessages,omitempty"`
74-
RetentionTimeSeconds *int `json:"storeRetentionTimeSeconds,omitempty"`
75-
}
76-
77-
var defaultHost = "0.0.0.0"
78-
var defaultPort = 60000
79-
var defaultKeepAliveInterval = 20
80-
var defaultEnableRelay = true
81-
var defaultMinPeersToPublish = 0
82-
var defaultEnableFilter = false
83-
var defaultEnableDiscV5 = false
84-
var defaultDiscV5UDPPort = uint(9000)
85-
var defaultLogLevel = "INFO"
86-
var defaultEnableStore = false
87-
var defaultDatabaseURL = "sqlite3://store.db"
88-
var defaultRetentionMaxMessages = 10000
89-
var defaultRetentionTimeSeconds = 30 * 24 * 60 * 60 // 30d
90-
91-
func getConfig(configJSON string) (wakuConfig, error) {
92-
var config wakuConfig
93-
if configJSON != "" {
94-
err := json.Unmarshal([]byte(configJSON), &config)
95-
if err != nil {
96-
return wakuConfig{}, err
97-
}
98-
}
99-
100-
if config.Host == nil {
101-
config.Host = &defaultHost
102-
}
103-
104-
if config.EnableRelay == nil {
105-
config.EnableRelay = &defaultEnableRelay
106-
}
107-
108-
if config.EnableFilter == nil {
109-
config.EnableFilter = &defaultEnableFilter
110-
}
111-
112-
if config.EnableDiscV5 == nil {
113-
config.EnableDiscV5 = &defaultEnableDiscV5
114-
}
115-
116-
if config.Host == nil {
117-
config.Host = &defaultHost
118-
}
119-
120-
if config.Port == nil {
121-
config.Port = &defaultPort
122-
}
123-
124-
if config.DiscV5UDPPort == nil {
125-
config.DiscV5UDPPort = &defaultDiscV5UDPPort
126-
}
127-
128-
if config.KeepAliveInterval == nil {
129-
config.KeepAliveInterval = &defaultKeepAliveInterval
130-
}
131-
132-
if config.MinPeersToPublish == nil {
133-
config.MinPeersToPublish = &defaultMinPeersToPublish
134-
}
135-
136-
if config.LogLevel == nil {
137-
config.LogLevel = &defaultLogLevel
138-
}
139-
140-
if config.EnableStore == nil {
141-
config.EnableStore = &defaultEnableStore
142-
}
143-
144-
if config.DatabaseURL == nil {
145-
config.DatabaseURL = &defaultDatabaseURL
146-
}
147-
148-
if config.RetentionMaxMessages == nil {
149-
config.RetentionMaxMessages = &defaultRetentionMaxMessages
150-
}
151-
152-
if config.RetentionTimeSeconds == nil {
153-
config.RetentionTimeSeconds = &defaultRetentionTimeSeconds
154-
}
155-
156-
return config, nil
157-
}
158-
15958
func NewNode(configJSON string) string {
16059
if wakuState.node != nil {
16160
return MakeJSONResponse(errors.New("go-waku already initialized. stop it first"))
@@ -196,7 +95,13 @@ func NewNode(configJSON string) string {
19695
}
19796

19897
if *config.EnableRelay {
199-
opts = append(opts, node.WithWakuRelayAndMinPeers(*config.MinPeersToPublish))
98+
var pubsubOpt []pubsub.Option
99+
if config.GossipSubParams != nil {
100+
params := GetGossipSubParams(config.GossipSubParams)
101+
pubsubOpt = append(pubsubOpt, pubsub.WithGossipSubParams(params))
102+
}
103+
104+
opts = append(opts, node.WithWakuRelayAndMinPeers(*config.MinPeersToPublish, pubsubOpt...))
200105
}
201106

202107
if *config.EnableFilter {

0 commit comments

Comments
 (0)