Commit 8d84ecd0 authored by Jeromy's avatar Jeromy
Browse files

functional parameters

parent d0426805
......@@ -18,12 +18,8 @@ import (
net "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
swarm "github.com/libp2p/go-libp2p-swarm"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
ma "github.com/multiformats/go-multiaddr"
gologging "github.com/whyrusleeping/go-logging"
msmux "github.com/whyrusleeping/go-smux-multistream"
yamux "github.com/whyrusleeping/go-smux-yamux"
)
// makeBasicHost creates a LibP2P host with a random peer ID listening on the
......@@ -42,37 +38,21 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
// Generate a key pair for this host. We will use it at least
// to obtain a valid host ID.
priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
if err != nil {
return nil, err
}
// Obtain Peer ID from public key
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
return nil, err
}
// Create a multiaddress
addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", listenPort))
if err != nil {
return nil, err
opts := []libp2p.Option{
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", listenPort)),
libp2p.WithPeerKey(priv),
}
// Create a peerstore
ps := pstore.NewPeerstore()
// If using secio, we add the keys to the peerstore
// for this peer ID.
if secio {
ps.AddPrivKey(pid, priv)
ps.AddPubKey(pid, pub)
if !secio {
opts = append(opts, libp2p.NoSecio)
}
basicHost, err := libp2p.NewWithCfg(ctx, &libp2p.Config{
ListenAddrs: []ma.Multiaddr{addr},
})
basicHost, err := libp2p.New(context.Background(), opts...)
if err != nil {
return nil, err
}
......@@ -82,6 +62,7 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
// Now we can build a full multiaddress to reach this host
// by encapsulating both addresses:
addr := basicHost.Addrs()[0]
fullAddr := addr.Encapsulate(hostAddr)
log.Printf("I am %s\n", fullAddr)
if secio {
......
......@@ -7,7 +7,6 @@ import (
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
ma "github.com/multiformats/go-multiaddr"
)
func main() {
......@@ -23,27 +22,23 @@ func main() {
fmt.Printf("Hello World, my hosts ID is %s\n", h.ID())
// If you want more control over the configuration, you can fill out fields
// in the libp2p config, and use `NewWithCfg`
cfg := new(libp2p.Config)
// If you want more control over the configuration, you can specify some
// options to the constructor
// Set your own keypair
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
panic(err)
}
cfg.PeerKey = priv
// Set your own listen address
maddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/9000")
if err != nil {
panic(err)
}
h2, err := libp2p.New(ctx,
// Use your own created keypair
libp2p.WithPeerKey(priv),
// Set your own listen address
// The config takes an array of addresses, specify as many as you want.
cfg.ListenAddrs = []ma.Multiaddr{maddr}
h2, err := libp2p.NewWithCfg(ctx, cfg)
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/9000"),
)
if err != nil {
panic(err)
}
......
......@@ -32,13 +32,88 @@ type Config struct {
DisableSecio bool
}
func New(ctx context.Context) (host.Host, error) {
return NewWithCfg(ctx, DefaultConfig())
type Option func(cfg *Config) error
func Transports(tpts []transport.Transport) Option {
return func(cfg *Config) error {
cfg.Transports = tpts
return nil
}
}
func ListenAddrStrings(s ...string) Option {
return func(cfg *Config) error {
for _, addrstr := range s {
a, err := ma.NewMultiaddr(addrstr)
if err != nil {
return err
}
cfg.ListenAddrs = append(cfg.ListenAddrs, a)
}
return nil
}
}
func ListenAddrs(addrs []ma.Multiaddr) Option {
return func(cfg *Config) error {
cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...)
return nil
}
}
func NoSecio(cfg *Config) error {
cfg.DisableSecio = true
return nil
}
func WithMuxer(m mux.Transport) Option {
return func(cfg *Config) error {
cfg.Muxer = m
return nil
}
}
func WithPeerstore(ps pstore.Peerstore) Option {
return func(cfg *Config) error {
cfg.Peerstore = ps
return nil
}
}
func WithNetProtector(prot pnet.Protector) Option {
return func(cfg *Config) error {
cfg.Protector = prot
return nil
}
}
func WithBandwidthReporter(rep metrics.Reporter) Option {
return func(cfg *Config) error {
cfg.Reporter = rep
return nil
}
}
func New(ctx context.Context, opts ...Option) (host.Host, error) {
cfg := DefaultConfig()
for _, opt := range opts {
if err := opt(cfg); err != nil {
return nil, err
}
}
return newWithCfg(ctx, cfg)
}
func WithPeerKey(sk crypto.PrivKey) Option {
return func(cfg *Config) error {
cfg.PeerKey = sk
return nil
}
}
// Construct instantiates a libp2p host using information from the given
// config. `nil` may be passed to use default options.
func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
if cfg == nil {
cfg = DefaultConfig()
}
......
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