2021-06-30 15:06:19 +00:00
|
|
|
package v1
|
2019-12-05 20:23:59 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-07-29 21:45:17 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-16 09:46:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
2019-12-05 20:23:59 +00:00
|
|
|
)
|
|
|
|
|
2021-04-12 14:23:55 +00:00
|
|
|
// GenesisTime of the beacon state as a uint64.
|
|
|
|
func (b *BeaconState) GenesisTime() uint64 {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.genesisTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
// genesisTime of the beacon state as a uint64.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) genesisTime() uint64 {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.state.GenesisTime
|
|
|
|
}
|
|
|
|
|
2022-02-14 13:34:38 +00:00
|
|
|
// GenesisValidatorsRoot of the beacon state.
|
|
|
|
func (b *BeaconState) GenesisValidatorsRoot() []byte {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.GenesisValidatorsRoot == nil {
|
|
|
|
return params.BeaconConfig().ZeroHash[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
2022-02-14 13:34:38 +00:00
|
|
|
return b.genesisValidatorsRoot()
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 13:34:38 +00:00
|
|
|
// genesisValidatorsRoot of the beacon state.
|
2021-04-12 14:23:55 +00:00
|
|
|
// This assumes that a lock is already held on BeaconState.
|
2022-02-14 13:34:38 +00:00
|
|
|
func (b *BeaconState) genesisValidatorsRoot() []byte {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.GenesisValidatorsRoot == nil {
|
|
|
|
return params.BeaconConfig().ZeroHash[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
root := make([]byte, 32)
|
|
|
|
copy(root, b.state.GenesisValidatorsRoot)
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
2021-08-09 21:54:24 +00:00
|
|
|
// Version of the beacon state. This method
|
|
|
|
// is strictly meant to be used without a lock
|
|
|
|
// internally.
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *BeaconState) Version() int {
|
2021-06-30 15:06:19 +00:00
|
|
|
return version.Phase0
|
|
|
|
}
|
|
|
|
|
2021-04-12 14:23:55 +00:00
|
|
|
// Slot of the current beacon chain state.
|
|
|
|
func (b *BeaconState) Slot() types.Slot {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.slot()
|
|
|
|
}
|
|
|
|
|
|
|
|
// slot of the current beacon chain state.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) slot() types.Slot {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.state.Slot
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork version of the beacon chain.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (b *BeaconState) Fork() *ethpb.Fork {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Fork == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.fork()
|
|
|
|
}
|
|
|
|
|
|
|
|
// fork version of the beacon chain.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
2021-07-29 21:45:17 +00:00
|
|
|
func (b *BeaconState) fork() *ethpb.Fork {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Fork == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prevVersion := make([]byte, len(b.state.Fork.PreviousVersion))
|
|
|
|
copy(prevVersion, b.state.Fork.PreviousVersion)
|
|
|
|
currVersion := make([]byte, len(b.state.Fork.CurrentVersion))
|
|
|
|
copy(currVersion, b.state.Fork.CurrentVersion)
|
2021-07-29 21:45:17 +00:00
|
|
|
return ðpb.Fork{
|
2021-04-12 14:23:55 +00:00
|
|
|
PreviousVersion: prevVersion,
|
|
|
|
CurrentVersion: currVersion,
|
|
|
|
Epoch: b.state.Fork.Epoch,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HistoricalRoots based on epochs stored in the beacon state.
|
|
|
|
func (b *BeaconState) HistoricalRoots() [][]byte {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.HistoricalRoots == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.historicalRoots()
|
|
|
|
}
|
|
|
|
|
|
|
|
// historicalRoots based on epochs stored in the beacon state.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) historicalRoots() [][]byte {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-23 16:53:50 +00:00
|
|
|
return bytesutil.SafeCopy2dBytes(b.state.HistoricalRoots)
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// balancesLength returns the length of the balances slice.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) balancesLength() int {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if b.state.Balances == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(b.state.Balances)
|
|
|
|
}
|