2018-07-19 16:31:50 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-31 04:41:27 +00:00
|
|
|
"fmt"
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/types"
|
2018-07-30 06:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/database"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-07-19 16:31:50 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "blockchain")
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
// ChainService represents a service that handles the internal
|
|
|
|
// logic of managing the full PoS beacon chain.
|
|
|
|
type ChainService struct {
|
2018-08-01 01:13:54 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
beaconDB *database.DB
|
|
|
|
chain *BeaconChain
|
|
|
|
web3Service *powchain.Web3Service
|
|
|
|
latestBeaconBlock chan *types.Block
|
|
|
|
processedBlockHashes [][32]byte
|
|
|
|
processedActiveStateHashes [][32]byte
|
|
|
|
processedCrystallizedStateHashes [][32]byte
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChainService instantiates a new service instance that will
|
|
|
|
// be registered into a running beacon node.
|
2018-07-30 06:14:50 +00:00
|
|
|
func NewChainService(ctx context.Context, beaconDB *database.DB, web3Service *powchain.Web3Service) (*ChainService, error) {
|
2018-07-19 16:31:50 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-07-31 04:41:27 +00:00
|
|
|
return &ChainService{
|
2018-08-01 01:13:54 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
beaconDB: beaconDB,
|
|
|
|
web3Service: web3Service,
|
|
|
|
latestBeaconBlock: make(chan *types.Block),
|
|
|
|
processedBlockHashes: [][32]byte{},
|
|
|
|
processedActiveStateHashes: [][32]byte{},
|
|
|
|
processedCrystallizedStateHashes: [][32]byte{},
|
2018-07-31 04:41:27 +00:00
|
|
|
}, nil
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start a blockchain service's main event loop.
|
|
|
|
func (c *ChainService) Start() {
|
2018-07-25 16:57:44 +00:00
|
|
|
log.Infof("Starting service")
|
2018-07-22 16:58:14 +00:00
|
|
|
|
|
|
|
beaconChain, err := NewBeaconChain(c.beaconDB.DB())
|
|
|
|
if err != nil {
|
2018-07-19 16:31:50 +00:00
|
|
|
log.Errorf("Unable to setup blockchain: %v", err)
|
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
c.chain = beaconChain
|
2018-07-29 16:22:15 +00:00
|
|
|
go c.updateChainState()
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the blockchain service's main event loop and associated goroutines.
|
|
|
|
func (c *ChainService) Stop() error {
|
|
|
|
defer c.cancel()
|
2018-07-25 16:57:44 +00:00
|
|
|
log.Info("Stopping service")
|
2018-07-31 04:41:27 +00:00
|
|
|
log.Infof("Persisting current active and crystallized states before closing")
|
|
|
|
if err := c.chain.PersistActiveState(); err != nil {
|
|
|
|
return fmt.Errorf("Error persisting active state: %v", err)
|
|
|
|
}
|
|
|
|
if err := c.chain.PersistCrystallizedState(); err != nil {
|
|
|
|
return fmt.Errorf("Error persisting crystallized state: %v", err)
|
|
|
|
}
|
2018-07-19 16:31:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
|
2018-08-01 01:13:54 +00:00
|
|
|
// ProcessedBlockHashes by the chain service.
|
|
|
|
func (c *ChainService) ProcessedBlockHashes() [][32]byte {
|
|
|
|
return c.processedBlockHashes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessedCrystallizedStateHashes by the chain service.
|
|
|
|
func (c *ChainService) ProcessedCrystallizedStateHashes() [][32]byte {
|
|
|
|
return c.processedCrystallizedStateHashes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessedActiveStateHashes by the chain service.
|
|
|
|
func (c *ChainService) ProcessedActiveStateHashes() [][32]byte {
|
|
|
|
return c.processedActiveStateHashes
|
2018-07-28 19:53:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 16:57:44 +00:00
|
|
|
// ProcessBlock accepts a new block for inclusion in the chain.
|
2018-07-31 04:41:27 +00:00
|
|
|
func (c *ChainService) ProcessBlock(block *types.Block) error {
|
|
|
|
h, err := block.Hash()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not hash incoming block: %v", err)
|
|
|
|
}
|
|
|
|
log.WithField("blockHash", fmt.Sprintf("0x%x", h)).Info("Received full block, processing validity conditions")
|
|
|
|
canProcess, err := c.chain.CanProcessBlock(c.web3Service.Client(), block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if canProcess {
|
|
|
|
c.latestBeaconBlock <- block
|
|
|
|
}
|
2018-07-25 16:57:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-01 01:13:54 +00:00
|
|
|
// ProcessCrystallizedState accepts a new crystallized state object for inclusion in the chain.
|
|
|
|
func (c *ChainService) ProcessCrystallizedState(state *types.CrystallizedState) error {
|
|
|
|
h, err := state.Hash()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not hash incoming block: %v", err)
|
|
|
|
}
|
|
|
|
log.WithField("stateHash", fmt.Sprintf("0x%x", h)).Info("Received crystallized state, processing validity conditions")
|
|
|
|
|
|
|
|
// TODO: Implement crystallized state verifier function and apply fork choice rules
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessActiveState accepts a new active state object for inclusion in the chain.
|
|
|
|
func (c *ChainService) ProcessActiveState(state *types.ActiveState) error {
|
|
|
|
h, err := state.Hash()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not hash incoming block: %v", err)
|
|
|
|
}
|
|
|
|
log.WithField("stateHash", fmt.Sprintf("0x%x", h)).Info("Received active state, processing validity conditions")
|
|
|
|
|
|
|
|
// TODO: Implement active state verifier function and apply fork choice rules
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:57:44 +00:00
|
|
|
// ContainsBlock checks if a block for the hash exists in the chain.
|
|
|
|
// This method must be safe to call from a goroutine
|
2018-07-28 19:53:02 +00:00
|
|
|
func (c *ChainService) ContainsBlock(h [32]byte) bool {
|
2018-07-25 16:57:44 +00:00
|
|
|
// TODO
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-01 01:13:54 +00:00
|
|
|
// ContainsCrystallizedState checks if a crystallized state for the hash exists in the chain.
|
|
|
|
func (c *ChainService) ContainsCrystallizedState(h [32]byte) bool {
|
|
|
|
// TODO
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainsActiveState checks if a active state for the hash exists in the chain.
|
|
|
|
func (c *ChainService) ContainsActiveState(h [32]byte) bool {
|
|
|
|
// TODO
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-31 04:41:27 +00:00
|
|
|
// CurrentCrystallizedState of the canonical chain.
|
|
|
|
func (c *ChainService) CurrentCrystallizedState() *types.CrystallizedState {
|
|
|
|
return c.chain.CrystallizedState()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentActiveState of the canonical chain.
|
|
|
|
func (c *ChainService) CurrentActiveState() *types.ActiveState {
|
|
|
|
return c.chain.ActiveState()
|
|
|
|
}
|
|
|
|
|
2018-07-29 16:22:15 +00:00
|
|
|
// updateChainState receives a beacon block, computes a new active state and writes it to db. Also
|
|
|
|
// it checks for if there is an epoch transition. If there is one it computes the validator rewards
|
|
|
|
// and penalties.
|
|
|
|
func (c *ChainService) updateChainState() {
|
2018-07-22 16:58:14 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case block := <-c.latestBeaconBlock:
|
|
|
|
// TODO: Using latest block hash for seed, this will eventually be replaced by randao
|
|
|
|
activeState, err := c.chain.computeNewActiveState(c.web3Service.LatestBlockHash())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Compute active state failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.chain.MutateActiveState(activeState)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Write active state to disk failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-29 16:22:15 +00:00
|
|
|
currentslot := block.SlotNumber()
|
|
|
|
|
|
|
|
transition := c.chain.isEpochTransition(currentslot)
|
|
|
|
if transition {
|
|
|
|
if err := c.chain.computeValidatorRewardsAndPenalties(); err != nil {
|
|
|
|
log.Errorf("Error computing validator rewards and penalties %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
case <-c.ctx.Done():
|
|
|
|
log.Debug("Chain service context closed, exiting goroutine")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|