Browse Source

fix: fix connection gater in transport constructor

pull/948/head
Steven Allen 5 years ago
parent
commit
46b58740de
  1. 8
      config/constructor_types.go
  2. 9
      config/reflection_magic.go
  3. 20
      libp2p_test.go

8
config/constructor_types.go

@ -28,12 +28,12 @@ var (
privKeyType = reflect.TypeOf((*crypto.PrivKey)(nil)).Elem() privKeyType = reflect.TypeOf((*crypto.PrivKey)(nil)).Elem()
pubKeyType = reflect.TypeOf((*crypto.PubKey)(nil)).Elem() pubKeyType = reflect.TypeOf((*crypto.PubKey)(nil)).Elem()
pstoreType = reflect.TypeOf((*peerstore.Peerstore)(nil)).Elem() pstoreType = reflect.TypeOf((*peerstore.Peerstore)(nil)).Elem()
connGaterType = reflect.TypeOf((*connmgr.ConnectionGater)(nil)).Elem()
// concrete types // concrete types
peerIDType = reflect.TypeOf((peer.ID)("")) peerIDType = reflect.TypeOf((peer.ID)(""))
upgraderType = reflect.TypeOf((*tptu.Upgrader)(nil)) upgraderType = reflect.TypeOf((*tptu.Upgrader)(nil))
pskType = reflect.TypeOf((pnet.PSK)(nil)) pskType = reflect.TypeOf((pnet.PSK)(nil))
connGaterType = reflect.TypeOf((*connmgr.ConnectionGater)(nil))
) )
var argTypes = map[reflect.Type]constructor{ var argTypes = map[reflect.Type]constructor{

9
config/reflection_magic.go

@ -121,7 +121,14 @@ func makeConstructor(
return func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) (interface{}, error) { return func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) (interface{}, error) {
arguments := make([]reflect.Value, len(argConstructors)) arguments := make([]reflect.Value, len(argConstructors))
for i, makeArg := range argConstructors { for i, makeArg := range argConstructors {
arguments[i] = reflect.ValueOf(makeArg(h, u, cg)) if arg := makeArg(h, u, cg); arg != nil {
arguments[i] = reflect.ValueOf(arg)
} else {
// ValueOf an un-typed nil yields a zero reflect
// value. However, we _want_ the zero value of
// the _type_.
arguments[i] = reflect.Zero(t.In(i))
}
} }
return callConstructor(v, arguments) return callConstructor(v, arguments)
}, nil }, nil

20
libp2p_test.go

@ -7,10 +7,14 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-tcp-transport" "github.com/libp2p/go-tcp-transport"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
) )
func TestNewHost(t *testing.T) { func TestNewHost(t *testing.T) {
@ -33,6 +37,22 @@ func TestBadTransportConstructor(t *testing.T) {
} }
} }
func TestTransportConstructor(t *testing.T) {
ctx := context.Background()
ctor := func(
h host.Host,
_ connmgr.ConnectionGater,
upgrader *tptu.Upgrader,
) transport.Transport {
return tcp.NewTCPTransport(upgrader)
}
h, err := New(ctx, Transport(ctor))
if err != nil {
t.Fatal(err)
}
h.Close()
}
func TestNoListenAddrs(t *testing.T) { func TestNoListenAddrs(t *testing.T) {
ctx := context.Background() ctx := context.Background()
h, err := New(ctx, NoListenAddrs) h, err := New(ctx, NoListenAddrs)

Loading…
Cancel
Save