From 6e5727cc7f42e2f61797cec05354ff97e9cd98be Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Wed, 6 Sep 2023 17:32:41 +1000 Subject: [PATCH] feat(cannon): Default cannon type slots (#184) * feat(cannon): Default cannon type slots * feat(cannon): Default cannon type slots * feat(cannon): Default cannon type slots --- .../beacon/eth/v2/attester_slashing.go | 10 ++-- .../beacon/eth/v2/bls_to_execution_change.go | 10 ++-- pkg/cannon/deriver/beacon/eth/v2/deposit.go | 10 ++-- .../beacon/eth/v2/execution_transaction.go | 10 ++-- .../beacon/eth/v2/proposer_slashing.go | 10 ++-- .../deriver/beacon/eth/v2/voluntary_exit.go | 10 ++-- pkg/cannon/iterator/iterator.go | 30 ++++++++++++ pkg/cannon/iterator/slot_defaults.go | 46 +++++++++++++++++++ pkg/cannon/iterator/slot_iterator.go | 5 +- 9 files changed, 109 insertions(+), 32 deletions(-) create mode 100644 pkg/cannon/iterator/slot_defaults.go diff --git a/pkg/cannon/deriver/beacon/eth/v2/attester_slashing.go b/pkg/cannon/deriver/beacon/eth/v2/attester_slashing.go index 94a233ae..3d54bc0c 100644 --- a/pkg/cannon/deriver/beacon/eth/v2/attester_slashing.go +++ b/pkg/cannon/deriver/beacon/eth/v2/attester_slashing.go @@ -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 { @@ -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 diff --git a/pkg/cannon/deriver/beacon/eth/v2/bls_to_execution_change.go b/pkg/cannon/deriver/beacon/eth/v2/bls_to_execution_change.go index 43e21e8e..760e186b 100644 --- a/pkg/cannon/deriver/beacon/eth/v2/bls_to_execution_change.go +++ b/pkg/cannon/deriver/beacon/eth/v2/bls_to_execution_change.go @@ -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 { @@ -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 diff --git a/pkg/cannon/deriver/beacon/eth/v2/deposit.go b/pkg/cannon/deriver/beacon/eth/v2/deposit.go index 9dd47724..eb894a2a 100644 --- a/pkg/cannon/deriver/beacon/eth/v2/deposit.go +++ b/pkg/cannon/deriver/beacon/eth/v2/deposit.go @@ -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 { @@ -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 diff --git a/pkg/cannon/deriver/beacon/eth/v2/execution_transaction.go b/pkg/cannon/deriver/beacon/eth/v2/execution_transaction.go index 8f021d59..ecea8d63 100644 --- a/pkg/cannon/deriver/beacon/eth/v2/execution_transaction.go +++ b/pkg/cannon/deriver/beacon/eth/v2/execution_transaction.go @@ -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 { @@ -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 diff --git a/pkg/cannon/deriver/beacon/eth/v2/proposer_slashing.go b/pkg/cannon/deriver/beacon/eth/v2/proposer_slashing.go index 40257d7c..650ef5eb 100644 --- a/pkg/cannon/deriver/beacon/eth/v2/proposer_slashing.go +++ b/pkg/cannon/deriver/beacon/eth/v2/proposer_slashing.go @@ -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 { @@ -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 diff --git a/pkg/cannon/deriver/beacon/eth/v2/voluntary_exit.go b/pkg/cannon/deriver/beacon/eth/v2/voluntary_exit.go index a23158a3..a2bbe749 100644 --- a/pkg/cannon/deriver/beacon/eth/v2/voluntary_exit.go +++ b/pkg/cannon/deriver/beacon/eth/v2/voluntary_exit.go @@ -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 { @@ -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 diff --git a/pkg/cannon/iterator/iterator.go b/pkg/cannon/iterator/iterator.go index e5fac36c..bd878e42 100644 --- a/pkg/cannon/iterator/iterator.go +++ b/pkg/cannon/iterator/iterator.go @@ -4,6 +4,7 @@ import ( "context" "errors" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/ethpandaops/xatu/pkg/proto/xatu" ) @@ -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 +} diff --git a/pkg/cannon/iterator/slot_defaults.go b/pkg/cannon/iterator/slot_defaults.go new file mode 100644 index 00000000..5312832f --- /dev/null +++ b/pkg/cannon/iterator/slot_defaults.go @@ -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), + } +) diff --git a/pkg/cannon/iterator/slot_iterator.go b/pkg/cannon/iterator/slot_iterator.go index 84e5cc64..04470ea2 100644 --- a/pkg/cannon/iterator/slot_iterator.go +++ b/pkg/cannon/iterator/slot_iterator.go @@ -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") }