From 0172962b9956d9ad22cdf3f4d0cdc61bbd8549d1 Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Thu, 19 Sep 2024 16:57:31 +1000 Subject: [PATCH] feat(discovery): Add beacon --- go.mod | 4 ++-- go.sum | 8 ++++---- pkg/clmimicry/mimicry.go | 22 ++++++++++----------- pkg/discovery/beaconp2p/config.go | 3 +++ pkg/discovery/beaconp2p/node.go | 16 ++++++++++++++- pkg/discovery/config.go | 8 +++++++- pkg/discovery/discovery.go | 2 +- pkg/discovery/ethereum/services/metadata.go | 2 +- 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 74b2aad8..2951c947 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/ethpandaops/xatu go 1.22.0 -replace github.com/probe-lab/hermes => github.com/ethpandaops/hermes v0.0.0-20240527060557-ae23464cdc41 +replace github.com/probe-lab/hermes => github.com/ethpandaops/hermes v0.0.0-20240919054858-186de8ab9894 replace github.com/ethpandaops/ethcore => ../ethcore @@ -27,7 +27,7 @@ require ( github.com/jellydator/ttlcache/v3 v3.2.0 github.com/klauspost/compress v1.17.9 github.com/lib/pq v1.10.9 - github.com/libp2p/go-libp2p v0.36.3 + github.com/libp2p/go-libp2p v0.36.2 github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/multiformats/go-multiaddr v0.13.0 github.com/oschwald/maxminddb-golang v1.12.0 diff --git a/go.sum b/go.sum index 91b035a0..d13eb490 100644 --- a/go.sum +++ b/go.sum @@ -248,8 +248,8 @@ github.com/ethpandaops/beacon v0.40.0 h1:p1/o5xs0PUACDvrifky4Q5J3lQNzhWZeOyjvKJ2 github.com/ethpandaops/beacon v0.40.0/go.mod h1:vYeQUrplQLR3P1DjwLjKfpRPIDpxNUxIwIvvEHsjM+Y= github.com/ethpandaops/ethwallclock v0.3.0 h1:xF5fwtBf+bHFHZKBnwiPFEuelW3sMM7SD3ZNFq1lJY4= github.com/ethpandaops/ethwallclock v0.3.0/go.mod h1:y0Cu+mhGLlem19vnAV2x0hpFS5KZ7oOi2SWYayv9l24= -github.com/ethpandaops/hermes v0.0.0-20240527060557-ae23464cdc41 h1:KM07rlGX9EQm6ZawvL0BDMJ5Rf3brZIJKsmiFS92wI0= -github.com/ethpandaops/hermes v0.0.0-20240527060557-ae23464cdc41/go.mod h1:uMPOUopuxLk1Qktn66udTjRBLqJJ+CNfiCyxNQda4Ow= +github.com/ethpandaops/hermes v0.0.0-20240919054858-186de8ab9894 h1:LMcP8ahEndzJEJTvMelgxn51IHHOpArSUW9oGw2/jGY= +github.com/ethpandaops/hermes v0.0.0-20240919054858-186de8ab9894/go.mod h1:JLdSTqZYx07rDmu4BtbDarJawsDgK0pp54GZNXOQ5+w= github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= @@ -603,8 +603,8 @@ github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6 github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg= github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFGkx3Q3WM= github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnFNsMtpsYUClFtro= -github.com/libp2p/go-libp2p v0.36.3 h1:NHz30+G7D8Y8YmznrVZZla0ofVANrvBl2c+oARfMeDQ= -github.com/libp2p/go-libp2p v0.36.3/go.mod h1:4Y5vFyCUiJuluEPmpnKYf6WFx5ViKPUYs/ixe9ANFZ8= +github.com/libp2p/go-libp2p v0.36.2 h1:BbqRkDaGC3/5xfaJakLV/BrpjlAuYqSB0lRvtzL3B/U= +github.com/libp2p/go-libp2p v0.36.2/go.mod h1:XO3joasRE4Eup8yCTTP/+kX+g92mOgRaadk46LmPhHY= github.com/libp2p/go-libp2p-asn-util v0.4.1 h1:xqL7++IKD9TBFMgnLPZR6/6iYhawHKHl950SO9L6n94= github.com/libp2p/go-libp2p-asn-util v0.4.1/go.mod h1:d/NI6XZ9qxw67b4e+NgpQexCIiFYJjErASrYW4PFDN8= github.com/libp2p/go-libp2p-mplex v0.9.0 h1:R58pDRAmuBXkYugbSSXR9wrTX3+1pFM1xP2bLuodIq8= diff --git a/pkg/clmimicry/mimicry.go b/pkg/clmimicry/mimicry.go index 1e5ffa79..c72a8f4e 100644 --- a/pkg/clmimicry/mimicry.go +++ b/pkg/clmimicry/mimicry.go @@ -100,22 +100,22 @@ func New(ctx context.Context, log logrus.FieldLogger, config *Config) (*Mimicry, } func (m *Mimicry) startHermes(ctx context.Context) error { - genConfig, netConfig, beaConfig, err := eth.GetConfigsByNetworkName(m.Config.Ethereum.Network) + conf, err := eth.DeriveKnownNetworkConfig(ctx, m.Config.Ethereum.Network) if err != nil { return fmt.Errorf("get config for %s: %w", m.Config.Ethereum.Network, err) } - m.networkConfig = netConfig - m.beaconConfig = beaConfig + m.networkConfig = conf.Network + m.beaconConfig = conf.Beacon - genesisRoot := genConfig.GenesisValidatorRoot - genesisTime := genConfig.GenesisTime + genesisRoot := conf.Genesis.GenesisValidatorRoot + genesisTime := conf.Genesis.GenesisTime // compute fork version and fork digest currentSlot := slots.Since(genesisTime) currentEpoch := slots.ToEpoch(currentSlot) - currentForkVersion, err := eth.GetCurrentForkVersion(currentEpoch, beaConfig) + currentForkVersion, err := eth.GetCurrentForkVersion(currentEpoch, m.beaconConfig) if err != nil { return fmt.Errorf("compute fork version for epoch %d: %w", currentEpoch, err) } @@ -127,14 +127,14 @@ func (m *Mimicry) startHermes(ctx context.Context) error { // Overriding configuration so that functions like ComputForkDigest take the // correct input data from the global configuration. - params.OverrideBeaconConfig(beaConfig) - params.OverrideBeaconNetworkConfig(netConfig) + params.OverrideBeaconConfig(m.beaconConfig) + params.OverrideBeaconNetworkConfig(m.networkConfig) nodeConfig := m.Config.Node.AsHermesConfig() - nodeConfig.GenesisConfig = genConfig - nodeConfig.NetworkConfig = netConfig - nodeConfig.BeaconConfig = beaConfig + nodeConfig.GenesisConfig = conf.Genesis + nodeConfig.NetworkConfig = m.networkConfig + nodeConfig.BeaconConfig = m.beaconConfig nodeConfig.ForkDigest = forkDigest nodeConfig.ForkVersion = currentForkVersion nodeConfig.PubSubSubscriptionRequestLimit = 200 diff --git a/pkg/discovery/beaconp2p/config.go b/pkg/discovery/beaconp2p/config.go index 0a737878..16559187 100644 --- a/pkg/discovery/beaconp2p/config.go +++ b/pkg/discovery/beaconp2p/config.go @@ -61,6 +61,9 @@ func NewNodeDiscoverer( return nil, err } + // Disable discv4 + conf.DiscV4 = false + return static.New(conf, log) default: return nil, fmt.Errorf("beacon p2p type %s is unknown", p2pType) diff --git a/pkg/discovery/beaconp2p/node.go b/pkg/discovery/beaconp2p/node.go index 93b8d313..205b7356 100644 --- a/pkg/discovery/beaconp2p/node.go +++ b/pkg/discovery/beaconp2p/node.go @@ -4,6 +4,7 @@ import ( "context" "time" + "github.com/attestantio/go-eth2-client/api" v1 "github.com/attestantio/go-eth2-client/api/v1" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/go-co-op/gocron" @@ -44,6 +45,8 @@ func NewNode(ctx context.Context, log logrus.FieldLogger, config *Config) (*Node client := mimicry.NewClient(log, &config.Node, mimicry.Mode(mimicry.ModeDiscovery), "xatu") + // Disable discv4 = false + p2pDisc, err := NewNodeDiscoverer(config.Discovery.Type, config.Discovery.Config, log) if err != nil { return nil, perrors.Wrap(err, "failed to create beacon node discoverer") @@ -111,6 +114,10 @@ func (p *Node) Start(ctx context.Context) error { return nil }) + if err := p.beacon.Start(ctx); err != nil { + return perrors.Wrap(err, "failed to start beacon node") + } + return nil } @@ -152,7 +159,9 @@ func (p *Node) fetchAndSetStatus(ctx context.Context) error { status.FinalizedRoot = tree.Root(checkpoint.Finalized.Root) status.FinalizedEpoch = common.Epoch(checkpoint.Finalized.Epoch) - headers, err := p.beacon.Node().FetchBeaconBlockHeader(ctx, nil) + headers, err := p.beacon.Node().FetchBeaconBlockHeader(ctx, &api.BeaconBlockHeaderOpts{ + Block: "head", + }) if err != nil { return err } @@ -181,6 +190,11 @@ func (p *Node) fetchAndSetStatus(ctx context.Context) error { if !p.statusSet { p.statusSet = true + // Start the libp2p node. + if err := p.client.Start(ctx); err != nil { + p.log.WithError(err).Fatal("Failed to start libp2p node") + } + // We now have a valid status and can start feeding peers in to the mimicry node. if err := p.startDiscovery(ctx); err != nil { p.log.WithError(err).Fatal("Failed to start discovery") diff --git a/pkg/discovery/config.go b/pkg/discovery/config.go index 9e7111cb..9a297ee6 100644 --- a/pkg/discovery/config.go +++ b/pkg/discovery/config.go @@ -17,7 +17,7 @@ type Config struct { P2P p2p.Config `yaml:"p2p"` // BeaconP2P configuration - BeaconP2P *beaconp2p.Config `yaml:"beaconP2p"` + BeaconP2P *beaconp2p.Config `yaml:"beaconP2P"` // Coordinator configuration Coordinator coordinator.Config `yaml:"coordinator"` @@ -32,5 +32,11 @@ func (c *Config) Validate() error { return fmt.Errorf("coordinator config error: %w", err) } + if c.BeaconP2P != nil { + if err := c.BeaconP2P.Validate(); err != nil { + return fmt.Errorf("beaconp2p config error: %w", err) + } + } + return nil } diff --git a/pkg/discovery/discovery.go b/pkg/discovery/discovery.go index 871689fd..b645513e 100644 --- a/pkg/discovery/discovery.go +++ b/pkg/discovery/discovery.go @@ -105,7 +105,7 @@ func (d *Discovery) Start(ctx context.Context) error { } // Start the beacon node - if d.Config.BeaconP2P.Enabled != nil && *d.Config.BeaconP2P.Enabled { + if d.Config.BeaconP2P != nil && d.Config.BeaconP2P.Enabled != nil && *d.Config.BeaconP2P.Enabled { d.log.Info("Starting beacon node") if err := d.startBeaconNode(ctx); err != nil { diff --git a/pkg/discovery/ethereum/services/metadata.go b/pkg/discovery/ethereum/services/metadata.go index a5fe1027..8c7b6da8 100644 --- a/pkg/discovery/ethereum/services/metadata.go +++ b/pkg/discovery/ethereum/services/metadata.go @@ -39,7 +39,7 @@ type MetadataService struct { func NewMetadataService(log logrus.FieldLogger, sbeacon beacon.Node, overrideNetworkName string) MetadataService { return MetadataService{ beacon: sbeacon, - log: log.WithField("module", "sentry/ethereum/metadata"), + log: log.WithField("module", "discovery/ethereum/metadata"), Network: &networks.Network{Name: networks.NetworkNameNone}, onReadyCallbacks: []func(context.Context) error{}, mu: sync.Mutex{},