Replace statefeed Initialize (#12285)

* refactor initialization to blocking startup method

* require genesisSetter in blockchain, fix tests

* work-around gazelle weirdness

* fix dep gazelle ignores

* only call SetGenesis once

* fix typo

* validator test setup and fix to return right error

* move waitForChainStart to Start

* wire up sync Service.genesisWaiter

* fix p2p genesisWaiter plumbing

* remove extra clock type, integrate into genesis

and rename

* use time.Now when no Nower is specified

* remove unused ClockSetter

* simplify rpc context checking

* fix typo

* use clock everywhere in sync; [32]byte val root

* don't use DeepEqual to compare [32]byte and []byte

* don't use clock in init sync, not wired up yet

* use clock waiter in blockchain as well

* use cancelable contexts in tests with goroutines

* missed a reference to WithClockSetter

* Update beacon-chain/startup/genesis.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update beacon-chain/blockchain/service_test.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* more clear docs

* doc for NewClock

* move clock typedef to more logical file name

* adding documentation

* gaz

* fixes for capella

* reducing test raciness

* fix races in committee cache tests

* lint

* add tests on Duration slot math helper

* startup package test coverage

* fix bad merge

* set non-zero genesis time in tests that call Start

* happy deepsource, happy me-epsource

* replace Synced event with channel

* remove unused error

* remove accidental wip commit

* gaz!

* remove unused event constants

* remove sync statefeed subscription to fix deadlock

* remove state notifier

* fix build

---------

Co-authored-by: Kasey Kirkham <kasey@users.noreply.github.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: nisdas <nishdas93@gmail.com>
This commit is contained in:
kasey
2023-05-02 23:34:01 -05:00
committed by GitHub
parent 5b8084b829
commit 918129cf36
119 changed files with 2207 additions and 2416 deletions

View File

@@ -173,6 +173,14 @@ func CurrentSlot(genesisTimeSec uint64) primitives.Slot {
return primitives.Slot((now - genesisTimeSec) / params.BeaconConfig().SecondsPerSlot)
}
// Duration computes the span of time between two instants, represented as Slots.
func Duration(start, end time.Time) primitives.Slot {
if end.Before(start) {
return 0
}
return primitives.Slot(uint64(end.Unix()-start.Unix()) / params.BeaconConfig().SecondsPerSlot)
}
// ValidateClock validates a provided slot against the local
// clock to ensure slots that are unreasonable are returned with
// an error.
@@ -233,7 +241,7 @@ func SyncCommitteePeriodStartEpoch(e primitives.Epoch) (primitives.Epoch, error)
// SecondsSinceSlotStart returns the number of seconds transcurred since the
// given slot start time
func SecondsSinceSlotStart(s primitives.Slot, genesisTime uint64, timeStamp uint64) (uint64, error) {
func SecondsSinceSlotStart(s primitives.Slot, genesisTime, timeStamp uint64) (uint64, error) {
if timeStamp < genesisTime+uint64(s)*params.BeaconConfig().SecondsPerSlot {
return 0, errors.New("could not compute seconds since slot start: invalid timestamp")
}

View File

@@ -491,3 +491,75 @@ func TestSecondsSinceSlotStart(t *testing.T) {
}
}
}
func TestDuration(t *testing.T) {
oneSlot := time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second
cases := []struct {
name string
start time.Time
endDelta time.Duration
expected primitives.Slot
}{
{
name: "end before start",
start: time.Now(),
endDelta: -64 * time.Second,
expected: 0,
},
{
name: "end equals start",
start: time.Now(),
endDelta: 0,
expected: 0,
},
{
name: "one slot apart",
start: time.Now(),
endDelta: oneSlot,
expected: 1,
},
{
name: "same slot",
start: time.Now(),
endDelta: time.Second,
expected: 0,
},
{
name: "don't round up",
start: time.Now(),
endDelta: oneSlot - time.Second,
expected: 0,
},
{
name: "don't round up pt 2",
start: time.Now(),
endDelta: 2*oneSlot - time.Second,
expected: 1,
},
{
name: "2 slots",
start: time.Now(),
endDelta: 2 * oneSlot,
expected: 2,
},
{
name: "1 epoch",
start: time.Now(),
endDelta: time.Duration(params.BeaconConfig().SlotsPerEpoch) * oneSlot,
expected: params.BeaconConfig().SlotsPerEpoch,
},
{
name: "1 epoch and change",
start: time.Now(),
endDelta: oneSlot + time.Second + time.Duration(params.BeaconConfig().SlotsPerEpoch)*oneSlot,
expected: params.BeaconConfig().SlotsPerEpoch + 1,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
end := c.start.Add(c.endDelta)
a := Duration(c.start, end)
require.Equal(t, c.expected, a)
})
}
}