Skip to content

Commit

Permalink
fix(sentry): Derive ethereum network from genesis root (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm authored Nov 23, 2022
1 parent 96a64a3 commit 913cd89
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 108 deletions.
35 changes: 9 additions & 26 deletions pkg/sentry/ethereum/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

v1 "github.com/attestantio/go-eth2-client/api/v1"
backoff "github.com/cenkalti/backoff/v4"
xatuv1 "github.com/ethpandaops/xatu/pkg/proto/eth/v1"
xatuethv1 "github.com/ethpandaops/xatu/pkg/proto/eth/v1"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum/networks"
"github.com/ethpandaops/xatu/pkg/wallclock"
"github.com/go-co-op/gocron"
Expand Down Expand Up @@ -111,35 +111,18 @@ func (m *MetadataService) Wallclock() *wallclock.EthereumBeaconChain {
}

func (m *MetadataService) DeriveNetworkName(ctx context.Context) error {
for _, slot := range networks.AllNetworkIdentifierSlots() {
// Grab the block that is at the slot.
block, err := m.beacon.FetchBlock(ctx, xatuv1.SlotAsString(slot))
if err != nil {
// A block in that slot might not exist, or the connection to the node may be bad.
continue
}

root, err := block.Root()
if err != nil {
// Invalid block root probably means the slot is empty.
continue
}

network := networks.DeriveNetworkName(slot, xatuv1.RootAsString(root))
if network == networks.NetworkNameUnknown {
// Unknown network. Try the next slot.
continue
}

if m.NetworkName == networks.NetworkNameNone {
m.log.WithField("network", network).Info("Detected ethereum network")
}
if m.Genesis == nil {
return errors.New("genesis is not available")
}

m.NetworkName = network
network := networks.DeriveNetworkName(xatuethv1.RootAsString(m.Genesis.GenesisValidatorsRoot))

break
if network != m.NetworkName {
m.log.WithField("network", network).Info("Detected ethereum network")
}

m.NetworkName = network

return nil
}

Expand Down
52 changes: 7 additions & 45 deletions pkg/sentry/ethereum/networks/network.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package networks

import "github.com/attestantio/go-eth2-client/spec/phase0"

type NetworkName string

var (
Expand All @@ -12,51 +10,15 @@ var (
NetworkNameGoerli NetworkName = "goerli"
)

type NetworkIdentifierSlot struct {
NetworkName NetworkName
Slot phase0.Slot
BlockRoot string
}

var NetworkSlotBlockRoots = []NetworkIdentifierSlot{
{
NetworkName: NetworkNameMainnet,
Slot: phase0.Slot(50),
BlockRoot: "0x68937f266e8f339e3d605b00424446f8db835a4f2548636a906095babc5fb308",
},
{
NetworkName: NetworkNameSepolia,
Slot: phase0.Slot(50),
BlockRoot: "0x79bed901a63e22bb4dbed9330372bc399ea4d5faec87de916a80410135f3475c",
},
{
NetworkName: NetworkNameGoerli,
Slot: phase0.Slot(50),
BlockRoot: "0x93379731ee4c8e438a2c74c850e80fa7b2ccab4d9e25e6dc5567d3a33059b4d4",
},
}

func AllNetworkIdentifierSlots() []phase0.Slot {
// Deduplicate the slots.
slots := map[phase0.Slot]struct{}{}

for _, networkSlotBlockRoot := range NetworkSlotBlockRoots {
slots[networkSlotBlockRoot.Slot] = struct{}{}
}

slotsSlice := make([]phase0.Slot, 0, len(slots))
for slot := range slots {
slotsSlice = append(slotsSlice, slot)
}

return slotsSlice
var NetworkGenesisRoots = map[string]NetworkName{
"0x043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb": NetworkNameGoerli,
"0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95": NetworkNameMainnet,
"0xd8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078": NetworkNameSepolia,
}

func DeriveNetworkName(slot phase0.Slot, blockRoot string) NetworkName {
for _, networkSlotBlockRoot := range NetworkSlotBlockRoots {
if networkSlotBlockRoot.Slot == slot && networkSlotBlockRoot.BlockRoot == blockRoot {
return networkSlotBlockRoot.NetworkName
}
func DeriveNetworkName(genesisRoot string) NetworkName {
if NetworkGenesisRoots[genesisRoot] != "" {
return NetworkGenesisRoots[genesisRoot]
}

return NetworkNameUnknown
Expand Down
37 changes: 0 additions & 37 deletions pkg/sentry/ethereum/networks/network_test.go

This file was deleted.

0 comments on commit 913cd89

Please sign in to comment.