From 36e9edd654f8e0166a74f174cafcdb426795a33e Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Wed, 19 May 2021 12:11:48 -0400 Subject: [PATCH] Minor changes from hf1 branch (#8906) * Copy new slot helpers from hf1 branch * Make HandleEth1DataSlice public for altair --- beacon-chain/core/helpers/BUILD.bazel | 1 + beacon-chain/core/helpers/slot_epoch.go | 8 +++++ beacon-chain/core/helpers/slot_epoch_test.go | 36 +++++++++++++++++++ .../state/stateV0/field_trie_helpers.go | 5 +-- beacon-chain/state/stateV0/helpers_test.go | 3 +- 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index c0714ae89a..45ba92a96b 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -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__", diff --git a/beacon-chain/core/helpers/slot_epoch.go b/beacon-chain/core/helpers/slot_epoch.go index b6374105aa..088b6dec0d 100644 --- a/beacon-chain/core/helpers/slot_epoch.go +++ b/beacon-chain/core/helpers/slot_epoch.go @@ -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 +} diff --git a/beacon-chain/core/helpers/slot_epoch_test.go b/beacon-chain/core/helpers/slot_epoch_test.go index 2c539e3570..8963116e78 100644 --- a/beacon-chain/core/helpers/slot_epoch_test.go +++ b/beacon-chain/core/helpers/slot_epoch_test.go @@ -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) + } + }) + } +} diff --git a/beacon-chain/state/stateV0/field_trie_helpers.go b/beacon-chain/state/stateV0/field_trie_helpers.go index 0999c3af06..c64326602c 100644 --- a/beacon-chain/state/stateV0/field_trie_helpers.go +++ b/beacon-chain/state/stateV0/field_trie_helpers.go @@ -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) diff --git a/beacon-chain/state/stateV0/helpers_test.go b/beacon-chain/state/stateV0/helpers_test.go index ce8181cd1c..2faf46fb2b 100644 --- a/beacon-chain/state/stateV0/helpers_test.go +++ b/beacon-chain/state/stateV0/helpers_test.go @@ -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) + }