rename constructors

This commit is contained in:
Yusef Napora 2019-12-11 17:21:58 -05:00
parent ca63ecd6bc
commit 8ab00e7201
2 changed files with 18 additions and 17 deletions

View File

@ -39,7 +39,7 @@ func makeNode(t *testing.T, seed int64, port int, kp *Keypair) (host.Host, error
t.Fatal(err)
}
tpt, err := NewTransport(priv, NoiseKeyPair(kp))
tpt, err := New(priv, NoiseKeyPair(kp))
if err != nil {
t.Fatal(err)
}
@ -66,7 +66,7 @@ func makeNodePipes(t *testing.T, seed int64, port int, rpid peer.ID, rpubkey [32
t.Fatal(err)
}
tpt, err := NewTransport(priv, UseNoisePipes, NoiseKeyPair(kp))
tpt, err := New(priv, UseNoisePipes, NoiseKeyPair(kp))
if err != nil {
t.Fatal(err)
}
@ -252,16 +252,16 @@ func TestLibp2pIntegration_XXFallback(t *testing.T) {
time.Sleep(time.Second)
}
func TestNewTransportGenerator(t *testing.T) {
func TestConstrucingWithMaker(t *testing.T) {
kp := GenerateKeypair()
ctx := context.Background()
h, err := libp2p.New(ctx,
libp2p.Security(ID,
NewTransportConstructor(NoiseKeyPair(kp), UseNoisePipes)))
Maker(NoiseKeyPair(kp), UseNoisePipes)))
if err != nil {
t.Fatalf("unable to create libp2p host with NewTransportConstructor: %v", err)
t.Fatalf("unable to create libp2p host with Maker: %v", err)
}
_ = h.Close()
}

View File

@ -44,19 +44,20 @@ type Transport struct {
type transportConstructor func(crypto.PrivKey) (*Transport, error)
// NewTransportConstructor returns a function that will construct a new Noise transport
// Maker returns a function that will construct a new Noise transport
// using the given Options. The returned function may be provided as a libp2p.Security
// option when configuring a libp2p Host using libp2p.New:
// option when configuring a libp2p Host using libp2p.New, and is compatible with the
// "reflection magic" that libp2p.New uses to inject the private identity key:
//
// host := libp2p.New(
// libp2p.Security(noise.ID, noise.NewTransportConstructor()))
// libp2p.Security(noise.ID, noise.Maker()))
//
// The transport can be configured by passing in Options.
//
// To enable the Noise Pipes pattern (which can be more efficient when reconnecting
// to a known peer), pass in the UseNoisePipes Option:
//
// NewTransportConstructor(UseNoisePipes)
// Maker(UseNoisePipes)
//
// To use a specific Noise keypair, pass in the NoiseKeyPair(kp) option, where
// kp is a noise.Keypair struct. This is most useful when using Noise Pipes, whose
@ -64,21 +65,21 @@ type transportConstructor func(crypto.PrivKey) (*Transport, error)
// the Noise keypair across process restarts makes it more likely that other peers
// will be able to use the more efficient IK handshake pattern.
//
// NewTransportConstructor(UseNoisePipes, NoiseKeypair(keypairLoadedFromDisk))
func NewTransportConstructor(options ...Option) transportConstructor {
// Maker(UseNoisePipes, NoiseKeypair(keypairLoadedFromDisk))
func Maker(options ...Option) transportConstructor {
return func(privKey crypto.PrivKey) (*Transport, error) {
return NewTransport(privKey, options...)
return New(privKey, options...)
}
}
// NewTransport creates a new Noise transport using the given private key as its
// New creates a new Noise transport using the given private key as its
// libp2p identity key. This function may be used when you want a transport
// instance and know the libp2p Host's identity key before the Host is initialized.
// When configuring a go-libp2p Host using libp2p.New, it's simpler to use
// NewTransportConstructor instead, which will receive the identity key when the Host
// Maker instead, which will receive the identity key when the Host
// is initialized.
//
// NewTransport supports all the same Options as NewTransportConstructor.
// New supports all the same Options as noise.Maker.
//
// To configure a go-libp2p Host to use the newly created transport, pass it into
// libp2p.New wrapped in a libp2p.Security Option. You will also need to
@ -86,11 +87,11 @@ func NewTransportConstructor(options ...Option) transportConstructor {
// identity key:
//
// privkey := loadPrivateKeyFromSomewhere()
// noiseTpt := noise.NewTransport(privkey)
// noiseTpt := noise.New(privkey)
// host := libp2p.New(
// libp2p.Identity(privkey),
// libp2p.Security(noise.ID, noiseTpt))
func NewTransport(privkey crypto.PrivKey, options ...Option) (*Transport, error) {
func New(privkey crypto.PrivKey, options ...Option) (*Transport, error) {
localID, err := peer.IDFromPrivateKey(privkey)
if err != nil {
return nil, err