Skip to content

Commit

Permalink
Tidy ups.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Jan 20, 2025
1 parent 2371842 commit 80eb4d7
Show file tree
Hide file tree
Showing 33 changed files with 341 additions and 56 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.23.1:
- add ability to override individual provider functions in mock client

0.23.0:
- add attester_slashing, block_gossip, bls_to_execution_change and proposer_slashing events
- add AttestationRewards, BlockRewards, and SyncCommitteeRewards functions
Expand Down
2 changes: 1 addition & 1 deletion http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

// defaultUserAgent is sent with requests if no other user agent has been supplied.
const defaultUserAgent = "go-eth2-client/0.23.0"
const defaultUserAgent = "go-eth2-client/0.23.1"

// post sends an HTTP post request and returns the body.
func (s *Service) post(ctx context.Context,
Expand Down
8 changes: 6 additions & 2 deletions mock/aggregateattestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import (
)

// AggregateAttestation fetches the aggregate attestation given an attestation.
func (*Service) AggregateAttestation(_ context.Context,
_ *api.AggregateAttestationOpts,
func (s *Service) AggregateAttestation(ctx context.Context,
opts *api.AggregateAttestationOpts,
) (
*api.Response[*phase0.Attestation],
error,
) {
if s.AggregateAttestationFunc != nil {
return s.AggregateAttestationFunc(ctx, opts)
}

return &api.Response[*phase0.Attestation]{
Data: &phase0.Attestation{
Data: &phase0.AttestationData{
Expand Down
8 changes: 6 additions & 2 deletions mock/attestationdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import (
)

// AttestationData fetches the attestation data for the given slot and committee index.
func (*Service) AttestationData(_ context.Context,
_ *api.AttestationDataOpts,
func (s *Service) AttestationData(ctx context.Context,
opts *api.AttestationDataOpts,
) (
*api.Response[*phase0.AttestationData],
error,
) {
if s.AttestationDataFunc != nil {
return s.AttestationDataFunc(ctx, opts)
}

return &api.Response[*phase0.AttestationData]{
Data: &phase0.AttestationData{
Source: &phase0.Checkpoint{},
Expand Down
8 changes: 6 additions & 2 deletions mock/attestationrewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import (
)

// AttestationRewards provides rewards to the given validators for attesting.
func (*Service) AttestationRewards(_ context.Context,
_ *api.AttestationRewardsOpts,
func (s *Service) AttestationRewards(ctx context.Context,
opts *api.AttestationRewardsOpts,
) (
*api.Response[*apiv1.AttestationRewards],
error,
) {
if s.AttestationRewardsFunc != nil {
return s.AttestationRewardsFunc(ctx, opts)
}

return &api.Response[*apiv1.AttestationRewards]{
Data: &apiv1.AttestationRewards{
IdealRewards: []apiv1.IdealAttestationRewards{},
Expand Down
11 changes: 10 additions & 1 deletion mock/attesterduties.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import (

// AttesterDuties obtains attester duties.
// If validatorIndices is nil it will return all duties for the given epoch.
func (*Service) AttesterDuties(_ context.Context, opts *api.AttesterDutiesOpts) (*api.Response[[]*apiv1.AttesterDuty], error) {
func (s *Service) AttesterDuties(ctx context.Context,
opts *api.AttesterDutiesOpts,
) (
*api.Response[[]*apiv1.AttesterDuty],
error,
) {
if s.AttesterDutiesFunc != nil {
return s.AttesterDutiesFunc(ctx, opts)
}

data := make([]*apiv1.AttesterDuty, len(opts.Indices))
for i := range opts.Indices {
data[i] = &apiv1.AttesterDuty{
Expand Down
8 changes: 6 additions & 2 deletions mock/beaconblockheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ import (
)

// BeaconBlockHeader provides the block header of a given block ID.
func (*Service) BeaconBlockHeader(_ context.Context,
_ *api.BeaconBlockHeaderOpts,
func (s *Service) BeaconBlockHeader(ctx context.Context,
opts *api.BeaconBlockHeaderOpts,
) (
*api.Response[*apiv1.BeaconBlockHeader],
error,
) {
if s.BeaconBlockHeaderFunc != nil {
return s.BeaconBlockHeaderFunc(ctx, opts)
}

return &api.Response[*apiv1.BeaconBlockHeader]{
Data: &apiv1.BeaconBlockHeader{
Header: &phase0.SignedBeaconBlockHeader{
Expand Down
8 changes: 6 additions & 2 deletions mock/beaconblockroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import (
)

// BeaconBlockRoot fetches a block's root given a block ID.
func (*Service) BeaconBlockRoot(_ context.Context,
_ *api.BeaconBlockRootOpts,
func (s *Service) BeaconBlockRoot(ctx context.Context,
opts *api.BeaconBlockRootOpts,
) (
*api.Response[*phase0.Root],
error,
) {
if s.BeaconBlockRootFunc != nil {
return s.BeaconBlockRootFunc(ctx, opts)
}

root := phase0.Root([32]byte{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
Expand Down
11 changes: 10 additions & 1 deletion mock/beaconstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import (
)

// BeaconState fetches a beacon state given a state ID.
func (*Service) BeaconState(_ context.Context, _ *api.BeaconStateOpts) (*api.Response[*spec.VersionedBeaconState], error) {
func (s *Service) BeaconState(ctx context.Context,
opts *api.BeaconStateOpts,
) (
*api.Response[*spec.VersionedBeaconState],
error,
) {
if s.BeaconStateFunc != nil {
return s.BeaconStateFunc(ctx, opts)
}

data := &spec.VersionedBeaconState{
Version: spec.DataVersionPhase0,
Phase0: &phase0.BeaconState{
Expand Down
11 changes: 10 additions & 1 deletion mock/beaconstateroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ import (
)

// BeaconStateRoot fetches a beacon state's root given a state ID.
func (*Service) BeaconStateRoot(_ context.Context, _ *api.BeaconStateRootOpts) (*api.Response[*phase0.Root], error) {
func (s *Service) BeaconStateRoot(ctx context.Context,
opts *api.BeaconStateRootOpts,
) (
*api.Response[*phase0.Root],
error,
) {
if s.BeaconStateRootFunc != nil {
return s.BeaconStateRootFunc(ctx, opts)
}

data := phase0.Root{}

return &api.Response[*phase0.Root]{
Expand Down
8 changes: 6 additions & 2 deletions mock/blockrewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import (
)

// BlockRewards provides rewards for proposing a block.
func (*Service) BlockRewards(_ context.Context,
_ *api.BlockRewardsOpts,
func (s *Service) BlockRewards(ctx context.Context,
opts *api.BlockRewardsOpts,
) (
*api.Response[*apiv1.BlockRewards],
error,
) {
if s.BlockRewardsFunc != nil {
return s.BlockRewardsFunc(ctx, opts)
}

return &api.Response[*apiv1.BlockRewards]{
Data: &apiv1.BlockRewards{},
Metadata: make(map[string]any),
Expand Down
11 changes: 10 additions & 1 deletion mock/depositcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ import (
)

// DepositContract provides details of the execution layer deposit contract for the chain.
func (*Service) DepositContract(_ context.Context, _ *api.DepositContractOpts) (*api.Response[*apiv1.DepositContract], error) {
func (s *Service) DepositContract(ctx context.Context,
opts *api.DepositContractOpts,
) (
*api.Response[*apiv1.DepositContract],
error,
) {
if s.DepositContractFunc != nil {
return s.DepositContractFunc(ctx, opts)
}

return &api.Response[*apiv1.DepositContract]{
Data: &apiv1.DepositContract{},
Metadata: make(map[string]any),
Expand Down
6 changes: 5 additions & 1 deletion mock/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
)

// Events feeds requested events with the given topics to the supplied handler.
func (*Service) Events(_ context.Context, _ []string, _ client.EventHandlerFunc) error {
func (s *Service) Events(ctx context.Context, topics []string, handler client.EventHandlerFunc) error {
if s.EventsFunc != nil {
return s.EventsFunc(ctx, topics, handler)
}

return nil
}
6 changes: 5 additions & 1 deletion mock/finality.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import (
)

// Finality provides the finality given a state ID.
func (*Service) Finality(_ context.Context, _ *api.FinalityOpts) (*api.Response[*apiv1.Finality], error) {
func (s *Service) Finality(ctx context.Context, opts *api.FinalityOpts) (*api.Response[*apiv1.Finality], error) {
if s.FinalityFunc != nil {
return s.FinalityFunc(ctx, opts)
}

return &api.Response[*apiv1.Finality]{
Data: &apiv1.Finality{
Finalized: &phase0.Checkpoint{
Expand Down
11 changes: 10 additions & 1 deletion mock/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ import (
)

// Fork fetches fork information for the given state.
func (s *Service) Fork(ctx context.Context, _ *api.ForkOpts) (*api.Response[*phase0.Fork], error) {
func (s *Service) Fork(ctx context.Context,
opts *api.ForkOpts,
) (
*api.Response[*phase0.Fork],
error,
) {
if s.ForkFunc != nil {
return s.ForkFunc(ctx, opts)
}

fork, err := s.forkAtEpoch(ctx, 1)
if err != nil {
return nil, err
Expand Down
38 changes: 38 additions & 0 deletions mock/forkchoice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 mock

import (
"context"

"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
)

// ForkChoice fetches all current fork choice context.
func (s *Service) ForkChoice(ctx context.Context,
opts *api.ForkChoiceOpts,
) (
*api.Response[*apiv1.ForkChoice],
error,
) {
if s.ForkChoiceFunc != nil {
return s.ForkChoiceFunc(ctx, opts)
}

return &api.Response[*apiv1.ForkChoice]{
Data: &apiv1.ForkChoice{},
Metadata: make(map[string]any),
}, nil
}
25 changes: 17 additions & 8 deletions mock/forkschedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@ import (
"context"

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

// ForkSchedule provides details of past and future changes in the chain's fork version.
func (*Service) ForkSchedule(_ context.Context, _ *api.ForkScheduleOpts) (*api.Response[[]*spec.Fork], error) {
data := []*spec.Fork{
func (s *Service) ForkSchedule(ctx context.Context,
opts *api.ForkScheduleOpts,
) (
*api.Response[[]*phase0.Fork],
error,
) {
if s.ForkScheduleFunc != nil {
return s.ForkScheduleFunc(ctx, opts)
}

data := []*phase0.Fork{
{
PreviousVersion: spec.Version{0x01, 0x02, 0x03, 0x04},
CurrentVersion: spec.Version{0x01, 0x02, 0x03, 0x04},
PreviousVersion: phase0.Version{0x01, 0x02, 0x03, 0x04},
CurrentVersion: phase0.Version{0x01, 0x02, 0x03, 0x04},
Epoch: 0,
},
{
PreviousVersion: spec.Version{0x01, 0x02, 0x03, 0x04},
CurrentVersion: spec.Version{0x11, 0x12, 0x13, 0x14},
PreviousVersion: phase0.Version{0x01, 0x02, 0x03, 0x04},
CurrentVersion: phase0.Version{0x11, 0x12, 0x13, 0x14},
Epoch: 1024,
},
}

return &api.Response[[]*spec.Fork]{
return &api.Response[[]*phase0.Fork]{
Data: data,
Metadata: make(map[string]any),
}, nil
Expand Down
6 changes: 5 additions & 1 deletion mock/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import (
)

// Genesis provides the genesis information of the chain.
func (s *Service) Genesis(_ context.Context, _ *api.GenesisOpts) (*api.Response[*apiv1.Genesis], error) {
func (s *Service) Genesis(ctx context.Context, opts *api.GenesisOpts) (*api.Response[*apiv1.Genesis], error) {
if s.GenesisFunc != nil {
return s.GenesisFunc(ctx, opts)
}

data := &apiv1.Genesis{
GenesisTime: s.genesisTime,
GenesisValidatorsRoot: phase0.Root([32]byte{
Expand Down
11 changes: 10 additions & 1 deletion mock/nodepeers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ import (
)

// NodePeers provides the peers of the node.
func (*Service) NodePeers(_ context.Context, _ *api.NodePeersOpts) (*api.Response[[]*apiv1.Peer], error) {
func (s *Service) NodePeers(ctx context.Context,
opts *api.NodePeersOpts,
) (
*api.Response[[]*apiv1.Peer],
error,
) {
if s.NodePeersFunc != nil {
return s.NodePeersFunc(ctx, opts)
}

return &api.Response[[]*apiv1.Peer]{
Data: []*apiv1.Peer{{
PeerID: "MOCK16Uiu2HAm7ukVy4XugqVShYbLih4H2jBJjYevevznBZaHsmd1FM96",
Expand Down
11 changes: 10 additions & 1 deletion mock/nodesyncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ import (
)

// NodeSyncing provides the state of the node's synchronization with the chain.
func (s *Service) NodeSyncing(_ context.Context, _ *api.NodeSyncingOpts) (*api.Response[*apiv1.SyncState], error) {
func (s *Service) NodeSyncing(ctx context.Context,
opts *api.NodeSyncingOpts,
) (
*api.Response[*apiv1.SyncState],
error,
) {
if s.NodeSyncingFunc != nil {
return s.NodeSyncingFunc(ctx, opts)
}

return &api.Response[*apiv1.SyncState]{
Data: &apiv1.SyncState{
HeadSlot: s.HeadSlot,
Expand Down
11 changes: 10 additions & 1 deletion mock/nodeversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ import (
)

// NodeVersion returns a free-text string with the node version.
func (s *Service) NodeVersion(_ context.Context, _ *api.NodeVersionOpts) (*api.Response[string], error) {
func (s *Service) NodeVersion(ctx context.Context,
opts *api.NodeVersionOpts,
) (
*api.Response[string],
error,
) {
if s.NodeVersionFunc != nil {
return s.NodeVersionFunc(ctx, opts)
}

return &api.Response[string]{
Data: s.nodeVersion,
Metadata: make(map[string]any),
Expand Down
Loading

0 comments on commit 80eb4d7

Please sign in to comment.