2019-08-21 19:50:27 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2019-08-22 16:41:05 +00:00
|
|
|
"time"
|
|
|
|
|
2019-08-28 20:14:00 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-08-23 01:13:56 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-08-21 19:50:27 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2019-08-29 15:34:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-08-21 19:50:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ChainInfoRetriever defines a common interface for methods in blockchain service which
|
2019-08-22 16:41:05 +00:00
|
|
|
// directly retrieves chain info related data.
|
2019-08-21 19:50:27 +00:00
|
|
|
type ChainInfoRetriever interface {
|
2019-08-22 16:41:05 +00:00
|
|
|
HeadRetriever
|
|
|
|
CanonicalRetriever
|
|
|
|
FinalizationRetriever
|
|
|
|
GenesisTime() time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadRetriever defines a common interface for methods in blockchain service which
|
|
|
|
// directly retrieves head related data.
|
|
|
|
type HeadRetriever interface {
|
2019-08-21 19:50:27 +00:00
|
|
|
HeadSlot() uint64
|
|
|
|
HeadRoot() []byte
|
2019-08-23 01:13:56 +00:00
|
|
|
HeadBlock() *ethpb.BeaconBlock
|
|
|
|
HeadState() *pb.BeaconState
|
2019-08-22 16:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanonicalRetriever defines a common interface for methods in blockchain service which
|
|
|
|
// directly retrieves canonical roots related data.
|
|
|
|
type CanonicalRetriever interface {
|
2019-08-21 19:50:27 +00:00
|
|
|
CanonicalRoot(slot uint64) []byte
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:41:05 +00:00
|
|
|
// FinalizationRetriever defines a common interface for methods in blockchain service which
|
|
|
|
// directly retrieves finalization related data.
|
|
|
|
type FinalizationRetriever interface {
|
|
|
|
FinalizedCheckpt() *ethpb.Checkpoint
|
|
|
|
}
|
|
|
|
|
2019-08-21 19:50:27 +00:00
|
|
|
// FinalizedCheckpt returns the latest finalized checkpoint tracked in fork choice service.
|
|
|
|
func (c *ChainService) FinalizedCheckpt() *ethpb.Checkpoint {
|
2019-08-29 15:34:26 +00:00
|
|
|
cp := c.forkChoiceStore.FinalizedCheckpt()
|
|
|
|
if cp != nil {
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
return ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]}
|
2019-08-21 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadSlot returns the slot of the head of the chain.
|
|
|
|
func (c *ChainService) HeadSlot() uint64 {
|
|
|
|
return c.headSlot
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadRoot returns the root of the head of the chain.
|
|
|
|
func (c *ChainService) HeadRoot() []byte {
|
|
|
|
c.canonicalRootsLock.RLock()
|
|
|
|
defer c.canonicalRootsLock.RUnlock()
|
|
|
|
|
2019-08-29 15:34:26 +00:00
|
|
|
root := c.canonicalRoots[c.headSlot]
|
|
|
|
if len(root) != 0 {
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
|
|
|
return params.BeaconConfig().ZeroHash[:]
|
2019-08-21 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 01:13:56 +00:00
|
|
|
// HeadBlock returns the head block of the chain.
|
|
|
|
func (c *ChainService) HeadBlock() *ethpb.BeaconBlock {
|
2019-08-28 20:14:00 +00:00
|
|
|
return proto.Clone(c.headBlock).(*ethpb.BeaconBlock)
|
2019-08-23 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadState returns the head state of the chain.
|
|
|
|
func (c *ChainService) HeadState() *pb.BeaconState {
|
2019-08-28 20:14:00 +00:00
|
|
|
return proto.Clone(c.headState).(*pb.BeaconState)
|
2019-08-23 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:50:27 +00:00
|
|
|
// CanonicalRoot returns the canonical root of a given slot.
|
|
|
|
func (c *ChainService) CanonicalRoot(slot uint64) []byte {
|
|
|
|
c.canonicalRootsLock.RLock()
|
|
|
|
defer c.canonicalRootsLock.RUnlock()
|
|
|
|
|
|
|
|
return c.canonicalRoots[slot]
|
|
|
|
}
|
2019-08-22 16:41:05 +00:00
|
|
|
|
|
|
|
// GenesisTime returns the genesis time of beacon chain.
|
|
|
|
func (c *ChainService) GenesisTime() time.Time {
|
|
|
|
return c.genesisTime
|
|
|
|
}
|