Skip to content

Commit

Permalink
refactor: Update dependencies and improve code readability (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm authored May 27, 2024
1 parent f8e159e commit 0b54e39
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 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-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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
63 changes: 41 additions & 22 deletions pkg/clmimicry/gossipsub_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package clmimicry
import (
"context"
"fmt"
"time"

"github.com/attestantio/go-eth2-client/spec/phase0"
v1 "github.com/ethpandaops/xatu/pkg/proto/eth/v1"
"github.com/ethpandaops/xatu/pkg/proto/libp2p"
"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"
Expand All @@ -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 := &ethtypes.Attestation{
Data: &ethtypes.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{
Expand All @@ -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)
}
Expand All @@ -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{
Expand All @@ -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))
Expand Down Expand Up @@ -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)},
}
}
Expand Down
18 changes: 4 additions & 14 deletions pkg/clmimicry/gossipsub_beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)),
Expand All @@ -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)
}
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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))
Expand Down

0 comments on commit 0b54e39

Please sign in to comment.