mirror of
https://github.com/wealdtech/ethdo.git
synced 2026-01-09 14:07:56 -05:00
Update code for spec change.
This commit is contained in:
@@ -61,7 +61,7 @@ func ParseAccount(ctx context.Context,
|
||||
// Private key.
|
||||
account, err = newScratchAccountFromPrivKey(data)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create account from public key")
|
||||
return nil, errors.Wrap(err, "failed to create account from private key")
|
||||
}
|
||||
if unlock {
|
||||
_, err = UnlockAccount(ctx, account, nil)
|
||||
|
||||
@@ -24,11 +24,15 @@ import (
|
||||
|
||||
// ParseEpoch parses input to calculate the desired epoch.
|
||||
func ParseEpoch(ctx context.Context, chainTime chaintime.Service, epochStr string) (phase0.Epoch, error) {
|
||||
currentEpoch := chainTime.CurrentEpoch()
|
||||
switch epochStr {
|
||||
case "", "current":
|
||||
return chainTime.CurrentEpoch(), nil
|
||||
case "", "current", "-0":
|
||||
return currentEpoch, nil
|
||||
case "last":
|
||||
return chainTime.CurrentEpoch() - 1, nil
|
||||
if currentEpoch > 0 {
|
||||
currentEpoch--
|
||||
}
|
||||
return currentEpoch, nil
|
||||
default:
|
||||
val, err := strconv.ParseInt(epochStr, 10, 64)
|
||||
if err != nil {
|
||||
@@ -37,6 +41,9 @@ func ParseEpoch(ctx context.Context, chainTime chaintime.Service, epochStr strin
|
||||
if val >= 0 {
|
||||
return phase0.Epoch(val), nil
|
||||
}
|
||||
return chainTime.CurrentEpoch() + phase0.Epoch(val), nil
|
||||
if phase0.Epoch(-val) > currentEpoch {
|
||||
return 0, nil
|
||||
}
|
||||
return currentEpoch + phase0.Epoch(val), nil
|
||||
}
|
||||
}
|
||||
|
||||
105
util/epoch_test.go
Normal file
105
util/epoch_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
// Copyright © 2032 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 TestParseEpoch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// genesis is 1 day ago.
|
||||
genesisTime := time.Now().AddDate(0, 0, -1)
|
||||
slotDuration := 12 * time.Second
|
||||
slotsPerEpoch := uint64(32)
|
||||
epochsPerSyncCommitteePeriod := uint64(256)
|
||||
mockGenesisTimeProvider := mock.NewGenesisTimeProvider(genesisTime)
|
||||
mockSpecProvider := mock.NewSpecProvider(slotDuration, slotsPerEpoch, 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.Epoch
|
||||
}{
|
||||
{
|
||||
name: "Genesis",
|
||||
input: "0",
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "Invalid",
|
||||
input: "invalid",
|
||||
err: `failed to parse epoch: strconv.ParseInt: parsing "invalid": invalid syntax`,
|
||||
},
|
||||
{
|
||||
name: "Absolute",
|
||||
input: "15",
|
||||
expected: 15,
|
||||
},
|
||||
{
|
||||
name: "Current",
|
||||
input: "current",
|
||||
expected: 225,
|
||||
},
|
||||
{
|
||||
name: "Last",
|
||||
input: "last",
|
||||
expected: 224,
|
||||
},
|
||||
{
|
||||
name: "RelativeZero",
|
||||
input: "-0",
|
||||
expected: 225,
|
||||
},
|
||||
{
|
||||
name: "Relative",
|
||||
input: "-5",
|
||||
expected: 220,
|
||||
},
|
||||
{
|
||||
name: "RelativeFar",
|
||||
input: "-500",
|
||||
expected: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
epoch, err := util.ParseEpoch(ctx, chainTime, test.input)
|
||||
if test.err != "" {
|
||||
require.EqualError(t, err, test.err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, test.expected, epoch)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user