Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
8d84ecd0
Commit
8d84ecd0
authored
Dec 14, 2017
by
Jeromy
Browse files
functional parameters
parent
d0426805
Changes
3
Hide whitespace changes
Inline
Side-by-side
examples/echo/main.go
View file @
8d84ecd0
...
@@ -18,12 +18,8 @@ import (
...
@@ -18,12 +18,8 @@ import (
net
"github.com/libp2p/go-libp2p-net"
net
"github.com/libp2p/go-libp2p-net"
peer
"github.com/libp2p/go-libp2p-peer"
peer
"github.com/libp2p/go-libp2p-peer"
pstore
"github.com/libp2p/go-libp2p-peerstore"
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"
ma
"github.com/multiformats/go-multiaddr"
gologging
"github.com/whyrusleeping/go-logging"
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
// 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
...
@@ -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
// Generate a key pair for this host. We will use it at least
// to obtain a valid host ID.
// 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
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
// Obtain Peer ID from public key
opts
:=
[]
libp2p
.
Option
{
pid
,
err
:=
peer
.
IDFromPublicKey
(
pub
)
libp2p
.
ListenAddrStrings
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
listenPort
)),
if
err
!=
nil
{
libp2p
.
WithPeerKey
(
priv
),
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
}
}
// Create a peerstore
if
!
secio
{
ps
:=
pstore
.
NewPeerstore
()
opts
=
append
(
opts
,
libp2p
.
NoSecio
)
// If using secio, we add the keys to the peerstore
// for this peer ID.
if
secio
{
ps
.
AddPrivKey
(
pid
,
priv
)
ps
.
AddPubKey
(
pid
,
pub
)
}
}
basicHost
,
err
:=
libp2p
.
NewWithCfg
(
ctx
,
&
libp2p
.
Config
{
basicHost
,
err
:=
libp2p
.
New
(
context
.
Background
(),
opts
...
)
ListenAddrs
:
[]
ma
.
Multiaddr
{
addr
},
})
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -82,6 +62,7 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
...
@@ -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
// Now we can build a full multiaddress to reach this host
// by encapsulating both addresses:
// by encapsulating both addresses:
addr
:=
basicHost
.
Addrs
()[
0
]
fullAddr
:=
addr
.
Encapsulate
(
hostAddr
)
fullAddr
:=
addr
.
Encapsulate
(
hostAddr
)
log
.
Printf
(
"I am %s
\n
"
,
fullAddr
)
log
.
Printf
(
"I am %s
\n
"
,
fullAddr
)
if
secio
{
if
secio
{
...
...
examples/libp2p-host/host.go
View file @
8d84ecd0
...
@@ -7,7 +7,6 @@ import (
...
@@ -7,7 +7,6 @@ import (
libp2p
"github.com/libp2p/go-libp2p"
libp2p
"github.com/libp2p/go-libp2p"
crypto
"github.com/libp2p/go-libp2p-crypto"
crypto
"github.com/libp2p/go-libp2p-crypto"
ma
"github.com/multiformats/go-multiaddr"
)
)
func
main
()
{
func
main
()
{
...
@@ -23,27 +22,23 @@ func main() {
...
@@ -23,27 +22,23 @@ func main() {
fmt
.
Printf
(
"Hello World, my hosts ID is %s
\n
"
,
h
.
ID
())
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
// If you want more control over the configuration, you can specify some
// in the libp2p config, and use `NewWithCfg`
// options to the constructor
cfg
:=
new
(
libp2p
.
Config
)
// Set your own keypair
// Set your own keypair
priv
,
_
,
err
:=
crypto
.
GenerateEd25519Key
(
rand
.
Reader
)
priv
,
_
,
err
:=
crypto
.
GenerateEd25519Key
(
rand
.
Reader
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
cfg
.
PeerKey
=
priv
// Set your own listen address
h2
,
err
:=
libp2p
.
New
(
ctx
,
maddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/9000"
)
// Use your own created keypair
if
err
!=
nil
{
libp2p
.
WithPeerKey
(
priv
),
panic
(
err
)
}
// The config takes an array of addresses, specify as many as you want.
cfg
.
ListenAddrs
=
[]
ma
.
Multiaddr
{
maddr
}
h2
,
err
:=
libp2p
.
NewWithCfg
(
ctx
,
cfg
)
// Set your own listen address
// The config takes an array of addresses, specify as many as you want.
libp2p
.
ListenAddrStrings
(
"/ip4/0.0.0.0/tcp/9000"
),
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
...
...
libp2p.go
View file @
8d84ecd0
...
@@ -32,13 +32,88 @@ type Config struct {
...
@@ -32,13 +32,88 @@ type Config struct {
DisableSecio
bool
DisableSecio
bool
}
}
func
New
(
ctx
context
.
Context
)
(
host
.
Host
,
error
)
{
type
Option
func
(
cfg
*
Config
)
error
return
NewWithCfg
(
ctx
,
DefaultConfig
())
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
func
newWithCfg
(
ctx
context
.
Context
,
cfg
*
Config
)
(
host
.
Host
,
error
)
{
// config. `nil` may be passed to use default options.
func
NewWithCfg
(
ctx
context
.
Context
,
cfg
*
Config
)
(
host
.
Host
,
error
)
{
if
cfg
==
nil
{
if
cfg
==
nil
{
cfg
=
DefaultConfig
()
cfg
=
DefaultConfig
()
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment