2022-11-25 15:38:22 +00:00
|
|
|
package core
|
2022-11-02 12:12:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RetrieveBeaconState(ctx context.Context, uri string) (*cltypes.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)
|
|
|
|
}
|
2022-12-07 20:26:45 +00:00
|
|
|
|
2022-11-02 12:12:44 +00:00
|
|
|
beaconState := &cltypes.BeaconState{}
|
|
|
|
err = beaconState.UnmarshalSSZ(marshaled)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("checkpoint sync failed %s", err)
|
|
|
|
}
|
|
|
|
return beaconState, nil
|
|
|
|
}
|