Skip to content

Commit

Permalink
feat(c-bindings): custom gossipsub params (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Mar 10, 2023
1 parent 8826e2d commit 3c4a863
Show file tree
Hide file tree
Showing 4 changed files with 514 additions and 103 deletions.
102 changes: 102 additions & 0 deletions library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ interface JsonConfig {
keepAliveInterval?: number;
relay?: boolean;
relayTopics?: Array<string>;
gossipsubParameters?: GossipSubParameters;
minPeersToPublish?: number;
filter?: boolean;
discV5?: boolean;
Expand Down Expand Up @@ -306,6 +307,7 @@ If a key is `undefined`, or `null`, a default value will be set.
Default `true`.
- `relayTopics`: Array of pubsub topics that WakuRelay will automatically subscribe to when the node starts
Default `[]`
- `gossipSubParameters`: custom gossipsub parameters. See `GossipSubParameters` section for defaults
- `minPeersToPublish`: The minimum number of peers required on a topic to allow broadcasting a message.
Default `0`.
- `filter`: Enable filter protocol.
Expand Down Expand Up @@ -338,6 +340,106 @@ For example:
}
```


### `GossipsubParameters` type

Type holding custom gossipsub configuration:

```ts
interface GossipSubParameters {
D?: number;
D_low?: number;
D_high?: number;
D_score?: number;
D_out?: number;
HistoryLength?: number;
HistoryGossip?: number;
D_lazy?: number;
GossipFactor?: number;
GossipRetransmission?: number;
HeartbeatInitialDelayMs?: number;
HeartbeatIntervalSeconds?: number;
SlowHeartbeatWarning?: number;
FanoutTTLSeconds?: number;
PrunePeers?: number;
PruneBackoffSeconds?: number;
UnsubscribeBackoffSeconds?: number;
Connectors?: number;
MaxPendingConnections?: number;
ConnectionTimeoutSeconds?: number;
DirectConnectTicks?: number;
DirectConnectInitialDelaySeconds?: number;
OpportunisticGraftTicks?: number;
OpportunisticGraftPeers?: number;
GraftFloodThresholdSeconds?: number;
MaxIHaveLength?: number;
MaxIHaveMessages?: number;
IWantFollowupTimeSeconds?: number;
}
```

Fields:

All fields are optional.
If a key is `undefined`, or `null`, a default value will be set.

- `d`: optimal degree for a GossipSub topic mesh.
Default `6`
- `dLow`: lower bound on the number of peers we keep in a GossipSub topic mesh
Default `5`
- `dHigh`: upper bound on the number of peers we keep in a GossipSub topic mesh.
Default `12`
- `dScore`: affects how peers are selected when pruning a mesh due to over subscription.
Default `4`
- `dOut`: sets the quota for the number of outbound connections to maintain in a topic mesh.
Default `2`
- `historyLength`: controls the size of the message cache used for gossip.
Default `5`
- `historyGossip`: controls how many cached message ids we will advertise in IHAVE gossip messages.
Default `3`
- `dLazy`: affects how many peers we will emit gossip to at each heartbeat.
Default `6`
- `gossipFactor`: affects how many peers we will emit gossip to at each heartbeat.
Default `0.25`
- `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`
- `heartbeatInitialDelayMs`: short delay in milliseconds before the heartbeat timer begins after the router is initialized.
Default `100` milliseconds
- `heartbeatIntervalSeconds`: controls the time between heartbeats.
Default `1` second
- `slowHeartbeatWarning`: duration threshold for heartbeat processing before emitting a warning.
Default `0.1`
- `fanoutTTLSeconds`: controls how long we keep track of the fanout state.
Default `60` seconds
- `prunePeers`: controls the number of peers to include in prune Peer eXchange.
Default `16`
- `pruneBackoffSeconds`: controls the backoff time for pruned peers.
Default `60` seconds
- `unsubscribeBackoffSeconds`: controls the backoff time to use when unsuscribing from a topic.
Default `10` seconds
- `connectors`: number of active connection attempts for peers obtained through PX.
Default `8`
- `maxPendingConnections`: maximum number of pending connections for peers attempted through px.
Default `128`
- `connectionTimeoutSeconds`: timeout in seconds for connection attempts.
Default `30` seconds
- `directConnectTicks`: the number of heartbeat ticks for attempting to reconnect direct peers that are not currently connected.
Default `300`
- `directConnectInitialDelaySeconds`: initial delay before opening connections to direct peers.
Default `1` second
- `opportunisticGraftTicks`: number of heartbeat ticks for attempting to improve the mesh with opportunistic grafting.
Default `60`
- `opportunisticGraftPeers`: the number of peers to opportunistically graft.
Default `2`
- `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
- `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`
- `maxIHaveMessages`: max number of IHAVE messages to accept from a peer within a heartbeat.
Default `10`
- `iWantFollowupTimeSeconds`: Time to wait for a message requested through IWANT following an IHAVE advertisement.
Default `3` seconds

### `extern char* waku_new(char* jsonConfig)`

Instantiates a Waku node.
Expand Down
30 changes: 30 additions & 0 deletions library/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ func main() {}
// - keepAliveInterval: interval in seconds to ping all peers
// - relay: Enable WakuRelay. Default `true`
// - relayTopics: Array of pubsub topics that WakuRelay will automatically subscribe to when the node starts
// - gossipsubParams: an object containing custom gossipsub parameters. All attributes are optional, and if not specified, it will use default values.
// - d: optimal degree for a GossipSub topic mesh. Default `6`
// - dLow: lower bound on the number of peers we keep in a GossipSub topic mesh. Default `5`
// - dHigh: upper bound on the number of peers we keep in a GossipSub topic mesh. Default `12`
// - dScore: affects how peers are selected when pruning a mesh due to over subscription. Default `4`
// - dOut: sets the quota for the number of outbound connections to maintain in a topic mesh. Default `2`
// - historyLength: controls the size of the message cache used for gossip. Default `5`
// - historyGossip: controls how many cached message ids we will advertise in IHAVE gossip messages. Default `3`
// - dLazy: affects how many peers we will emit gossip to at each heartbeat. Default `6`
// - gossipFactor: affects how many peers we will emit gossip to at each heartbeat. Default `0.25`
// - 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`
// - heartbeatInitialDelayMs: short delay in milliseconds before the heartbeat timer begins after the router is initialized. Default `100` milliseconds
// - heartbeatIntervalSeconds: controls the time between heartbeats. Default `1` second
// - slowHeartbeatWarning: duration threshold for heartbeat processing before emitting a warning. Default `0.1`
// - fanoutTTLSeconds: controls how long we keep track of the fanout state. Default `60` seconds
// - prunePeers: controls the number of peers to include in prune Peer eXchange. Default `16`
// - pruneBackoffSeconds: controls the backoff time for pruned peers. Default `60` seconds
// - unsubscribeBackoffSeconds: controls the backoff time to use when unsuscribing from a topic. Default `10` seconds
// - connectors: number of active connection attempts for peers obtained through PX. Default `8`
// - maxPendingConnections: maximum number of pending connections for peers attempted through px. Default `128`
// - connectionTimeoutSeconds: timeout in seconds for connection attempts. Default `30` seconds
// - directConnectTicks: the number of heartbeat ticks for attempting to reconnect direct peers that are not currently connected. Default `300`
// - directConnectInitialDelaySeconds: initial delay before opening connections to direct peers. Default `1` second
// - opportunisticGraftTicks: number of heartbeat ticks for attempting to improve the mesh with opportunistic grafting. Default `60`
// - opportunisticGraftPeers: the number of peers to opportunistically graft. Default `2`
// - 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
// - 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`
// - maxIHaveMessages: max number of IHAVE messages to accept from a peer within a heartbeat. Default `10`
// - iWantFollowupTimeSeconds: Time to wait for a message requested through IWANT following an IHAVE advertisement. Default `3` seconds
//
// - minPeersToPublish: The minimum number of peers required on a topic to allow broadcasting a message. Default `0`
// - filter: Enable Filter. Default `false`
// - discV5: Enable DiscoveryV5. Default `false`
Expand Down
111 changes: 8 additions & 103 deletions mobile/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/p2p/enode"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -54,108 +55,6 @@ func randomHex(n int) (string, error) {
return hex.EncodeToString(bytes), nil
}

type wakuConfig struct {
Host *string `json:"host,omitempty"`
Port *int `json:"port,omitempty"`
AdvertiseAddress *string `json:"advertiseAddr,omitempty"`
NodeKey *string `json:"nodeKey,omitempty"`
LogLevel *string `json:"logLevel,omitempty"`
KeepAliveInterval *int `json:"keepAliveInterval,omitempty"`
EnableRelay *bool `json:"relay"`
RelayTopics []string `json:"relayTopics,omitempty"`
EnableFilter *bool `json:"filter,omitempty"`
MinPeersToPublish *int `json:"minPeersToPublish,omitempty"`
EnableDiscV5 *bool `json:"discV5,omitempty"`
DiscV5BootstrapNodes []string `json:"discV5BootstrapNodes,omitempty"`
DiscV5UDPPort *uint `json:"discV5UDPPort,omitempty"`
EnableStore *bool `json:"store,omitempty"`
DatabaseURL *string `json:"databaseURL,omitempty"`
RetentionMaxMessages *int `json:"storeRetentionMaxMessages,omitempty"`
RetentionTimeSeconds *int `json:"storeRetentionTimeSeconds,omitempty"`
}

var defaultHost = "0.0.0.0"
var defaultPort = 60000
var defaultKeepAliveInterval = 20
var defaultEnableRelay = true
var defaultMinPeersToPublish = 0
var defaultEnableFilter = false
var defaultEnableDiscV5 = false
var defaultDiscV5UDPPort = uint(9000)
var defaultLogLevel = "INFO"
var defaultEnableStore = false
var defaultDatabaseURL = "sqlite3://store.db"
var defaultRetentionMaxMessages = 10000
var defaultRetentionTimeSeconds = 30 * 24 * 60 * 60 // 30d

func getConfig(configJSON string) (wakuConfig, error) {
var config wakuConfig
if configJSON != "" {
err := json.Unmarshal([]byte(configJSON), &config)
if err != nil {
return wakuConfig{}, err
}
}

if config.Host == nil {
config.Host = &defaultHost
}

if config.EnableRelay == nil {
config.EnableRelay = &defaultEnableRelay
}

if config.EnableFilter == nil {
config.EnableFilter = &defaultEnableFilter
}

if config.EnableDiscV5 == nil {
config.EnableDiscV5 = &defaultEnableDiscV5
}

if config.Host == nil {
config.Host = &defaultHost
}

if config.Port == nil {
config.Port = &defaultPort
}

if config.DiscV5UDPPort == nil {
config.DiscV5UDPPort = &defaultDiscV5UDPPort
}

if config.KeepAliveInterval == nil {
config.KeepAliveInterval = &defaultKeepAliveInterval
}

if config.MinPeersToPublish == nil {
config.MinPeersToPublish = &defaultMinPeersToPublish
}

if config.LogLevel == nil {
config.LogLevel = &defaultLogLevel
}

if config.EnableStore == nil {
config.EnableStore = &defaultEnableStore
}

if config.DatabaseURL == nil {
config.DatabaseURL = &defaultDatabaseURL
}

if config.RetentionMaxMessages == nil {
config.RetentionMaxMessages = &defaultRetentionMaxMessages
}

if config.RetentionTimeSeconds == nil {
config.RetentionTimeSeconds = &defaultRetentionTimeSeconds
}

return config, nil
}

func NewNode(configJSON string) string {
if wakuState.node != nil {
return MakeJSONResponse(errors.New("go-waku already initialized. stop it first"))
Expand Down Expand Up @@ -196,7 +95,13 @@ func NewNode(configJSON string) string {
}

if *config.EnableRelay {
opts = append(opts, node.WithWakuRelayAndMinPeers(*config.MinPeersToPublish))
var pubsubOpt []pubsub.Option
if config.GossipSubParams != nil {
params := GetGossipSubParams(config.GossipSubParams)
pubsubOpt = append(pubsubOpt, pubsub.WithGossipSubParams(params))
}

opts = append(opts, node.WithWakuRelayAndMinPeers(*config.MinPeersToPublish, pubsubOpt...))
}

if *config.EnableFilter {
Expand Down
Loading

0 comments on commit 3c4a863

Please sign in to comment.