Skip to content

Commit

Permalink
feat(discovery): Add beacon
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Sep 19, 2024
1 parent ace2784 commit 0172962
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
22 changes: 11 additions & 11 deletions pkg/clmimicry/mimicry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/discovery/beaconp2p/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion pkg/discovery/beaconp2p/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion pkg/discovery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/ethereum/services/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down

0 comments on commit 0172962

Please sign in to comment.