From 0b54e398e12090cdc84011033862899b47e28138 Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Mon, 27 May 2024 17:26:51 +1000 Subject: [PATCH] refactor: Update dependencies and improve code readability (#324) --- go.mod | 4 +- go.sum | 8 ++-- pkg/clmimicry/gossipsub_attestation.go | 63 ++++++++++++++++--------- pkg/clmimicry/gossipsub_beacon_block.go | 18 ++----- 4 files changed, 51 insertions(+), 42 deletions(-) diff --git a/go.mod b/go.mod index 8e719e3b..c0d9dc83 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-20240501013510-f8170ec5da4d +replace github.com/probe-lab/hermes => github.com/ethpandaops/hermes v0.0.0-20240527060557-ae23464cdc41 require ( github.com/IBM/sarama v1.43.0 @@ -12,7 +12,7 @@ require ( github.com/cenkalti/backoff/v4 v4.2.1 github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 github.com/creasty/defaults v1.7.0 - github.com/ethereum/go-ethereum v1.13.14 + github.com/ethereum/go-ethereum v1.13.15 github.com/ethpandaops/beacon v0.35.0 github.com/ethpandaops/ethcore v0.0.0-20240422023000-2a5727b18756 github.com/ethpandaops/ethwallclock v0.3.0 diff --git a/go.sum b/go.sum index 010468fe..5f71e1e4 100644 --- a/go.sum +++ b/go.sum @@ -258,16 +258,16 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/ethereum/c-kzg-4844 v0.4.0 h1:3MS1s4JtA868KpJxroZoepdV0ZKBp3u/O5HcZ7R3nlY= github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= -github.com/ethereum/go-ethereum v1.13.14 h1:EwiY3FZP94derMCIam1iW4HFVrSgIcpsu0HwTQtm6CQ= -github.com/ethereum/go-ethereum v1.13.14/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU= +github.com/ethereum/go-ethereum v1.13.15 h1:U7sSGYGo4SPjP6iNIifNoyIAiNjrmQkz6EwQG+/EZWo= +github.com/ethereum/go-ethereum v1.13.15/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU= github.com/ethpandaops/beacon v0.35.0 h1:ZkHfxm41N0wkv503Xdb6rFxLuEnIonClUQWUPFHS5VU= github.com/ethpandaops/beacon v0.35.0/go.mod h1:B+SLxj1gnDd/Ia7cl/uuhzo1wyVf2p2puL6lmzPdPro= github.com/ethpandaops/ethcore v0.0.0-20240422023000-2a5727b18756 h1:8JWjrRfP14m0oxOk03m11n/xgdY5ceyUf/ZxYdOs5gE= github.com/ethpandaops/ethcore v0.0.0-20240422023000-2a5727b18756/go.mod h1:ZvKqL6CKxiraefdXPHeJurV2pDD/f2HF2uklDVdrry8= 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-20240501013510-f8170ec5da4d h1:mWBuU7mhpN11c2bbcImFyLSl+TJ208N4aGhMhadflIw= -github.com/ethpandaops/hermes v0.0.0-20240501013510-f8170ec5da4d/go.mod h1:WOtvdP1zl53vuoOX6PRZ9oAbbJUiDso50efmAjoUZzI= +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/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= diff --git a/pkg/clmimicry/gossipsub_attestation.go b/pkg/clmimicry/gossipsub_attestation.go index b23e43d9..05c1565c 100644 --- a/pkg/clmimicry/gossipsub_attestation.go +++ b/pkg/clmimicry/gossipsub_attestation.go @@ -3,7 +3,6 @@ package clmimicry import ( "context" "fmt" - "time" "github.com/attestantio/go-eth2-client/spec/phase0" v1 "github.com/ethpandaops/xatu/pkg/proto/eth/v1" @@ -11,6 +10,7 @@ import ( "github.com/ethpandaops/xatu/pkg/proto/xatu" "github.com/google/uuid" "github.com/probe-lab/hermes/host" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" ethtypes "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/timestamppb" @@ -21,9 +21,38 @@ func (m *Mimicry) handleGossipAttestation(ctx context.Context, clientMeta *xatu.ClientMeta, event *host.TraceEvent, payload map[string]any) error { // Extract attestation data - eAttestation, ok := payload["Attestation"].(*ethtypes.Attestation) - if !ok { - return fmt.Errorf("invalid attestation") + eAttestation := ðtypes.Attestation{ + Data: ðtypes.AttestationData{}, + } + + if slot, ok := payload["Slot"].(primitives.Slot); ok { + eAttestation.Data.Slot = slot + } else { + return fmt.Errorf("invalid slot") + } + + if committeeIndex, ok := payload["CommIdx"].(primitives.CommitteeIndex); ok { + eAttestation.Data.CommitteeIndex = committeeIndex + } else { + return fmt.Errorf("invalid committee index") + } + + if beaconBlockRoot, ok := payload["BeaconBlockRoot"].([]byte); ok { + eAttestation.Data.BeaconBlockRoot = beaconBlockRoot + } else { + return fmt.Errorf("invalid beacon block root") + } + + if source, ok := payload["Source"].(*ethtypes.Checkpoint); ok { + eAttestation.Data.Source = source + } else { + return fmt.Errorf("invalid source") + } + + if target, ok := payload["Target"].(*ethtypes.Checkpoint); ok { + eAttestation.Data.Target = target + } else { + return fmt.Errorf("invalid target") } attestation := &v1.Attestation{ @@ -48,7 +77,7 @@ func (m *Mimicry) handleGossipAttestation(ctx context.Context, return fmt.Errorf("failed to clone client metadata") } - additionalData, err := m.createAdditionalGossipSubAttestationData(ctx, payload, eAttestation) + additionalData, err := m.createAdditionalGossipSubAttestationData(ctx, payload, eAttestation, event) if err != nil { return fmt.Errorf("failed to create additional data: %w", err) } @@ -57,15 +86,10 @@ func (m *Mimicry) handleGossipAttestation(ctx context.Context, Libp2PTraceGossipsubBeaconAttestation: additionalData, } - timestamp, ok := payload["Timestamp"].(time.Time) - if !ok { - return fmt.Errorf("invalid timestamp") - } - decoratedEvent := &xatu.DecoratedEvent{ Event: &xatu.Event{ Name: xatu.Event_LIBP2P_TRACE_GOSSIPSUB_BEACON_ATTESTATION, - DateTime: timestamppb.New(timestamp.Add(m.clockDrift)), + DateTime: timestamppb.New(event.Timestamp.Add(m.clockDrift)), Id: uuid.New().String(), }, Meta: &xatu.Meta{ @@ -82,19 +106,15 @@ func (m *Mimicry) handleGossipAttestation(ctx context.Context, func (m *Mimicry) createAdditionalGossipSubAttestationData(ctx context.Context, payload map[string]any, attestation *ethtypes.Attestation, + event *host.TraceEvent, ) (*xatu.ClientMeta_AdditionalLibP2PTraceGossipSubBeaconAttestationData, error) { wallclockSlot, wallclockEpoch, err := m.ethereum.Metadata().Wallclock().Now() if err != nil { return nil, fmt.Errorf("failed to get wallclock time: %w", err) } - timestamp, ok := payload["Timestamp"].(time.Time) - if !ok { - return nil, fmt.Errorf("invalid timestamp") - } - // Add Clock Drift - timestampAdjusted := timestamp.Add(m.clockDrift) + timestampAdjusted := event.Timestamp.Add(m.clockDrift) attestionSlot := m.ethereum.Metadata().Wallclock().Slots().FromNumber(uint64(attestation.Data.Slot)) epoch := m.ethereum.Metadata().Wallclock().Epochs().FromSlot(uint64(attestation.Data.Slot)) @@ -162,18 +182,17 @@ func (m *Mimicry) createAdditionalGossipSubAttestationData(ctx context.Context, } // If the attestation is unaggreated, we can append the validator position within the committee - if attestation.AggregationBits.Count() == 1 { - position := uint64(attestation.AggregationBits.BitIndices()[0]) - + position, ok := payload["AggregatePos"].(int) + if ok { validatorIndex, err := m.ethereum.Duties().GetValidatorIndex( phase0.Epoch(epoch.Number()), phase0.Slot(attestation.Data.Slot), phase0.CommitteeIndex(attestation.Data.CommitteeIndex), - position, + uint64(position), ) if err == nil { extra.AttestingValidator = &xatu.AttestingValidatorV2{ - CommitteeIndex: &wrapperspb.UInt64Value{Value: position}, + CommitteeIndex: &wrapperspb.UInt64Value{Value: uint64(attestation.Data.CommitteeIndex)}, Index: &wrapperspb.UInt64Value{Value: uint64(validatorIndex)}, } } diff --git a/pkg/clmimicry/gossipsub_beacon_block.go b/pkg/clmimicry/gossipsub_beacon_block.go index 8be26bbb..719be104 100644 --- a/pkg/clmimicry/gossipsub_beacon_block.go +++ b/pkg/clmimicry/gossipsub_beacon_block.go @@ -3,7 +3,6 @@ package clmimicry import ( "context" "fmt" - "time" "github.com/ethpandaops/xatu/pkg/proto/libp2p" "github.com/ethpandaops/xatu/pkg/proto/libp2p/gossipsub" @@ -34,11 +33,6 @@ func (m *Mimicry) handleGossipBeaconBlock(ctx context.Context, return fmt.Errorf("invalid proposer index") } - timestamp, ok := payload["Timestamp"].(time.Time) - if !ok { - return fmt.Errorf("invalid timestamp") - } - data := &gossipsub.BeaconBlock{ Slot: wrapperspb.UInt64(uint64(slot)), Block: wrapperspb.String(fmt.Sprintf("0x%x", blockRoot)), @@ -50,7 +44,7 @@ func (m *Mimicry) handleGossipBeaconBlock(ctx context.Context, return fmt.Errorf("failed to clone client metadata") } - additionalData, err := m.createAdditionalGossipSubBeaconBlockData(ctx, payload, slot) + additionalData, err := m.createAdditionalGossipSubBeaconBlockData(ctx, payload, slot, event) if err != nil { return fmt.Errorf("failed to create additional data: %w", err) } @@ -62,7 +56,7 @@ func (m *Mimicry) handleGossipBeaconBlock(ctx context.Context, decoratedEvent := &xatu.DecoratedEvent{ Event: &xatu.Event{ Name: xatu.Event_LIBP2P_TRACE_GOSSIPSUB_BEACON_BLOCK, - DateTime: timestamppb.New(timestamp.Add(m.clockDrift)), + DateTime: timestamppb.New(event.Timestamp.Add(m.clockDrift)), Id: uuid.New().String(), }, Meta: &xatu.Meta{ @@ -79,6 +73,7 @@ func (m *Mimicry) handleGossipBeaconBlock(ctx context.Context, func (m *Mimicry) createAdditionalGossipSubBeaconBlockData(ctx context.Context, payload map[string]any, slotNumber primitives.Slot, + event *host.TraceEvent, ) (*xatu.ClientMeta_AdditionalLibP2PTraceGossipSubBeaconBlockData, error) { wallclockSlot, wallclockEpoch, err := m.ethereum.Metadata().Wallclock().Now() if err != nil { @@ -96,13 +91,8 @@ func (m *Mimicry) createAdditionalGossipSubBeaconBlockData(ctx context.Context, }, } - timestamp, ok := payload["Timestamp"].(time.Time) - if !ok { - return nil, fmt.Errorf("invalid timestamp") - } - // Add Clock Drift - timestampAdjusted := timestamp.Add(m.clockDrift) + timestampAdjusted := event.Timestamp.Add(m.clockDrift) slot := m.ethereum.Metadata().Wallclock().Slots().FromNumber(uint64(slotNumber)) epoch := m.ethereum.Metadata().Wallclock().Epochs().FromSlot(uint64(slotNumber))