-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspec.go
121 lines (100 loc) · 4.05 KB
/
spec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package beacon
import (
"time"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/bloxapp/beacon-kit/clock"
)
// Spec contains the network-specific Beacon Chain configuration
// and provides helper methods to access it.
type Spec struct {
Network Network
GenesisTime time.Time
GenesisSlot phase0.Slot
GenesisForkVersion phase0.Version
FarFutureEpoch phase0.Epoch
SlotsPerEpoch phase0.Slot
SecondsPerSlot uint64
MaxCommitteesPerSlot uint64
TargetCommitteeSize uint64
TargetAggregatorsPerCommittee uint64
AttestationSubnetCount uint64
// AttestationPropagationSlotRange is the maximum number of slots
// during which an attestation can be propagated, after which
// there is no point in submitting it.
AttestationPropagationSlotRange phase0.Slot
SyncCommitteeSize uint64
TargetAggregatorsPerSyncSubcommittee uint64
SyncCommitteeSubnetCount uint64
EpochsPerSyncCommitteePeriod phase0.Epoch
DomainBeaconProposer phase0.DomainType
DomainBeaconAttester phase0.DomainType
DomainRandao phase0.DomainType
DomainDeposit phase0.DomainType
DomainVoluntaryExit phase0.DomainType
DomainSelectionProof phase0.DomainType
DomainAggregateAndProof phase0.DomainType
DomainSyncCommittee phase0.DomainType
DomainSyncCommitteeSelectionProof phase0.DomainType
DomainContributionAndProof phase0.DomainType
DomainApplicationMask phase0.DomainType
DomainApplicationBuilder phase0.DomainType
AltairForkEpoch phase0.Epoch
BellatrixForkEpoch phase0.Epoch
}
func (s *Spec) Clock() clock.Clock {
return clock.New(clock.Params{
GenesisTime: s.GenesisTime,
SlotsPerEpoch: s.SlotsPerEpoch,
SlotDuration: s.SlotDuration(),
})
}
// SlotDuration returns the time.Duration of a slot.
func (s *Spec) SlotDuration() time.Duration {
return time.Duration(s.SecondsPerSlot) * time.Second
}
// TimeAtSlot returns the time at the start of the given slot.
func (s *Spec) TimeAtSlot(slot phase0.Slot) time.Time {
return s.GenesisTime.Add(time.Duration(slot) * s.SlotDuration())
}
// SlotAtTime returns the slot at the given time.
func (s *Spec) SlotAtTime(t time.Time) phase0.Slot {
return phase0.Slot(t.Sub(s.GenesisTime) / s.SlotDuration())
}
// EpochFromSlot returns the epoch at the given slot.
func (s *Spec) EpochFromSlot(slot phase0.Slot) phase0.Epoch {
return phase0.Epoch(slot / s.SlotsPerEpoch)
}
// StartSlot returns the first slot in the given epoch.
func (s *Spec) StartSlot(epoch phase0.Epoch) phase0.Slot {
return phase0.Slot(epoch) * s.SlotsPerEpoch
}
// EndSlot returns the last slot in the given epoch.
func (s *Spec) EndSlot(epoch phase0.Epoch) phase0.Slot {
return s.StartSlot(epoch) + s.SlotsPerEpoch - 1
}
// CommitteesAtSlot returns the number of committees per slot.
func (s *Spec) CommitteesAtSlot(activeValidators uint64) uint64 {
n := activeValidators / uint64(s.SlotsPerEpoch) / s.TargetCommitteeSize
if n > s.MaxCommitteesPerSlot {
return s.MaxCommitteesPerSlot
}
if n == 0 {
return 1
}
return n
}
// AttestationSubnetID returns the subnet ID for an attestation.
// See https://github.com/ethereum/consensus-specs/blob/395fdd456657482b7257c8b9a9d68bea68917aaf/specs/phase0/validator.md#broadcast-attestation
func (s *Spec) AttestationSubnetID(slot phase0.Slot, committeeIndex phase0.CommitteeIndex, committeesAtSlot uint64) SubnetID {
slotsSinceEpochStart := slot % s.SlotsPerEpoch
committeesSinceEpochStart := committeesAtSlot * uint64(slotsSinceEpochStart)
return SubnetID((committeesSinceEpochStart + uint64(committeeIndex)) % s.AttestationSubnetCount)
}
// SyncCommitteeSubnetID returns the subnet ID for a sync committee.
func (s *Spec) SyncCommitteeSubnetID(index phase0.CommitteeIndex) SubnetID {
return SubnetID(index) / SubnetID(s.SyncSubcommitteeSize())
}
// SyncSubcommitteeSize returns the size of a sync subcommittee.
func (s *Spec) SyncSubcommitteeSize() uint64 {
return s.SyncCommitteeSize / s.SyncCommitteeSubnetCount
}