mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
Use a prysm specific DHT protocol (#2558)
* use a prysm specific DHT * gazelle * space
This commit is contained in:
@@ -36,6 +36,7 @@ go_library(
|
||||
"@com_github_libp2p_go_libp2p_connmgr//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_host//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//opts:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_net//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_peer//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_peerstore//:go_default_library",
|
||||
|
||||
@@ -26,7 +26,7 @@ func optionConnectionManager(maxPeers int) libp2p.Option {
|
||||
maxPeers = 5
|
||||
}
|
||||
minPeers := int(math.Max(5, float64(maxPeers-5)))
|
||||
cm := connmgr.NewConnManager(minPeers, maxPeers, 5*time.Minute)
|
||||
cm := connmgr.NewConnManager(minPeers, maxPeers, 20*time.Second)
|
||||
|
||||
return libp2p.ConnectionManager(cm)
|
||||
}
|
||||
|
||||
@@ -43,12 +43,9 @@ func setupPeerNegotiation(h host.Host, contractAddress string, exclusions []peer
|
||||
log.WithError(err).WithFields(logrus.Fields{
|
||||
"peer": conn.RemotePeer(),
|
||||
"address": conn.RemoteMultiaddr(),
|
||||
}).Warn("Failed to open stream with newly connected peer")
|
||||
}).Debug("Failed to open stream with newly connected peer")
|
||||
|
||||
log.Warn("Temporarily disabled -- not disconnecting peer. See https://github.com/prysmaticlabs/prysm/issues/2408")
|
||||
// if err := h.Network().ClosePeer(conn.RemotePeer()); err != nil {
|
||||
// log.WithError(err).Error("failed to disconnect peer")
|
||||
// }
|
||||
h.ConnManager().TagPeer(conn.RemotePeer(), "handshake", -10000)
|
||||
return
|
||||
}
|
||||
defer s.Close()
|
||||
@@ -88,6 +85,9 @@ func setupPeerNegotiation(h host.Host, contractAddress string, exclusions []peer
|
||||
if err := h.Network().ClosePeer(conn.RemotePeer()); err != nil {
|
||||
log.WithError(err).Error("failed to disconnect peer")
|
||||
}
|
||||
h.ConnManager().TagPeer(conn.RemotePeer(), "ContractAddress", -5000)
|
||||
} else {
|
||||
h.ConnManager().TagPeer(conn.RemotePeer(), "ContractAddress", 10000)
|
||||
}
|
||||
}()
|
||||
},
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/libp2p/go-libp2p"
|
||||
host "github.com/libp2p/go-libp2p-host"
|
||||
kaddht "github.com/libp2p/go-libp2p-kad-dht"
|
||||
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
|
||||
libp2pnet "github.com/libp2p/go-libp2p-net"
|
||||
peer "github.com/libp2p/go-libp2p-peer"
|
||||
protocol "github.com/libp2p/go-libp2p-protocol"
|
||||
@@ -97,7 +98,18 @@ func NewServer(cfg *ServerConfig) (*Server, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dht := kaddht.NewDHT(ctx, h, dsync.MutexWrap(ds.NewMapDatastore()))
|
||||
dopts := []dhtopts.Option{
|
||||
dhtopts.Datastore(dsync.MutexWrap(ds.NewMapDatastore())),
|
||||
dhtopts.Protocols(
|
||||
protocol.ID(prysmProtocolPrefix + "/dht"),
|
||||
),
|
||||
}
|
||||
|
||||
dht, err := kaddht.New(ctx, h, dopts...)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
// Wrap host with a routed host so that peers can be looked up in the
|
||||
// distributed hash table by their peer ID.
|
||||
h = rhost.Wrap(h, dht)
|
||||
|
||||
@@ -10,7 +10,6 @@ go_library(
|
||||
"@com_github_gogo_protobuf//io:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_host//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//opts:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//pb:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_net//:go_default_library",
|
||||
],
|
||||
|
||||
@@ -13,13 +13,14 @@ import (
|
||||
|
||||
ggio "github.com/gogo/protobuf/io"
|
||||
"github.com/libp2p/go-libp2p"
|
||||
host "github.com/libp2p/go-libp2p-host"
|
||||
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
|
||||
"github.com/libp2p/go-libp2p-host"
|
||||
dhtpb "github.com/libp2p/go-libp2p-kad-dht/pb"
|
||||
net "github.com/libp2p/go-libp2p-net"
|
||||
"github.com/libp2p/go-libp2p-net"
|
||||
"github.com/prysmaticlabs/prysm/shared/p2p"
|
||||
)
|
||||
|
||||
const dhtProtocol = "/prysm/0.0.0/dht"
|
||||
|
||||
func main() {
|
||||
if len(os.Args) == 1 {
|
||||
log.Fatal("Error: Bootnode address not provided.")
|
||||
@@ -40,9 +41,9 @@ func main() {
|
||||
log.Fatalf("Error: Failed to create peer from string: %v", err)
|
||||
}
|
||||
|
||||
s, err := h.NewStream(ctx, pi.ID, dhtopts.ProtocolDHT)
|
||||
s, err := h.NewStream(ctx, pi.ID, dhtProtocol)
|
||||
if err != nil {
|
||||
log.Printf("proto = %s", dhtopts.ProtocolDHT)
|
||||
log.Printf("proto = %s", dhtProtocol)
|
||||
log.Fatalf("Error: Failed to create ProtocolDHT stream: %v", err)
|
||||
}
|
||||
|
||||
@@ -66,7 +67,7 @@ func pingPeer(ctx context.Context, h host.Host, p *dhtpb.Message_Peer) error {
|
||||
return err
|
||||
}
|
||||
|
||||
s, err := h.NewStream(ctx, pi.ID, dhtopts.ProtocolDHT)
|
||||
s, err := h.NewStream(ctx, pi.ID, dhtProtocol)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ go_library(
|
||||
"@com_github_libp2p_go_libp2p//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_crypto//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//opts:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_protocol//:go_default_library",
|
||||
"@com_github_multiformats_go_multiaddr//:go_default_library",
|
||||
],
|
||||
)
|
||||
@@ -22,12 +24,14 @@ go_library(
|
||||
go_image(
|
||||
name = "image",
|
||||
srcs = ["bootnode.go"],
|
||||
importpath = "github.com/prysmaticlabs/prysm/tools/bootnode",
|
||||
visibility = ["//visibility:private"],
|
||||
static = "on",
|
||||
tags = ["manual"],
|
||||
goarch = "amd64",
|
||||
goos = "linux",
|
||||
importpath = "github.com/prysmaticlabs/prysm/tools/bootnode",
|
||||
pure = "on",
|
||||
race = "off",
|
||||
static = "on",
|
||||
tags = ["manual"],
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
"//shared/version:go_default_library",
|
||||
"@com_github_ipfs_go_datastore//:go_default_library",
|
||||
@@ -36,10 +40,10 @@ go_image(
|
||||
"@com_github_libp2p_go_libp2p//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_crypto//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_kad_dht//opts:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_protocol//:go_default_library",
|
||||
"@com_github_multiformats_go_multiaddr//:go_default_library",
|
||||
],
|
||||
race = "off",
|
||||
pure = "on",
|
||||
)
|
||||
|
||||
container_push(
|
||||
|
||||
@@ -20,6 +20,8 @@ import (
|
||||
libp2p "github.com/libp2p/go-libp2p"
|
||||
crypto "github.com/libp2p/go-libp2p-crypto"
|
||||
kaddht "github.com/libp2p/go-libp2p-kad-dht"
|
||||
"github.com/libp2p/go-libp2p-kad-dht/opts"
|
||||
"github.com/libp2p/go-libp2p-protocol"
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
"github.com/prysmaticlabs/prysm/shared/version"
|
||||
)
|
||||
@@ -32,6 +34,8 @@ var (
|
||||
log = logging.Logger("prysm-bootnode")
|
||||
)
|
||||
|
||||
const dhtProtocol = "/prysm/0.0.0/dht"
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
@@ -51,13 +55,24 @@ func main() {
|
||||
}
|
||||
opts = addPrivateKeyOpt(opts)
|
||||
|
||||
host, err := libp2p.New(context.Background(), opts...)
|
||||
ctx := context.Background()
|
||||
|
||||
host, err := libp2p.New(ctx, opts...)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create new host. %v", err)
|
||||
}
|
||||
|
||||
dstore := dsync.MutexWrap(ds.NewMapDatastore())
|
||||
dht := kaddht.NewDHT(context.Background(), host, dstore)
|
||||
dopts := []dhtopts.Option{
|
||||
dhtopts.Datastore(dsync.MutexWrap(ds.NewMapDatastore())),
|
||||
dhtopts.Protocols(
|
||||
protocol.ID(dhtProtocol),
|
||||
),
|
||||
}
|
||||
|
||||
dht, err := kaddht.New(ctx, host, dopts...)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create new dht: %v", err)
|
||||
}
|
||||
if err := dht.Bootstrap(context.Background()); err != nil {
|
||||
log.Fatalf("Failed to bootstrap DHT. %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user