Skip to content

Commit

Permalink
Use go-eth2-client mock.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Nov 12, 2023
1 parent e50ced4 commit e575fc0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 238 deletions.
102 changes: 42 additions & 60 deletions services/chaintime/standard/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,36 +43,36 @@ 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",
},
{
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",
},
{
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",
},
{
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),
},
},
}
Expand All @@ -104,79 +90,75 @@ 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())
}

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())
}

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())
}

func TestTimestampToSlot(t *testing.T) {
genesisTime := time.Now()
s, _, _, _, _, err := createService(genesisTime)
s, err := createService(genesisTime)
require.NoError(t, err)

tests := []struct {
Expand Down Expand Up @@ -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 {
Expand Down
178 changes: 0 additions & 178 deletions testing/mock/eth2client.go

This file was deleted.

0 comments on commit e575fc0

Please sign in to comment.