From 231f8e46b2ac26a2b344c958bbe2765c0ea41961 Mon Sep 17 00:00:00 2001 From: vyzo Date: Sun, 30 Jul 2017 11:41:04 +0300 Subject: [PATCH] core: address factory composition for constructPeerHost - Adds AddrsFactory to ConstructPeerHostOpts - Composes the AddrsFactory option with the relay filter License: MIT Signed-off-by: vyzo --- core/core.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/core/core.go b/core/core.go index c785215e73b..b70a0276bfd 100644 --- a/core/core.go +++ b/core/core.go @@ -710,6 +710,7 @@ func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) { } type ConstructPeerHostOpts struct { + AddrsFactory p2pbhost.AddrsFactory DisableNatPortMap bool DisableRelay bool EnableRelayHop bool @@ -739,6 +740,7 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr hostOpts = append(hostOpts, p2pbhost.NATPortMap) } + addrsFactory := opts.AddrsFactory if !opts.DisableRelay { filterRelayAddr := func(addrs []ma.Multiaddr) []ma.Multiaddr { var raddrs []ma.Multiaddr @@ -751,7 +753,16 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr } return raddrs } - hostOpts = append(hostOpts, p2pbhost.AddrsFactory(filterRelayAddr)) + + if addrsFactory != nil { + addrsFactory = composeAddrsFactory(addrsFactory, filterRelayAddr) + } else { + addrsFactory = filterRelayAddr + } + } + + if addrsFactory != nil { + hostOpts = append(hostOpts, addrsFactory) } host := p2pbhost.New(network, hostOpts...) @@ -771,6 +782,12 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr return host, nil } +func composeAddrsFactory(f, g p2pbhost.AddrsFactory) p2pbhost.AddrsFactory { + return func(addrs []ma.Multiaddr) []ma.Multiaddr { + return f(g(addrs)) + } +} + // startListening on the network addresses func startListening(ctx context.Context, host p2phost.Host, cfg *config.Config) error { listenAddrs, err := listenAddresses(cfg)