Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update events handlers. #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
dev:
- support single_attestation event
- support change to attestation event; this event now emits a spec.VersionedAttestation
- support change to attester_slashing event; this event now emits an electra.AttesterSlashing
- update Events endpoint to provide specific handlers for each event

0.24.0:
- support electra
Expand Down
111 changes: 111 additions & 0 deletions api/eventsopts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright © 2025 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"context"

apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/electra"
"github.com/attestantio/go-eth2-client/spec/phase0"
)

// EventsOpts are the options for obtaining events.
type EventsOpts struct {
Common CommonOpts

// Topics are the topics of events to which we want to listen.
Topics []string

// Handler is a generic handler function to which to send all events.
// In general, it is better to use event-specific handlers as they avoid casting, and also provide a context.
Handler EventHandlerFunc

// AttestationHandler is a handler for the attestation event.
AttestationHandler AttestationEventHandlerFunc
// AttesterSlashingHandler is a handler for the attester_slashing event.
AttesterSlashingHandler AttesterSlashingEventHandlerFunc
// BlobSidecarHandler is a handler for the blob_sidecar event.
BlobSidecarHandler BlobSidecarEventHandlerFunc
// BlockHandler is a handler for the block event.
BlockHandler BlockEventHandlerFunc
// BlockGossipHandler is a handler for the block_gossip event.
BlockGossipHandler BlockGossipEventHandlerFunc
// BLSToExecutionChangeHandler is a handler for the bls_to_execution_change event.
BLSToExecutionChangeHandler BLSToExecutionChangeEventHandlerFunc
// ChainReorgHandler is a handler for the chain_reorg event.
ChainReorgHandler ChainReorgEventHandlerFunc
// ContributionAndProofHandler is a handler for the contribution_and_proof event.
ContributionAndProofHandler ContributionAndProofEventHandlerFunc
// FinalizedCheckpointHandler is a handler for the finalized_checkpoint event.
FinalizedCheckpointHandler FinalizedCheckpointEventHandlerFunc
// HeadHandler is a handler for the head event.
HeadHandler HeadEventHandlerFunc
// PayloadAttributesHandler is a handler for the payload_attributes event.
PayloadAttributesHandler PayloadAttributesEventHandlerFunc
// ProposerSlashingHandler is a handler for the proposer_slashing event.
ProposerSlashingHandler ProposerSlashingEventHandlerFunc
// SingleAttestationHandler is a handler for the single_attestation event.
SingleAttestationHandler SingleAttestationEventHandlerFunc
// VoluntaryExitHandler is a handler for the voluntary_exit event.
VoluntaryExitHandler VoluntaryExitEventHandlerFunc
}

// EventHandlerFunc is the handler for generic events.
type EventHandlerFunc func(*apiv1.Event)

// AttestationEventHandlerFunc is the handler for attestation events.
type AttestationEventHandlerFunc func(context.Context, *spec.VersionedAttestation)

// AttesterSlashingEventHandlerFunc is the handler for attestation_slashing events.
type AttesterSlashingEventHandlerFunc func(context.Context, *electra.AttesterSlashing)

// BlobSidecarEventHandlerFunc is the handler for blob_sidecar events.
type BlobSidecarEventHandlerFunc func(context.Context, *apiv1.BlobSidecarEvent)

// BlockEventHandlerFunc is the handler for block events.
type BlockEventHandlerFunc func(context.Context, *apiv1.BlockEvent)

// BlockGossipEventHandlerFunc is the handler for block_gossip events.
type BlockGossipEventHandlerFunc func(context.Context, *apiv1.BlockGossipEvent)

// BLSToExecutionChangeEventHandlerFunc is the handler for bls_to_execution_change events.
type BLSToExecutionChangeEventHandlerFunc func(context.Context, *capella.SignedBLSToExecutionChange)

// ChainReorgEventHandlerFunc is the handler for chain_reorg events.
type ChainReorgEventHandlerFunc func(context.Context, *apiv1.ChainReorgEvent)

// ContributionAndProofEventHandlerFunc is the handler for contribution_and_proof events.
type ContributionAndProofEventHandlerFunc func(context.Context, *altair.SignedContributionAndProof)

// FinalizedCheckpointEventHandlerFunc is the handler for finalized_checkpoint events.
type FinalizedCheckpointEventHandlerFunc func(context.Context, *apiv1.FinalizedCheckpointEvent)

// HeadEventHandlerFunc is the handler for head events.
type HeadEventHandlerFunc func(context.Context, *apiv1.HeadEvent)

// PayloadAttributesEventHandlerFunc is the handler for payload_attributes events.
type PayloadAttributesEventHandlerFunc func(context.Context, *apiv1.PayloadAttributesEvent)

// ProposerSlashingEventHandlerFunc is the handler for proposer_slashing events.
type ProposerSlashingEventHandlerFunc func(context.Context, *phase0.ProposerSlashing)

// SingleAttestationEventHandlerFunc is the handler for single_attestation events.
type SingleAttestationEventHandlerFunc func(context.Context, *electra.SingleAttestation)

// VoluntaryExitEventHandlerFunc is the handler for voluntary_exit events.
type VoluntaryExitEventHandlerFunc func(context.Context, *phase0.SignedVoluntaryExit)
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
golang.org/x/crypto v0.32.0
golang.org/x/crypto v0.33.0
golang.org/x/sync v0.2.0
)

Expand All @@ -29,7 +29,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/dot v1.6.4 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -47,8 +47,8 @@ require (
github.com/rogpeppe/go-internal v1.11.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/Knetic/govaluate.v3 v3.0.0 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
Expand Down
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/dot v1.6.4 h1:cG9ycT67d9Yw22G+mAb4XiuUz6E6H1S0zePp/5Cwe/c=
github.com/emicklei/dot v1.6.4/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/ferranbt/fastssz v0.1.4 h1:OCDB+dYDEQDvAgtAGnTSidK1Pe2tW3nFV40XyMkTeDY=
github.com/ferranbt/fastssz v0.1.4/go.mod h1:Ea3+oeoRGGLGm5shYAeDgu6PGUlcvQhE2fILyD9+tGg=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -108,8 +107,8 @@ go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZE
go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20191116160921-f9c825593386/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
Expand All @@ -124,15 +123,14 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da h1:noIWHXmPHxILtqtCOPIhSt0ABwskkZKjD3bXGnZGpNY=
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
Expand Down
Loading
Loading