Skip to content

Commit

Permalink
Minor changes from hf1 branch (#8906)
Browse files Browse the repository at this point in the history
* Copy new slot helpers from hf1 branch

* Make HandleEth1DataSlice public for altair
  • Loading branch information
prestonvanloon authored May 19, 2021
1 parent 9db4eba commit 36e9edd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions beacon-chain/core/helpers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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__",
Expand Down
8 changes: 8 additions & 0 deletions beacon-chain/core/helpers/slot_epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
36 changes: 36 additions & 0 deletions beacon-chain/core/helpers/slot_epoch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
5 changes: 3 additions & 2 deletions beacon-chain/state/stateV0/field_trie_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/state/stateV0/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

0 comments on commit 36e9edd

Please sign in to comment.