Skip to content

Commit

Permalink
Merge pull request #1149 from maticnetwork/bootnode
Browse files Browse the repository at this point in the history
fix: bootnode
  • Loading branch information
temaniarpit27 authored Jan 30, 2024
2 parents 5c8625c + 0a3d419 commit e4e713c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
13 changes: 12 additions & 1 deletion cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
package main
// Keep package as bootnode during upstram merge.
package bootnode

import (
"crypto/ecdsa"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/netutil"
)

// nolint
func main() {
var (
listenAddr = flag.String("addr", ":30301", "listen address")
Expand Down Expand Up @@ -213,3 +215,12 @@ func doPortMapping(natm nat.Interface, ln *enode.LocalNode, addr *net.UDPAddr) *

return extaddr
}

// Implemented separate functions so that there are minimal conflicts during upstream merge
func PrintNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) {
printNotice(nodeKey, addr)
}

func DoPortMapping(natm nat.Interface, ln *enode.LocalNode, addr *net.UDPAddr) *net.UDPAddr {
return doPortMapping(natm, ln, addr)
}
27 changes: 10 additions & 17 deletions internal/cli/bootnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"syscall"
"time"

"github.com/ethereum/go-ethereum/cmd/bootnode"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/cli/flagset"
Expand Down Expand Up @@ -213,33 +214,25 @@ func (b *BootnodeCommand) Run(args []string) int {
}

conn, err := net.ListenUDP("udp", addr)

if err != nil {
b.UI.Error(fmt.Sprintf("failed to listen udp addr '%s': %v", b.listenAddr, err))
return 1
}
defer conn.Close()

realaddr := conn.LocalAddr().(*net.UDPAddr)
if natm != nil {
if !realaddr.IP.IsLoopback() {
go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
}
db, _ := enode.OpenDB("")
ln := enode.NewLocalNode(db, nodeKey)

if ext, err := natm.ExternalIP(); err == nil {
// nolint: govet
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
listenerAddr := conn.LocalAddr().(*net.UDPAddr)
if natm != nil {
natAddr := bootnode.DoPortMapping(natm, ln, listenerAddr)
if natAddr != nil {
listenerAddr = natAddr
}
}

n := enode.NewV4(&nodeKey.PublicKey, addr.IP, addr.Port, addr.Port)
b.UI.Info(n.String())

if b.dryRun {
return 0
}
bootnode.PrintNotice(&nodeKey.PublicKey, *listenerAddr)

db, _ := enode.OpenDB("")
ln := enode.NewLocalNode(db, nodeKey)
cfg := discover.Config{
PrivateKey: nodeKey,
Log: log.Root(),
Expand Down

0 comments on commit e4e713c

Please sign in to comment.