Minor changes from hf1 branch (#8906)

* Copy new slot helpers from hf1 branch

* Make HandleEth1DataSlice public for altair
This commit is contained in:
Preston Van Loon
2021-05-19 12:11:48 -04:00
committed by GitHub
parent 9db4eba72b
commit 36e9edd654
5 changed files with 50 additions and 3 deletions

View File

@@ -27,6 +27,7 @@ go_library(
"//shared/keystore:__pkg__",
"//shared/p2putils:__pkg__",
"//shared/testutil:__pkg__",
"//shared/testutil/altair:__pkg__",
"//slasher:__subpackages__",
"//spectest:__subpackages__",
"//tools:__subpackages__",

View File

@@ -193,3 +193,11 @@ func VotingPeriodStartTime(genesis uint64, slot types.Slot) uint64 {
startTime := uint64((slot - slot.ModSlot(slots)).Mul(params.BeaconConfig().SecondsPerSlot))
return genesis + startTime
}
// PrevSlot returns previous slot, with an exception in slot 0 to prevent underflow.
func PrevSlot(slot types.Slot) types.Slot {
if slot > 0 {
return slot.Sub(1)
}
return 0
}

View File

@@ -345,3 +345,39 @@ func TestValidateSlotClock_HandlesBadSlot(t *testing.T) {
assert.ErrorContains(t, "which exceeds max allowed value relative to the local clock", ValidateSlotClock(types.Slot(2*MaxSlotBuffer+1), uint64(genTime)), "no error from bad slot")
assert.ErrorContains(t, "which exceeds max allowed value relative to the local clock", ValidateSlotClock(1<<63, uint64(genTime)), "no error from bad slot")
}
func TestPrevSlot(t *testing.T) {
tests := []struct {
name string
slot types.Slot
want types.Slot
}{
{
name: "no underflow",
slot: 0,
want: 0,
},
{
name: "slot 1",
slot: 1,
want: 0,
},
{
name: "slot 2",
slot: 2,
want: 1,
},
{
name: "max",
slot: 1<<64 - 1,
want: 1<<64 - 1 - 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := PrevSlot(tt.slot); got != tt.want {
t.Errorf("PrevSlot() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -44,7 +44,7 @@ func fieldConverters(field fieldIndex, indices []uint64, elements interface{}, c
return nil, errors.Errorf("Wanted type of %v but got %v",
reflect.TypeOf([]*ethpb.Eth1Data{}).Name(), reflect.TypeOf(elements).Name())
}
return handleEth1DataSlice(val, indices, convertAll)
return HandleEth1DataSlice(val, indices, convertAll)
case validators:
val, ok := elements.([]*ethpb.Validator)
if !ok {
@@ -64,7 +64,8 @@ func fieldConverters(field fieldIndex, indices []uint64, elements interface{}, c
}
}
func handleEth1DataSlice(val []*ethpb.Eth1Data, indices []uint64, convertAll bool) ([][32]byte, error) {
// HandleEth1DataSlice processes a list of eth1data and indices into the appropriate roots.
func HandleEth1DataSlice(val []*ethpb.Eth1Data, indices []uint64, convertAll bool) ([][32]byte, error) {
length := len(indices)
if convertAll {
length = len(val)

View File

@@ -18,6 +18,7 @@ func Test_handlePendingAttestation_OutOfRange(t *testing.T) {
func Test_handleEth1DataSlice_OutOfRange(t *testing.T) {
items := make([]*ethpb.Eth1Data, 1)
indices := []uint64{3}
_, err := handleEth1DataSlice(items, indices, false)
_, err := HandleEth1DataSlice(items, indices, false)
assert.ErrorContains(t, "index 3 greater than number of items in eth1 data slice 1", err)
}