Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial config spec #93

Merged
merged 13 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
180 changes: 180 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package config

import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/multiformats/go-multiaddr"
)

type JSONMaddr struct {
multiaddr.Multiaddr
}

func (jm *JSONMaddr) UnmarshalJSON(b []byte) error {
ma, err := multiaddr.NewMultiaddr(string(b))
if err != nil {
return err
}
jm = &JSONMaddr{ma}
return nil
}

type MaddrArray []multiaddr.Multiaddr

func (maa *MaddrArray) UnmarshalJSON(b []byte) error {
maStrings := strings.Split(string(b), ",")
*maa = make(MaddrArray, len(maStrings))
for i, s := range strings.Split(string(b), ",") {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
return err
}
(*maa)[i] = ma
}
return nil
}

type Bootstrap struct {
Enabled bool
Peers MaddrArray
}

type ConnectionManager struct {
Enabled bool
LowWaterMark int
HighWaterMark int
GracePeriod time.Duration
}

type GossipSubHeartbeat struct {
Interval time.Duration
InitialDelay time.Duration
}

type PubSub struct {
Enabled bool
Router string
Sign bool
SignStrict bool
GossipSubHeartbeat GossipSubHeartbeat
}

type Relay struct {
Enabled bool
Active bool
Hop bool
Discovery bool
Auto bool
}

type DHT struct {
Mode string
}

type PProf struct {
Enabled bool
Port uint
}

const DHTFullMode = "full"
const DHTClientMode = "client"

type Config struct {
ListenAddr JSONMaddr
Quiet bool
ID string
Bootstrap Bootstrap
DHT DHT
ConnectionManager ConnectionManager
QUIC bool
NatPortMap bool
PubSub PubSub
Relay Relay
AutoNat bool
HostAddresses MaddrArray
AnnounceAddresses MaddrArray
NoListen bool
MetricsAddress string
PProf PProf
}

func (c *Config) UnmarshalJSON(b []byte) error {
// settings defaults
type defaultConfig Config
ndc := defaultConfig(NewDefaultConfig())
dc := &ndc
if err := json.Unmarshal(b, dc); err != nil {
return err
}
*c = Config(*dc)

// validation
if err := c.Validate(); err != nil {
return err
}

return nil
}

func (c *Config) Validate() error {
if c.DHT.Mode != DHTClientMode && c.DHT.Mode != DHTFullMode && c.DHT.Mode != "" {
return errors.New(fmt.Sprintf("unknown DHT mode %s", c.DHT))
}
if c.Relay.Auto == true && (c.Relay.Enabled == false || c.DHT.Mode == "") {
return errors.New("can't have autorelay enabled without Relay enabled and DHT enabled")
}
return nil
}

func NewDefaultConfig() Config {
defaultListen, _ := multiaddr.NewMultiaddr("/unix/tmp/p2pd.sock")
return Config{
ListenAddr: JSONMaddr{defaultListen},
Quiet: false,
ID: "",
Bootstrap: Bootstrap{
Enabled: false,
Peers: make(MaddrArray, 0),
},
DHT: DHT{
Mode: "",
},
ConnectionManager: ConnectionManager{
Enabled: false,
LowWaterMark: 256,
HighWaterMark: 512,
GracePeriod: 120,
},
QUIC: false,
NatPortMap: false,
PubSub: PubSub{
Enabled: false,
Router: "gossipsub",
Sign: true,
SignStrict: false,
GossipSubHeartbeat: GossipSubHeartbeat{
Interval: 0,
InitialDelay: 0,
},
},
Relay: Relay{
Enabled: true,
Hop: false,
Discovery: false,
Auto: false,
},
AutoNat: false,
HostAddresses: make(MaddrArray, 0),
AnnounceAddresses: make(MaddrArray, 0),
NoListen: false,
MetricsAddress: "",
PProf: PProf{
Enabled: false,
Port: 0,
},
}
}
25 changes: 25 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"encoding/json"
"fmt"
"testing"

"github.com/multiformats/go-multiaddr"
)

func TestDefaultConfig(t *testing.T) {
const inputJson = "{}"
var c Config
if err := json.Unmarshal([]byte(inputJson), &c); err != nil {
t.Fatal(err)
}

defaultListen, err := multiaddr.NewMultiaddr("/unix/tmp/p2pd.sock")
if err != nil {
t.Fatal(err)
}
if c.ListenAddr.String() != defaultListen.String() {
t.Fatal(fmt.Sprintf("Expected %s, got %s", defaultListen.String(), c.ListenAddr.String()))
}
}
7 changes: 4 additions & 3 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package p2pd
import (
"context"
"fmt"
"github.com/libp2p/go-libp2p-daemon/config"
"sync"

logging "github.com/ipfs/go-log"
Expand Down Expand Up @@ -35,15 +36,15 @@ type Daemon struct {
handlers map[proto.ID]ma.Multiaddr
}

func NewDaemon(ctx context.Context, maddr ma.Multiaddr, dhtEnabled bool, dhtClient bool, opts ...libp2p.Option) (*Daemon, error) {
func NewDaemon(ctx context.Context, maddr ma.Multiaddr, dhtMode string, opts ...libp2p.Option) (*Daemon, error) {
d := &Daemon{
ctx: ctx,
handlers: make(map[proto.ID]ma.Multiaddr),
}

if dhtEnabled || dhtClient {
if dhtMode != "" {
var dhtOpts []dhtopts.Option
if dhtClient {
if dhtMode == config.DHTClientMode {
dhtOpts = append(dhtOpts, dhtopts.Client(true))
}

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ github.com/gxed/hashland/keccakpg v0.0.1 h1:wrk3uMNaMxbXiHibbPO4S0ymqJMm41WiudyF
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
github.com/gxed/hashland/murmur3 v0.0.1 h1:SheiaIt0sda5K+8FLz952/1iWS9zrnKsEJaOJu4ZbSc=
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v0.0.0-20180415215157-1395d1447324 h1:PV190X5/DzQ/tbFFG5YpT5mH6q+cHlfgqI5JuRnH9oE=
github.com/huin/goupnp v0.0.0-20180415215157-1395d1447324/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag=
github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo=
github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc=
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/ipfs/go-cid v0.0.1 h1:GBjWPktLnNyX0JiQCNFpUuUSoMw5KMyqrsejHYlILBE=
Expand All @@ -73,6 +76,7 @@ github.com/ipfs/go-todocounter v0.0.1 h1:kITWA5ZcQZfrUnDNkRn04Xzh0YFaDFXsoO2A81E
github.com/ipfs/go-todocounter v0.0.1/go.mod h1:l5aErvQc8qKE2r7NDMjmq5UNAvuZy0rC8BHOplkWvZ4=
github.com/jackpal/gateway v1.0.4 h1:LS5EHkLuQ6jzaHwULi0vL+JO0mU/n4yUtK8oUjHHOlM=
github.com/jackpal/gateway v1.0.4/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA=
github.com/jackpal/gateway v1.0.5 h1:qzXWUJfuMdlLMtt0a3Dgt+xkWQiA5itDEITVJtuSwMc=
github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA=
github.com/jackpal/go-nat-pmp v1.0.1 h1:i0LektDkO1QlrTm/cSuP+PyBCDnYvjPLGl4LdWEMiaA=
github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
Expand Down
Loading