Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
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
d0426805
Commit
d0426805
authored
7 years ago
by
Jeromy
Browse files
Options
Download
Email Patches
Plain Diff
use new constructor for examples
parent
c937b88e
master
2018-Q4-OKR
feat/protobuf
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-nza0mn
jenkinsfile
multistream-ping
punching
revert-276-update-go-detect-race
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
v5.0.15
v5.0.14
v5.0.13
v5.0.12
v5.0.11
v5.0.10
v5.0.9
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/echo/main.go
+4
-18
examples/echo/main.go
examples/libp2p-host/host.go
+21
-20
examples/libp2p-host/host.go
libp2p.go
+13
-9
libp2p.go
with
38 additions
and
47 deletions
+38
-47
examples/echo/main.go
View file @
d0426805
...
...
@@ -12,6 +12,7 @@ import (
mrand
"math/rand"
golog
"github.com/ipfs/go-log"
libp2p
"github.com/libp2p/go-libp2p"
crypto
"github.com/libp2p/go-libp2p-crypto"
host
"github.com/libp2p/go-libp2p-host"
net
"github.com/libp2p/go-libp2p-net"
...
...
@@ -69,28 +70,13 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
ps
.
AddPubKey
(
pid
,
pub
)
}
// Set up stream multiplexer
tpt
:=
msmux
.
NewBlankTransport
()
tpt
.
AddTransport
(
"/yamux/1.0.0"
,
yamux
.
DefaultTransport
)
// Create swarm (implements libP2P Network)
swrm
,
err
:=
swarm
.
NewSwarmWithProtector
(
context
.
Background
(),
[]
ma
.
Multiaddr
{
addr
},
pid
,
ps
,
nil
,
tpt
,
nil
,
)
basicHost
,
err
:=
libp2p
.
NewWithCfg
(
ctx
,
&
libp2p
.
Config
{
ListenAddrs
:
[]
ma
.
Multiaddr
{
addr
},
})
if
err
!=
nil
{
return
nil
,
err
}
netw
:=
(
*
swarm
.
Network
)(
swrm
)
basicHost
:=
bhost
.
New
(
netw
)
// Build host multiaddress
hostAddr
,
_
:=
ma
.
NewMultiaddr
(
fmt
.
Sprintf
(
"/ipfs/%s"
,
basicHost
.
ID
()
.
Pretty
()))
...
...
This diff is collapsed.
Click to expand it.
examples/libp2p-host/host.go
View file @
d0426805
...
...
@@ -5,47 +5,48 @@ import (
"crypto/rand"
"fmt"
libp2p
"github.com/libp2p/go-libp2p"
crypto
"github.com/libp2p/go-libp2p-crypto"
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"
)
func
main
()
{
// Generate an identity keypair using go's cryptographic randomness source
priv
,
pub
,
err
:=
crypto
.
GenerateEd25519Key
(
rand
.
Reader
)
// The context governs the lifetime of the libp2p node
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
// To construct a simple host with all the default settings, just use `New`
h
,
err
:=
libp2p
.
New
(
ctx
)
if
err
!=
nil
{
panic
(
err
)
}
// A peers ID is the hash of its public key
pid
,
err
:=
peer
.
IDFromPublicKey
(
pub
)
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
)
// Set your own keypair
priv
,
_
,
err
:=
crypto
.
GenerateEd25519Key
(
rand
.
Reader
)
if
err
!=
nil
{
panic
(
err
)
}
cfg
.
PeerKey
=
priv
// We've created the identity, now we need to store it.
// A peerstore holds information about peers, including your own
ps
:=
pstore
.
NewPeerstore
()
ps
.
AddPrivKey
(
pid
,
priv
)
ps
.
AddPubKey
(
pid
,
pub
)
// Set your own listen address
maddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/9000"
)
if
err
!=
nil
{
panic
(
err
)
}
//
Make a context to govern the lifespan of the swarm
c
tx
:=
context
.
Background
()
//
The config takes an array of addresses, specify as many as you want.
c
fg
.
ListenAddrs
=
[]
ma
.
Multiaddr
{
maddr
}
// Put all this together
netw
,
err
:=
swarm
.
NewNetwork
(
ctx
,
[]
ma
.
Multiaddr
{
maddr
},
pid
,
ps
,
nil
)
h2
,
err
:=
libp2p
.
NewWithCfg
(
ctx
,
cfg
)
if
err
!=
nil
{
panic
(
err
)
}
myhost
:=
bhost
.
New
(
netw
)
fmt
.
Printf
(
"Hello World, my hosts ID is %s
\n
"
,
myhost
.
ID
())
fmt
.
Printf
(
"Hello World, my second hosts ID is %s
\n
"
,
h2
.
ID
())
}
This diff is collapsed.
Click to expand it.
libp2p.go
View file @
d0426805
...
...
@@ -22,13 +22,14 @@ import (
// Config describes a set of settings for a libp2p node
type
Config
struct
{
Transports
[]
transport
.
Transport
Muxer
mux
.
Transport
ListenAddrs
[]
ma
.
Multiaddr
PeerKey
crypto
.
PrivKey
Peerstore
pstore
.
Peerstore
Protector
pnet
.
Protector
Reporter
metrics
.
Reporter
Transports
[]
transport
.
Transport
Muxer
mux
.
Transport
ListenAddrs
[]
ma
.
Multiaddr
PeerKey
crypto
.
PrivKey
Peerstore
pstore
.
Peerstore
Protector
pnet
.
Protector
Reporter
metrics
.
Reporter
DisableSecio
bool
}
func
New
(
ctx
context
.
Context
)
(
host
.
Host
,
error
)
{
...
...
@@ -63,8 +64,11 @@ func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
ps
=
pstore
.
NewPeerstore
()
}
ps
.
AddPrivKey
(
pid
,
cfg
.
PeerKey
)
ps
.
AddPubKey
(
pid
,
cfg
.
PeerKey
.
GetPublic
())
// If secio is disabled, don't add our private key to the peerstore
if
!
cfg
.
DisableSecio
{
ps
.
AddPrivKey
(
pid
,
cfg
.
PeerKey
)
ps
.
AddPubKey
(
pid
,
cfg
.
PeerKey
.
GetPublic
())
}
swrm
,
err
:=
swarm
.
NewSwarmWithProtector
(
ctx
,
cfg
.
ListenAddrs
,
pid
,
ps
,
cfg
.
Protector
,
cfg
.
Muxer
,
cfg
.
Reporter
)
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help