-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slot option for proposer duties.
- Loading branch information
Showing
8 changed files
with
234 additions
and
24 deletions.
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
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
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
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
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
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
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,49 @@ | ||
// Copyright © 2023 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 util | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
"github.com/pkg/errors" | ||
"github.com/wealdtech/ethdo/services/chaintime" | ||
) | ||
|
||
// ParseSlot parses input to calculate the desired slot. | ||
func ParseSlot(_ context.Context, chainTime chaintime.Service, slotStr string) (phase0.Slot, error) { | ||
currentSlot := chainTime.CurrentSlot() | ||
switch slotStr { | ||
case "", "current", "head", "-0": | ||
return currentSlot, nil | ||
case "last": | ||
if currentSlot > 0 { | ||
currentSlot-- | ||
} | ||
return currentSlot, nil | ||
default: | ||
val, err := strconv.ParseInt(slotStr, 10, 64) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "failed to parse slot") | ||
} | ||
if val >= 0 { | ||
return phase0.Slot(val), nil | ||
} | ||
if phase0.Slot(-val) > currentSlot { | ||
return 0, nil | ||
} | ||
return currentSlot + phase0.Slot(val), 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,105 @@ | ||
// Copyright © 2023 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 util_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
standardchaintime "github.com/wealdtech/ethdo/services/chaintime/standard" | ||
"github.com/wealdtech/ethdo/testing/mock" | ||
"github.com/wealdtech/ethdo/util" | ||
) | ||
|
||
func TestParseSlot(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// genesis is 1 day ago. | ||
genesisTime := time.Now().AddDate(0, 0, -1) | ||
slotDuration := 12 * time.Second | ||
slotsPerSlot := uint64(32) | ||
epochsPerSyncCommitteePeriod := uint64(256) | ||
mockGenesisTimeProvider := mock.NewGenesisTimeProvider(genesisTime) | ||
mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerSlot, epochsPerSyncCommitteePeriod) | ||
chainTime, err := standardchaintime.New(context.Background(), | ||
standardchaintime.WithLogLevel(zerolog.Disabled), | ||
standardchaintime.WithGenesisTimeProvider(mockGenesisTimeProvider), | ||
standardchaintime.WithSpecProvider(mockSpecProvider), | ||
) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
input string | ||
err string | ||
expected phase0.Slot | ||
}{ | ||
{ | ||
name: "Genesis", | ||
input: "0", | ||
expected: 0, | ||
}, | ||
{ | ||
name: "Invalid", | ||
input: "invalid", | ||
err: `failed to parse slot: strconv.ParseInt: parsing "invalid": invalid syntax`, | ||
}, | ||
{ | ||
name: "Absolute", | ||
input: "15", | ||
expected: 15, | ||
}, | ||
{ | ||
name: "Current", | ||
input: "current", | ||
expected: 7200, | ||
}, | ||
{ | ||
name: "Last", | ||
input: "last", | ||
expected: 7199, | ||
}, | ||
{ | ||
name: "RelativeZero", | ||
input: "-0", | ||
expected: 7200, | ||
}, | ||
{ | ||
name: "Relative", | ||
input: "-5", | ||
expected: 7195, | ||
}, | ||
{ | ||
name: "RelativeFar", | ||
input: "-50000", | ||
expected: 0, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
slot, err := util.ParseSlot(ctx, chainTime, test.input) | ||
if test.err != "" { | ||
require.EqualError(t, err, test.err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, test.expected, slot) | ||
} | ||
}) | ||
} | ||
} |