Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support disc v4/v5 mix mode in bootnode #312

Open
wants to merge 1 commit into
base: optimism
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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/enode"
"github.com/ethereum/go-ethereum/p2p/nat"
Expand All @@ -43,7 +44,8 @@ func main() {
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")
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")

Expand Down Expand Up @@ -123,16 +125,45 @@ func main() {
}

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

//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,
}
_, 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
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