Commit b13e618e authored by tavit ohanian's avatar tavit ohanian

initial tutorial using fork of libp2p

parents
use this code to test bring up and exercise libp2p nodes.
usage:
go build -o libp2p-node
chmod +x libp2p-node
./libp2p-node
module example.com/hello
go 1.15
require (
4d63.com/gochecknoinits v0.0.0-20200108094044-eb73b47b9fc4 // indirect
github.com/alecthomas/gocyclo v0.0.0-20150208221726-aa8f8b160214 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20210225214923-2e10b2664254 // indirect
github.com/jgautheron/goconst v1.4.0 // indirect
github.com/libp2p/go-libp2p v0.8.1
github.com/libp2p/go-libp2p-core v0.8.4
github.com/mdempsky/maligned v0.0.0-20210215075342-f0178ee82032 // indirect
github.com/mdempsky/unconvert v0.0.0-20200228143138-95ecdbfc0b5f // indirect
github.com/mibk/dupl v1.0.0 // indirect
github.com/multiformats/go-multiaddr v0.3.1
github.com/opennota/check v0.0.0-20180911053232-0c771f5545ff // indirect
github.com/securego/gosec v0.0.0-20200401082031-e946c8c39989 // indirect
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 // indirect
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed // indirect
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
)
replace github.com/libp2p/go-libp2p v0.8.1 => /home/tavit/Work/dapp/gospace/go1.15.2/src/go-libp2p
This diff is collapsed.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/libp2p/go-libp2p"
peerstore "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
multiaddr "github.com/multiformats/go-multiaddr"
)
func main() {
// create a background context (i.e. one that never cancels)
ctx := context.Background()
// start a libp2p node that listens on TCP port 2000 on the IPv4
// loopback interface
node, err := libp2p.New(ctx,
// libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/2000"),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
libp2p.Ping(false),
)
if err != nil {
panic(err)
}
// configure our own ping protocol
pingService := &ping.PingService{Host: node}
node.SetStreamHandler(ping.ID, pingService.PingHandler)
// print the node's PeerInfo in multiaddr format
peerInfo := peerstore.AddrInfo{
ID: node.ID(),
Addrs: node.Addrs(),
}
addrs, err := peerstore.AddrInfoToP2pAddrs(&peerInfo)
fmt.Println("libp2p node address:", addrs[0])
fmt.Println("libp2p node ID:", node.ID())
fmt.Println("libp2p node Host:", node.Mux())
// if a remote peer has been passed on the command line, connect to it
// and send it 5 ping messages, otherwise wait for a signal to stop
if len(os.Args) > 1 {
addr, err := multiaddr.NewMultiaddr(os.Args[1])
if err != nil {
panic(err)
}
peer, err := peerstore.AddrInfoFromP2pAddr(addr)
if err != nil {
panic(err)
}
if err := node.Connect(ctx, *peer); err != nil {
panic(err)
}
fmt.Println("sending 5 ping messages to", addr)
ch := pingService.Ping(ctx, peer.ID)
for i := 0; i < 5; i++ {
res := <-ch
fmt.Println("got ping response!", "RTT:", res.RTT)
}
} else {
// wait for a SIGINT or SIGTERM signal
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
fmt.Println("Received signal, shutting down...")
}
// shut the node down
if err := node.Close(); err != nil {
panic(err)
}
}
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