feat: support disc v4/v5 mix mode in bootnode

This commit is contained in:
2129zxl 2024-05-06 23:13:01 +08:00
parent 3e896c875a
commit a48ee55c64
2 changed files with 48 additions and 9 deletions

View file

@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log" "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"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/p2p/nat"
@ -43,7 +44,8 @@ func main() {
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") 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>)") 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)") 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") 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)") verbosity = flag.Int("verbosity", 3, "log verbosity (0-5)")
vmodule = flag.String("vmodule", "", "log verbosity pattern") vmodule = flag.String("vmodule", "", "log verbosity pattern")
@ -123,16 +125,45 @@ func main() {
} }
printNotice(&nodeKey.PublicKey, *listenerAddr) printNotice(&nodeKey.PublicKey, *listenerAddr)
//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 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,
}
_, err := discover.ListenV4(conn, ln, cfg)
log.Info("discv4 protocol enabled")
if err != nil {
utils.Fatalf("%v", err)
}
}
if *runv5 {
cfg := discover.Config{ cfg := discover.Config{
PrivateKey: nodeKey, PrivateKey: nodeKey,
NetRestrict: restrictList, NetRestrict: restrictList,
} }
if *runv5 { _, err := discover.ListenV5(sharedconn, ln, cfg)
if _, err := discover.ListenV5(conn, ln, cfg); err != nil { log.Info("discv5 protocol enabled")
utils.Fatalf("%v", err) if err != nil {
}
} else {
if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
utils.Fatalf("%v", err) utils.Fatalf("%v", err)
} }
} }

View file

@ -425,6 +425,14 @@ type sharedUDPConn struct {
unhandled chan discover.ReadPacket 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 // ReadFromUDP implements discover.UDPConn
func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) { func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
packet, ok := <-s.unhandled packet, ok := <-s.unhandled
@ -549,7 +557,7 @@ func (srv *Server) setupDiscovery() error {
// connection, so v5 can read unhandled messages from v4. // connection, so v5 can read unhandled messages from v4.
if srv.DiscoveryV4 && srv.DiscoveryV5 { if srv.DiscoveryV4 && srv.DiscoveryV5 {
unhandled = make(chan discover.ReadPacket, 100) unhandled = make(chan discover.ReadPacket, 100)
sconn = &sharedUDPConn{conn, unhandled} sconn = NewSharedUDPConn(conn, unhandled)
} }
// Start discovery services. // Start discovery services.