Implement GetForkSchedule in the config API (#8345)

* span

* update ethereumapis

* implementation + tests

* typo

* fix variable shadowing

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
Radosław Kapka
2021-01-28 20:44:23 +01:00
committed by GitHub
parent 09b1e06885
commit 8d505e06bd
5 changed files with 91 additions and 10 deletions

View File

@@ -2,9 +2,9 @@ package beaconv1
import (
"context"
"errors"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
@@ -18,8 +18,37 @@ import (
)
// GetForkSchedule retrieve all scheduled upcoming forks this node is aware of.
func (bs *Server) GetForkSchedule(ctx context.Context, req *ptypes.Empty) (*ethpb.ForkScheduleResponse, error) {
return nil, errors.New("unimplemented")
func (bs *Server) GetForkSchedule(ctx context.Context, _ *ptypes.Empty) (*ethpb.ForkScheduleResponse, error) {
ctx, span := trace.StartSpan(ctx, "beaconv1.GetForkSchedule")
defer span.End()
schedule := params.BeaconConfig().ForkVersionSchedule
if len(schedule) == 0 {
return &ethpb.ForkScheduleResponse{
Data: make([]*ethpb.Fork, 0),
}, nil
}
epochs := sortedEpochs(schedule)
forks := make([]*ethpb.Fork, len(schedule))
var previous, current []byte
for i, e := range epochs {
if i == 0 {
previous = params.BeaconConfig().GenesisForkVersion
} else {
previous = current
}
current = schedule[e]
forks[i] = &ethpb.Fork{
PreviousVersion: previous,
CurrentVersion: current,
Epoch: e,
}
}
return &ethpb.ForkScheduleResponse{
Data: forks,
}, nil
}
// GetSpec retrieves specification configuration (without Phase 1 params) used on this node. Specification params list
@@ -38,7 +67,7 @@ func (bs *Server) GetSpec(ctx context.Context, _ *ptypes.Empty) (*ethpb.SpecResp
}
// GetDepositContract retrieves deposit contract address and genesis fork version.
func (bs *Server) GetDepositContract(ctx context.Context, req *ptypes.Empty) (*ethpb.DepositContractResponse, error) {
func (bs *Server) GetDepositContract(ctx context.Context, _ *ptypes.Empty) (*ethpb.DepositContractResponse, error) {
ctx, span := trace.StartSpan(ctx, "beaconv1.GetDepositContract")
defer span.End()
@@ -50,6 +79,17 @@ func (bs *Server) GetDepositContract(ctx context.Context, req *ptypes.Empty) (*e
}, nil
}
func sortedEpochs(forkSchedule map[uint64][]byte) []uint64 {
sortedEpochs := make([]uint64, len(forkSchedule))
i := 0
for k := range forkSchedule {
sortedEpochs[i] = k
i++
}
sort.Slice(sortedEpochs, func(a, b int) bool { return sortedEpochs[a] < sortedEpochs[b] })
return sortedEpochs
}
func prepareConfigSpec() (map[string]string, error) {
data := make(map[string]string)
config := *params.BeaconConfig()

View File

@@ -231,7 +231,6 @@ func TestGetDepositContract(t *testing.T) {
params.SetupTestConfigCleanup(t)
config := params.BeaconConfig()
config.DepositChainID = chainId
config.DepositContractAddress = address
params.OverrideBeaconConfig(config)
@@ -241,3 +240,45 @@ func TestGetDepositContract(t *testing.T) {
assert.Equal(t, uint64(chainId), resp.Data.ChainId)
assert.Equal(t, address, resp.Data.Address)
}
func TestForkSchedule_Ok(t *testing.T) {
genesisForkVersion := []byte("Genesis")
firstForkVersion, firstForkEpoch := []byte("First"), uint64(100)
secondForkVersion, secondForkEpoch := []byte("Second"), uint64(200)
thirdForkVersion, thirdForkEpoch := []byte("Third"), uint64(300)
params.SetupTestConfigCleanup(t)
config := params.BeaconConfig()
config.GenesisForkVersion = genesisForkVersion
// Create fork schedule adding keys in non-sorted order.
schedule := make(map[uint64][]byte, 3)
schedule[secondForkEpoch] = secondForkVersion
schedule[firstForkEpoch] = firstForkVersion
schedule[thirdForkEpoch] = thirdForkVersion
config.ForkVersionSchedule = schedule
params.OverrideBeaconConfig(config)
s := &Server{}
resp, err := s.GetForkSchedule(context.Background(), &types.Empty{})
require.NoError(t, err)
require.Equal(t, 3, len(resp.Data))
fork := resp.Data[0]
assert.DeepEqual(t, genesisForkVersion, fork.PreviousVersion)
assert.DeepEqual(t, firstForkVersion, fork.CurrentVersion)
assert.Equal(t, firstForkEpoch, fork.Epoch)
fork = resp.Data[1]
assert.DeepEqual(t, firstForkVersion, fork.PreviousVersion)
assert.DeepEqual(t, secondForkVersion, fork.CurrentVersion)
assert.Equal(t, secondForkEpoch, fork.Epoch)
fork = resp.Data[2]
assert.DeepEqual(t, secondForkVersion, fork.PreviousVersion)
assert.DeepEqual(t, thirdForkVersion, fork.CurrentVersion)
assert.Equal(t, thirdForkEpoch, fork.Epoch)
}
func TestForkSchedule_NoForks(t *testing.T) {
s := &Server{}
resp, err := s.GetForkSchedule(context.Background(), &types.Empty{})
require.NoError(t, err)
assert.Equal(t, 0, len(resp.Data))
}

View File

@@ -2573,8 +2573,8 @@ def prysm_deps():
name = "com_github_prysmaticlabs_ethereumapis",
build_file_generation = "off",
importpath = "github.com/prysmaticlabs/ethereumapis",
sum = "h1:roqXwVG8cKjq6sOCbB+T5Kh+dYr1wpkk00c7/DdrqLg=",
version = "v0.0.0-20210115110118-c595a4e0c0a5",
sum = "h1:W8MMbOp3rkI972HrLaJDBgOzolv8K8S7SUf9reFtqtw=",
version = "v0.0.0-20210127105958-025470485ece",
)
go_repository(
name = "com_github_prysmaticlabs_go_bitfield",

2
go.mod
View File

@@ -84,7 +84,7 @@ require (
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/procfs v0.3.0 // indirect
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/prysmaticlabs/ethereumapis v0.0.0-20210115110118-c595a4e0c0a5
github.com/prysmaticlabs/ethereumapis v0.0.0-20210127105958-025470485ece
github.com/prysmaticlabs/go-bitfield v0.0.0-20210126073541-0bc6a2fd086d
github.com/prysmaticlabs/prombbolt v0.0.0-20210126082820-9b7adba6db7c
github.com/rs/cors v1.7.0

4
go.sum
View File

@@ -1106,8 +1106,8 @@ github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38i
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20201126065335-1fb46e307951 h1:Jncuyb/nIJgXbEe0iGz3MN5JmijPVGzwk3G5FR01phI=
github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20201126065335-1fb46e307951/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6wCncIu1hNwHpM=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210115110118-c595a4e0c0a5 h1:roqXwVG8cKjq6sOCbB+T5Kh+dYr1wpkk00c7/DdrqLg=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210115110118-c595a4e0c0a5/go.mod h1:k7b2dxy6RppCG6kmOJkNOXzRpEoTdsPygc2aQhsUsZk=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210127105958-025470485ece h1:W8MMbOp3rkI972HrLaJDBgOzolv8K8S7SUf9reFtqtw=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210127105958-025470485ece/go.mod h1:k7b2dxy6RppCG6kmOJkNOXzRpEoTdsPygc2aQhsUsZk=
github.com/prysmaticlabs/go-bitfield v0.0.0-20200322041314-62c2aee71669 h1:cX6YRZnZ9sgMqM5U14llxUiXVNJ3u07Res1IIjTOgtI=
github.com/prysmaticlabs/go-bitfield v0.0.0-20200322041314-62c2aee71669/go.mod h1:hCwmef+4qXWjv0jLDbQdWnL0Ol7cS7/lCSS26WR+u6s=
github.com/prysmaticlabs/go-bitfield v0.0.0-20210126073541-0bc6a2fd086d h1:zHsHj3qQc11cDBb4TdBKHamEAXT8P3yQVqlgMF/USjU=