Commit a0d37e91 authored by Petar Radovic's avatar Petar Radovic Committed by GitHub

options struct in libp2p and api (#550)

parent db1fb013
......@@ -113,13 +113,12 @@ Welcome to the Swarm.... Bzzz Bzzzz Bzzzz
password = p
}
b, err := node.NewBee(node.Options{
b, err := node.NewBee(c.config.GetString(optionNameP2PAddr), logger, node.Options{
DataDir: c.config.GetString(optionNameDataDir),
DBCapacity: c.config.GetUint64(optionNameDBCapacity),
Password: password,
APIAddr: c.config.GetString(optionNameAPIAddr),
DebugAPIAddr: debugAPIAddr,
Addr: c.config.GetString(optionNameP2PAddr),
NATAddr: c.config.GetString(optionNameNATAddr),
EnableWS: c.config.GetBool(optionNameP2PWSEnable),
EnableQUIC: c.config.GetBool(optionNameP2PQUICEnable),
......@@ -130,7 +129,6 @@ Welcome to the Swarm.... Bzzz Bzzzz Bzzzz
TracingEnabled: c.config.GetBool(optionNameTracingEnabled),
TracingEndpoint: c.config.GetString(optionNameTracingEndpoint),
TracingServiceName: c.config.GetString(optionNameTracingServiceName),
Logger: logger,
PaymentThreshold: c.config.GetUint64(optionNamePaymentThreshold),
PaymentTolerance: c.config.GetUint64(optionNamePaymentTolerance),
})
......
......@@ -153,11 +153,7 @@ func TestLimitWriter(t *testing.T) {
// newTestServer creates an http server to serve the bee http api endpoints.
func newTestServer(t *testing.T, storer storage.Storer) *url.URL {
t.Helper()
s := api.New(api.Options{
Storer: storer,
Logger: logging.New(ioutil.Discard, 0),
Tags: tags.NewTags(),
})
s := api.New(tags.NewTags(), storer, nil, logging.New(ioutil.Discard, 0), nil)
ts := httptest.NewServer(s)
srvUrl, err := url.Parse(ts.URL)
if err != nil {
......
......@@ -20,17 +20,13 @@ type Service interface {
}
type server struct {
Options
http.Handler
metrics metrics
}
type Options struct {
Tags *tags.Tags
Storer storage.Storer
CORSAllowedOrigins []string
Logger logging.Logger
Tracer *tracing.Tracer
http.Handler
metrics metrics
}
const (
......@@ -38,10 +34,14 @@ const (
TargetsRecoveryHeader = "swarm-recovery-targets"
)
func New(o Options) Service {
func New(tags *tags.Tags, storer storage.Storer, corsAllowedOrigins []string, logger logging.Logger, tracer *tracing.Tracer) Service {
s := &server{
Options: o,
metrics: newMetrics(),
Tags: tags,
Storer: storer,
CORSAllowedOrigins: corsAllowedOrigins,
Logger: logger,
Tracer: tracer,
metrics: newMetrics(),
}
s.setupRouting()
......
......@@ -30,11 +30,7 @@ func newTestServer(t *testing.T, o testServerOptions) *http.Client {
if o.Logger == nil {
o.Logger = logging.New(ioutil.Discard, 0)
}
s := api.New(api.Options{
Tags: o.Tags,
Storer: o.Storer,
Logger: o.Logger,
})
s := api.New(o.Tags, o.Storer, nil, o.Logger, nil)
ts := httptest.NewServer(s)
t.Cleanup(ts.Close)
......
......@@ -74,11 +74,7 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
}
func newBZZTestServer(t *testing.T, o testServerOptions) *http.Client {
s := api.New(api.Options{
Storer: o.Storer,
Tags: o.Tags,
Logger: logging.New(ioutil.Discard, 0),
})
s := api.New(o.Tags, o.Storer, nil, logging.New(ioutil.Discard, 0), nil)
ts := httptest.NewServer(s)
t.Cleanup(ts.Close)
......
......@@ -69,30 +69,27 @@ type Bee struct {
}
type Options struct {
DataDir string
DBCapacity uint64
Password string
APIAddr string
DebugAPIAddr string
Addr string
NATAddr string
EnableWS bool
EnableQUIC bool
NetworkID uint64
WelcomeMessage string
Bootnodes []string
CORSAllowedOrigins []string
Logger logging.Logger
TracingEnabled bool
TracingEndpoint string
TracingServiceName string
PaymentThreshold uint64
PaymentTolerance uint64
DataDir string
DBCapacity uint64
Password string
APIAddr string
DebugAPIAddr string
NATAddr string
EnableWS bool
EnableQUIC bool
NetworkID uint64
WelcomeMessage string
Bootnodes []string
CORSAllowedOrigins []string
TracingEnabled bool
TracingEndpoint string
TracingServiceName string
DisconnectThreshold uint64
PaymentThreshold uint64
PaymentTolerance uint64
}
func NewBee(o Options) (*Bee, error) {
logger := o.Logger
func NewBee(addr string, logger logging.Logger, o Options) (*Bee, error) {
tracer, tracerCloser, err := tracing.NewTracer(&tracing.Options{
Enabled: o.TracingEnabled,
Endpoint: o.TracingEndpoint,
......@@ -157,15 +154,12 @@ func NewBee(o Options) (*Bee, error) {
addressbook := addressbook.New(stateStore)
signer := crypto.NewDefaultSigner(swarmPrivateKey)
p2ps, err := libp2p.New(p2pCtx, signer, o.NetworkID, address, o.Addr, libp2p.Options{
p2ps, err := libp2p.New(p2pCtx, signer, o.NetworkID, address, addr, addressbook, logger, tracer, libp2p.Options{
PrivateKey: libp2pPrivateKey,
NATAddr: o.NATAddr,
EnableWS: o.EnableWS,
EnableQUIC: o.EnableQUIC,
Addressbook: addressbook,
WelcomeMessage: o.WelcomeMessage,
Logger: logger,
Tracer: tracer,
})
if err != nil {
return nil, fmt.Errorf("p2p service: %w", err)
......@@ -323,13 +317,7 @@ func NewBee(o Options) (*Bee, error) {
var apiService api.Service
if o.APIAddr != "" {
// API server
apiService = api.New(api.Options{
Tags: tagg,
Storer: ns,
CORSAllowedOrigins: o.CORSAllowedOrigins,
Logger: logger,
Tracer: tracer,
})
apiService = api.New(tagg, ns, o.CORSAllowedOrigins, logger, tracer)
apiListener, err := net.Listen("tcp", o.APIAddr)
if err != nil {
return nil, fmt.Errorf("api listener: %w", err)
......
......@@ -23,7 +23,7 @@ import (
)
func TestAddresses(t *testing.T) {
s, _ := newService(t, 1, libp2p.Options{})
s, _ := newService(t, 1, libp2pServiceOpts{})
addrs, err := s.Addresses()
if err != nil {
......@@ -38,9 +38,9 @@ func TestConnectDisconnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
......@@ -64,9 +64,9 @@ func TestDoubleConnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
......@@ -89,9 +89,9 @@ func TestDoubleDisconnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
......@@ -122,9 +122,9 @@ func TestMultipleConnectDisconnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
......@@ -163,9 +163,9 @@ func TestConnectDisconnectOnAllAddresses(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addrs, err := s1.Addresses()
if err != nil {
......@@ -193,9 +193,9 @@ func TestDoubleConnectOnAllAddresses(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addrs, err := s1.Addresses()
if err != nil {
......@@ -229,9 +229,9 @@ func TestDifferentNetworkIDs(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, _ := newService(t, 1, libp2p.Options{})
s1, _ := newService(t, 1, libp2pServiceOpts{})
s2, _ := newService(t, 2, libp2p.Options{})
s2, _ := newService(t, 2, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
......@@ -247,14 +247,18 @@ func TestConnectWithEnabledQUICAndWSTransports(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{
EnableQUIC: true,
EnableWS: true,
s1, overlay1 := newService(t, 1, libp2pServiceOpts{
libp2pOpts: libp2p.Options{
EnableQUIC: true,
EnableWS: true,
},
})
s2, overlay2 := newService(t, 1, libp2p.Options{
EnableQUIC: true,
EnableWS: true,
s2, overlay2 := newService(t, 1, libp2pServiceOpts{
libp2pOpts: libp2p.Options{
EnableQUIC: true,
EnableWS: true,
},
})
addr := serviceUnderlayAddress(t, s1)
......@@ -272,8 +276,8 @@ func TestConnectRepeatHandshake(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
_, err := s2.Connect(ctx, addr)
......@@ -341,11 +345,11 @@ func TestTopologyNotifier(t *testing.T) {
}
)
notifier1 := mockNotifier(n1c, n1d)
s1, overlay1 := newService(t, 1, libp2p.Options{Addressbook: ab1})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{Addressbook: ab1})
s1.SetNotifier(notifier1)
notifier2 := mockNotifier(n2c, n2d)
s2, overlay2 := newService(t, 1, libp2p.Options{Addressbook: ab2})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{Addressbook: ab2})
s2.SetNotifier(notifier2)
addr := serviceUnderlayAddress(t, s1)
......@@ -422,10 +426,10 @@ func TestTopologyLocalNotifier(t *testing.T) {
}
)
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
notifier2 := mockNotifier(n2c, n2d)
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
s2.SetNotifier(notifier2)
addr := serviceUnderlayAddress(t, s1)
......
......@@ -11,7 +11,6 @@ import (
"time"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/libp2p"
)
func TestHeaders(t *testing.T) {
......@@ -23,9 +22,9 @@ func TestHeaders(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
var gotHeaders p2p.Headers
handled := make(chan struct{})
......@@ -70,9 +69,9 @@ func TestHeaders_empty(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
var gotHeaders p2p.Headers
handled := make(chan struct{})
......@@ -126,9 +125,9 @@ func TestHeadler(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, _ := newService(t, 1, libp2p.Options{})
s2, _ := newService(t, 1, libp2pServiceOpts{})
var gotReceivedHeaders p2p.Headers
handled := make(chan struct{})
......
......@@ -66,12 +66,9 @@ type Options struct {
EnableQUIC bool
LightNode bool
WelcomeMessage string
Addressbook addressbook.Putter
Logger logging.Logger
Tracer *tracing.Tracer
}
func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay swarm.Address, addr string, o Options) (*Service, error) {
func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay swarm.Address, addr string, ab addressbook.Putter, logger logging.Logger, tracer *tracing.Tracer, o Options) (*Service, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, fmt.Errorf("address: %w", err)
......@@ -181,7 +178,7 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
}
}
handshakeService, err := handshake.New(signer, advertisableAddresser, overlay, networkID, o.LightNode, o.WelcomeMessage, o.Logger)
handshakeService, err := handshake.New(signer, advertisableAddresser, overlay, networkID, o.LightNode, o.WelcomeMessage, logger)
if err != nil {
return nil, fmt.Errorf("handshake service: %w", err)
}
......@@ -196,9 +193,9 @@ func New(ctx context.Context, signer beecrypto.Signer, networkID uint64, overlay
metrics: newMetrics(),
networkID: networkID,
peers: peerRegistry,
addressbook: o.Addressbook,
logger: o.Logger,
tracer: o.Tracer,
addressbook: ab,
logger: logger,
tracer: tracer,
connectionBreaker: breaker.NewBreaker(breaker.Options{}), // use default options
}
// Construct protocols.
......
......@@ -7,6 +7,7 @@ package libp2p_test
import (
"bytes"
"context"
"crypto/ecdsa"
"io/ioutil"
"sort"
"testing"
......@@ -22,8 +23,15 @@ import (
"github.com/multiformats/go-multiaddr"
)
type libp2pServiceOpts struct {
Logger logging.Logger
Addressbook addressbook.Interface
PrivateKey *ecdsa.PrivateKey
libp2pOpts libp2p.Options
}
// newService constructs a new libp2p service.
func newService(t *testing.T, networkID uint64, o libp2p.Options) (s *libp2p.Service, overlay swarm.Address) {
func newService(t *testing.T, networkID uint64, o libp2pServiceOpts) (s *libp2p.Service, overlay swarm.Address) {
t.Helper()
swarmKey, err := crypto.GenerateSecp256k1Key()
......@@ -57,7 +65,7 @@ func newService(t *testing.T, networkID uint64, o libp2p.Options) (s *libp2p.Ser
}
ctx, cancel := context.WithCancel(context.Background())
s, err = libp2p.New(ctx, crypto.NewDefaultSigner(swarmKey), networkID, overlay, addr, o)
s, err = libp2p.New(ctx, crypto.NewDefaultSigner(swarmKey), networkID, overlay, addr, o.Addressbook, o.Logger, nil, o.libp2pOpts)
if err != nil {
t.Fatal(err)
}
......
......@@ -11,7 +11,6 @@ import (
"testing"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/libp2p"
"github.com/multiformats/go-multistream"
)
......@@ -19,9 +18,9 @@ func TestNewStream(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, _ := newService(t, 1, libp2p.Options{})
s2, _ := newService(t, 1, libp2pServiceOpts{})
if err := s1.AddProtocol(newTestProtocol(func(_ context.Context, _ p2p.Peer, _ p2p.Stream) error {
return nil
......@@ -51,7 +50,7 @@ func TestNewStreamMulti(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
var (
h1calls, h2calls int32
h1 = func(_ context.Context, _ p2p.Peer, s p2p.Stream) error {
......@@ -65,7 +64,7 @@ func TestNewStreamMulti(t *testing.T) {
return nil
}
)
s2, _ := newService(t, 1, libp2p.Options{})
s2, _ := newService(t, 1, libp2pServiceOpts{})
if err := s1.AddProtocol(newTestMultiProtocol(h1, h2)); err != nil {
t.Fatal(err)
......@@ -96,9 +95,9 @@ func TestNewStream_errNotSupported(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, _ := newService(t, 1, libp2p.Options{})
s2, _ := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
......@@ -131,9 +130,9 @@ func TestNewStream_semanticVersioning(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, _ := newService(t, 1, libp2p.Options{})
s2, _ := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
......@@ -190,9 +189,9 @@ func TestDisconnectError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2p.Options{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
if err := s1.AddProtocol(newTestProtocol(func(_ context.Context, _ p2p.Peer, _ p2p.Stream) error {
return p2p.NewDisconnectError(errors.New("test error"))
......
......@@ -11,7 +11,6 @@ import (
"time"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/libp2p"
"github.com/ethersphere/bee/pkg/tracing"
)
......@@ -34,9 +33,9 @@ func TestTracing(t *testing.T) {
}
defer closer2.Close()
s1, overlay1 := newService(t, 1, libp2p.Options{})
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, _ := newService(t, 1, libp2p.Options{})
s2, _ := newService(t, 1, libp2pServiceOpts{})
var handledTracingSpan string
handled := make(chan struct{})
......
......@@ -12,7 +12,7 @@ import (
func TestDynamicWelcomeMessage(t *testing.T) {
const TestWelcomeMessage = "Hello World!"
svc, _ := newService(t, 1, libp2p.Options{WelcomeMessage: TestWelcomeMessage})
svc, _ := newService(t, 1, libp2pServiceOpts{libp2pOpts: libp2p.Options{WelcomeMessage: TestWelcomeMessage}})
t.Run("Get current message - OK", func(t *testing.T) {
got := svc.GetWelcomeMessage()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment