From 8ddd8be02d956063d0d94e781501df55de5287de Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Fri, 29 Mar 2019 16:24:28 -0700 Subject: [PATCH 01/13] initial config spec --- specs/CONFIG.md | 207 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 specs/CONFIG.md diff --git a/specs/CONFIG.md b/specs/CONFIG.md new file mode 100644 index 00000000..d5c968e8 --- /dev/null +++ b/specs/CONFIG.md @@ -0,0 +1,207 @@ +# libp2p Daemon Config Spec + +The libp2p daemon is an interface to libp2p, and as such has a large number of +configuration options. For complex deployments, these options can become +unwieldy on the command line. For this, we allow the ability to provide a +configuration spec via json from a file or from stdin + +_At the moment, this is a living document. As such, it will be susceptible to +changes until stabilization._ + +## Considerations + +As this is a spec that must be shared between multiple implementations, it is +probably best that the structure be generated from some shared source of truth, +instead of depending on discipline to keep the implementation consistent +between language implementations. For instance, the config structure could be +generated as a protobuf message. + +## Command Line + +There are two ways to provide a JSON configuration to the daemon. Both methods +cause all other configuration command line options to be ignored. + +### Read from a file +`p2pd -f ./conf.json` + +### Read from stdin +`cat ./conf.json | p2pd -i` + + +## Schema + +* `Field Name` + * Description + * Type + * `default` + +* `Listen` + * Daemon control listen multiaddr + * Maddr String + * `"/unix/tmp/p2pd.sock"` +* `Quiet` + * Be Quiet + * Boolean + * `false` +* `ID` + * Peer identity; private key file + * String + * `""` +* `Bootstrap` + * `Enabled` + * Connects to bootstrap peers and bootstraps the dht if enabled + * Boolean + * `false` + * `Peers` + * List of bootstrap peers; defaults to the IPFS DHT peers + * Array[Maddr String] + * `[]` +* `DHT` + * `Enabled` + * Enables the DHT in full node mode + * Boolean + * `false` + * `ClientMode` + * Enables the DHT in client mode + * Boolean + * `false` +* `ConnectionManager` + * `Enabled` + * Enables the Connection Manager + * Boolean + * `false` + * `LowWaterMark` + * Connection Manager Low Water mark + * Integer + * `256` + * `HighWaterMark` + * Connection Manager High Water mark + * Integer + * `512` + * `GracePeriod` + * Connection Manager grace period (in seconds) + * Integer + * `120` +* `QUIC` + * Enables the QUIC transport + * Boolean + * `false` +* `NatPortMap` + * Enables NAT port mapping + * Boolean + * `false` +* `PubSub` + * `Enabled` + * Enables pubsub + * Boolean + * `false` + * `Router` + * Specifies the pubsub router implementation + * String + * `"gossipsub"` + * `Sign` + * Enables pubsub message signing + * Boolean + * `true` + * `SignStrict` + * Enables pubsub strict signature verification + * Boolean + * `false` + * `GossipSubHeartbeat` + * `Interval` + * Specifies the gossipsub heartbeat interval + * Integer + * `0` + * `InitialDelay` + * Specifies the gossipsub initial heartbeat delay + * Integer + * `0` +* `Relay` + * `Enabled` + * Enables circuit relay + * Boolean + * `true` + * `Active` + * Enables active mode for relay + * Boolean + * `false` + * `Hop` + * Enables hop for relay + * Boolean + * `false` + * `Discovery` + * Enables passive discovery for relay + * Boolean + * `false` + * `Auto` + * Enables autorelay + * Boolean + * `false` +* `AutoNat` + * Enables the AutoNAT service + * Boolean + * `false` +* `HostAddresses` + * List of multiaddrs the host should listen on + * Array[Maddr String] + * `[]` +* `AnnounceAddresses` + * List of multiaddrs the host should announce to the network + * Array[Maddr String] + * `[]` +* `NoListen` + * Sets the host to listen on no addresses + * Boolean + * `false` +* `MetricsAddress` + * An address to bind the metrics handler to + * Maddr String + * `"""` + +### Default Example + +```json +{ + "ListenAddr": "/unix/tmp/p2pd.sock", + "Quiet": false, + "ID": "", + "Bootstrap": { + "Enabled": false, + "Peers": [] + }, + "DHT": { + "Enabled": false, + "ClientMode": false + }, + "ConnectionManager": { + "Enabled": false, + "LowWaterMark": 256, + "HighWaterMark": 512, + "GracePeriod": 120 + }, + "QUIC": false, + "NatPortMap": false, + "PubSub": { + "Enabled": false, + "Router": "gossipsub", + "Sign": true, + "SignStrict": true, + "GossipSubHeartbeat": { + "Interval": 0, + "InitialDelay": 0 + } + }, + "Relay": { + "Enabled": true, + "Active": false, + "Hop": false, + "Discovery": false, + "Auto": false + }, + "AutoNat": false, + "HostAddresses": [], + "AnnounceAddresses": [], + "NoListen": false, + "MetricsAddress": "" +} +``` From b450d7dcfe9e22ad6c291e91d6940c16811422de Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 2 Apr 2019 18:07:11 -0700 Subject: [PATCH 02/13] modify daemon to use config files --- config/config.go | 167 ++++++++++++++++++++++++++++ config/config_test.go | 25 +++++ daemon.go | 7 +- p2pd/main.go | 251 +++++++++++++++++++++++++++++------------- specs/CONFIG.md | 19 +--- test/utils.go | 2 +- 6 files changed, 375 insertions(+), 96 deletions(-) create mode 100644 config/config.go create mode 100644 config/config_test.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 00000000..88965c9b --- /dev/null +++ b/config/config.go @@ -0,0 +1,167 @@ +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 + } + jma := ma.(JSONMaddr) + jm = &jma + 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 +} + +const DHTFullMode = "full" +const DHTClientMode = "client" + + +type Config struct { + ListenAddr JSONMaddr + Quiet bool + ID string + Bootstrap bootstrap + DHT string + ConnectionManager connectionManager + QUIC bool + NatPortMap bool + PubSub pubSub + Relay relay + AutoNat bool + HostAddresses MaddrArray + AnnounceAddresses MaddrArray + NoListen bool + MetricsAddress string +} + +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 != DHTClientMode && c.DHT != DHTFullMode && c.DHT != "" { + return errors.New(fmt.Sprintf("unknown DHT mode %s", c.DHT)) + } + if c.Relay.Auto == true && (c.Relay.Enabled == false || c.DHT == "") { + 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: "", + 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: nil, + } +} \ No newline at end of file diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 00000000..b55b9fdc --- /dev/null +++ b/config/config_test.go @@ -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())) + } +} \ No newline at end of file diff --git a/daemon.go b/daemon.go index e9c78dcb..59243662 100644 --- a/daemon.go +++ b/daemon.go @@ -3,6 +3,7 @@ package p2pd import ( "context" "fmt" + "github.com/libp2p/go-libp2p-daemon/config" "sync" logging "github.com/ipfs/go-log" @@ -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)) } diff --git a/p2pd/main.go b/p2pd/main.go index 8f25b288..d3bf60f5 100644 --- a/p2pd/main.go +++ b/p2pd/main.go @@ -1,11 +1,16 @@ package main import ( + "bufio" "context" + "encoding/json" "flag" "fmt" + "github.com/libp2p/go-libp2p-daemon/config" + "io/ioutil" "log" "net/http" + "os" "strings" libp2p "github.com/libp2p/go-libp2p" @@ -85,6 +90,8 @@ func main() { announceAddrs := flag.String("announceAddrs", "", "comma separated list of multiaddrs the host should announce to the network") noListen := flag.Bool("noListenAddrs", false, "sets the host to listen on no addresses") metricsAddr := flag.String("metricsAddr", "", "an address to bind the metrics handler to") + configFilename := flag.String("f", "", "a file from which to read a json representation of the deamon config") + configStdin := flag.Bool("i", false, "have the daemon read the json config from stdin") pprof := flag.Bool("pprof", false, "Enables the HTTP pprof handler, listening on the first port "+ "available in the range [6060-7800], or on the user-provided port via -pprofPort") pprofPort := flag.Uint("pprofPort", 0, "Binds the HTTP pprof handler to a specific port; "+ @@ -92,6 +99,7 @@ func main() { flag.Parse() + var c config.Config if *pprof { // an invalid port number will fail within the function. go pprofHTTP(int(*pprofPort)) @@ -99,104 +107,200 @@ func main() { var opts []libp2p.Option - maddr, err := multiaddr.NewMultiaddr(*maddrString) - if err != nil { - log.Fatal(err) + if *configStdin { + stdin := bufio.NewReader(os.Stdin) + body, err := ioutil.ReadAll(stdin) + if err != nil { + log.Fatal(err) + } + if err := json.Unmarshal(body, c); err != nil { + log.Fatal(err) + } + } else if *configFilename != "" { + body, err := ioutil.ReadFile(*configFilename) + if err != nil { + log.Fatal(err) + } + if err := json.Unmarshal(body, c); err != nil { + log.Fatal(err) + } + } else { + c = config.NewDefaultConfig() + maddr, err := multiaddr.NewMultiaddr(*maddrString) + if err != nil { + log.Fatal(err) + } + c.ListenAddr = config.JSONMaddr{maddr} + if *id != "" { + c.ID = *id + } + if *hostAddrs != "" { + addrStrings := strings.Split(*hostAddrs, ",") + ha := make([]multiaddr.Multiaddr, len(addrStrings)) + for i, s := range addrStrings { + ma, err := multiaddr.NewMultiaddr(s) + if err != nil { + log.Fatal(err) + } + (ha)[i] = ma + } + c.HostAddresses = ha + } + if *announceAddrs != "" { + addrStrings := strings.Split(*announceAddrs, ",") + ha := make([]multiaddr.Multiaddr, len(addrStrings)) + for i, s := range addrStrings { + ma, err := multiaddr.NewMultiaddr(s) + if err != nil { + log.Fatal(err) + } + (ha)[i] = ma + } + c.AnnounceAddresses = ha + } + if *connMgr { + c.ConnectionManager.Enabled = true + c.ConnectionManager.GracePeriod = *connMgrGrace + c.ConnectionManager.HighWaterMark = *connMgrHi + c.ConnectionManager.LowWaterMark = *connMgrLo + } + if *QUIC { + c.QUIC = true + } + if *natPortMap { + c.NatPortMap = true + } + if *relayEnabled { + c.Relay.Enabled = true + if *relayActive { + c.Relay.Active = true + } + if *relayHop { + c.Relay.Hop = true + } + if *relayDiscovery { + c.Relay.Discovery = true + } + } + if *autoRelay { + c.Relay.Auto = true + } + if *noListen { + c.NoListen = true + } + if *autonat { + c.AutoNat = true + } + if *pubsub { + c.PubSub.Enabled = true + c.PubSub.Router = *pubsubRouter + c.PubSub.Sign = *pubsubSign + c.PubSub.SignStrict = *pubsubSignStrict + if *gossipsubHeartbeatInterval > 0 { + c.PubSub.GossipSubHeartbeat.Interval = *gossipsubHeartbeatInterval + } + if *gossipsubHeartbeatInitialDelay > 0 { + c.PubSub.GossipSubHeartbeat.InitialDelay = *gossipsubHeartbeatInitialDelay + } + } + if *bootstrapPeers != "" { + addrStrings := strings.Split(*bootstrapPeers, ",") + bps := make([]multiaddr.Multiaddr, len(addrStrings)) + for i, s := range addrStrings { + ma, err := multiaddr.NewMultiaddr(s) + if err != nil { + log.Fatal(err) + } + (bps)[i] = ma + } + c.Bootstrap.Peers = bps + } + if *bootstrap { + c.Bootstrap.Enabled = true + } + if *quiet { + c.Quiet = true + } + if *metricsAddr != "" { + c.MetricsAddress = *metricsAddr + } + if *dht { + c.DHT = config.DHTFullMode + } else if *dhtClient { + c.DHT = config.DHTClientMode + } + + if err := c.Validate(); err != nil { + log.Fatal(err) + } } - if *id != "" { - key, err := p2pd.ReadIdentity(*id) + // collect opts + if c.ID != "" { + key, err := p2pd.ReadIdentity(c.ID) if err != nil { log.Fatal(err) } opts = append(opts, libp2p.Identity(key)) } - - if *hostAddrs != "" { - addrs := strings.Split(*hostAddrs, ",") - opts = append(opts, libp2p.ListenAddrStrings(addrs...)) + if len(c.HostAddresses) > 0 { + opts = append(opts, libp2p.ListenAddrs(c.HostAddresses...)) } - - if *announceAddrs != "" { - addrs := strings.Split(*announceAddrs, ",") - maddrs := make([]multiaddr.Multiaddr, 0, len(addrs)) - for _, a := range addrs { - maddr, err := multiaddr.NewMultiaddr(a) - if err != nil { - log.Fatal(err) - } - maddrs = append(maddrs, maddr) - } + if len(c.AnnounceAddresses) > 0 { opts = append(opts, libp2p.AddrsFactory(func([]multiaddr.Multiaddr) []multiaddr.Multiaddr { - return maddrs + return c.AnnounceAddresses })) } - - if *connMgr { - cm := connmgr.NewConnManager(*connMgrLo, *connMgrHi, *connMgrGrace) + if c.ConnectionManager.Enabled { + cm := connmgr.NewConnManager(c.ConnectionManager.LowWaterMark, + c.ConnectionManager.HighWaterMark, + c.ConnectionManager.GracePeriod) opts = append(opts, libp2p.ConnectionManager(cm)) } - - if *QUIC { + if c.QUIC { opts = append(opts, libp2p.DefaultTransports, libp2p.Transport(quic.NewTransport), ) - - // if we explicitly specify a transport, we must also explicitly specify the listen addrs - if *hostAddrs == "" { - opts = append(opts, - libp2p.ListenAddrStrings( - "/ip4/0.0.0.0/tcp/0", - "/ip4/0.0.0.0/udp/0/quic", - "/ip6/::1/tcp/0", - "/ip6/::1/udp/0/quic", - )) + if len(c.HostAddresses) == 0 { + log.Fatal("if we explicitly specify a transport, we must also explicitly specify the listen addrs") } } - - if *natPortMap { + if c.NatPortMap { opts = append(opts, libp2p.NATPortMap()) } - - if *relayEnabled { + if c.Relay.Enabled { var relayOpts []relay.RelayOpt - if *relayActive { + if c.Relay.Active { relayOpts = append(relayOpts, relay.OptActive) } - if *relayHop { + if c.Relay.Hop { relayOpts = append(relayOpts, relay.OptHop) } - if *relayDiscovery { + if c.Relay.Discovery { relayOpts = append(relayOpts, relay.OptDiscovery) } opts = append(opts, libp2p.EnableRelay(relayOpts...)) - } - if *autoRelay { - if !(*dht || *dhtClient) { - log.Fatal("DHT must be enabled in order to enable autorelay") - } - if !*relayEnabled { - log.Fatal("Relay must be enabled to enable autorelay") + if c.Relay.Auto { + opts = append(opts, libp2p.EnableAutoRelay()) } - opts = append(opts, libp2p.EnableAutoRelay()) } - - if *noListen { + if c.NoListen { opts = append(opts, libp2p.NoListenAddrs) } - d, err := p2pd.NewDaemon(context.Background(), maddr, *dht, *dhtClient, opts...) + // start daemon + d, err := p2pd.NewDaemon(context.Background(), c.ListenAddr, c.DHT, opts...) if err != nil { log.Fatal(err) } - if *autonat { + if c.AutoNat { var opts []libp2p.Option // allow the AutoNAT service to dial back quic addrs. - if *QUIC { + if c.QUIC { opts = append(opts, libp2p.DefaultTransports, libp2p.Transport(quic.NewTransport), @@ -208,46 +312,35 @@ func main() { } } - if *pubsub { - if *gossipsubHeartbeatInterval > 0 { - ps.GossipSubHeartbeatInterval = *gossipsubHeartbeatInterval - } + if c.PubSub.Enabled { + ps.GossipSubHeartbeatInterval = c.PubSub.GossipSubHeartbeat.Interval + ps.GossipSubHeartbeatInitialDelay = c.PubSub.GossipSubHeartbeat.InitialDelay - if *gossipsubHeartbeatInitialDelay > 0 { - ps.GossipSubHeartbeatInitialDelay = *gossipsubHeartbeatInitialDelay - } - - err = d.EnablePubsub(*pubsubRouter, *pubsubSign, *pubsubSignStrict) + err = d.EnablePubsub(c.PubSub.Router, c.PubSub.Sign, c.PubSub.SignStrict) if err != nil { log.Fatal(err) } } - if *bootstrapPeers != "" { - for _, s := range strings.Split(*bootstrapPeers, ",") { - ma, err := multiaddr.NewMultiaddr(s) - if err != nil { - log.Fatalf("error parsing bootstrap peer %q: %v", s, err) - } - p2pd.BootstrapPeers = append(p2pd.BootstrapPeers, ma) - } + if len(c.Bootstrap.Peers) > 0 { + p2pd.BootstrapPeers = c.Bootstrap.Peers } - if *bootstrap { + if c.Bootstrap.Enabled { err = d.Bootstrap() if err != nil { log.Fatal(err) } } - if !*quiet { - fmt.Printf("Control socket: %s\n", maddr.String()) + if !c.Quiet { + fmt.Printf("Control socket: %s\n", c.ListenAddr.String()) fmt.Printf("Peer ID: %s\n", d.ID().Pretty()) fmt.Printf("Peer Addrs:\n") for _, addr := range d.Addrs() { fmt.Printf("%s\n", addr.String()) } - if *bootstrap && *bootstrapPeers != "" { + if c.Bootstrap.Enabled && len(c.Bootstrap.Peers) > 0 { fmt.Printf("Bootstrap peers:\n") for _, p := range p2pd.BootstrapPeers { fmt.Printf("%s\n", p) @@ -255,9 +348,9 @@ func main() { } } - if *metricsAddr != "" { + if c.MetricsAddress != "" { http.Handle("/metrics", promhttp.Handler()) - go func() { log.Println(http.ListenAndServe(*metricsAddr, nil)) }() + go func() { log.Println(http.ListenAndServe(c.MetricsAddress, nil)) }() } select {} diff --git a/specs/CONFIG.md b/specs/CONFIG.md index d5c968e8..a4379cab 100644 --- a/specs/CONFIG.md +++ b/specs/CONFIG.md @@ -35,7 +35,7 @@ cause all other configuration command line options to be ignored. * Type * `default` -* `Listen` +* `ListenAddr` * Daemon control listen multiaddr * Maddr String * `"/unix/tmp/p2pd.sock"` @@ -58,13 +58,9 @@ cause all other configuration command line options to be ignored. * `[]` * `DHT` * `Enabled` - * Enables the DHT in full node mode - * Boolean - * `false` - * `ClientMode` - * Enables the DHT in client mode - * Boolean - * `false` + * Enables the DHT in full node mode or client mode + * String (`"full"`|`"client"`|`""`) + * `""` * `ConnectionManager` * `Enabled` * Enables the Connection Manager @@ -169,10 +165,7 @@ cause all other configuration command line options to be ignored. "Enabled": false, "Peers": [] }, - "DHT": { - "Enabled": false, - "ClientMode": false - }, + "DHT": "", "ConnectionManager": { "Enabled": false, "LowWaterMark": 256, @@ -185,7 +178,7 @@ cause all other configuration command line options to be ignored. "Enabled": false, "Router": "gossipsub", "Sign": true, - "SignStrict": true, + "SignStrict": false, "GossipSubHeartbeat": { "Interval": 0, "InitialDelay": 0 diff --git a/test/utils.go b/test/utils.go index 5aa37278..be332ce9 100644 --- a/test/utils.go +++ b/test/utils.go @@ -40,7 +40,7 @@ func createTempDir(t *testing.T) (string, string, func()) { func createDaemon(t *testing.T, daemonAddr ma.Multiaddr) (*p2pd.Daemon, func()) { ctx, cancelCtx := context.WithCancel(context.Background()) - daemon, err := p2pd.NewDaemon(ctx, daemonAddr, false, false) + daemon, err := p2pd.NewDaemon(ctx, daemonAddr, "") daemon.EnablePubsub("gossipsub", false, false) if err != nil { t.Fatal(err) From dc47fd22cd16769a5e340bf873177a9166cdb2ae Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 2 Apr 2019 18:59:03 -0700 Subject: [PATCH 03/13] string type not nil --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 88965c9b..b1528213 100644 --- a/config/config.go +++ b/config/config.go @@ -162,6 +162,6 @@ func NewDefaultConfig() Config { HostAddresses: make(MaddrArray, 0), AnnounceAddresses: make(MaddrArray, 0), NoListen: false, - MetricsAddress: nil, + MetricsAddress: "", } } \ No newline at end of file From 5b3e88d5b9e3c7ef9cf5b8eda1142d4a9fddf59a Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 2 Apr 2019 19:02:51 -0700 Subject: [PATCH 04/13] go fmt --- config/config.go | 92 +++++++++++++++++++++---------------------- config/config_test.go | 2 +- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/config/config.go b/config/config.go index b1528213..b99c7601 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,6 @@ import ( "github.com/multiformats/go-multiaddr" ) - type JSONMaddr struct { multiaddr.Multiaddr } @@ -42,57 +41,56 @@ func (maa *MaddrArray) UnmarshalJSON(b []byte) error { type bootstrap struct { Enabled bool - Peers MaddrArray + Peers MaddrArray } type connectionManager struct { - Enabled bool - LowWaterMark int + Enabled bool + LowWaterMark int HighWaterMark int - GracePeriod time.Duration + GracePeriod time.Duration } type gossipSubHeartbeat struct { - Interval time.Duration + Interval time.Duration InitialDelay time.Duration } type pubSub struct { - Enabled bool - Router string - Sign bool - SignStrict bool + Enabled bool + Router string + Sign bool + SignStrict bool GossipSubHeartbeat gossipSubHeartbeat } type relay struct { - Enabled bool - Active bool - Hop bool + Enabled bool + Active bool + Hop bool Discovery bool - Auto bool + Auto bool } const DHTFullMode = "full" const DHTClientMode = "client" - type Config struct { - ListenAddr JSONMaddr - Quiet bool - ID string - Bootstrap bootstrap - DHT string + ListenAddr JSONMaddr + Quiet bool + ID string + Bootstrap bootstrap + DHT string ConnectionManager connectionManager - QUIC bool - NatPortMap bool - PubSub pubSub - Relay relay - AutoNat bool - HostAddresses MaddrArray + QUIC bool + NatPortMap bool + PubSub pubSub + Relay relay + AutoNat bool + HostAddresses MaddrArray AnnounceAddresses MaddrArray - NoListen bool - MetricsAddress string + NoListen bool + MetricsAddress string } func (c *Config) UnmarshalJSON(b []byte) error { @@ -127,41 +125,41 @@ func NewDefaultConfig() Config { defaultListen, _ := multiaddr.NewMultiaddr("/unix/tmp/p2pd.sock") return Config{ ListenAddr: JSONMaddr{defaultListen}, - Quiet: false, - ID: "", + Quiet: false, + ID: "", Bootstrap: bootstrap{ Enabled: false, - Peers: make(MaddrArray, 0), + Peers: make(MaddrArray, 0), }, DHT: "", ConnectionManager: connectionManager{ - Enabled: false, - LowWaterMark: 256, + Enabled: false, + LowWaterMark: 256, HighWaterMark: 512, - GracePeriod: 120, + GracePeriod: 120, }, - QUIC: false, + QUIC: false, NatPortMap: false, PubSub: pubSub{ - Enabled: false, - Router: "gossipsub", - Sign: true, + Enabled: false, + Router: "gossipsub", + Sign: true, SignStrict: false, GossipSubHeartbeat: gossipSubHeartbeat{ - Interval: 0, + Interval: 0, InitialDelay: 0, }, }, Relay: relay{ - Enabled: true, - Hop: false, + Enabled: true, + Hop: false, Discovery: false, - Auto: false, + Auto: false, }, - AutoNat: false, - HostAddresses: make(MaddrArray, 0), + AutoNat: false, + HostAddresses: make(MaddrArray, 0), AnnounceAddresses: make(MaddrArray, 0), - NoListen: false, - MetricsAddress: "", + NoListen: false, + MetricsAddress: "", } -} \ No newline at end of file +} diff --git a/config/config_test.go b/config/config_test.go index b55b9fdc..ba0d638f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -22,4 +22,4 @@ func TestDefaultConfig(t *testing.T) { if c.ListenAddr.String() != defaultListen.String() { t.Fatal(fmt.Sprintf("Expected %s, got %s", defaultListen.String(), c.ListenAddr.String())) } -} \ No newline at end of file +} From c04c9795b1ef725d672c52748506e3cbd0749a25 Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Wed, 3 Apr 2019 14:00:42 -0700 Subject: [PATCH 05/13] proper type assertion --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index b99c7601..d26479c9 100644 --- a/config/config.go +++ b/config/config.go @@ -19,7 +19,7 @@ func (jm *JSONMaddr) UnmarshalJSON(b []byte) error { if err != nil { return err } - jma := ma.(JSONMaddr) + jma := JSONMaddr{ma} jm = &jma return nil } From ec4de673565384e87d51f68e462cba60b57c4e3c Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 9 Apr 2019 00:01:09 +0100 Subject: [PATCH 06/13] schema json --- specs/CONFIG.md | 7 +- specs/config.schema.json | 159 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 specs/config.schema.json diff --git a/specs/CONFIG.md b/specs/CONFIG.md index a4379cab..50a1b695 100644 --- a/specs/CONFIG.md +++ b/specs/CONFIG.md @@ -57,10 +57,9 @@ cause all other configuration command line options to be ignored. * Array[Maddr String] * `[]` * `DHT` - * `Enabled` - * Enables the DHT in full node mode or client mode - * String (`"full"`|`"client"`|`""`) - * `""` + * Enables the DHT in full node mode or client mode + * String (`"full"`|`"client"`|`""`) + * `""` * `ConnectionManager` * `Enabled` * Enables the Connection Manager diff --git a/specs/config.schema.json b/specs/config.schema.json new file mode 100644 index 00000000..d4f6d6c2 --- /dev/null +++ b/specs/config.schema.json @@ -0,0 +1,159 @@ +{ + "$schema": "http://json-schema.org/schema#", + "definitions": { + "maddr": { + "type": "string" + } + }, + "type": "object", + "properties": { + "ListenAddr": { + "$ref": "#/definitions/maddr", + "default": "/unix/tmp/p2pd.sock" + }, + "Quiet": { + "type": "boolean", + "default": false + }, + "ID": { + "type": "string", + "default": "" + }, + "Bootstrap": { + "type": "object", + "properties": { + "Enabled": { + "type": "boolean", + "default": false + }, + "Peers": { + "type": "array", + "items": { + "$ref": "#/definitions/maddr" + }, + "default": [] + } + } + }, + "DHT": { + "enum": [ + "full", + "client", + "" + ], + "default": "" + }, + "ConnectionManager": { + "type": "object", + "properties": { + "Enabled": { + "type": "boolean", + "default": false + }, + "LowWaterMark": { + "type": "integer", + "default": 256 + }, + "HighWaterMark": { + "type": "integer", + "default": 512 + }, + "GradePeriod": { + "type": "integer", + "default": 120 + } + } + }, + "QUIC": { + "type": "boolean", + "default": false + }, + "NatPortMap": { + "type": "boolean", + "default": false + }, + "PubSub": { + "type": "object", + "properties": { + "Enabled": { + "type": "boolean", + "default": "false" + }, + "Router": { + "type": "string", + "default": "gossipsub" + }, + "Sign": { + "type": "boolean", + "default": true + }, + "SignStrict": { + "type": "boolean", + "default": false + }, + "GossipSubHeartbeat": { + "type": "object", + "properties": { + "Interval": { + "type": "integer", + "default": 0 + }, + "InitialDelay": { + "type": "integer", + "default": 0 + } + } + } + } + }, + "Relay": { + "type": "object", + "properties": { + "Enabled": { + "type": "boolean", + "default": true + }, + "Active": { + "type": "boolean", + "default": false + }, + "Hop": { + "type": "boolean", + "default": false + }, + "Discovery": { + "type": "boolean", + "default": false + }, + "Auto": { + "type": "boolean", + "default": false + } + } + }, + "AutoNat": { + "type": "boolean", + "default": false + }, + "HostAddresses": { + "type": "array", + "items": {"$ref": "#/definitions/maddr"}, + "default": [] + }, + "AnnounceAddresses": { + "type": "array", + "items": {"$ref": "#/definitions/maddr"}, + "default": [] + }, + "NoListen": { + "type": "boolean", + "default": false + }, + "MetricsAddress": { + "type": "string", + "format": "ipv4", + "default": "" + } + }, + "additionalProperties": false +} \ No newline at end of file From 17981dbe6b168f5a127e5e0ee61276408a11f3fd Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Fri, 12 Apr 2019 22:23:36 +0100 Subject: [PATCH 07/13] enum on dht mode --- specs/CONFIG.md | 11 +++++++---- specs/config.schema.json | 17 +++++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/specs/CONFIG.md b/specs/CONFIG.md index 50a1b695..a3483649 100644 --- a/specs/CONFIG.md +++ b/specs/CONFIG.md @@ -57,9 +57,10 @@ cause all other configuration command line options to be ignored. * Array[Maddr String] * `[]` * `DHT` - * Enables the DHT in full node mode or client mode - * String (`"full"`|`"client"`|`""`) - * `""` + * `Mode` + * Enables the DHT in full node mode or client mode + * Enum, String (`"full"`|`"client"`|`""`) + * `""` * `ConnectionManager` * `Enabled` * Enables the Connection Manager @@ -164,7 +165,9 @@ cause all other configuration command line options to be ignored. "Enabled": false, "Peers": [] }, - "DHT": "", + "DHT": { + "Mode": "" + }, "ConnectionManager": { "Enabled": false, "LowWaterMark": 256, diff --git a/specs/config.schema.json b/specs/config.schema.json index d4f6d6c2..b7a6183e 100644 --- a/specs/config.schema.json +++ b/specs/config.schema.json @@ -36,12 +36,17 @@ } }, "DHT": { - "enum": [ - "full", - "client", - "" - ], - "default": "" + "type": "object", + "properties": { + "Mode": { + "enum": [ + "full", + "client", + "" + ], + "default": "" + } + } }, "ConnectionManager": { "type": "object", From 66c74fb3070bf14ec721b4b0e35b2b4d4ef709c0 Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Fri, 12 Apr 2019 22:38:44 +0100 Subject: [PATCH 08/13] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..496ee2ca --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file From 6044ec432b30ff72df1ddd493e3d873eb713a5a6 Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 16 Apr 2019 19:19:59 +0100 Subject: [PATCH 09/13] updates for pprof --- config/config.go | 24 ++++++-- p2pd/main.go | 24 +++++--- specs/CONFIG.md | 130 ++------------------------------------- specs/config.schema.json | 99 ++++++++++++++++++++--------- 4 files changed, 111 insertions(+), 166 deletions(-) diff --git a/config/config.go b/config/config.go index d26479c9..20ca330a 100644 --- a/config/config.go +++ b/config/config.go @@ -72,6 +72,15 @@ type relay struct { Auto bool } +type dht struct { + Mode string +} + +type pprof struct { + Enabled bool + Port uint +} + const DHTFullMode = "full" const DHTClientMode = "client" @@ -80,7 +89,7 @@ type Config struct { Quiet bool ID string Bootstrap bootstrap - DHT string + DHT dht ConnectionManager connectionManager QUIC bool NatPortMap bool @@ -91,6 +100,7 @@ type Config struct { AnnounceAddresses MaddrArray NoListen bool MetricsAddress string + PProf pprof } func (c *Config) UnmarshalJSON(b []byte) error { @@ -112,10 +122,10 @@ func (c *Config) UnmarshalJSON(b []byte) error { } func (c *Config) Validate() error { - if c.DHT != DHTClientMode && c.DHT != DHTFullMode && c.DHT != "" { + 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 == "") { + 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 @@ -131,7 +141,9 @@ func NewDefaultConfig() Config { Enabled: false, Peers: make(MaddrArray, 0), }, - DHT: "", + DHT: dht{ + Mode: "", + }, ConnectionManager: connectionManager{ Enabled: false, LowWaterMark: 256, @@ -161,5 +173,9 @@ func NewDefaultConfig() Config { AnnounceAddresses: make(MaddrArray, 0), NoListen: false, MetricsAddress: "", + PProf: pprof{ + Enabled: false, + Port: 0, + }, } } diff --git a/p2pd/main.go b/p2pd/main.go index d3bf60f5..25b0de89 100644 --- a/p2pd/main.go +++ b/p2pd/main.go @@ -6,7 +6,6 @@ import ( "encoding/json" "flag" "fmt" - "github.com/libp2p/go-libp2p-daemon/config" "io/ioutil" "log" "net/http" @@ -17,6 +16,7 @@ import ( relay "github.com/libp2p/go-libp2p-circuit" connmgr "github.com/libp2p/go-libp2p-connmgr" p2pd "github.com/libp2p/go-libp2p-daemon" + "github.com/libp2p/go-libp2p-daemon/config" ps "github.com/libp2p/go-libp2p-pubsub" quic "github.com/libp2p/go-libp2p-quic-transport" identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" @@ -100,11 +100,6 @@ func main() { flag.Parse() var c config.Config - if *pprof { - // an invalid port number will fail within the function. - go pprofHTTP(int(*pprofPort)) - } - var opts []libp2p.Option if *configStdin { @@ -225,15 +220,26 @@ func main() { c.MetricsAddress = *metricsAddr } if *dht { - c.DHT = config.DHTFullMode + c.DHT.Mode = config.DHTFullMode } else if *dhtClient { - c.DHT = config.DHTClientMode + c.DHT.Mode = config.DHTClientMode + } + if *pprof { + c.PProf.Enabled = true + if pprofPort != nil { + c.PProf.Port = *pprofPort + } } if err := c.Validate(); err != nil { log.Fatal(err) } } + + if c.PProf.Enabled { + // an invalid port number will fail within the function. + go pprofHTTP(int(c.PProf.Port)) + } // collect opts if c.ID != "" { @@ -292,7 +298,7 @@ func main() { } // start daemon - d, err := p2pd.NewDaemon(context.Background(), c.ListenAddr, c.DHT, opts...) + d, err := p2pd.NewDaemon(context.Background(), c.ListenAddr, c.DHT.Mode, opts...) if err != nil { log.Fatal(err) } diff --git a/specs/CONFIG.md b/specs/CONFIG.md index a3483649..64f82356 100644 --- a/specs/CONFIG.md +++ b/specs/CONFIG.md @@ -29,131 +29,8 @@ cause all other configuration command line options to be ignored. ## Schema +Please see the json [schema file](config.schema.json). -* `Field Name` - * Description - * Type - * `default` - -* `ListenAddr` - * Daemon control listen multiaddr - * Maddr String - * `"/unix/tmp/p2pd.sock"` -* `Quiet` - * Be Quiet - * Boolean - * `false` -* `ID` - * Peer identity; private key file - * String - * `""` -* `Bootstrap` - * `Enabled` - * Connects to bootstrap peers and bootstraps the dht if enabled - * Boolean - * `false` - * `Peers` - * List of bootstrap peers; defaults to the IPFS DHT peers - * Array[Maddr String] - * `[]` -* `DHT` - * `Mode` - * Enables the DHT in full node mode or client mode - * Enum, String (`"full"`|`"client"`|`""`) - * `""` -* `ConnectionManager` - * `Enabled` - * Enables the Connection Manager - * Boolean - * `false` - * `LowWaterMark` - * Connection Manager Low Water mark - * Integer - * `256` - * `HighWaterMark` - * Connection Manager High Water mark - * Integer - * `512` - * `GracePeriod` - * Connection Manager grace period (in seconds) - * Integer - * `120` -* `QUIC` - * Enables the QUIC transport - * Boolean - * `false` -* `NatPortMap` - * Enables NAT port mapping - * Boolean - * `false` -* `PubSub` - * `Enabled` - * Enables pubsub - * Boolean - * `false` - * `Router` - * Specifies the pubsub router implementation - * String - * `"gossipsub"` - * `Sign` - * Enables pubsub message signing - * Boolean - * `true` - * `SignStrict` - * Enables pubsub strict signature verification - * Boolean - * `false` - * `GossipSubHeartbeat` - * `Interval` - * Specifies the gossipsub heartbeat interval - * Integer - * `0` - * `InitialDelay` - * Specifies the gossipsub initial heartbeat delay - * Integer - * `0` -* `Relay` - * `Enabled` - * Enables circuit relay - * Boolean - * `true` - * `Active` - * Enables active mode for relay - * Boolean - * `false` - * `Hop` - * Enables hop for relay - * Boolean - * `false` - * `Discovery` - * Enables passive discovery for relay - * Boolean - * `false` - * `Auto` - * Enables autorelay - * Boolean - * `false` -* `AutoNat` - * Enables the AutoNAT service - * Boolean - * `false` -* `HostAddresses` - * List of multiaddrs the host should listen on - * Array[Maddr String] - * `[]` -* `AnnounceAddresses` - * List of multiaddrs the host should announce to the network - * Array[Maddr String] - * `[]` -* `NoListen` - * Sets the host to listen on no addresses - * Boolean - * `false` -* `MetricsAddress` - * An address to bind the metrics handler to - * Maddr String - * `"""` - ### Default Example ```json @@ -197,6 +74,9 @@ cause all other configuration command line options to be ignored. "HostAddresses": [], "AnnounceAddresses": [], "NoListen": false, - "MetricsAddress": "" + "MetricsAddress": "", + "PProf": { + "Enabled": false + } } ``` diff --git a/specs/config.schema.json b/specs/config.schema.json index b7a6183e..836f018b 100644 --- a/specs/config.schema.json +++ b/specs/config.schema.json @@ -9,29 +9,34 @@ "properties": { "ListenAddr": { "$ref": "#/definitions/maddr", - "default": "/unix/tmp/p2pd.sock" + "default": "/unix/tmp/p2pd.sock", + "$comment": "Daemon control listen multiaddr" }, "Quiet": { "type": "boolean", - "default": false + "default": false, + "$comment": "Be Quiet" }, "ID": { "type": "string", - "default": "" + "default": "", + "$comment": "Peer identity; private key file" }, "Bootstrap": { "type": "object", "properties": { "Enabled": { "type": "boolean", - "default": false + "default": false, + "$comment": "Connects to bootstrap peers and bootstraps the dht if enabled" }, "Peers": { "type": "array", "items": { "$ref": "#/definitions/maddr" }, - "default": [] + "default": [], + "$comment": "List of bootstrap peers; defaults to the IPFS DHT peers" } } }, @@ -44,7 +49,8 @@ "client", "" ], - "default": "" + "default": "", + "$comment": "Enables the DHT in full node mode or client mode" } } }, @@ -53,59 +59,71 @@ "properties": { "Enabled": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables the Connection Manager" }, "LowWaterMark": { "type": "integer", - "default": 256 + "default": 256, + "$comment": "Connection Manager Low Water mark" }, "HighWaterMark": { "type": "integer", - "default": 512 + "default": 512, + "$comment": "Connection Manager High Water mark" }, "GradePeriod": { "type": "integer", - "default": 120 + "default": 120, + "$comment": "Connection Manager grace period (in seconds)" } } }, "QUIC": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables the QUIC transport" }, "NatPortMap": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables NAT port mapping" }, "PubSub": { "type": "object", "properties": { "Enabled": { "type": "boolean", - "default": "false" + "default": "false", + "$comment": "Enables pubsub" }, "Router": { "type": "string", - "default": "gossipsub" + "default": "gossipsub", + "$comment": "Specifies the pubsub router implementation" }, "Sign": { "type": "boolean", - "default": true + "default": true, + "$comment": "Enables pubsub message signing" }, "SignStrict": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables pubsub strict signature verification" }, "GossipSubHeartbeat": { "type": "object", "properties": { "Interval": { "type": "integer", - "default": 0 + "default": 0, + "$comment": "Specifies the gossipsub heartbeat interval" }, "InitialDelay": { "type": "integer", - "default": 0 + "default": 0, + "$comment": "Specifies the gossipsub initial heartbeat delay" } } } @@ -116,48 +134,73 @@ "properties": { "Enabled": { "type": "boolean", - "default": true + "default": true, + "$comment": "Enables circuit relay" }, "Active": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables active mode for relay" }, "Hop": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables hop for relay" }, "Discovery": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables passive discovery for relay" }, "Auto": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables autorelay" } } }, "AutoNat": { "type": "boolean", - "default": false + "default": false, + "$comment": "Enables the AutoNAT service" }, "HostAddresses": { "type": "array", "items": {"$ref": "#/definitions/maddr"}, - "default": [] + "default": [], + "$comment": "List of multiaddrs the host should listen on" }, "AnnounceAddresses": { "type": "array", "items": {"$ref": "#/definitions/maddr"}, - "default": [] + "default": [], + "$comment": "List of multiaddrs the host should announce to the network" }, "NoListen": { "type": "boolean", - "default": false + "default": false, + "$comment": "Sets the host to listen on no addresses" }, "MetricsAddress": { "type": "string", "format": "ipv4", - "default": "" + "default": "", + "$comment": "An address to bind the metrics handler to" + }, + "PProf": { + "type": "object", + "properties": { + "Enabled": { + "type": "boolean", + "default": false, + "$comment": "Enables the HTTP pprof handler, listening on the first port available in the range [6060-7800], or on the user-provided port via PProfPort" + }, + "Port": { + "type": "integer", + "default": 0, + "$comment": "Binds the HTTP pprof handler to a specific port; has no effect unless PProf is enabled" + } + } } }, "additionalProperties": false From f6b9de07ab0fe839ac402d59672c1a8390ce45d4 Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 16 Apr 2019 19:32:14 +0100 Subject: [PATCH 10/13] fmt --- p2pd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2pd/main.go b/p2pd/main.go index 25b0de89..2607dab1 100644 --- a/p2pd/main.go +++ b/p2pd/main.go @@ -235,7 +235,7 @@ func main() { log.Fatal(err) } } - + if c.PProf.Enabled { // an invalid port number will fail within the function. go pprofHTTP(int(c.PProf.Port)) From 3fbe5879a562235a0fac672b0a7fb652963cf5f1 Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Fri, 19 Apr 2019 08:14:21 +0200 Subject: [PATCH 11/13] review comments --- config/config.go | 3 +-- p2pd/main.go | 4 ++-- specs/CONFIG.md | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 20ca330a..e87b830b 100644 --- a/config/config.go +++ b/config/config.go @@ -19,8 +19,7 @@ func (jm *JSONMaddr) UnmarshalJSON(b []byte) error { if err != nil { return err } - jma := JSONMaddr{ma} - jm = &jma + jm = &JSONMaddr{ma} return nil } diff --git a/p2pd/main.go b/p2pd/main.go index 2607dab1..b0910474 100644 --- a/p2pd/main.go +++ b/p2pd/main.go @@ -108,7 +108,7 @@ func main() { if err != nil { log.Fatal(err) } - if err := json.Unmarshal(body, c); err != nil { + if err := json.Unmarshal(body, &c); err != nil { log.Fatal(err) } } else if *configFilename != "" { @@ -116,7 +116,7 @@ func main() { if err != nil { log.Fatal(err) } - if err := json.Unmarshal(body, c); err != nil { + if err := json.Unmarshal(body, &c); err != nil { log.Fatal(err) } } else { diff --git a/specs/CONFIG.md b/specs/CONFIG.md index 64f82356..08db36a4 100644 --- a/specs/CONFIG.md +++ b/specs/CONFIG.md @@ -31,7 +31,7 @@ cause all other configuration command line options to be ignored. ## Schema Please see the json [schema file](config.schema.json). -### Default Example +### Complete (Default Options) Example ```json { From 234661e946eab73f7a2ef893313a810031ce71fe Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 23 Apr 2019 13:37:48 -0700 Subject: [PATCH 12/13] override with flags --- p2pd/main.go | 201 ++++++++++++++++++++++++++------------------------- 1 file changed, 101 insertions(+), 100 deletions(-) diff --git a/p2pd/main.go b/p2pd/main.go index b0910474..7a6251fe 100644 --- a/p2pd/main.go +++ b/p2pd/main.go @@ -121,119 +121,120 @@ func main() { } } else { c = config.NewDefaultConfig() - maddr, err := multiaddr.NewMultiaddr(*maddrString) - if err != nil { - log.Fatal(err) - } - c.ListenAddr = config.JSONMaddr{maddr} - if *id != "" { - c.ID = *id - } - if *hostAddrs != "" { - addrStrings := strings.Split(*hostAddrs, ",") - ha := make([]multiaddr.Multiaddr, len(addrStrings)) - for i, s := range addrStrings { - ma, err := multiaddr.NewMultiaddr(s) - if err != nil { - log.Fatal(err) - } - (ha)[i] = ma + } + + maddr, err := multiaddr.NewMultiaddr(*maddrString) + if err != nil { + log.Fatal(err) + } + c.ListenAddr = config.JSONMaddr{maddr} + if *id != "" { + c.ID = *id + } + if *hostAddrs != "" { + addrStrings := strings.Split(*hostAddrs, ",") + ha := make([]multiaddr.Multiaddr, len(addrStrings)) + for i, s := range addrStrings { + ma, err := multiaddr.NewMultiaddr(s) + if err != nil { + log.Fatal(err) } - c.HostAddresses = ha + (ha)[i] = ma } - if *announceAddrs != "" { - addrStrings := strings.Split(*announceAddrs, ",") - ha := make([]multiaddr.Multiaddr, len(addrStrings)) - for i, s := range addrStrings { - ma, err := multiaddr.NewMultiaddr(s) - if err != nil { - log.Fatal(err) - } - (ha)[i] = ma + c.HostAddresses = ha + } + if *announceAddrs != "" { + addrStrings := strings.Split(*announceAddrs, ",") + ha := make([]multiaddr.Multiaddr, len(addrStrings)) + for i, s := range addrStrings { + ma, err := multiaddr.NewMultiaddr(s) + if err != nil { + log.Fatal(err) } - c.AnnounceAddresses = ha + (ha)[i] = ma } - if *connMgr { - c.ConnectionManager.Enabled = true - c.ConnectionManager.GracePeriod = *connMgrGrace - c.ConnectionManager.HighWaterMark = *connMgrHi - c.ConnectionManager.LowWaterMark = *connMgrLo - } - if *QUIC { - c.QUIC = true - } - if *natPortMap { - c.NatPortMap = true - } - if *relayEnabled { - c.Relay.Enabled = true - if *relayActive { - c.Relay.Active = true - } - if *relayHop { - c.Relay.Hop = true - } - if *relayDiscovery { - c.Relay.Discovery = true - } + c.AnnounceAddresses = ha + } + if *connMgr { + c.ConnectionManager.Enabled = true + c.ConnectionManager.GracePeriod = *connMgrGrace + c.ConnectionManager.HighWaterMark = *connMgrHi + c.ConnectionManager.LowWaterMark = *connMgrLo + } + if *QUIC { + c.QUIC = true + } + if *natPortMap { + c.NatPortMap = true + } + if *relayEnabled { + c.Relay.Enabled = true + if *relayActive { + c.Relay.Active = true } - if *autoRelay { - c.Relay.Auto = true + if *relayHop { + c.Relay.Hop = true } - if *noListen { - c.NoListen = true + if *relayDiscovery { + c.Relay.Discovery = true } - if *autonat { - c.AutoNat = true + } + if *autoRelay { + c.Relay.Auto = true + } + if *noListen { + c.NoListen = true + } + if *autonat { + c.AutoNat = true + } + if *pubsub { + c.PubSub.Enabled = true + c.PubSub.Router = *pubsubRouter + c.PubSub.Sign = *pubsubSign + c.PubSub.SignStrict = *pubsubSignStrict + if *gossipsubHeartbeatInterval > 0 { + c.PubSub.GossipSubHeartbeat.Interval = *gossipsubHeartbeatInterval } - if *pubsub { - c.PubSub.Enabled = true - c.PubSub.Router = *pubsubRouter - c.PubSub.Sign = *pubsubSign - c.PubSub.SignStrict = *pubsubSignStrict - if *gossipsubHeartbeatInterval > 0 { - c.PubSub.GossipSubHeartbeat.Interval = *gossipsubHeartbeatInterval - } - if *gossipsubHeartbeatInitialDelay > 0 { - c.PubSub.GossipSubHeartbeat.InitialDelay = *gossipsubHeartbeatInitialDelay - } + if *gossipsubHeartbeatInitialDelay > 0 { + c.PubSub.GossipSubHeartbeat.InitialDelay = *gossipsubHeartbeatInitialDelay } - if *bootstrapPeers != "" { - addrStrings := strings.Split(*bootstrapPeers, ",") - bps := make([]multiaddr.Multiaddr, len(addrStrings)) - for i, s := range addrStrings { - ma, err := multiaddr.NewMultiaddr(s) - if err != nil { - log.Fatal(err) - } - (bps)[i] = ma + } + if *bootstrapPeers != "" { + addrStrings := strings.Split(*bootstrapPeers, ",") + bps := make([]multiaddr.Multiaddr, len(addrStrings)) + for i, s := range addrStrings { + ma, err := multiaddr.NewMultiaddr(s) + if err != nil { + log.Fatal(err) } - c.Bootstrap.Peers = bps - } - if *bootstrap { - c.Bootstrap.Enabled = true - } - if *quiet { - c.Quiet = true - } - if *metricsAddr != "" { - c.MetricsAddress = *metricsAddr - } - if *dht { - c.DHT.Mode = config.DHTFullMode - } else if *dhtClient { - c.DHT.Mode = config.DHTClientMode + (bps)[i] = ma } - if *pprof { - c.PProf.Enabled = true - if pprofPort != nil { - c.PProf.Port = *pprofPort - } + c.Bootstrap.Peers = bps + } + if *bootstrap { + c.Bootstrap.Enabled = true + } + if *quiet { + c.Quiet = true + } + if *metricsAddr != "" { + c.MetricsAddress = *metricsAddr + } + if *dht { + c.DHT.Mode = config.DHTFullMode + } else if *dhtClient { + c.DHT.Mode = config.DHTClientMode + } + if *pprof { + c.PProf.Enabled = true + if pprofPort != nil { + c.PProf.Port = *pprofPort } + } - if err := c.Validate(); err != nil { - log.Fatal(err) - } + if err := c.Validate(); err != nil { + log.Fatal(err) } if c.PProf.Enabled { From ad5d34fe364b874e3c00a47e5ecb45f99682b73b Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 23 Apr 2019 14:35:40 -0700 Subject: [PATCH 13/13] export config structs --- config/config.go | 44 ++++++++++++++++++++++---------------------- go.sum | 4 ++++ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index e87b830b..2dde4ba4 100644 --- a/config/config.go +++ b/config/config.go @@ -38,32 +38,32 @@ func (maa *MaddrArray) UnmarshalJSON(b []byte) error { return nil } -type bootstrap struct { +type Bootstrap struct { Enabled bool Peers MaddrArray } -type connectionManager struct { +type ConnectionManager struct { Enabled bool LowWaterMark int HighWaterMark int GracePeriod time.Duration } -type gossipSubHeartbeat struct { +type GossipSubHeartbeat struct { Interval time.Duration InitialDelay time.Duration } -type pubSub struct { +type PubSub struct { Enabled bool Router string Sign bool SignStrict bool - GossipSubHeartbeat gossipSubHeartbeat + GossipSubHeartbeat GossipSubHeartbeat } -type relay struct { +type Relay struct { Enabled bool Active bool Hop bool @@ -71,11 +71,11 @@ type relay struct { Auto bool } -type dht struct { +type DHT struct { Mode string } -type pprof struct { +type PProf struct { Enabled bool Port uint } @@ -87,19 +87,19 @@ type Config struct { ListenAddr JSONMaddr Quiet bool ID string - Bootstrap bootstrap - DHT dht - ConnectionManager connectionManager + Bootstrap Bootstrap + DHT DHT + ConnectionManager ConnectionManager QUIC bool NatPortMap bool - PubSub pubSub - Relay relay + PubSub PubSub + Relay Relay AutoNat bool HostAddresses MaddrArray AnnounceAddresses MaddrArray NoListen bool MetricsAddress string - PProf pprof + PProf PProf } func (c *Config) UnmarshalJSON(b []byte) error { @@ -125,7 +125,7 @@ func (c *Config) Validate() error { 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 errors.New("can't have autorelay enabled without Relay enabled and DHT enabled") } return nil } @@ -136,14 +136,14 @@ func NewDefaultConfig() Config { ListenAddr: JSONMaddr{defaultListen}, Quiet: false, ID: "", - Bootstrap: bootstrap{ + Bootstrap: Bootstrap{ Enabled: false, Peers: make(MaddrArray, 0), }, - DHT: dht{ + DHT: DHT{ Mode: "", }, - ConnectionManager: connectionManager{ + ConnectionManager: ConnectionManager{ Enabled: false, LowWaterMark: 256, HighWaterMark: 512, @@ -151,17 +151,17 @@ func NewDefaultConfig() Config { }, QUIC: false, NatPortMap: false, - PubSub: pubSub{ + PubSub: PubSub{ Enabled: false, Router: "gossipsub", Sign: true, SignStrict: false, - GossipSubHeartbeat: gossipSubHeartbeat{ + GossipSubHeartbeat: GossipSubHeartbeat{ Interval: 0, InitialDelay: 0, }, }, - Relay: relay{ + Relay: Relay{ Enabled: true, Hop: false, Discovery: false, @@ -172,7 +172,7 @@ func NewDefaultConfig() Config { AnnounceAddresses: make(MaddrArray, 0), NoListen: false, MetricsAddress: "", - PProf: pprof{ + PProf: PProf{ Enabled: false, Port: 0, }, diff --git a/go.sum b/go.sum index d3527b8c..37e49c8f 100644 --- a/go.sum +++ b/go.sum @@ -46,7 +46,9 @@ 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= @@ -54,6 +56,7 @@ 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= @@ -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=