2021-03-22 15:19:38 +00:00
|
|
|
package statefetcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
)
|
|
|
|
|
2021-03-29 21:04:35 +00:00
|
|
|
// Fetcher is responsible for retrieving the BeaconState.
|
|
|
|
type Fetcher interface {
|
|
|
|
State(ctx context.Context, stateId []byte) (iface.BeaconState, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateProvider is a real implementation of Fetcher.
|
|
|
|
type StateProvider struct {
|
2021-03-22 15:19:38 +00:00
|
|
|
BeaconDB db.ReadOnlyDatabase
|
|
|
|
ChainInfoFetcher blockchain.ChainInfoFetcher
|
|
|
|
GenesisTimeFetcher blockchain.TimeFetcher
|
|
|
|
StateGenService stategen.StateManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// State returns the BeaconState for a given identifier. The identifier can be one of:
|
|
|
|
// - "head" (canonical head in node's view)
|
|
|
|
// - "genesis"
|
|
|
|
// - "finalized"
|
|
|
|
// - "justified"
|
|
|
|
// - <slot>
|
|
|
|
// - <hex encoded stateRoot with 0x prefix>
|
2021-03-29 21:04:35 +00:00
|
|
|
func (p *StateProvider) State(ctx context.Context, stateId []byte) (iface.BeaconState, error) {
|
2021-03-22 15:19:38 +00:00
|
|
|
var (
|
|
|
|
s iface.BeaconState
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
stateIdString := strings.ToLower(string(stateId))
|
|
|
|
switch stateIdString {
|
|
|
|
case "head":
|
2021-03-29 21:04:35 +00:00
|
|
|
s, err = p.ChainInfoFetcher.HeadState(ctx)
|
2021-03-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get head state")
|
|
|
|
}
|
|
|
|
case "genesis":
|
2021-03-29 21:04:35 +00:00
|
|
|
s, err = p.BeaconDB.GenesisState(ctx)
|
2021-03-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get genesis state")
|
|
|
|
}
|
|
|
|
case "finalized":
|
2021-03-29 21:04:35 +00:00
|
|
|
checkpoint := p.ChainInfoFetcher.FinalizedCheckpt()
|
|
|
|
s, err = p.StateGenService.StateByRoot(ctx, bytesutil.ToBytes32(checkpoint.Root))
|
2021-03-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get finalized state")
|
|
|
|
}
|
|
|
|
case "justified":
|
2021-03-29 21:04:35 +00:00
|
|
|
checkpoint := p.ChainInfoFetcher.CurrentJustifiedCheckpt()
|
|
|
|
s, err = p.StateGenService.StateByRoot(ctx, bytesutil.ToBytes32(checkpoint.Root))
|
2021-03-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get justified state")
|
|
|
|
}
|
|
|
|
default:
|
2021-05-17 07:56:00 +00:00
|
|
|
if len(stateId) == 32 {
|
2021-03-29 21:04:35 +00:00
|
|
|
s, err = p.stateByHex(ctx, stateId)
|
2021-03-22 15:19:38 +00:00
|
|
|
} else {
|
|
|
|
slotNumber, parseErr := strconv.ParseUint(stateIdString, 10, 64)
|
|
|
|
if parseErr != nil {
|
|
|
|
// ID format does not match any valid options.
|
|
|
|
return nil, errors.New("invalid state ID: " + stateIdString)
|
|
|
|
}
|
2021-03-29 21:04:35 +00:00
|
|
|
s, err = p.stateBySlot(ctx, types.Slot(slotNumber))
|
2021-03-22 15:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:04:35 +00:00
|
|
|
func (p *StateProvider) stateByHex(ctx context.Context, stateId []byte) (iface.BeaconState, error) {
|
|
|
|
headState, err := p.ChainInfoFetcher.HeadState(ctx)
|
2021-03-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get head state")
|
|
|
|
}
|
|
|
|
for i, root := range headState.StateRoots() {
|
|
|
|
if bytes.Equal(root, stateId) {
|
|
|
|
blockRoot := headState.BlockRoots()[i]
|
2021-03-29 21:04:35 +00:00
|
|
|
return p.StateGenService.StateByRoot(ctx, bytesutil.ToBytes32(blockRoot))
|
2021-03-22 15:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("state not found in the last %d state roots in head state", len(headState.StateRoots()))
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:04:35 +00:00
|
|
|
func (p *StateProvider) stateBySlot(ctx context.Context, slot types.Slot) (iface.BeaconState, error) {
|
|
|
|
currentSlot := p.GenesisTimeFetcher.CurrentSlot()
|
2021-03-22 15:19:38 +00:00
|
|
|
if slot > currentSlot {
|
|
|
|
return nil, errors.New("slot cannot be in the future")
|
|
|
|
}
|
2021-03-29 21:04:35 +00:00
|
|
|
state, err := p.StateGenService.StateBySlot(ctx, slot)
|
2021-03-22 15:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get state")
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|