2018-11-24 18:57:07 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2018-12-01 22:09:12 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/randao"
|
2018-11-24 18:57:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/types"
|
2018-12-20 22:00:38 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-11-24 18:57:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
// ExecuteStateTransition defines the procedure for a state transition function.
|
|
|
|
// Spec:
|
|
|
|
// We now define the state transition function. At a high level the state transition is made up of two parts:
|
|
|
|
// - The per-slot transitions, which happens every slot, and only affects a parts of the state.
|
|
|
|
// - The per-epoch transitions, which happens at every epoch boundary (i.e. state.slot % EPOCH_LENGTH == 0), and affects the entire state.
|
|
|
|
// The per-slot transitions generally focus on verifying aggregate signatures and saving temporary records relating to the per-slot
|
|
|
|
// activity in the BeaconState. The per-epoch transitions focus on the validator registry, including adjusting balances and activating
|
|
|
|
// and exiting validators, as well as processing crosslinks and managing block justification/finalization.
|
|
|
|
func ExecuteStateTransition(
|
|
|
|
beaconState *types.BeaconState,
|
2018-12-20 22:00:38 +00:00
|
|
|
block *pb.BeaconBlock,
|
|
|
|
) (*types.BeaconState, error) {
|
2018-12-19 05:18:42 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
newState := beaconState.CopyState()
|
|
|
|
|
|
|
|
currentSlot := newState.Slot()
|
|
|
|
newState.SetSlot(currentSlot + 1)
|
|
|
|
|
|
|
|
newState, err = randao.UpdateRandaoLayers(newState, newState.Slot())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to update randao layer %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
newHashes, err := newState.CalculateNewBlockHashes(block, currentSlot)
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to calculate recent blockhashes")
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
newState.SetLatestBlockHashes(newHashes)
|
2018-12-19 05:18:42 +00:00
|
|
|
|
|
|
|
if block != nil {
|
|
|
|
newState = ProcessBlock(newState, block)
|
|
|
|
if newState.Slot()%params.BeaconConfig().EpochLength == 0 {
|
|
|
|
newState = NewEpochTransition(newState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState, nil
|
|
|
|
}
|
|
|
|
|
2018-12-20 01:45:21 +00:00
|
|
|
// ProcessBlock describes the per block operations that happen on every slot.
|
2018-12-20 22:00:38 +00:00
|
|
|
func ProcessBlock(state *types.BeaconState, block *pb.BeaconBlock) *types.BeaconState {
|
2018-12-20 01:45:21 +00:00
|
|
|
_ = block
|
|
|
|
// TODO(#1073): This function will encompass all the per block slot transition functions, this will
|
|
|
|
// contain checks for randao,proposer validity and block operations.
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
// NewEpochTransition describes the per epoch operations that are performed on the
|
|
|
|
// beacon state.
|
|
|
|
func NewEpochTransition(state *types.BeaconState) *types.BeaconState {
|
|
|
|
// TODO(#1074): This will encompass all the related logic to epoch transitions.
|
|
|
|
return state
|
|
|
|
}
|