mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
reorg test structure to match tox and CI jobs, drop bumpversion for bump-my-version and move config to pyproject.toml, fix docs building
This commit is contained in:
1
tests/interop/go_pkgs/examples/README.md
Normal file
1
tests/interop/go_pkgs/examples/README.md
Normal file
@ -0,0 +1 @@
|
||||
Copied and modified from https://github.com/libp2p/go-libp2p-examples.
|
||||
137
tests/interop/go_pkgs/examples/echo/main.go
Normal file
137
tests/interop/go_pkgs/examples/echo/main.go
Normal file
@ -0,0 +1,137 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
utils "interop/utils"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/network"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-core/peerstore"
|
||||
|
||||
golog "github.com/ipfs/go-log"
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// LibP2P code uses golog to log messages. They log with different
|
||||
// string IDs (i.e. "swarm"). We can control the verbosity level for
|
||||
// all loggers with:
|
||||
golog.SetAllLoggers(golog.LevelDebug) // Change to DEBUG for extra info
|
||||
|
||||
// Parse options from the command line
|
||||
listenF := flag.Int("l", 0, "wait for incoming connections")
|
||||
target := flag.String("d", "", "target peer to dial")
|
||||
protocolID := flag.String("security", "", "security protocol used for secure channel")
|
||||
seed := flag.Int64("seed", 0, "set random seed for id generation")
|
||||
flag.Parse()
|
||||
|
||||
if *listenF == 0 {
|
||||
log.Fatal("Please provide a port to bind on with -l")
|
||||
}
|
||||
|
||||
// Make a host that listens on the given multiaddress
|
||||
ha, err := utils.MakeBasicHost(*listenF, *protocolID, *seed)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Set a stream handler on host A. /echo/1.0.0 is
|
||||
// a user-defined protocol name.
|
||||
ha.SetStreamHandler("/echo/1.0.0", func(s network.Stream) {
|
||||
log.Println("Got a new stream!")
|
||||
if err := doEcho(s); err != nil {
|
||||
log.Println(err)
|
||||
s.Reset()
|
||||
} else {
|
||||
s.Close()
|
||||
}
|
||||
})
|
||||
|
||||
if *target == "" {
|
||||
log.Println("listening for connections")
|
||||
select {} // hang forever
|
||||
}
|
||||
/**** This is where the listener code ends ****/
|
||||
|
||||
// The following code extracts target's the peer ID from the
|
||||
// given multiaddress
|
||||
ipfsaddr, err := ma.NewMultiaddr(*target)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
pid, err := ipfsaddr.ValueForProtocol(ma.P_IPFS)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
peerid, err := peer.IDB58Decode(pid)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// Decapsulate the /ipfs/<peerID> part from the target
|
||||
// /ip4/<a.b.c.d>/ipfs/<peer> becomes /ip4/<a.b.c.d>
|
||||
targetPeerAddr, _ := ma.NewMultiaddr(
|
||||
fmt.Sprintf("/ipfs/%s", peer.IDB58Encode(peerid)))
|
||||
targetAddr := ipfsaddr.Decapsulate(targetPeerAddr)
|
||||
log.Println("!@# targetAddr=", targetAddr)
|
||||
|
||||
// We have a peer ID and a targetAddr so we add it to the peerstore
|
||||
// so LibP2P knows how to contact it
|
||||
ha.Peerstore().AddAddr(peerid, targetAddr, peerstore.PermanentAddrTTL)
|
||||
|
||||
log.Println("!@# ipfsaddr=", ipfsaddr)
|
||||
pinfo, err := peer.AddrInfoFromP2pAddr(ipfsaddr)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to parse %v to pinfo\n", ipfsaddr)
|
||||
}
|
||||
|
||||
err = ha.Connect(context.Background(), *pinfo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println("connect with peer", *pinfo)
|
||||
|
||||
// make a new stream from host B to host A
|
||||
// it should be handled on host A by the handler we set above because
|
||||
// we use the same /echo/1.0.0 protocol
|
||||
s, err := ha.NewStream(context.Background(), peerid, "/echo/1.0.0")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
log.Println("opened stream")
|
||||
|
||||
_, err = s.Write([]byte("Hello, world!\n"))
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
out, err := ioutil.ReadAll(s)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
log.Printf("read reply: %q\n", out)
|
||||
}
|
||||
|
||||
// doEcho reads a line of data a stream and writes it back
|
||||
func doEcho(s network.Stream) error {
|
||||
buf := bufio.NewReader(s)
|
||||
str, err := buf.ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("read: %s\n", str)
|
||||
_, err = s.Write([]byte(str))
|
||||
return err
|
||||
}
|
||||
12
tests/interop/go_pkgs/examples/go.mod
Normal file
12
tests/interop/go_pkgs/examples/go.mod
Normal file
@ -0,0 +1,12 @@
|
||||
module interop
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/ipfs/go-log v1.0.5
|
||||
github.com/libp2p/go-libp2p v0.27.8
|
||||
github.com/libp2p/go-libp2p-core v0.19.0
|
||||
github.com/libp2p/go-libp2p-noise v0.3.0
|
||||
github.com/libp2p/go-libp2p-secio v0.2.2
|
||||
github.com/multiformats/go-multiaddr v0.9.0
|
||||
)
|
||||
1244
tests/interop/go_pkgs/examples/go.sum
Normal file
1244
tests/interop/go_pkgs/examples/go.sum
Normal file
File diff suppressed because it is too large
Load Diff
82
tests/interop/go_pkgs/examples/utils/host.go
Normal file
82
tests/interop/go_pkgs/examples/utils/host.go
Normal file
@ -0,0 +1,82 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
mrand "math/rand"
|
||||
|
||||
"github.com/libp2p/go-libp2p"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/libp2p/go-libp2p-core/host"
|
||||
plaintext "github.com/libp2p/go-libp2p-core/sec/insecure"
|
||||
noise "github.com/libp2p/go-libp2p-noise"
|
||||
secio "github.com/libp2p/go-libp2p-secio"
|
||||
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
|
||||
// MakeBasicHost creates a LibP2P host with a random peer ID listening on the
|
||||
// given multiaddress. It won't encrypt the connection if insecure is true.
|
||||
func MakeBasicHost(listenPort int, protocolID string, randseed int64) (host.Host, error) {
|
||||
|
||||
// If the seed is zero, use real cryptographic randomness. Otherwise, use a
|
||||
// deterministic randomness source to make generated keys stay the same
|
||||
// across multiple runs
|
||||
var r io.Reader
|
||||
if randseed == 0 {
|
||||
r = rand.Reader
|
||||
} else {
|
||||
r = mrand.New(mrand.NewSource(randseed))
|
||||
}
|
||||
|
||||
// Generate a key pair for this host. We will use it at least
|
||||
// to obtain a valid host ID.
|
||||
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
opts := []libp2p.Option{
|
||||
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", listenPort)),
|
||||
libp2p.Identity(priv),
|
||||
libp2p.DisableRelay(),
|
||||
}
|
||||
|
||||
if protocolID == plaintext.ID {
|
||||
opts = append(opts, libp2p.NoSecurity)
|
||||
} else if protocolID == noise.ID {
|
||||
tpt, err := noise.New(priv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts = append(opts, libp2p.Security(protocolID, tpt))
|
||||
} else if protocolID == secio.ID {
|
||||
tpt, err := secio.New(priv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts = append(opts, libp2p.Security(protocolID, tpt))
|
||||
} else {
|
||||
return nil, fmt.Errorf("security protocolID '%s' is not supported", protocolID)
|
||||
}
|
||||
|
||||
basicHost, err := libp2p.New(context.Background(), opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Build host multiaddress
|
||||
hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", basicHost.ID().Pretty()))
|
||||
|
||||
// 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)
|
||||
log.Printf("Now run \"./echo -l %d -d %s -security %s\" on a different terminal\n", listenPort+1, fullAddr, protocolID)
|
||||
|
||||
return basicHost, nil
|
||||
}
|
||||
Reference in New Issue
Block a user