2022-05-18 16:12:54 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-04-27 18:01:59 +00:00
|
|
|
// This file will contain functions to query the Beacon Chain Server.
|
|
|
|
|
|
|
|
package beaconclient
|
|
|
|
|
|
|
|
import (
|
2022-09-29 01:39:56 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2022-04-27 18:01:59 +00:00
|
|
|
"fmt"
|
2022-09-29 01:39:56 +00:00
|
|
|
"io"
|
2022-04-27 18:01:59 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-09 21:32:46 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/loghelper"
|
2022-04-27 18:01:59 +00:00
|
|
|
)
|
|
|
|
|
2022-05-13 12:48:31 +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"`
|
|
|
|
}
|
|
|
|
|
2022-09-29 01:39:56 +00:00
|
|
|
// A helper function to query endpoints that utilize slots.
|
|
|
|
func querySsz(endpoint string, slot Slot) ([]byte, int, error) {
|
2022-05-13 12:48:31 +00:00
|
|
|
log.WithFields(log.Fields{"endpoint": endpoint}).Debug("Querying endpoint")
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
|
|
|
if err != nil {
|
2022-09-29 01:39:56 +00:00
|
|
|
loghelper.LogSlotError(slot.Number(), err).Error("Unable to create a request!")
|
|
|
|
return nil, 0, fmt.Errorf("Unable to create a request!: %s", err.Error())
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
2022-09-29 01:39:56 +00:00
|
|
|
req.Header.Set("Accept", "application/octet-stream")
|
2022-05-13 12:48:31 +00:00
|
|
|
response, err := client.Do(req)
|
|
|
|
if err != nil {
|
2022-09-29 01:39:56 +00:00
|
|
|
loghelper.LogSlotError(slot.Number(), err).Error("Unable to query Beacon Node!")
|
|
|
|
return nil, 0, fmt.Errorf("Unable to query Beacon Node: %s", err.Error())
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
2022-09-29 01:39:56 +00:00
|
|
|
|
|
|
|
rc := response.StatusCode
|
|
|
|
// Any 2xx code is OK.
|
|
|
|
if rc < 200 || rc >= 300 {
|
|
|
|
return nil, rc, fmt.Errorf("HTTP Error: %d", rc)
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 01:39:56 +00:00
|
|
|
var body bytes.Buffer
|
|
|
|
buf := bufio.NewWriter(&body)
|
|
|
|
_, err = io.Copy(buf, response.Body)
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogSlotError(slot.Number(), err).Error("Unable to turn response into a []bytes array!")
|
|
|
|
return nil, rc, fmt.Errorf("Unable to turn response into a []bytes array!: %s", err.Error())
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
2022-09-29 01:39:56 +00:00
|
|
|
|
|
|
|
return body.Bytes(), rc, nil
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|