mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
caf9bdbc6f
* commit initial work * checkpoint current work * gaz * checkpoint * req/resp changes * initial-sync * finally works * fix error * fix bugs * fix issue * fix issues * fix refs * tests * more text fixes * more text fixes * more text fixes * fix tests * fix tests * tests * finally fix builds * finally * comments * fix fuzz * share common library * fix * fix * add in more defensive nil checks * add in more defensive nil checks * imports * Apply suggestions from code review Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Apply suggestions from code review Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_interface.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_wrapper.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_interface.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * imports * fix bad changes * fix * terence's review * terence's review * fmt * Update beacon-chain/rpc/beacon/blocks.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * fix tests * fix * fix all tests Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl>
73 lines
2.7 KiB
Go
73 lines
2.7 KiB
Go
package helpers
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/pkg/errors"
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
|
"github.com/prysmaticlabs/prysm/shared/interfaces"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
// VerifyNilBeaconBlock checks if any composite field of input signed beacon block is nil.
|
|
// Access to these nil fields will result in run time panic,
|
|
// it is recommended to run these checks as first line of defense.
|
|
func VerifyNilBeaconBlock(b interfaces.SignedBeaconBlock) error {
|
|
if b == nil || b.IsNil() {
|
|
return errors.New("signed beacon block can't be nil")
|
|
}
|
|
if b.Block().IsNil() {
|
|
return errors.New("beacon block can't be nil")
|
|
}
|
|
if b.Block().Body().IsNil() {
|
|
return errors.New("beacon block body can't be nil")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// BlockRootAtSlot returns the block root stored in the BeaconState for a recent slot.
|
|
// It returns an error if the requested block root is not within the slot range.
|
|
//
|
|
// Spec pseudocode definition:
|
|
// def get_block_root_at_slot(state: BeaconState, slot: Slot) -> Root:
|
|
// """
|
|
// Return the block root at a recent ``slot``.
|
|
// """
|
|
// assert slot < state.slot <= slot + SLOTS_PER_HISTORICAL_ROOT
|
|
// return state.block_roots[slot % SLOTS_PER_HISTORICAL_ROOT]
|
|
func BlockRootAtSlot(state iface.ReadOnlyBeaconState, slot types.Slot) ([]byte, error) {
|
|
if math.MaxUint64-slot < params.BeaconConfig().SlotsPerHistoricalRoot {
|
|
return []byte{}, errors.New("slot overflows uint64")
|
|
}
|
|
if slot >= state.Slot() || state.Slot() > slot+params.BeaconConfig().SlotsPerHistoricalRoot {
|
|
return []byte{}, errors.Errorf("slot %d out of bounds", slot)
|
|
}
|
|
return state.BlockRootAtIndex(uint64(slot % params.BeaconConfig().SlotsPerHistoricalRoot))
|
|
}
|
|
|
|
// StateRootAtSlot returns the cached state root at that particular slot. If no state
|
|
// root has been cached it will return a zero-hash.
|
|
func StateRootAtSlot(state iface.ReadOnlyBeaconState, slot types.Slot) ([]byte, error) {
|
|
if slot >= state.Slot() || state.Slot() > slot+params.BeaconConfig().SlotsPerHistoricalRoot {
|
|
return []byte{}, errors.Errorf("slot %d out of bounds", slot)
|
|
}
|
|
return state.StateRootAtIndex(uint64(slot % params.BeaconConfig().SlotsPerHistoricalRoot))
|
|
}
|
|
|
|
// BlockRoot returns the block root stored in the BeaconState for epoch start slot.
|
|
//
|
|
// Spec pseudocode definition:
|
|
// def get_block_root(state: BeaconState, epoch: Epoch) -> Root:
|
|
// """
|
|
// Return the block root at the start of a recent ``epoch``.
|
|
// """
|
|
// return get_block_root_at_slot(state, compute_start_slot_at_epoch(epoch))
|
|
func BlockRoot(state iface.ReadOnlyBeaconState, epoch types.Epoch) ([]byte, error) {
|
|
s, err := StartSlot(epoch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return BlockRootAtSlot(state, s)
|
|
}
|