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

core: implement duty gater #2530

Merged
merged 2 commits into from
Aug 10, 2023
Merged
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
67 changes: 67 additions & 0 deletions core/gater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1

package core

import (
"context"
"testing"
"time"

"github.com/obolnetwork/charon/app/eth2wrap"
)

const defaultAllowedFutureEpochs = 2

// DutyGaterFunc is a function that returns true if the duty is allowed to be processed.
// It checks whether or not duties received from peers over the wire are too far in the future.
// It doesn't check whether or not the duty is in the past, that is done by Deadliner.
type DutyGaterFunc func(Duty) bool

// WithDutyGaterForT returns a function that sets the nowFunc and allowedFutureEpochs in
// order to create a DutyGaterFunc for use in tests.
func WithDutyGaterForT(_ *testing.T, nowFunc func() time.Time, allowedFutureEpochs int) func(*dutyGaterOptions) {
return func(o *dutyGaterOptions) {
o.nowFunc = nowFunc
o.allowedFutureEpochs = allowedFutureEpochs
}
}

type dutyGaterOptions struct {
allowedFutureEpochs int
nowFunc func() time.Time
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is nowFunc introduced to make this testable?

}

// NewDutyGater returns a new instance of DutyGaterFunc.
func NewDutyGater(ctx context.Context, eth2Cl eth2wrap.Client, opts ...func(*dutyGaterOptions)) (DutyGaterFunc, error) {
o := dutyGaterOptions{
allowedFutureEpochs: defaultAllowedFutureEpochs,
nowFunc: time.Now,
}
for _, opt := range opts {
opt(&o)
}

genesisTime, err := eth2Cl.GenesisTime(ctx)
if err != nil {
return nil, err
}

Check warning on line 47 in core/gater.go

View check run for this annotation

Codecov / codecov/patch

core/gater.go#L46-L47

Added lines #L46 - L47 were not covered by tests

slotDuration, err := eth2Cl.SlotDuration(ctx)
if err != nil {
return nil, err
}

Check warning on line 52 in core/gater.go

View check run for this annotation

Codecov / codecov/patch

core/gater.go#L51-L52

Added lines #L51 - L52 were not covered by tests

slotsPerEpoch, err := eth2Cl.SlotsPerEpoch(ctx)
if err != nil {
return nil, err
}

Check warning on line 57 in core/gater.go

View check run for this annotation

Codecov / codecov/patch

core/gater.go#L56-L57

Added lines #L56 - L57 were not covered by tests

return func(duty Duty) bool {
currentSlot := o.nowFunc().Sub(genesisTime) / slotDuration
currentEpoch := uint64(currentSlot) / slotsPerEpoch

dutyEpoch := uint64(duty.Slot) / slotsPerEpoch

return dutyEpoch <= currentEpoch+uint64(o.allowedFutureEpochs)
}, nil
}
47 changes: 47 additions & 0 deletions core/gater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1

package core_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/obolnetwork/charon/core"
"github.com/obolnetwork/charon/testutil/beaconmock"
)

func TestDutyGater(t *testing.T) {
now := time.Now()
allowedFutureEpochs := 2

// Allow slots 0-3.
slotDuration := time.Second
bmock, err := beaconmock.New(
beaconmock.WithGenesisTime(now),
beaconmock.WithSlotDuration(slotDuration),
beaconmock.WithSlotsPerEpoch(2),
)
require.NoError(t, err)

gater, err := core.NewDutyGater(context.Background(), bmock, core.WithDutyGaterForT(t,
func() time.Time { return now },
allowedFutureEpochs,
))
require.NoError(t, err)

// Allow slots 0-5.
require.True(t, gater(core.Duty{Slot: 0})) // Current epoch
require.True(t, gater(core.Duty{Slot: 1}))
require.True(t, gater(core.Duty{Slot: 2})) // N+1 epoch
require.True(t, gater(core.Duty{Slot: 3}))
require.True(t, gater(core.Duty{Slot: 4})) // N+2 epoch
require.True(t, gater(core.Duty{Slot: 5}))

// Disallow slots 6 and after.
require.False(t, gater(core.Duty{Slot: 6})) // N+3 epoch
require.False(t, gater(core.Duty{Slot: 7}))
require.False(t, gater(core.Duty{Slot: 1000}))
}
Loading