2022-04-27 18:01:59 +00:00
|
|
|
// This file will contain functions to query the Beacon Chain Server.
|
|
|
|
|
|
|
|
package beaconclient
|
|
|
|
|
|
|
|
import (
|
2022-05-12 22:29:40 +00:00
|
|
|
"encoding/json"
|
2022-04-27 18:01:59 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A helper function to query endpoints that utilize slots.
|
2022-05-06 15:03:15 +00:00
|
|
|
func querySsz(endpoint string, slot string) ([]byte, int, error) {
|
|
|
|
log.WithFields(log.Fields{"endpoint": endpoint}).Debug("Querying endpoint")
|
2022-04-27 18:01:59 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogSlotError(slot, err).Error("Unable to create a request!")
|
2022-05-06 15:03:15 +00:00
|
|
|
return nil, 0, fmt.Errorf("Unable to create a request!: %s", err.Error())
|
2022-04-27 18:01:59 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("Accept", "application/octet-stream")
|
|
|
|
response, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogSlotError(slot, err).Error("Unable to query Beacon Node!")
|
2022-05-06 15:03:15 +00:00
|
|
|
return nil, 0, fmt.Errorf("Unable to query Beacon Node: %s", err.Error())
|
2022-04-27 18:01:59 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
2022-05-06 15:03:15 +00:00
|
|
|
rc := response.StatusCode
|
2022-04-27 18:01:59 +00:00
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogSlotError(slot, err).Error("Unable to turn response into a []bytes array!")
|
2022-05-06 15:03:15 +00:00
|
|
|
return nil, rc, fmt.Errorf("Unable to turn response into a []bytes array!: %s", err.Error())
|
2022-04-27 18:01:59 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
return body, rc, nil
|
2022-04-27 18:01:59 +00:00
|
|
|
}
|
2022-05-12 22:29:40 +00:00
|
|
|
|
|
|
|
// Object to unmarshal the BlockRootResponse
|
|
|
|
type BlockRootResponse struct {
|
|
|
|
Data BlockRootMessage `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object to unmarshal the BlockRoot Message
|
|
|
|
type BlockRootMessage struct {
|
|
|
|
Root string `json:"root"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// A function to query the blockroot for a given slot.
|
|
|
|
func queryBlockRoot(endpoint string, slot string) (string, error) {
|
|
|
|
log.WithFields(log.Fields{"endpoint": endpoint}).Debug("Querying endpoint")
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogSlotError(slot, err).Error("Unable to create a request!")
|
|
|
|
return "", fmt.Errorf("Unable to create a request!: %s", err.Error())
|
|
|
|
}
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
response, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogSlotError(slot, err).Error("Unable to query Beacon Node!")
|
|
|
|
return "", fmt.Errorf("Unable to query Beacon Node: %s", err.Error())
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogSlotError(slot, err).Error("Unable to turn response into a []bytes array!")
|
|
|
|
return "", fmt.Errorf("Unable to turn response into a []bytes array!: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := BlockRootResponse{}
|
|
|
|
if err := json.Unmarshal(body, &resp); err != nil {
|
|
|
|
loghelper.LogEndpoint(endpoint).WithFields(log.Fields{
|
|
|
|
"rawMessage": string(body),
|
|
|
|
"err": err,
|
|
|
|
}).Error("Unable to unmarshal the block root")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.Data.Root, nil
|
|
|
|
}
|