erigon-pulse/cl/sentinel/config.go
2023-12-24 03:54:50 +01:00

140 lines
4.1 KiB
Go

/*
Copyright 2022 Erigon-Lightclient contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sentinel
import (
"crypto/ecdsa"
"fmt"
"net"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/log/v3"
"github.com/libp2p/go-libp2p"
mplex "github.com/libp2p/go-libp2p-mplex"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/p2p/security/noise"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/multiformats/go-multiaddr"
)
type SentinelConfig struct {
NetworkConfig *clparams.NetworkConfig
GenesisConfig *clparams.GenesisConfig
BeaconConfig *clparams.BeaconChainConfig
IpAddr string
Port int
TCPPort uint
// Optional
LocalIP string
EnableUPnP bool
RelayNodeAddr string
HostAddress string
HostDNS string
NoDiscovery bool
TmpDir string
LocalDiscovery bool
EnableBlocks bool
}
func convertToCryptoPrivkey(privkey *ecdsa.PrivateKey) (crypto.PrivKey, error) {
privBytes := privkey.D.Bytes()
if len(privBytes) < 32 {
privBytes = append(make([]byte, 32-len(privBytes)), privBytes...)
}
return crypto.UnmarshalSecp256k1PrivateKey(privBytes)
}
func privKeyOption(privkey *ecdsa.PrivateKey) libp2p.Option {
return func(cfg *libp2p.Config) error {
ifaceKey, err := convertToCryptoPrivkey(privkey)
if err != nil {
return err
}
log.Debug("[Sentinel] ECDSA private key generated")
return cfg.Apply(libp2p.Identity(ifaceKey))
}
}
// multiAddressBuilder takes in an ip address string and port to produce a go multiaddr format.
func multiAddressBuilder(ipAddr string, port uint) (multiaddr.Multiaddr, error) {
parsedIP := net.ParseIP(ipAddr)
if parsedIP.To4() == nil && parsedIP.To16() == nil {
return nil, fmt.Errorf("invalid ip address provided: %s", ipAddr)
}
if parsedIP.To4() != nil {
return multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, port))
}
return multiaddr.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/%d", ipAddr, port))
}
func buildOptions(cfg *SentinelConfig, s *Sentinel) ([]libp2p.Option, error) {
var priKey = s.discoverConfig.PrivateKey
listen, err := multiAddressBuilder(cfg.IpAddr, cfg.TCPPort)
if err != nil {
return nil, err
}
if cfg.LocalIP != "" {
if net.ParseIP(cfg.LocalIP) == nil {
return nil, fmt.Errorf("invalid local ip provided: %s", cfg.LocalIP)
}
listen, err = multiAddressBuilder(cfg.LocalIP, cfg.TCPPort)
if err != nil {
return nil, err
}
}
options := []libp2p.Option{
privKeyOption(priKey),
libp2p.ListenAddrs(listen),
libp2p.UserAgent("erigon/caplin"),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
libp2p.DefaultMuxers,
}
options = append(options, libp2p.Security(noise.ID, noise.New), libp2p.DisableRelay())
if cfg.EnableUPnP {
options = append(options, libp2p.NATPortMap()) // Allow to use UPnP
}
if cfg.HostAddress != "" {
options = append(options, libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
external, err := multiAddressBuilder(cfg.HostAddress, cfg.TCPPort)
if err != nil {
return nil
} else {
addrs = append(addrs, external)
}
return addrs
}))
}
if cfg.HostDNS != "" {
options = append(options, libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
external, err := multiaddr.NewMultiaddr(fmt.Sprintf("/dns4/%s/tcp/%d", cfg.HostDNS, cfg.TCPPort))
if err != nil {
return nil
} else {
addrs = append(addrs, external)
}
return addrs
}))
}
// Disable Ping Service.
options = append(options, libp2p.Ping(false))
return options, nil
}