mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-10 03:31:20 +00:00
2f10b1c7b1
* Remove gogoproto compiler * Remove more gogoproto * Improvements * Fix gengo * More scripts * Gazelle, fix deps * Fix version and errors * Fix gocast for arrays * Fix ethapis * Fixes * Fix compile errors * fix go.mod * //proto/... builds * Update for protov2 * temp fix compilation to move on * Change everything to emptypb.empty * Add grpc to proto/slashings * Fix almost all build failures * Oher build problems * FIX THIS FUCKING THING * gaz literally every .bazel * Final touches * Final final touches * Fix proto * Begin moving proto.Marshal to native * Fix site_data * Fixes * Fix duplicate gateway * Fix gateway target * Fix ethapis * Fixes from review * Update * Fix * Fix status test * Fix fuzz * Add isprotoslice to fun * Change DeepEqual to DeepSSZEqual for proto arrays * Fix build * Fix gaz * Update go * Fixes * Fixes * Add case for nil validators after copy * Fix cast * Fix test * Fix imports * Go mod * Only use extension where needed * Fixes * Split gateway from gengo * gaz * go mod * Add back hydrated state * fix hydrate * Fix proto.clone * Fies * Revert "Split gateway from gengo" This reverts commit 7298bb2054d446e427d9af97e13b8fabe8695085. * Revert "gaz" This reverts commit ca952565701a88727e22302d6c8d60ac48d97255. * Merge all gateway into one target * go mod * Gaz * Add generate v1_gateway files * run pb again * goimports * gaz * Fix comments * Fix protos * Fix PR * Fix protos * Update grpc-gateway and ethapis * Update ethapis and gen-go-cast * Go tidy * Reorder * Fix ethapis * fix spec tests * Fix script * Remove unused import * Fix fuzz * Fix gomod * Update version * Error if the cloned result is nil * Handle optional slots * ADd more empty checks to clone * Undo fuzz changes * Fix build.bazel * Gaz * Redo fuzz changes * Undo some eth1data changes * Update go.mod Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Undo clone beacon state * Remove gogo proto more and unused v1_gateway * Add manual fix for nil vals * Fix gaz * tidy * Tidy again * Add detailed error * Revert "Add detailed error" This reverts commit 59bc053dcd59569a54c95b07739d5a379665ec5d. * Undo varint changes * Fix nil validators in deposit test * Commit * Undo Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
174 lines
4.9 KiB
Go
174 lines
4.9 KiB
Go
package p2p
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/ecdsa"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
|
|
gcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/fileutil"
|
|
"github.com/prysmaticlabs/prysm/shared/iputils"
|
|
"github.com/sirupsen/logrus"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
const keyPath = "network-keys"
|
|
const metaDataPath = "metaData"
|
|
|
|
const dialTimeout = 1 * time.Second
|
|
|
|
// SerializeENR takes the enr record in its key-value form and serializes it.
|
|
func SerializeENR(record *enr.Record) (string, error) {
|
|
if record == nil {
|
|
return "", errors.New("could not serialize nil record")
|
|
}
|
|
buf := bytes.NewBuffer([]byte{})
|
|
if err := record.EncodeRLP(buf); err != nil {
|
|
return "", errors.Wrap(err, "could not encode ENR record to bytes")
|
|
}
|
|
enrString := base64.URLEncoding.EncodeToString(buf.Bytes())
|
|
return enrString, nil
|
|
}
|
|
|
|
func convertFromInterfacePrivKey(privkey crypto.PrivKey) *ecdsa.PrivateKey {
|
|
typeAssertedKey := (*ecdsa.PrivateKey)(privkey.(*crypto.Secp256k1PrivateKey))
|
|
typeAssertedKey.Curve = gcrypto.S256() // Temporary hack, so libp2p Secp256k1 is recognized as geth Secp256k1 in disc v5.1.
|
|
return typeAssertedKey
|
|
}
|
|
|
|
func convertToInterfacePrivkey(privkey *ecdsa.PrivateKey) crypto.PrivKey {
|
|
typeAssertedKey := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(privkey))
|
|
return typeAssertedKey
|
|
}
|
|
|
|
func convertToInterfacePubkey(pubkey *ecdsa.PublicKey) crypto.PubKey {
|
|
typeAssertedKey := crypto.PubKey((*crypto.Secp256k1PublicKey)(pubkey))
|
|
return typeAssertedKey
|
|
}
|
|
|
|
// Determines a private key for p2p networking from the p2p service's
|
|
// configuration struct. If no key is found, it generates a new one.
|
|
func privKey(cfg *Config) (*ecdsa.PrivateKey, error) {
|
|
defaultKeyPath := path.Join(cfg.DataDir, keyPath)
|
|
privateKeyPath := cfg.PrivateKey
|
|
|
|
_, err := os.Stat(defaultKeyPath)
|
|
defaultKeysExist := !os.IsNotExist(err)
|
|
if err != nil && defaultKeysExist {
|
|
return nil, err
|
|
}
|
|
|
|
if privateKeyPath == "" && !defaultKeysExist {
|
|
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
convertedKey := convertFromInterfacePrivKey(priv)
|
|
return convertedKey, nil
|
|
}
|
|
if defaultKeysExist && privateKeyPath == "" {
|
|
privateKeyPath = defaultKeyPath
|
|
}
|
|
return privKeyFromFile(privateKeyPath)
|
|
}
|
|
|
|
// Retrieves a p2p networking private key from a file path.
|
|
func privKeyFromFile(path string) (*ecdsa.PrivateKey, error) {
|
|
src, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
log.WithError(err).Error("Error reading private key from file")
|
|
return nil, err
|
|
}
|
|
dst := make([]byte, hex.DecodedLen(len(src)))
|
|
_, err = hex.Decode(dst, src)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to decode hex string")
|
|
}
|
|
unmarshalledKey, err := crypto.UnmarshalSecp256k1PrivateKey(dst)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return convertFromInterfacePrivKey(unmarshalledKey), nil
|
|
}
|
|
|
|
// Retrieves node p2p metadata from a set of configuration values
|
|
// from the p2p service.
|
|
func metaDataFromConfig(cfg *Config) (*pbp2p.MetaData, error) {
|
|
defaultKeyPath := path.Join(cfg.DataDir, metaDataPath)
|
|
metaDataPath := cfg.MetaDataDir
|
|
|
|
_, err := os.Stat(defaultKeyPath)
|
|
defaultMetadataExist := !os.IsNotExist(err)
|
|
if err != nil && defaultMetadataExist {
|
|
return nil, err
|
|
}
|
|
if metaDataPath == "" && !defaultMetadataExist {
|
|
metaData := &pbp2p.MetaData{
|
|
SeqNumber: 0,
|
|
Attnets: bitfield.NewBitvector64(),
|
|
}
|
|
dst, err := proto.Marshal(metaData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fileutil.WriteFile(defaultKeyPath, dst); err != nil {
|
|
return nil, err
|
|
}
|
|
return metaData, nil
|
|
}
|
|
if defaultMetadataExist && metaDataPath == "" {
|
|
metaDataPath = defaultKeyPath
|
|
}
|
|
src, err := ioutil.ReadFile(metaDataPath)
|
|
if err != nil {
|
|
log.WithError(err).Error("Error reading metadata from file")
|
|
return nil, err
|
|
}
|
|
metaData := &pbp2p.MetaData{}
|
|
if err := proto.Unmarshal(src, metaData); err != nil {
|
|
return nil, err
|
|
}
|
|
return metaData, nil
|
|
}
|
|
|
|
// Retrieves an external ipv4 address and converts into a libp2p formatted value.
|
|
func ipAddr() net.IP {
|
|
ip, err := iputils.ExternalIP()
|
|
if err != nil {
|
|
log.Fatalf("Could not get IPv4 address: %v", err)
|
|
}
|
|
return net.ParseIP(ip)
|
|
}
|
|
|
|
// Attempt to dial an address to verify its connectivity
|
|
func verifyConnectivity(addr string, port uint, protocol string) {
|
|
if addr != "" {
|
|
a := net.JoinHostPort(addr, fmt.Sprintf("%d", port))
|
|
fields := logrus.Fields{
|
|
"protocol": protocol,
|
|
"address": a,
|
|
}
|
|
conn, err := net.DialTimeout(protocol, a, dialTimeout)
|
|
if err != nil {
|
|
log.WithError(err).WithFields(fields).Warn("IP address is not accessible")
|
|
return
|
|
}
|
|
if err := conn.Close(); err != nil {
|
|
log.WithError(err).Debug("Could not close connection")
|
|
}
|
|
}
|
|
}
|