diff --git a/services/chaintime/standard/service_test.go b/services/chaintime/standard/service_test.go index 3279681..4749d72 100644 --- a/services/chaintime/standard/service_test.go +++ b/services/chaintime/standard/service_test.go @@ -18,35 +18,21 @@ import ( "testing" "time" + "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/chaind/services/chaintime" "github.com/wealdtech/chaind/services/chaintime/standard" - "github.com/wealdtech/chaind/testing/mock" ) func TestService(t *testing.T) { + ctx := context.Background() genesisTime := time.Now() - slotDuration := 12 * time.Second - slotsPerEpoch := uint64(32) - epochsPerSyncCommitteePeriod := uint64(256) - forkSchedule := []*phase0.Fork{ - { - PreviousVersion: phase0.Version{0x01, 0x02, 0x03, 0x04}, - CurrentVersion: phase0.Version{0x01, 0x02, 0x03, 0x04}, - Epoch: 0, - }, - { - PreviousVersion: phase0.Version{0x01, 0x02, 0x03, 0x04}, - CurrentVersion: phase0.Version{0x05, 0x06, 0x07, 0x08}, - Epoch: 10, - }, - } - - mockGenesisProvider := mock.NewGenesisProvider(genesisTime) - mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerEpoch, epochsPerSyncCommitteePeriod) - mockForkScheduleProvider := mock.NewForkScheduleProvider(forkSchedule) + mockConsensusClient, err := mock.New(ctx, + mock.WithGenesisTime(genesisTime), + ) + require.NoError(t, err) tests := []struct { name string @@ -57,8 +43,8 @@ func TestService(t *testing.T) { name: "GenesisProviderMissing", params: []standard.Parameter{ standard.WithLogLevel(zerolog.Disabled), - standard.WithSpecProvider(mockSpecProvider), - standard.WithForkScheduleProvider(mockForkScheduleProvider), + standard.WithSpecProvider(mockConsensusClient), + standard.WithForkScheduleProvider(mockConsensusClient), }, err: "problem with parameters: no genesis provider specified", }, @@ -66,8 +52,8 @@ func TestService(t *testing.T) { name: "SpecProviderMissing", params: []standard.Parameter{ standard.WithLogLevel(zerolog.Disabled), - standard.WithGenesisProvider(mockGenesisProvider), - standard.WithForkScheduleProvider(mockForkScheduleProvider), + standard.WithGenesisProvider(mockConsensusClient), + standard.WithForkScheduleProvider(mockConsensusClient), }, err: "problem with parameters: no spec provider specified", }, @@ -75,8 +61,8 @@ func TestService(t *testing.T) { name: "ForkScheduleProviderMissing", params: []standard.Parameter{ standard.WithLogLevel(zerolog.Disabled), - standard.WithGenesisProvider(mockGenesisProvider), - standard.WithSpecProvider(mockSpecProvider), + standard.WithGenesisProvider(mockConsensusClient), + standard.WithSpecProvider(mockConsensusClient), }, err: "problem with parameters: no fork schedule provider specified", }, @@ -84,9 +70,9 @@ func TestService(t *testing.T) { name: "Good", params: []standard.Parameter{ standard.WithLogLevel(zerolog.Disabled), - standard.WithGenesisProvider(mockGenesisProvider), - standard.WithSpecProvider(mockSpecProvider), - standard.WithForkScheduleProvider(mockForkScheduleProvider), + standard.WithGenesisProvider(mockConsensusClient), + standard.WithSpecProvider(mockConsensusClient), + standard.WithForkScheduleProvider(mockConsensusClient), }, }, } @@ -104,37 +90,28 @@ 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 - slotsPerEpoch := uint64(32) - epochsPerSyncCommitteePeriod := uint64(256) - forkSchedule := []*phase0.Fork{ - { - PreviousVersion: phase0.Version{0x01, 0x02, 0x03, 0x04}, - CurrentVersion: phase0.Version{0x01, 0x02, 0x03, 0x04}, - Epoch: 0, - }, - { - PreviousVersion: phase0.Version{0x01, 0x02, 0x03, 0x04}, - CurrentVersion: phase0.Version{0x05, 0x06, 0x07, 0x08}, - Epoch: 10, - }, - } +func createService(genesisTime time.Time) (chaintime.Service, error) { + ctx := context.Background() - mockGenesisProvider := mock.NewGenesisProvider(genesisTime) - mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerEpoch, epochsPerSyncCommitteePeriod) - mockForkScheduleProvider := mock.NewForkScheduleProvider(forkSchedule) - s, err := standard.New(context.Background(), - standard.WithGenesisProvider(mockGenesisProvider), - standard.WithSpecProvider(mockSpecProvider), - standard.WithForkScheduleProvider(mockForkScheduleProvider), + mockConsensusClient, err := mock.New(ctx, + mock.WithGenesisTime(genesisTime), ) - return s, slotDuration, slotsPerEpoch, epochsPerSyncCommitteePeriod, forkSchedule, err + + s, err := standard.New(ctx, + standard.WithGenesisProvider(mockConsensusClient), + standard.WithSpecProvider(mockConsensusClient), + standard.WithForkScheduleProvider(mockConsensusClient), + ) + if err != nil { + return nil, err + } + + return s, nil } func TestGenesisTime(t *testing.T) { genesisTime := time.Now() - s, _, _, _, _, err := createService(genesisTime) + s, err := createService(genesisTime) require.NoError(t, err) require.Equal(t, genesisTime, s.GenesisTime()) @@ -142,25 +119,30 @@ func TestGenesisTime(t *testing.T) { func TestStartOfSlot(t *testing.T) { genesisTime := time.Now() - s, slotDuration, _, _, _, err := createService(genesisTime) + s, err := createService(genesisTime) require.NoError(t, err) + slotDuration := 12 * time.Second + require.Equal(t, genesisTime, s.StartOfSlot(0)) require.Equal(t, genesisTime.Add(1000*slotDuration), s.StartOfSlot(1000)) } func TestStartOfEpoch(t *testing.T) { genesisTime := time.Now() - s, slotDuration, slotsPerEpoch, _, _, err := createService(genesisTime) + s, err := createService(genesisTime) require.NoError(t, err) + slotDuration := 12 * time.Second + slotsPerEpoch := 32 + require.Equal(t, genesisTime, s.StartOfEpoch(0)) require.Equal(t, genesisTime.Add(time.Duration(1000*slotsPerEpoch)*slotDuration), s.StartOfEpoch(1000)) } func TestCurrentSlot(t *testing.T) { genesisTime := time.Now().Add(-60 * time.Second) - s, _, _, _, _, err := createService(genesisTime) + s, err := createService(genesisTime) require.NoError(t, err) require.Equal(t, phase0.Slot(5), s.CurrentSlot()) @@ -168,7 +150,7 @@ func TestCurrentSlot(t *testing.T) { func TestCurrentEpoch(t *testing.T) { genesisTime := time.Now().Add(-1000 * time.Second) - s, _, _, _, _, err := createService(genesisTime) + s, err := createService(genesisTime) require.NoError(t, err) require.Equal(t, phase0.Epoch(2), s.CurrentEpoch()) @@ -176,7 +158,7 @@ func TestCurrentEpoch(t *testing.T) { func TestTimestampToSlot(t *testing.T) { genesisTime := time.Now() - s, _, _, _, _, err := createService(genesisTime) + s, err := createService(genesisTime) require.NoError(t, err) tests := []struct { @@ -215,7 +197,7 @@ func TestTimestampToSlot(t *testing.T) { func TestTimestampToEpoch(t *testing.T) { genesisTime := time.Now() - s, _, _, _, _, err := createService(genesisTime) + s, err := createService(genesisTime) require.NoError(t, err) tests := []struct { diff --git a/testing/mock/eth2client.go b/testing/mock/eth2client.go deleted file mode 100644 index 9be7890..0000000 --- a/testing/mock/eth2client.go +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright © 2020 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 { - genesis *apiv1.Genesis -} - -// NewGenesisProvider returns a mock genesis provider with the provided value. -func NewGenesisProvider(genesisTime time.Time) eth2client.GenesisProvider { - return &GenesisProvider{ - genesis: &apiv1.Genesis{ - GenesisTime: genesisTime, - }, - } -} - -// Genesis is a mock. -func (m *GenesisProvider) Genesis(_ context.Context, - _ *api.GenesisOpts, -) ( - *api.Response[*apiv1.Genesis], - error, -) { - return &api.Response[*apiv1.Genesis]{ - Data: m.genesis, - 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 (*AttestationsSubmitter) SubmitAttestations(_ context.Context, _ []*phase0.Attestation) 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 (*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 (*AggregateAttestationsSubmitter) SubmitAggregateAttestations(_ context.Context, _ []*phase0.SignedAggregateAndProof) 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 (*BeaconCommitteeSubscriptionsSubmitter) SubmitBeaconCommitteeSubscriptions(_ context.Context, _ []*apiv1.BeaconCommitteeSubscription) error { - return nil -}