From 3a7117dcbfb8d5cf10fac3e7fd81b3b805ceaf1d Mon Sep 17 00:00:00 2001 From: Potuz Date: Thu, 16 Jun 2022 19:26:55 -0300 Subject: [PATCH] Do not downcast time in currentslot (#10897) * Do not downcast time in currentslot * no magic constants Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/blockchain/execution_engine_test.go | 2 ++ beacon-chain/blockchain/receive_block_test.go | 2 ++ time/slots/slottime.go | 7 +++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/beacon-chain/blockchain/execution_engine_test.go b/beacon-chain/blockchain/execution_engine_test.go index fa033d698..c2aba785c 100644 --- a/beacon-chain/blockchain/execution_engine_test.go +++ b/beacon-chain/blockchain/execution_engine_test.go @@ -409,6 +409,8 @@ func Test_NotifyNewPayload(t *testing.T) { require.NoError(t, err) service, err := NewService(ctx, opts...) require.NoError(t, err) + st := params.BeaconConfig().SlotsPerEpoch.Mul(uint64(epochsSinceFinalitySaveHotStateDB)) + service.genesisTime = time.Now().Add(time.Duration(-1*int64(st)*int64(params.BeaconConfig().SecondsPerSlot)) * time.Second) r, err := bellatrixBlk.Block().HashTreeRoot() require.NoError(t, err) ojc := ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]} diff --git a/beacon-chain/blockchain/receive_block_test.go b/beacon-chain/blockchain/receive_block_test.go index 08a103b51..a96b2f2d5 100644 --- a/beacon-chain/blockchain/receive_block_test.go +++ b/beacon-chain/blockchain/receive_block_test.go @@ -322,6 +322,8 @@ func TestCheckSaveHotStateDB_Disabling(t *testing.T) { opts := testServiceOptsWithDB(t) s, err := NewService(context.Background(), opts...) require.NoError(t, err) + st := params.BeaconConfig().SlotsPerEpoch.Mul(uint64(epochsSinceFinalitySaveHotStateDB)) + s.genesisTime = time.Now().Add(time.Duration(-1*int64(st)*int64(params.BeaconConfig().SecondsPerSlot)) * time.Second) s.store.SetFinalizedCheckptAndPayloadHash(ðpb.Checkpoint{}, [32]byte{}) require.NoError(t, s.checkSaveHotStateDB(context.Background())) s.genesisTime = time.Now() diff --git a/time/slots/slottime.go b/time/slots/slottime.go index ce7fffb74..847b4af9c 100644 --- a/time/slots/slottime.go +++ b/time/slots/slottime.go @@ -164,12 +164,11 @@ func Since(time time.Time) types.Slot { // CurrentSlot returns the current slot as determined by the local clock and // provided genesis time. func CurrentSlot(genesisTimeSec uint64) types.Slot { - now := prysmTime.Now().Unix() - genesis := int64(genesisTimeSec) // lint:ignore uintcast -- Genesis timestamp will not exceed int64 in your lifetime. - if now < genesis { + now := uint64(prysmTime.Now().Unix()) + if now < genesisTimeSec { return 0 } - return types.Slot(uint64(now-genesis) / params.BeaconConfig().SecondsPerSlot) + return types.Slot((now - genesisTimeSec) / params.BeaconConfig().SecondsPerSlot) } // ValidateClock validates a provided slot against the local