Skip to content

Commit

Permalink
Merge pull request #1147 from gponsinet/fix-network-handler
Browse files Browse the repository at this point in the history
fix(network): handler sometimes not set
  • Loading branch information
Godefroy Ponsinet committed Mar 21, 2019
2 parents 2582ba4 + a46f107 commit b38bb62
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 70 deletions.
2 changes: 1 addition & 1 deletion client/react-native/desktop/Makefile
Expand Up @@ -4,7 +4,7 @@ deps:
GOPROXY=https://goproxy.berty.io GO111MODULE=on go mod vendor
GO111MODULE=off go get -u github.com/asticode/go-astilectron-bundler/...

build:
build: clean
GOPROXY=https://goproxy.berty.io GO111MODULE=on go mod vendor
sed s%TMPL_MAKEFILE_PATH%$(MAKEFILE_DIR)%g bundler.json.tmpl > bundler.json
cp -rf $(MAKEFILE_DIR)/../web/build $(MAKEFILE_DIR)/../desktop/resources/app
Expand Down
3 changes: 1 addition & 2 deletions client/react-native/gomobile/core/core.go
Expand Up @@ -22,7 +22,6 @@ var (
rootContext = context.Background()
NotificationDriver = MobileNotification{}.New() // Android / iOS
ElectronNotificationDriver *notification.Driver // Electron Desktop client
networkDriver *network.Network
)

// Setup call it at first native start
Expand Down Expand Up @@ -207,7 +206,7 @@ func daemon(cfg *MobileOptions) error {

var a *account.Account

networkDriver, err = network.New(rootContext, network.WithDefaultMobileOptions())
networkDriver, err := network.New(rootContext, network.WithDefaultMobileOptions())
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion client/react-native/gomobile/core/network.go
Expand Up @@ -12,7 +12,9 @@ func GetNetworkConfig() (string, error) {
defer panicHandler()
waitDaemon(accountName)

cfg := networkDriver.Config()
a, _ := account.Get(rootContext, accountName)

cfg := a.Network().Config()
data, err := json.Marshal(cfg)
if err != nil {
return "", err
Expand Down
30 changes: 23 additions & 7 deletions core/manager/account/p2p.go
Expand Up @@ -3,6 +3,7 @@ package account
import (
"context"

"berty.tech/core/network"
network_config "berty.tech/core/network/config"
"berty.tech/core/pkg/tracing"
"github.com/pkg/errors"
Expand All @@ -13,15 +14,30 @@ func (a *Account) UpdateNetwork(ctx context.Context, opts ...network_config.Opti
defer tracer.Finish()
ctx = tracer.Context()

err := a.network.Close(ctx)
updated, err := network.New(ctx, append(
[]network_config.Option{network.WithConfig(a.network.Config())},
opts...,
)...)

if err != nil {
return err
return errors.New("account failed to update network")
}
if err := a.node.UseNetworkDriver(ctx, updated); err != nil {
return a.node.UseNetworkDriver(ctx, a.network)
}

if err := a.network.Update(ctx, opts...); err != nil {
return errors.New("account failed to update network")
a.node.UseNetworkMetric(ctx, updated.Metric())

err = a.network.Close(ctx)
if err != nil {
logger().Error("last network cannot be closed: " + err.Error())
}
a.metric = a.network.Metric()
a.node.UseNetworkMetric(ctx, a.metric)
return a.node.UseNetworkDriver(ctx, a.network)

a.network = updated
a.metric = updated.Metric()
return nil
}

func (a *Account) Network() network.Driver {
return a.network
}
11 changes: 11 additions & 0 deletions core/network/config/config.go
Expand Up @@ -121,26 +121,33 @@ func (cfg *Config) Override(override *Config) error {
// Apply applies the given options to the config, returning the first error
// encountered (if any).
func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {
// reset config
cfg.Config = libp2p_config.Config{}

libp2pOpts := []libp2p_config.Option{}

logger().Debug("apply 0")
for _, opt := range opts {
if err := opt(cfg); err != nil {
return err
}
}

logger().Debug("apply 1")
if cfg.OverridePersist {
if err := cfg.OverridePersistConfig(); err != nil {
return err
}
}

logger().Debug("apply 2")
if cfg.Persist {
if err := cfg.ApplyPersistConfig(); err != nil {
return err
}
}

logger().Debug("apply 21")
// add ws transport
if cfg.WS {
libp2pOpts = append(libp2pOpts, libp2p.Transport(ws.New))
Expand Down Expand Up @@ -182,6 +189,7 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {
libp2pOpts = append(libp2pOpts, libp2p.EnableRelay(circuit.OptActive, circuit.OptDiscovery))
}

logger().Debug("apply 22")
// private network
if cfg.SwarmKey != "" {
prot, err := pnet.NewProtector(strings.NewReader(cfg.SwarmKey))
Expand All @@ -191,6 +199,7 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {
libp2pOpts = append(libp2pOpts, libp2p.PrivateNetwork(prot))
}

logger().Debug("apply 3")
// identity
if cfg.Identity != "" {
bytes, err := base64.StdEncoding.DecodeString(cfg.Identity)
Expand All @@ -212,6 +221,7 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {

libp2pOpts = append(libp2pOpts, libp2p.NATPortMap())

logger().Debug("apply 4")
// override libp2p configuration
err := cfg.Config.Apply(append(libp2pOpts, libp2p.FallbackDefaults)...)
if err != nil {
Expand All @@ -220,6 +230,7 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {

// override conn manager

logger().Debug("apply 5")
// override ping service
cfg.Config.DisablePing = true
return nil
Expand Down
3 changes: 3 additions & 0 deletions core/network/driver.go
Expand Up @@ -261,6 +261,8 @@ func (net *Network) SendTo(ctx context.Context, pi pstore.PeerInfo, e *entity.En
}

func (net *Network) handleEnvelope(s inet.Stream) {
logger().Debug(fmt.Sprintf("NETWORK ADDR HANDLE %p %+v", net, net))
logger().Debug("receiving envelope")
if net.handler == nil {
logger().Error("handler is not set")
return
Expand Down Expand Up @@ -317,6 +319,7 @@ func (net *Network) Join(ctx context.Context, contactID string) error {
}

func (net *Network) OnEnvelopeHandler(f func(context.Context, *entity.Envelope) (*entity.Void, error)) {
logger().Debug(fmt.Sprintf("ON_ENVELOPE_HANDLER %p", f))
net.handler = f
}

Expand Down
3 changes: 0 additions & 3 deletions core/network/interface.go
Expand Up @@ -38,9 +38,6 @@ type Driver interface {
// Return current config
Config() *config.Config

// Update update current network with config
Update(context.Context, ...config.Option) error

// Close cleanups things
Close(context.Context) error
}
52 changes: 4 additions & 48 deletions core/network/network.go
Expand Up @@ -41,10 +41,10 @@ func New(ctx context.Context, opts ...config.Option) (*Network, error) {
tracer := tracing.EnterFunc(ctx)
defer tracer.Finish()

ctx, cancel := context.WithCancel(ctx)

var err error

ctx, cancel := context.WithCancel(ctx)

net := &Network{
config: &config.Config{},
updating: &sync.Mutex{},
Expand All @@ -63,12 +63,11 @@ func New(ctx context.Context, opts ...config.Option) (*Network, error) {
return nil, err
}

net.init(ctx)

if net.Config().PeerCache {
net.cache = NewPeerCache(net.host)
}

net.init(ctx)

return net, nil
}

Expand All @@ -83,51 +82,9 @@ func (net *Network) init(ctx context.Context) {
}
}

// Update create new network and permit to override previous config
func (net *Network) Update(ctx context.Context, opts ...config.Option) error {
net.updating.Lock()
defer net.updating.Unlock()

ctx, cancel := context.WithCancel(ctx)

var err error

update := &Network{
config: &config.Config{},
updating: net.updating,
handler: net.handler,
shutdown: cancel,
cache: NewNoopCache(),
}

if err := update.config.Apply(ctx, append([]config.Option{WithConfig(net.config)}, opts...)...); err != nil {
cancel()
return err
}

update.host, err = update.config.NewNode(ctx)
if err != nil {
cancel()
return err
}

if update.Config().PeerCache {
net.cache = NewPeerCache(update.host)
}

net.Close(ctx)

*net = *update

net.init(ctx)

return nil
}

func (net *Network) Close(ctx context.Context) error {
tracer := tracing.EnterFunc(ctx)
defer tracer.Finish()

net.shutdown()

// FIXME: save cache to speedup next connections
Expand All @@ -140,6 +97,5 @@ func (net *Network) Close(ctx context.Context) error {
logger().Error("p2p close error", zap.Error(err))
}
}

return nil
}
8 changes: 0 additions & 8 deletions core/network/network_test.go
Expand Up @@ -91,14 +91,6 @@ func TestDriver(t *testing.T) {
So(err, ShouldBeNil)
})

Convey("update clients", FailureHalts, func() {
So(homer.Update(ctx, WithClientTestOptions()), ShouldBeNil)

So(lisa.Update(ctx, WithClientTestOptions()), ShouldBeNil)

So(bart.Update(ctx, WithClientTestOptions()), ShouldBeNil)
})

Convey("bootstrap clients", FailureHalts, func() {

bootstrap := []string{}
Expand Down
3 changes: 3 additions & 0 deletions core/node/network.go
Expand Up @@ -2,6 +2,7 @@ package node

import (
"context"
"fmt"

"berty.tech/core/network"
network_metric "berty.tech/core/network/metric"
Expand Down Expand Up @@ -38,6 +39,8 @@ func (n *Node) UseNetworkDriver(ctx context.Context, driver network.Driver) erro
// configure network
n.networkDriver.OnEnvelopeHandler(n.HandleEnvelope)

logger().Debug(fmt.Sprintf("NETWORK ADDR NODE %p %+v", n.networkDriver, n.networkDriver))

_ = n.networkDriver.Join(ctx, n.UserID())

// FIXME: subscribe to every owned device IDs
Expand Down

0 comments on commit b38bb62

Please sign in to comment.