mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-05 10:32:19 +00:00
cda14447ad
Co-authored-by: Giulio <giulio.rebuffo@gmail.com>
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
|
"github.com/ledgerwatch/erigon/cl/utils"
|
|
"github.com/ledgerwatch/log/v3"
|
|
)
|
|
|
|
func RetrieveBeaconState(ctx context.Context, beaconConfig *clparams.BeaconChainConfig, genesisConfig *clparams.GenesisConfig, uri string) (*state.BeaconState, error) {
|
|
log.Info("[Checkpoint Sync] Requesting beacon state", "uri", uri)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Set("Accept", "application/octet-stream")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("checkpoint sync failed %s", err)
|
|
}
|
|
r, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
err = r.Body.Close()
|
|
}()
|
|
if r.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("checkpoint sync failed, bad status code %d", r.StatusCode)
|
|
}
|
|
marshaled, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("checkpoint sync failed %s", err)
|
|
}
|
|
|
|
epoch := utils.GetCurrentEpoch(genesisConfig.GenesisTime, beaconConfig.SecondsPerSlot, beaconConfig.SlotsPerEpoch)
|
|
|
|
beaconState := state.New(beaconConfig)
|
|
err = beaconState.DecodeSSZ(marshaled, int(beaconConfig.GetCurrentStateVersion(epoch)))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("checkpoint sync failed %s", err)
|
|
}
|
|
return beaconState, nil
|
|
}
|