-
Notifications
You must be signed in to change notification settings - Fork 88
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
slotDuration, err := eth2Cl.SlotDuration(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
slotsPerEpoch, err := eth2Cl.SlotsPerEpoch(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?