2022-05-09 13:02:34 +00:00
|
|
|
package state_native
|
|
|
|
|
|
|
|
import (
|
2022-05-27 16:38:00 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/time"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2022-05-09 13:02:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
|
|
|
)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
|
|
|
// CurrentEpochParticipation corresponding to participation bits on the beacon chain.
|
|
|
|
func (b *BeaconState) CurrentEpochParticipation() ([]byte, error) {
|
2022-05-09 13:02:34 +00:00
|
|
|
if b.version == version.Phase0 {
|
|
|
|
return nil, errNotSupported("CurrentEpochParticipation", b.version)
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
if b.currentEpochParticipation == nil {
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
return b.currentEpochParticipationVal(), nil
|
2022-01-13 11:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PreviousEpochParticipation corresponding to participation bits on the beacon chain.
|
|
|
|
func (b *BeaconState) PreviousEpochParticipation() ([]byte, error) {
|
2022-05-09 13:02:34 +00:00
|
|
|
if b.version == version.Phase0 {
|
|
|
|
return nil, errNotSupported("PreviousEpochParticipation", b.version)
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
if b.previousEpochParticipation == nil {
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
return b.previousEpochParticipationVal(), nil
|
2022-01-13 11:23:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 16:38:00 +00:00
|
|
|
// UnrealizedCheckpointBalances returns the total balances: active, target attested in
|
|
|
|
// current epoch and target attested in previous epoch. This function is used to
|
|
|
|
// compute the "unrealized justification" that a synced Beacon Block will have.
|
|
|
|
func (b *BeaconState) UnrealizedCheckpointBalances() (uint64, uint64, uint64, error) {
|
|
|
|
if b.version == version.Phase0 {
|
|
|
|
return 0, 0, 0, errNotSupported("UnrealizedCheckpointBalances", b.version)
|
|
|
|
}
|
|
|
|
|
2022-06-29 23:37:21 +00:00
|
|
|
currentEpoch := time.CurrentEpoch(b)
|
2022-05-27 16:38:00 +00:00
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
cp := b.currentEpochParticipation
|
|
|
|
pp := b.previousEpochParticipation
|
|
|
|
if cp == nil || pp == nil {
|
|
|
|
return 0, 0, 0, ErrNilParticipation
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateutil.UnrealizedCheckpointBalances(cp, pp, b.validators, currentEpoch)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
// currentEpochParticipationVal corresponding to participation bits on the beacon chain.
|
2022-01-13 11:23:53 +00:00
|
|
|
// This assumes that a lock is already held on BeaconState.
|
2022-01-27 08:42:32 +00:00
|
|
|
func (b *BeaconState) currentEpochParticipationVal() []byte {
|
|
|
|
tmp := make([]byte, len(b.currentEpochParticipation))
|
|
|
|
copy(tmp, b.currentEpochParticipation)
|
2022-01-13 11:23:53 +00:00
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:42:32 +00:00
|
|
|
// previousEpochParticipationVal corresponding to participation bits on the beacon chain.
|
2022-01-13 11:23:53 +00:00
|
|
|
// This assumes that a lock is already held on BeaconState.
|
2022-01-27 08:42:32 +00:00
|
|
|
func (b *BeaconState) previousEpochParticipationVal() []byte {
|
|
|
|
tmp := make([]byte, len(b.previousEpochParticipation))
|
|
|
|
copy(tmp, b.previousEpochParticipation)
|
2022-01-13 11:23:53 +00:00
|
|
|
return tmp
|
|
|
|
}
|