Skip to content

Commit

Permalink
feat(network): made datastore logs toggleable
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi authored and gfanton committed Mar 14, 2019
1 parent 803d968 commit fdf69bc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
32 changes: 18 additions & 14 deletions core/cmd/berty/daemon.go
Expand Up @@ -35,19 +35,20 @@ type daemonOptions struct {
privateKeyFile string `mapstructure:"private-key-file"`

// p2p
identity string `mapstructure:"identity"`
bootstrap []string `mapstructure:"bootstrap"`
noP2P bool `mapstructure:"no-p2p"`
bindP2P []string `mapstructure:"bind-p2p"`
transportP2P []string `mapstructure:"transport-p2p"`
hop bool `mapstructure:"hop"` // relay hop
ble bool `mapstructure:"ble"`
mdns bool `mapstructure:"mdns"`
dhtServer bool `mapstructure:"dht"`
PrivateNetwork bool `mapstructure:"private-network"`
SwarmKeyPath string `mapstructure:"swarm-key"`

nickname string `mapstructure:"nickname"`

identity string `mapstructure:"identity"`
bootstrap []string `mapstructure:"bootstrap"`
noP2P bool `mapstructure:"no-p2p"`
bindP2P []string `mapstructure:"bind-p2p"`
transportP2P []string `mapstructure:"transport-p2p"`
hop bool `mapstructure:"hop"` // relay hop
ble bool `mapstructure:"ble"`
mdns bool `mapstructure:"mdns"`
dhtServer bool `mapstructure:"dht"`
PrivateNetwork bool `mapstructure:"private-network"`
SwarmKeyPath string `mapstructure:"swarm-key"`
dhtkvLogDatastore bool `mapstructure:"dhtkv-log-ds"`
nickname string `mapstructure:"nickname"`
}

func daemonSetupFlags(flags *pflag.FlagSet, opts *daemonOptions) {
Expand All @@ -60,7 +61,8 @@ func daemonSetupFlags(flags *pflag.FlagSet, opts *daemonOptions) {
flags.StringVar(&opts.privateKeyFile, "private-key-file", "", "set private key file for node")
flags.BoolVar(&opts.withBot, "bot", false, "enable bot")
flags.BoolVar(&opts.mdns, "mdns", true, "enable mdns discovery")
flags.BoolVar(&opts.dhtServer, "dhtkv-server", false, "enable server mode for DHT-CSKV (client mode by default)")
flags.BoolVar(&opts.dhtServer, "dhtkv-server", false, "enable server mode for DHT (Client mode by default)")
flags.BoolVar(&opts.dhtkvLogDatastore, "dhtkv-log-ds", false, "enable logs for DHT-CSKV datastore")
flags.BoolVar(&opts.PrivateNetwork, "private-network", true, "enable private network with the default swarm key")
flags.StringVar(&opts.SwarmKeyPath, "swarm-key", "", "path to a custom swarm key, only peers that use the same swarm key will be able to talk with you")
flags.StringVar(&opts.grpcBind, "grpc-bind", ":1337", "gRPC listening address")
Expand Down Expand Up @@ -170,6 +172,8 @@ func daemon(opts *daemonOptions) error {
Identity: opts.identity,
Persist: false,
OverridePersist: false,

DHTKVLogDatastore: opts.dhtkvLogDatastore,
}),
),
))
Expand Down
16 changes: 8 additions & 8 deletions core/network/dhtcskv/dht_cskv.go
Expand Up @@ -25,19 +25,19 @@ var defaultServerConfig = dht.BootstrapConfig{
Timeout: 40 * time.Second,
}

func New(ctx context.Context, host host.Host, server bool, config dht.BootstrapConfig) (*dht.IpfsDHT, error) {
func New(ctx context.Context, host host.Host, server bool, logDatastore bool, config dht.BootstrapConfig) (*dht.IpfsDHT, error) {
var datastore ds.Batching

if server {
if server && config.Queries == 0 {
config = defaultServerConfig
} else if config.Queries == 0 {
config = defaultClientConfig
}

if logDatastore {
datastore = ds.NewLogDatastore(ds.NewMapDatastore(), "DHT_datastore")
if config.Queries == 0 {
config = defaultServerConfig
}
} else {
datastore = ds.NewMapDatastore()
if config.Queries == 0 {
config = defaultClientConfig
}
}

dhtkv, err := dht.New(
Expand Down
3 changes: 2 additions & 1 deletion core/network/p2p/driver.go
Expand Up @@ -58,6 +58,7 @@ type driverConfig struct {

// DHT Client-Server Key-Value
dhtkvBoostrapConfig dht.BootstrapConfig
dhtkvLogDatastore bool
dhtkvServer bool

// MDNS
Expand Down Expand Up @@ -144,7 +145,7 @@ func newDriver(ctx context.Context, cfg driverConfig) (*Driver, error) {
return nil, err
}

driver.dhtCskv, err = dhtcskv.New(ctx, host, cfg.dhtkvServer, cfg.dhtkvBoostrapConfig)
driver.dhtCskv, err = dhtcskv.New(ctx, host, cfg.dhtkvServer, cfg.dhtkvLogDatastore, cfg.dhtkvBoostrapConfig)
if err != nil {
if closeErr := host.Close(); closeErr != nil {
logger().Error("failed to close host", zap.Error(closeErr))
Expand Down
8 changes: 8 additions & 0 deletions core/network/p2p/options.go
Expand Up @@ -108,6 +108,14 @@ func WithDHTCSKVBoostrapConfig(bc *dht.BootstrapConfig) Option {
}
}

// WithDHTCSKVBoostrapConfig specifies parameters used bootstrapping the DHT Client-Server Key-Value.
func WithDHTCSKVLogDatastore() Option {
return func(dc *driverConfig) error {
dc.dhtkvLogDatastore = true
return nil
}
}

// WithClientDHTCSKV enable server mode for the DHT Client-Server Key-Value
func WithServerDHTCSKV() Option {
return func(dc *driverConfig) error {
Expand Down
2 changes: 1 addition & 1 deletion experiment/scw/berty.service
Expand Up @@ -5,7 +5,7 @@ After=network.target
[Service]
User=root
Group=root
ExecStart=/bin/bash -c '/usr/bin/berty daemon --dhtkv-server --log-level=debug --mdns=false --hop --bind-p2p /ip4/0.0.0.0/tcp/4004,/ip4/0.0.0.0/tcp/80,/ip4/0.0.0.0/tcp/443,/ip4/0.0.0.0/udp/4004/quic --p2p-identity=`/bin/cat /root/pk`'
ExecStart=/bin/bash -c '/usr/bin/berty daemon --dhtkv-server --dhtkv-log-ds --log-level=debug --mdns=false --hop --bind-p2p /ip4/0.0.0.0/tcp/4004,/ip4/0.0.0.0/tcp/80,/ip4/0.0.0.0/tcp/443,/ip4/0.0.0.0/udp/4004/quic --p2p-identity=`/bin/cat /root/pk`'
Restart=always

[Install]
Expand Down

0 comments on commit fdf69bc

Please sign in to comment.