Skip to content

Commit

Permalink
feat: support disc v4/v5 mix mode in bootnode
Browse files Browse the repository at this point in the history
  • Loading branch information
2129zxl authored and krish-nr committed May 13, 2024
1 parent 3c5e2fd commit 28c4eb1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
68 changes: 49 additions & 19 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
"github.com/ethereum/go-ethereum/p2p/enode"
Expand All @@ -39,19 +40,19 @@ import (

func main() {
var (
listenAddr = flag.String("addr", ":30301", "listen address")
genKey = flag.String("genkey", "", "generate a node key")
writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit")
nodeKeyFile = flag.String("nodekey", "", "private key filename")
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>)")
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-5)")
vmodule = flag.String("logvmodule", "", "log verbosity pattern")
listenAddr = flag.String("addr", ":30301", "listen address")
genKey = flag.String("genkey", "", "generate a node key")
writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit")
nodeKeyFile = flag.String("nodekey", "", "private key filename")
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>)")
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
runv5 = flag.Bool("v5", true, "run a v5 topic discovery bootnode")
runv4 = flag.Bool("v4", false, "run a v4 topic discovery bootnode")
verbosity = flag.Int("verbosity", 3, "log verbosity (0-5)")
vmodule = flag.String("vmodule", "", "log verbosity pattern")
network = flag.String("network", "", "testnet/mainnet")
staticP2pNodes = flag.String("staticnodes", "", "static p2p nodes for discovery")

nodeKey *ecdsa.PrivateKey
err error
)
Expand Down Expand Up @@ -146,17 +147,46 @@ func main() {
}

printNotice(&nodeKey.PublicKey, *listenerAddr)
cfg := discover.Config{
PrivateKey: nodeKey,
NetRestrict: restrictList,
StaticV4Nodes: staticV4Nodes,

//support v4 & v5
var (
sharedconn discover.UDPConn = conn
unhandled chan discover.ReadPacket
)

if !*runv5 && !*runv4 {
utils.Fatalf("%v", fmt.Errorf("at least one protocol need to be set (v4/v5)"))
}
if *runv5 {
if _, err := discover.ListenV5(conn, ln, cfg); err != nil {

// If both versions of discovery are running, setup a shared
// connection, so v5 can read unhandled messages from v4.
if *runv5 && *runv4 {
unhandled = make(chan discover.ReadPacket, 100)
sharedconn = p2p.NewSharedUDPConn(conn, unhandled)
}

// Start discovery services.
if *runv4 {
cfg := discover.Config{
PrivateKey: nodeKey,
NetRestrict: restrictList,
Unhandled: unhandled,
StaticV4Nodes: staticV4Nodes,
}
_, err := discover.ListenV4(conn, ln, cfg)
log.Info("discv4 protocol enabled")
if err != nil {
utils.Fatalf("%v", err)
}
} else {
if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
}
if *runv5 {
cfg := discover.Config{
PrivateKey: nodeKey,
NetRestrict: restrictList,
}
_, err := discover.ListenV5(sharedconn, ln, cfg)
log.Info("discv5 protocol enabled")
if err != nil {
utils.Fatalf("%v", err)
}
}
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/ethereum/go-ethereum

go 1.21

toolchain go1.21.6

require (
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
github.com/Microsoft/go-winio v0.6.1
Expand Down
10 changes: 9 additions & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ type sharedUDPConn struct {
unhandled chan discover.ReadPacket
}

// NewSharedUDPConn inits a new SharedUDPConn instance
func NewSharedUDPConn(conn *net.UDPConn, unhandled chan discover.ReadPacket) *sharedUDPConn {
return &sharedUDPConn{
UDPConn: conn,
unhandled: unhandled,
}
}

// ReadFromUDP implements discover.UDPConn
func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
packet, ok := <-s.unhandled
Expand Down Expand Up @@ -549,7 +557,7 @@ func (srv *Server) setupDiscovery() error {
// connection, so v5 can read unhandled messages from v4.
if srv.DiscoveryV4 && srv.DiscoveryV5 {
unhandled = make(chan discover.ReadPacket, 100)
sconn = &sharedUDPConn{conn, unhandled}
sconn = NewSharedUDPConn(conn, unhandled)
}

// Start discovery services.
Expand Down

0 comments on commit 28c4eb1

Please sign in to comment.