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/validatorapi: relax SlotFromTimestamp checks #3066

Merged
merged 3 commits into from
May 6, 2024
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
28 changes: 2 additions & 26 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,12 @@
return errors.Wrap(err, "new versioned signed validator registration")
}

slot, err := slotFromTimestamp(ctx, eth2Cl, reg.V1.Message.Timestamp)
slot, err := validatorapi.SlotFromTimestamp(ctx, eth2Cl, reg.V1.Message.Timestamp)

Check warning on line 650 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L650

Added line #L650 was not covered by tests
if err != nil {
return errors.Wrap(err, "calculate slot from timestamp")
}

if err = recaster.Store(ctx, core.NewBuilderRegistrationDuty(slot), core.SignedDataSet{pubkey: signedData}); err != nil {
if err = recaster.Store(ctx, core.NewBuilderRegistrationDuty(uint64(slot)), core.SignedDataSet{pubkey: signedData}); err != nil {

Check warning on line 655 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L655

Added line #L655 was not covered by tests
return errors.Wrap(err, "recaster store registration")
}
}
Expand Down Expand Up @@ -1062,27 +1062,3 @@

return resp[:7]
}

// slotFromTimestamp returns slot from the provided timestamp.
func slotFromTimestamp(ctx context.Context, eth2Cl eth2wrap.Client, timestamp time.Time) (uint64, error) {
genesis, err := eth2Cl.GenesisTime(ctx)
if err != nil {
return 0, err
} else if timestamp.Before(genesis) {
return 0, errors.New("registration timestamp before genesis")
}

eth2Resp, err := eth2Cl.Spec(ctx, &eth2api.SpecOpts{})
if err != nil {
return 0, err
}

slotDuration, ok := eth2Resp.Data["SECONDS_PER_SLOT"].(time.Duration)
if !ok {
return 0, errors.New("fetch slot duration")
}

delta := timestamp.Sub(genesis)

return uint64(delta / slotDuration), nil
}
86 changes: 0 additions & 86 deletions app/app_internal_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion app/log/loki/lokipb/v1/loki.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/peerinfo/peerinfopb/v1/peerinfo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/protonil/testdata/v1/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cluster/manifestpb/v1/manifest.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/corepb/v1/consensus.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/corepb/v1/core.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/corepb/v1/parsigex.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/corepb/v1/priority.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 39 additions & 26 deletions core/validatorapi/validatorapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,42 @@
zeroAddress = "0x0000000000000000000000000000000000000000"
)

// SlotFromTimestamp returns the Ethereum slot associated to a timestamp, given the genesis configuration fetched
// from client.
func SlotFromTimestamp(ctx context.Context, client eth2wrap.Client, timestamp time.Time) (eth2p0.Slot, error) {
genesis, err := client.GenesisTime(ctx)
if err != nil {
return 0, err

Check warning on line 42 in core/validatorapi/validatorapi.go

View check run for this annotation

Codecov / codecov/patch

core/validatorapi/validatorapi.go#L42

Added line #L42 was not covered by tests
} else if timestamp.Before(genesis) {
// if timestamp is in the past (can happen in testing scenarios, there's no strict form of checking on it), fall back on current timestamp.
nextTimestamp := time.Now()

log.Info(
ctx,
"timestamp before genesis, defaulting to current timestamp",
z.I64("genesis_timestamp", genesis.Unix()),
z.I64("overridden_timestamp", timestamp.Unix()),
z.I64("new_timestamp", nextTimestamp.Unix()),
)

timestamp = nextTimestamp
}

eth2Resp, err := client.Spec(ctx, &eth2api.SpecOpts{})
if err != nil {
return 0, err
}

Check warning on line 61 in core/validatorapi/validatorapi.go

View check run for this annotation

Codecov / codecov/patch

core/validatorapi/validatorapi.go#L60-L61

Added lines #L60 - L61 were not covered by tests

slotDuration, ok := eth2Resp.Data["SECONDS_PER_SLOT"].(time.Duration)
if !ok {
return 0, errors.New("fetch slot duration")
}

Check warning on line 66 in core/validatorapi/validatorapi.go

View check run for this annotation

Codecov / codecov/patch

core/validatorapi/validatorapi.go#L65-L66

Added lines #L65 - L66 were not covered by tests

delta := timestamp.Sub(genesis)

return eth2p0.Slot(delta / slotDuration), nil
}

// NewComponentInsecure returns a new instance of the validator API core workflow component
// that does not perform signature verification.
func NewComponentInsecure(_ *testing.T, eth2Cl eth2wrap.Client, shareIdx int) (*Component, error) {
Expand Down Expand Up @@ -461,7 +497,7 @@
if err != nil {
return err
}
slot, err := c.slotFromTimestamp(ctx, timestamp)
slot, err := SlotFromTimestamp(ctx, c.eth2Cl, timestamp)
if err != nil {
return err
}
Expand Down Expand Up @@ -499,7 +535,7 @@
return nil // Nothing to do
}

slot, err := c.slotFromTimestamp(ctx, time.Now())
slot, err := SlotFromTimestamp(ctx, c.eth2Cl, time.Now())
if err != nil {
return err
}
Expand Down Expand Up @@ -981,29 +1017,6 @@
return resp, nil
}

func (c Component) slotFromTimestamp(ctx context.Context, timestamp time.Time) (eth2p0.Slot, error) {
genesis, err := c.eth2Cl.GenesisTime(ctx)
if err != nil {
return 0, err
} else if timestamp.Before(genesis) {
return 0, errors.New("registration timestamp before genesis")
}

eth2Resp, err := c.eth2Cl.Spec(ctx, &eth2api.SpecOpts{})
if err != nil {
return 0, err
}

slotDuration, ok := eth2Resp.Data["SECONDS_PER_SLOT"].(time.Duration)
if !ok {
return 0, errors.New("fetch slot duration")
}

delta := timestamp.Sub(genesis)

return eth2p0.Slot(delta / slotDuration), nil
}

func (c Component) getProposerPubkey(ctx context.Context, duty core.Duty) (core.PubKey, error) {
// Get proposer pubkey (this is a blocking query).
defSet, err := c.dutyDefFunc(ctx, duty)
Expand Down Expand Up @@ -1115,7 +1128,7 @@
}
timestamp = timestamp.Add(slotDuration) // Use slot 1 for timestamp to override pre-generated registrations.

slot, err := c.slotFromTimestamp(ctx, time.Now())
slot, err := SlotFromTimestamp(ctx, c.eth2Cl, time.Now())
if err != nil {
return nil, err
}
Expand Down
Loading
Loading