fix: bootnode

This commit is contained in:
Anshal Shukla 2024-01-29 19:55:25 +05:30
parent 241af1fa1e
commit 3a9d1539bb
2 changed files with 24 additions and 21 deletions

View file

@ -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"
@ -213,3 +214,12 @@ func doPortMapping(natm nat.Interface, ln *enode.LocalNode, addr *net.UDPAddr) *
return extaddr
}
// Implemented separate function 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)
}

View file

@ -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"
@ -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
}
realaddr := conn.LocalAddr().(*net.UDPAddr)
if natm != nil {
if !realaddr.IP.IsLoopback() {
go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
}
if ext, err := natm.ExternalIP(); err == nil {
// nolint: govet
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
}
}
n := enode.NewV4(&nodeKey.PublicKey, addr.IP, addr.Port, addr.Port)
b.UI.Info(n.String())
if b.dryRun {
return 0
}
defer conn.Close()
db, _ := enode.OpenDB("")
ln := enode.NewLocalNode(db, nodeKey)
listenerAddr := conn.LocalAddr().(*net.UDPAddr)
if natm != nil {
natAddr := bootnode.DoPortMapping(natm, ln, listenerAddr)
if natAddr != nil {
listenerAddr = natAddr
}
}
bootnode.PrintNotice(&nodeKey.PublicKey, *listenerAddr)
cfg := discover.Config{
PrivateKey: nodeKey,
Log: log.Root(),