mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-09 11:11:20 +00:00
4c47756aed
* remove validation package * structs cleanup * merge with apimiddleware removal * more validation and Bls capitalization * builder test fix * use strconv for uint->str conversions * use DecodeHexWithLength * use exact param names * rename http package to httputil * change conversions to fmt.Sprintf * handle query paramsd and route variables * spans and receiver name * split structs, move bytes helper * missing ok check * fix reference to indexed failure * errors fixup * add godoc to helper * fix BLS casing and chainhead ref * review * fix import in tests * gzl
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package beacon_api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/beacon"
|
|
"github.com/prysmaticlabs/prysm/v4/network/httputil"
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
)
|
|
|
|
type GenesisProvider interface {
|
|
GetGenesis(ctx context.Context) (*beacon.Genesis, *httputil.DefaultErrorJson, error)
|
|
}
|
|
|
|
type beaconApiGenesisProvider struct {
|
|
jsonRestHandler JsonRestHandler
|
|
}
|
|
|
|
func (c beaconApiValidatorClient) waitForChainStart(ctx context.Context) (*ethpb.ChainStartResponse, error) {
|
|
genesis, httpError, err := c.genesisProvider.GetGenesis(ctx)
|
|
|
|
for err != nil {
|
|
if httpError == nil || httpError.Code != http.StatusNotFound {
|
|
return nil, errors.Wrap(err, "failed to get genesis data")
|
|
}
|
|
|
|
// Error 404 means that the chain genesis info is not yet known, so we query it every second until it's ready
|
|
select {
|
|
case <-time.After(time.Second):
|
|
genesis, httpError, err = c.genesisProvider.GetGenesis(ctx)
|
|
case <-ctx.Done():
|
|
return nil, errors.New("context canceled")
|
|
}
|
|
}
|
|
|
|
genesisTime, err := strconv.ParseUint(genesis.GenesisTime, 10, 64)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to parse genesis time: %s", genesis.GenesisTime)
|
|
}
|
|
|
|
chainStartResponse := ðpb.ChainStartResponse{}
|
|
chainStartResponse.Started = true
|
|
chainStartResponse.GenesisTime = genesisTime
|
|
|
|
if !validRoot(genesis.GenesisValidatorsRoot) {
|
|
return nil, errors.Errorf("invalid genesis validators root: %s", genesis.GenesisValidatorsRoot)
|
|
}
|
|
|
|
genesisValidatorRoot, err := hexutil.Decode(genesis.GenesisValidatorsRoot)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to decode genesis validators root")
|
|
}
|
|
chainStartResponse.GenesisValidatorsRoot = genesisValidatorRoot
|
|
|
|
return chainStartResponse, nil
|
|
}
|
|
|
|
// GetGenesis gets the genesis information from the beacon node via the /eth/v1/beacon/genesis endpoint
|
|
func (c beaconApiGenesisProvider) GetGenesis(ctx context.Context) (*beacon.Genesis, *httputil.DefaultErrorJson, error) {
|
|
genesisJson := &beacon.GetGenesisResponse{}
|
|
errorJson, err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/genesis", genesisJson)
|
|
if err != nil {
|
|
return nil, errorJson, errors.Wrap(err, "failed to get json response")
|
|
}
|
|
|
|
if genesisJson.Data == nil {
|
|
return nil, nil, errors.New("genesis data is nil")
|
|
}
|
|
|
|
return genesisJson.Data, nil, nil
|
|
}
|