Skip to content

Commit

Permalink
feat(cannon): Default cannon type slots (#184)
Browse files Browse the repository at this point in the history
* feat(cannon): Default cannon type slots

* feat(cannon): Default cannon type slots

* feat(cannon): Default cannon type slots
  • Loading branch information
samcm committed Sep 6, 2023
1 parent 7ab9fb6 commit 6e5727c
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 32 deletions.
10 changes: 5 additions & 5 deletions pkg/cannon/deriver/beacon/eth/v2/attester_slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func (a *AttesterSlashingDeriver) run(ctx context.Context) {
return err
}

// Update our location
if err := a.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

// Send the events
for _, event := range events {
for _, fn := range a.onEventCallbacks {
Expand All @@ -128,11 +133,6 @@ func (a *AttesterSlashingDeriver) run(ctx context.Context) {
}
}

// Update our location
if err := a.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

bo.Reset()

return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/cannon/deriver/beacon/eth/v2/bls_to_execution_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func (b *BLSToExecutionChangeDeriver) run(ctx context.Context) {
return err
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

// Send the events
for _, event := range events {
for _, fn := range b.onEventCallbacks {
Expand All @@ -131,11 +136,6 @@ func (b *BLSToExecutionChangeDeriver) run(ctx context.Context) {
}
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

bo.Reset()

return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/cannon/deriver/beacon/eth/v2/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (b *DepositDeriver) run(ctx context.Context) {
return err
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

// Send the events
for _, event := range events {
for _, fn := range b.onEventCallbacks {
Expand All @@ -129,11 +134,6 @@ func (b *DepositDeriver) run(ctx context.Context) {
}
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

bo.Reset()

return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/cannon/deriver/beacon/eth/v2/execution_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ func (b *ExecutionTransactionDeriver) run(ctx context.Context) {
return err
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

// Send the events
for _, event := range events {
for _, fn := range b.onEventCallbacks {
Expand All @@ -132,11 +137,6 @@ func (b *ExecutionTransactionDeriver) run(ctx context.Context) {
}
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

bo.Reset()

return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/cannon/deriver/beacon/eth/v2/proposer_slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func (b *ProposerSlashingDeriver) run(ctx context.Context) {
return err
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

// Send the events
for _, event := range events {
for _, fn := range b.onEventCallbacks {
Expand All @@ -128,11 +133,6 @@ func (b *ProposerSlashingDeriver) run(ctx context.Context) {
}
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

bo.Reset()

return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/cannon/deriver/beacon/eth/v2/voluntary_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (b *VoluntaryExitDeriver) run(ctx context.Context) {
return err
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

// Send the events
for _, event := range events {
for _, fn := range b.onEventCallbacks {
Expand All @@ -129,11 +134,6 @@ func (b *VoluntaryExitDeriver) run(ctx context.Context) {
}
}

// Update our location
if err := b.iterator.UpdateLocation(ctx, location); err != nil {
return err
}

bo.Reset()

return nil
Expand Down
30 changes: 30 additions & 0 deletions pkg/cannon/iterator/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethpandaops/xatu/pkg/proto/xatu"
)

Expand All @@ -14,4 +15,33 @@ type Iterator interface {

var (
ErrLocationUpToDate = errors.New("location up to date")

SlotZero = phase0.Slot(0)
)

func GetDefaultSlotLocationForNetworkAndType(networkID string, cannonType xatu.CannonType) phase0.Slot {
switch networkID {
case "5":
return getSlotFromNetworkAndType(cannonType, GoerliDefaultSlotStartingPosition)
case "1":
return getSlotFromNetworkAndType(cannonType, MainnetDefaultSlotStartingPosition)
case "11155111":
return getSlotFromNetworkAndType(cannonType, SepoliaDefaultSlotStartingPosition)
case "17000":
return getSlotFromNetworkAndType(cannonType, HoleskyDefaultSlotStartingPosition)
default:
return SlotZero
}
}

func getSlotFromNetworkAndType(typ xatu.CannonType, networkDefaults DefaultSlotStartingPositions) phase0.Slot {
if networkDefaults == nil {
return SlotZero
}

if slot, exists := networkDefaults[typ]; exists {
return slot
}

return SlotZero
}
46 changes: 46 additions & 0 deletions pkg/cannon/iterator/slot_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package iterator

import (
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethpandaops/xatu/pkg/proto/xatu"
)

type DefaultSlotStartingPositions map[xatu.CannonType]phase0.Slot

var (
GoerliDefaultSlotStartingPosition = DefaultSlotStartingPositions{
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_ATTESTER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_PROPOSER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_VOLUNTARY_EXIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_DEPOSIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_BLS_TO_EXECUTION_CHANGE: phase0.Slot(162304 * 32), // Capella fork
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_EXECUTION_TRANSACTION: phase0.Slot(112260 * 32), // Bellatrix fork
}

MainnetDefaultSlotStartingPosition = DefaultSlotStartingPositions{
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_ATTESTER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_PROPOSER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_VOLUNTARY_EXIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_DEPOSIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_BLS_TO_EXECUTION_CHANGE: phase0.Slot(194048 * 32), // Capella fork
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_EXECUTION_TRANSACTION: phase0.Slot(144896 * 32), // Bellatrix fork
}

SepoliaDefaultSlotStartingPosition = DefaultSlotStartingPositions{
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_ATTESTER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_PROPOSER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_VOLUNTARY_EXIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_DEPOSIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_BLS_TO_EXECUTION_CHANGE: phase0.Slot(56832 * 32), // Capella fork
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_EXECUTION_TRANSACTION: phase0.Slot(100 * 32), // Bellatrix fork
}

HoleskyDefaultSlotStartingPosition = DefaultSlotStartingPositions{
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_ATTESTER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_PROPOSER_SLASHING: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_VOLUNTARY_EXIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_DEPOSIT: phase0.Slot(0),
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_BLS_TO_EXECUTION_CHANGE: phase0.Slot(256 * 32), // Capella fork
xatu.CannonType_BEACON_API_ETH_V2_BEACON_BLOCK_EXECUTION_TRANSACTION: phase0.Slot(0),
}
)
5 changes: 3 additions & 2 deletions pkg/cannon/iterator/slot_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func (s *SlotIterator) Next(ctx context.Context) (*xatu.CannonLocation, error) {
return nil, errors.Wrap(err, "failed to get cannon location")
}

// If location is empty we haven't started yet, start at slot 0
// If location is empty we haven't started yet, start at the network default for the type. If the network default
// is empty, we'll start at slot 0.
if location == nil {
loc, errr := s.createLocationFromSlotNumber(0)
loc, errr := s.createLocationFromSlotNumber(GetDefaultSlotLocationForNetworkAndType(s.networkID, s.cannonType))
if errr != nil {
return nil, errors.Wrap(err, "failed to create location from slot number 0")
}
Expand Down

0 comments on commit 6e5727c

Please sign in to comment.