Skip to content

Commit 92580d6

Browse files
zsfelfoldifjl
authored andcommitted
p2p, p2p/discover, p2p/discv5: implement UDP port sharing (#15200)
This commit affects p2p/discv5 "topic discovery" by running it on the same UDP port where the old discovery works. This is realized by giving an "unhandled" packet channel to the old v4 discovery packet handler where all invalid packets are sent. These packets are then processed by v5. v5 packets are always invalid when interpreted by v4 and vice versa. This is ensured by adding one to the first byte of the packet hash in v5 packets. DiscoveryV5Bootnodes is also changed to point to new bootnodes that are implementing the changed packet format with modified hash. Existing and new v5 bootnodes are both running on different ports ATM.
1 parent 02aeb3d commit 92580d6

17 files changed

Lines changed: 150 additions & 91 deletions

File tree

‎cmd/bootnode/main.go‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/ecdsa"
2222
"flag"
2323
"fmt"
24+
"net"
2425
"os"
2526

2627
"github.com/ethereum/go-ethereum/cmd/utils"
@@ -96,12 +97,32 @@ func main() {
9697
}
9798
}
9899

100+
addr, err := net.ResolveUDPAddr("udp", *listenAddr)
101+
if err != nil {
102+
utils.Fatalf("-ResolveUDPAddr: %v", err)
103+
}
104+
conn, err := net.ListenUDP("udp", addr)
105+
if err != nil {
106+
utils.Fatalf("-ListenUDP: %v", err)
107+
}
108+
109+
realaddr := conn.LocalAddr().(*net.UDPAddr)
110+
if natm != nil {
111+
if !realaddr.IP.IsLoopback() {
112+
go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
113+
}
114+
// TODO: react to external IP changes over time.
115+
if ext, err := natm.ExternalIP(); err == nil {
116+
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
117+
}
118+
}
119+
99120
if *runv5 {
100-
if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
121+
if _, err := discv5.ListenUDP(nodeKey, conn, realaddr, "", restrictList); err != nil {
101122
utils.Fatalf("%v", err)
102123
}
103124
} else {
104-
if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
125+
if _, err := discover.ListenUDP(nodeKey, conn, realaddr, nil, "", restrictList); err != nil {
105126
utils.Fatalf("%v", err)
106127
}
107128
}

‎cmd/faucet/faucet.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ func newFaucet(genesis *core.Genesis, port int, enodes []*discv5.Node, network u
223223
NoDiscovery: true,
224224
DiscoveryV5: true,
225225
ListenAddr: fmt.Sprintf(":%d", port),
226-
DiscoveryV5Addr: fmt.Sprintf(":%d", port+1),
227226
MaxPeers: 25,
228227
BootstrapNodesV5: enodes,
229228
},

‎cmd/utils/flags.go‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,14 +636,6 @@ func setListenAddress(ctx *cli.Context, cfg *p2p.Config) {
636636
}
637637
}
638638

639-
// setDiscoveryV5Address creates a UDP listening address string from set command
640-
// line flags for the V5 discovery protocol.
641-
func setDiscoveryV5Address(ctx *cli.Context, cfg *p2p.Config) {
642-
if ctx.GlobalIsSet(ListenPortFlag.Name) {
643-
cfg.DiscoveryV5Addr = fmt.Sprintf(":%d", ctx.GlobalInt(ListenPortFlag.Name)+1)
644-
}
645-
}
646-
647639
// setNAT creates a port mapper from command line flags.
648640
func setNAT(ctx *cli.Context, cfg *p2p.Config) {
649641
if ctx.GlobalIsSet(NATFlag.Name) {
@@ -794,7 +786,6 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
794786
setNodeKey(ctx, cfg)
795787
setNAT(ctx, cfg)
796788
setListenAddress(ctx, cfg)
797-
setDiscoveryV5Address(ctx, cfg)
798789
setBootstrapNodes(ctx, cfg)
799790
setBootstrapNodesV5(ctx, cfg)
800791

@@ -830,7 +821,6 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
830821
// --dev mode can't use p2p networking.
831822
cfg.MaxPeers = 0
832823
cfg.ListenAddr = ":0"
833-
cfg.DiscoveryV5Addr = ":0"
834824
cfg.NoDiscovery = true
835825
cfg.DiscoveryV5 = false
836826
}

‎les/backend.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,8 @@ func (s *LightEthereum) Start(srvr *p2p.Server) error {
221221
s.startBloomHandlers()
222222
log.Warn("Light client mode is an experimental feature")
223223
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.networkId)
224-
// search the topic belonging to the oldest supported protocol because
225-
// servers always advertise all supported protocols
226-
protocolVersion := ClientProtocolVersions[len(ClientProtocolVersions)-1]
224+
// clients are searching for the first advertised protocol in the list
225+
protocolVersion := AdvertiseProtocolVersions[0]
227226
s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash(), protocolVersion))
228227
s.protocolManager.Start()
229228
return nil

‎les/protocol.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ const (
4141

4242
// Supported versions of the les protocol (first is primary)
4343
var (
44-
ClientProtocolVersions = []uint{lpv2, lpv1}
45-
ServerProtocolVersions = []uint{lpv2, lpv1}
44+
ClientProtocolVersions = []uint{lpv2, lpv1}
45+
ServerProtocolVersions = []uint{lpv2, lpv1}
46+
AdvertiseProtocolVersions = []uint{lpv2} // clients are searching for the first advertised protocol in the list
4647
)
4748

4849
// Number of implemented message corresponding to different protocol versions.

‎les/server.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
5656
return nil, err
5757
}
5858

59-
lesTopics := make([]discv5.Topic, len(ServerProtocolVersions))
60-
for i, pv := range ServerProtocolVersions {
59+
lesTopics := make([]discv5.Topic, len(AdvertiseProtocolVersions))
60+
for i, pv := range AdvertiseProtocolVersions {
6161
lesTopics[i] = lesTopic(eth.BlockChain().Genesis().Hash(), pv)
6262
}
6363

‎mobile/geth.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
116116
P2P: p2p.Config{
117117
NoDiscovery: true,
118118
DiscoveryV5: true,
119-
DiscoveryV5Addr: ":0",
120119
BootstrapNodesV5: config.BootstrapNodes.nodes,
121120
ListenAddr: ":0",
122121
NAT: nat.Any(),

‎node/defaults.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ var DefaultConfig = Config{
4141
WSPort: DefaultWSPort,
4242
WSModules: []string{"net", "web3"},
4343
P2P: p2p.Config{
44-
ListenAddr: ":30303",
45-
DiscoveryV5Addr: ":30304",
46-
MaxPeers: 25,
47-
NAT: nat.Any(),
44+
ListenAddr: ":30303",
45+
MaxPeers: 25,
46+
NAT: nat.Any(),
4847
},
4948
}
5049

‎p2p/discover/udp.go‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,23 @@ type reply struct {
210210
matched chan<- bool
211211
}
212212

213+
// ReadPacket is sent to the unhandled channel when it could not be processed
214+
type ReadPacket struct {
215+
Data []byte
216+
Addr *net.UDPAddr
217+
}
218+
213219
// ListenUDP returns a new table that listens for UDP packets on laddr.
214-
func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, error) {
215-
addr, err := net.ResolveUDPAddr("udp", laddr)
216-
if err != nil {
217-
return nil, err
218-
}
219-
conn, err := net.ListenUDP("udp", addr)
220-
if err != nil {
221-
return nil, err
222-
}
223-
tab, _, err := newUDP(priv, conn, natm, nodeDBPath, netrestrict)
220+
func ListenUDP(priv *ecdsa.PrivateKey, conn conn, realaddr *net.UDPAddr, unhandled chan ReadPacket, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, error) {
221+
tab, _, err := newUDP(priv, conn, realaddr, unhandled, nodeDBPath, netrestrict)
224222
if err != nil {
225223
return nil, err
226224
}
227225
log.Info("UDP listener up", "self", tab.self)
228226
return tab, nil
229227
}
230228

231-
func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, *udp, error) {
229+
func newUDP(priv *ecdsa.PrivateKey, c conn, realaddr *net.UDPAddr, unhandled chan ReadPacket, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, *udp, error) {
232230
udp := &udp{
233231
conn: c,
234232
priv: priv,
@@ -237,16 +235,6 @@ func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath strin
237235
gotreply: make(chan reply),
238236
addpending: make(chan *pending),
239237
}
240-
realaddr := c.LocalAddr().(*net.UDPAddr)
241-
if natm != nil {
242-
if !realaddr.IP.IsLoopback() {
243-
go nat.Map(natm, udp.closing, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
244-
}
245-
// TODO: react to external IP changes over time.
246-
if ext, err := natm.ExternalIP(); err == nil {
247-
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
248-
}
249-
}
250238
// TODO: separate TCP port
251239
udp.ourEndpoint = makeEndpoint(realaddr, uint16(realaddr.Port))
252240
tab, err := newTable(udp, PubkeyID(&priv.PublicKey), realaddr, nodeDBPath)
@@ -256,7 +244,7 @@ func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath strin
256244
udp.Table = tab
257245

258246
go udp.loop()
259-
go udp.readLoop()
247+
go udp.readLoop(unhandled)
260248
return udp.Table, udp, nil
261249
}
262250

@@ -492,8 +480,11 @@ func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte,
492480
}
493481

494482
// readLoop runs in its own goroutine. it handles incoming UDP packets.
495-
func (t *udp) readLoop() {
483+
func (t *udp) readLoop(unhandled chan ReadPacket) {
496484
defer t.conn.Close()
485+
if unhandled != nil {
486+
defer close(unhandled)
487+
}
497488
// Discovery packets are defined to be no larger than 1280 bytes.
498489
// Packets larger than this size will be cut at the end and treated
499490
// as invalid because their hash won't match.
@@ -509,7 +500,12 @@ func (t *udp) readLoop() {
509500
log.Debug("UDP read error", "err", err)
510501
return
511502
}
512-
t.handlePacket(from, buf[:nbytes])
503+
if t.handlePacket(from, buf[:nbytes]) != nil && unhandled != nil {
504+
select {
505+
case unhandled <- ReadPacket{buf[:nbytes], from}:
506+
default:
507+
}
508+
}
513509
}
514510
}
515511

‎p2p/discover/udp_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func newUDPTest(t *testing.T) *udpTest {
7070
remotekey: newkey(),
7171
remoteaddr: &net.UDPAddr{IP: net.IP{10, 0, 1, 99}, Port: 30303},
7272
}
73-
test.table, test.udp, _ = newUDP(test.localkey, test.pipe, nil, "", nil)
73+
realaddr := test.pipe.LocalAddr().(*net.UDPAddr)
74+
test.table, test.udp, _ = newUDP(test.localkey, test.pipe, realaddr, nil, "", nil)
7475
return test
7576
}
7677

0 commit comments

Comments
 (0)