Use standard mock from go-eth2-client.

This commit is contained in:
Jim McDonald
2025-02-08 11:46:49 +00:00
parent 0488b13ba1
commit 4d3bd966e0
4 changed files with 115 additions and 197 deletions

View File

@@ -18,22 +18,41 @@ import (
"testing"
"time"
"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/mock"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
"github.com/wealdtech/ethdo/services/chaintime"
"github.com/wealdtech/ethdo/services/chaintime/standard"
"github.com/wealdtech/ethdo/testing/mock"
)
func TestService(t *testing.T) {
genesisTime := time.Now()
slotDuration := 12 * time.Second
slotsPerEpoch := uint64(32)
epochsPerSyncCommitteePeriod := uint64(256)
ctx := context.Background()
mockGenesisProvider := mock.NewGenesisProvider(genesisTime)
mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerEpoch, epochsPerSyncCommitteePeriod)
mockClient, err := mock.New(ctx)
require.NoError(t, err)
// genesis is 1 day ago.
genesisTime := time.Now().AddDate(0, 0, -1)
mockClient.GenesisFunc = func(context.Context, *api.GenesisOpts) (*api.Response[*apiv1.Genesis], error) {
return &api.Response[*apiv1.Genesis]{
Data: &apiv1.Genesis{
GenesisTime: genesisTime,
},
Metadata: make(map[string]any),
}, nil
}
mockClient.SpecFunc = func(context.Context, *api.SpecOpts) (*api.Response[map[string]any], error) {
return &api.Response[map[string]any]{
Data: map[string]any{
"SECONDS_PER_SLOT": time.Second * 12,
"SLOTS_PER_EPOCH": uint64(32),
"EPOCHS_PER_SYNC_COMMITTEE_PERIOD": uint64(256),
},
Metadata: make(map[string]any),
}, nil
}
tests := []struct {
name string
@@ -44,7 +63,7 @@ func TestService(t *testing.T) {
name: "GenesisProviderMissing",
params: []standard.Parameter{
standard.WithLogLevel(zerolog.Disabled),
standard.WithSpecProvider(mockSpecProvider),
standard.WithSpecProvider(mockClient),
},
err: "problem with parameters: no genesis provider specified",
},
@@ -52,7 +71,7 @@ func TestService(t *testing.T) {
name: "SpecProviderMissing",
params: []standard.Parameter{
standard.WithLogLevel(zerolog.Disabled),
standard.WithGenesisProvider(mockGenesisProvider),
standard.WithGenesisProvider(mockClient),
},
err: "problem with parameters: no spec provider specified",
},
@@ -60,8 +79,8 @@ func TestService(t *testing.T) {
name: "Good",
params: []standard.Parameter{
standard.WithLogLevel(zerolog.Disabled),
standard.WithGenesisProvider(mockGenesisProvider),
standard.WithSpecProvider(mockSpecProvider),
standard.WithGenesisProvider(mockClient),
standard.WithSpecProvider(mockClient),
},
},
}
@@ -80,7 +99,14 @@ func TestService(t *testing.T) {
// createService is a helper that creates a mock chaintime service.
func createService(genesisTime time.Time) (chaintime.Service, time.Duration, uint64, uint64, []*phase0.Fork, error) {
slotDuration := 12 * time.Second
ctx := context.Background()
mockClient, err := mock.New(ctx)
if err != nil {
return nil, 0, 0, 0, nil, err
}
secondsPerSlot := time.Second * 12
slotsPerEpoch := uint64(32)
epochsPerSyncCommitteePeriod := uint64(256)
forkSchedule := []*phase0.Fork{
@@ -96,13 +122,36 @@ func createService(genesisTime time.Time) (chaintime.Service, time.Duration, uin
},
}
mockGenesisProvider := mock.NewGenesisProvider(genesisTime)
mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerEpoch, epochsPerSyncCommitteePeriod)
s, err := standard.New(context.Background(),
standard.WithGenesisProvider(mockGenesisProvider),
standard.WithSpecProvider(mockSpecProvider),
mockClient.GenesisFunc = func(context.Context, *api.GenesisOpts) (*api.Response[*apiv1.Genesis], error) {
return &api.Response[*apiv1.Genesis]{
Data: &apiv1.Genesis{
GenesisTime: genesisTime,
},
Metadata: make(map[string]any),
}, nil
}
mockClient.SpecFunc = func(context.Context, *api.SpecOpts) (*api.Response[map[string]any], error) {
return &api.Response[map[string]any]{
Data: map[string]any{
"SECONDS_PER_SLOT": secondsPerSlot,
"SLOTS_PER_EPOCH": slotsPerEpoch,
"EPOCHS_PER_SYNC_COMMITTEE_PERIOD": epochsPerSyncCommitteePeriod,
},
Metadata: make(map[string]any),
}, nil
}
mockClient.ForkScheduleFunc = func(context.Context, *api.ForkScheduleOpts) (*api.Response[[]*phase0.Fork], error) {
return &api.Response[[]*phase0.Fork]{
Data: forkSchedule,
Metadata: make(map[string]any),
}, nil
}
s, err := standard.New(ctx,
standard.WithGenesisProvider(mockClient),
standard.WithSpecProvider(mockClient),
)
return s, slotDuration, slotsPerEpoch, epochsPerSyncCommitteePeriod, forkSchedule, err
return s, secondsPerSlot, slotsPerEpoch, epochsPerSyncCommitteePeriod, forkSchedule, err
}
func TestGenesisTime(t *testing.T) {

View File

@@ -1,163 +0,0 @@
// Copyright © 2021 Weald Technology Trading.
// 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"
"time"
eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/phase0"
)
// GenesisProvider is a mock for eth2client.GenesisProvider.
type GenesisProvider struct {
genesisTime time.Time
}
// NewGenesisProvider returns a mock genesis provider with the provided value.
func NewGenesisProvider(genesisTime time.Time) eth2client.GenesisProvider {
return &GenesisProvider{
genesisTime: genesisTime,
}
}
// Genesisis a mock.
func (m *GenesisProvider) Genesis(_ context.Context, _ *api.GenesisOpts) (*api.Response[*apiv1.Genesis], error) {
return &api.Response[*apiv1.Genesis]{
Data: &apiv1.Genesis{
GenesisTime: m.genesisTime,
},
Metadata: make(map[string]any),
}, nil
}
// SpecProvider is a mock for eth2client.SpecProvider.
type SpecProvider struct {
spec map[string]any
}
// NewSpecProvider returns a mock spec provider with the provided values.
func NewSpecProvider(slotDuration time.Duration,
slotsPerEpoch uint64,
epochsPerSyncCommitteePeriod uint64,
) eth2client.SpecProvider {
return &SpecProvider{
spec: map[string]any{
"SECONDS_PER_SLOT": slotDuration,
"SLOTS_PER_EPOCH": slotsPerEpoch,
"EPOCHS_PER_SYNC_COMMITTEE_PERIOD": epochsPerSyncCommitteePeriod,
},
}
}
// Spec is a mock.
func (m *SpecProvider) Spec(_ context.Context, _ *api.SpecOpts) (*api.Response[map[string]any], error) {
return &api.Response[map[string]any]{
Data: m.spec,
Metadata: make(map[string]any),
}, nil
}
// ForkScheduleProvider is a mock for eth2client.ForkScheduleProvider.
type ForkScheduleProvider struct {
schedule []*phase0.Fork
}
// NewForkScheduleProvider returns a mock spec provider with the provided values.
func NewForkScheduleProvider(schedule []*phase0.Fork) eth2client.ForkScheduleProvider {
return &ForkScheduleProvider{
schedule: schedule,
}
}
// ForkSchedule is a mock.
func (m *ForkScheduleProvider) ForkSchedule(_ context.Context, _ *api.ForkScheduleOpts) (*api.Response[[]*phase0.Fork], error) {
return &api.Response[[]*phase0.Fork]{
Data: m.schedule,
Metadata: make(map[string]any),
}, nil
}
// SlotsPerEpochProvider is a mock for eth2client.SlotsPerEpochProvider.
type SlotsPerEpochProvider struct {
slotsPerEpoch uint64
}
// NewSlotsPerEpochProvider returns a mock slots per epoch provider with the provided value.
func NewSlotsPerEpochProvider(slotsPerEpoch uint64) eth2client.SlotsPerEpochProvider {
return &SlotsPerEpochProvider{
slotsPerEpoch: slotsPerEpoch,
}
}
// SlotsPerEpoch is a mock.
func (m *SlotsPerEpochProvider) SlotsPerEpoch(_ context.Context) (uint64, error) {
return m.slotsPerEpoch, nil
}
// AttestationsSubmitter is a mock for eth2client.AttestationsSubmitter.
type AttestationsSubmitter struct{}
// NewAttestationSubmitter returns a mock attestations submitter with the provided value.
func NewAttestationSubmitter() eth2client.AttestationsSubmitter {
return &AttestationsSubmitter{}
}
// SubmitAttestations is a mock.
func (m *AttestationsSubmitter) SubmitAttestations(_ context.Context, _ *api.SubmitAttestationsOpts) error {
return nil
}
// BeaconBlockSubmitter is a mock for eth2client.BeaconBlockSubmitter.
type BeaconBlockSubmitter struct{}
// NewBeaconBlockSubmitter returns a mock beacon block submitter with the provided value.
func NewBeaconBlockSubmitter() eth2client.BeaconBlockSubmitter {
return &BeaconBlockSubmitter{}
}
// SubmitBeaconBlock is a mock.
func (m *BeaconBlockSubmitter) SubmitBeaconBlock(_ context.Context, _ *spec.VersionedSignedBeaconBlock) error {
return nil
}
// AggregateAttestationsSubmitter is a mock for eth2client.AggregateAttestationsSubmitter.
type AggregateAttestationsSubmitter struct{}
// NewAggregateAttestationsSubmitter returns a mock aggregate attestation submitter with the provided value.
func NewAggregateAttestationsSubmitter() eth2client.AggregateAttestationsSubmitter {
return &AggregateAttestationsSubmitter{}
}
// SubmitAggregateAttestations is a mock.
func (m *AggregateAttestationsSubmitter) SubmitAggregateAttestations(_ context.Context, _ *api.SubmitAggregateAttestationsOpts) error {
return nil
}
// BeaconCommitteeSubscriptionsSubmitter is a mock for eth2client.BeaconCommitteeSubscriptionsSubmitter.
type BeaconCommitteeSubscriptionsSubmitter struct{}
// NewBeaconCommitteeSubscriptionsSubmitter returns a mock beacon committee subscription submitter with the provided value.
func NewBeaconCommitteeSubscriptionsSubmitter() eth2client.BeaconCommitteeSubscriptionsSubmitter {
return &BeaconCommitteeSubscriptionsSubmitter{}
}
// SubmitBeaconCommitteeSubscriptions is a mock.
func (m *BeaconCommitteeSubscriptionsSubmitter) SubmitBeaconCommitteeSubscriptions(_ context.Context, _ []*apiv1.BeaconCommitteeSubscription) error {
return nil
}

View File

@@ -18,28 +18,44 @@ import (
"testing"
"time"
"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/mock"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
standardchaintime "github.com/wealdtech/ethdo/services/chaintime/standard"
"github.com/wealdtech/ethdo/testing/mock"
"github.com/wealdtech/ethdo/util"
)
func TestParseEpoch(t *testing.T) {
ctx := context.Background()
mockClient, err := mock.New(ctx)
require.NoError(t, err)
// genesis is 1 day ago.
genesisTime := time.Now().AddDate(0, 0, -1)
slotDuration := 12 * time.Second
slotsPerEpoch := uint64(32)
epochsPerSyncCommitteePeriod := uint64(256)
mockGenesisProvider := mock.NewGenesisProvider(genesisTime)
mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerEpoch, epochsPerSyncCommitteePeriod)
mockClient.GenesisFunc = func(context.Context, *api.GenesisOpts) (*api.Response[*apiv1.Genesis], error) {
return &api.Response[*apiv1.Genesis]{
Data: &apiv1.Genesis{
GenesisTime: genesisTime,
},
Metadata: make(map[string]any),
}, nil
}
mockClient.SpecFunc = func(context.Context, *api.SpecOpts) (*api.Response[map[string]any], error) {
return &api.Response[map[string]any]{
Data: map[string]any{
"SECONDS_PER_SLOT": time.Second * 12,
"SLOTS_PER_EPOCH": uint64(32),
},
Metadata: make(map[string]any),
}, nil
}
chainTime, err := standardchaintime.New(context.Background(),
standardchaintime.WithLogLevel(zerolog.Disabled),
standardchaintime.WithGenesisProvider(mockGenesisProvider),
standardchaintime.WithSpecProvider(mockSpecProvider),
standardchaintime.WithGenesisProvider(mockClient),
standardchaintime.WithSpecProvider(mockClient),
)
require.NoError(t, err)

View File

@@ -18,28 +18,44 @@ import (
"testing"
"time"
"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/mock"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
standardchaintime "github.com/wealdtech/ethdo/services/chaintime/standard"
"github.com/wealdtech/ethdo/testing/mock"
"github.com/wealdtech/ethdo/util"
)
func TestParseSlot(t *testing.T) {
ctx := context.Background()
mockClient, err := mock.New(ctx)
require.NoError(t, err)
// genesis is 1 day ago.
genesisTime := time.Now().AddDate(0, 0, -1)
slotDuration := 12 * time.Second
slotsPerSlot := uint64(32)
epochsPerSyncCommitteePeriod := uint64(256)
mockGenesisProvider := mock.NewGenesisProvider(genesisTime)
mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerSlot, epochsPerSyncCommitteePeriod)
mockClient.GenesisFunc = func(context.Context, *api.GenesisOpts) (*api.Response[*apiv1.Genesis], error) {
return &api.Response[*apiv1.Genesis]{
Data: &apiv1.Genesis{
GenesisTime: genesisTime,
},
Metadata: make(map[string]any),
}, nil
}
mockClient.SpecFunc = func(context.Context, *api.SpecOpts) (*api.Response[map[string]any], error) {
return &api.Response[map[string]any]{
Data: map[string]any{
"SECONDS_PER_SLOT": time.Second * 12,
"SLOTS_PER_EPOCH": uint64(32),
},
Metadata: make(map[string]any),
}, nil
}
chainTime, err := standardchaintime.New(context.Background(),
standardchaintime.WithLogLevel(zerolog.Disabled),
standardchaintime.WithGenesisProvider(mockGenesisProvider),
standardchaintime.WithSpecProvider(mockSpecProvider),
standardchaintime.WithGenesisProvider(mockClient),
standardchaintime.WithSpecProvider(mockClient),
)
require.NoError(t, err)