Interact with Beacon Node endpoints #24
@ -10,6 +10,18 @@ This document will go through various application components
|
||||
|
||||
# Components
|
||||
|
||||
## Boot
|
||||
## `internal/boot`
|
||||
|
||||
The boot package in `internal` is utilized to start the application. Everything in the boot process must complete successfully for the application to start. If it does not, the application will not start.
|
||||
|
||||
## `pkg/database`
|
||||
|
||||
The `database` package allows us to interact with a postgres DB. We utilize the interface to ensure we can interact with any `sql` database as well. I copied most of the code here from `vulcanize/go-ethereum`. Down the road, internal teams should be able to reference the package instead of copy pasting it and re-implementing it.
|
||||
|
||||
## `pkg/beaconclient`
|
||||
|
||||
This package will contain code to interact with the beacon client.
|
||||
|
||||
## `pkg/version`
|
||||
|
||||
A generic package which can be utilized to easily version our applications.
|
||||
|
@ -1,48 +1,21 @@
|
||||
package boot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient/healthcheck"
|
||||
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
|
||||
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql/postgres"
|
||||
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
|
||||
)
|
||||
|
||||
var (
|
||||
bcHealthEndpoint = "/eth/v1/node/health"
|
||||
maxRetry = 5 // Max times to try to connect to the DB or BC at boot.
|
||||
retryInterval = 30 // The time to wait between each try.
|
||||
DB sql.Database = &postgres.DB{}
|
||||
)
|
||||
|
||||
// This function will ensure that we can connect to the beacon client.
|
||||
// Keep in mind, the beacon client will allow you to connect to it but it might
|
||||
// Not allow you to make http requests. This is part of its built in logic, and you will have
|
||||
// to follow their provided guidelines. https://lighthouse-book.sigmaprime.io/api-bn.html#security
|
||||
func checkBeaconClient(bcAddress string, bcPort int) error {
|
||||
log.Debug("Attempting to connect to the beacon client")
|
||||
bcEndpoint := "http://" + bcAddress + ":" + strconv.Itoa(bcPort) + bcHealthEndpoint
|
||||
resp, err := http.Get(bcEndpoint)
|
||||
if err != nil {
|
||||
loghelper.LogError(err).Error("Unable to get bc endpoint: ", bcEndpoint)
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
log.Error("We recieved a non 2xx status code when checking the health of the beacon node.")
|
||||
log.Error("Health Endpoint Status Code: ", resp.StatusCode)
|
||||
return fmt.Errorf("beacon Node Provided a non 2xx status code, code provided: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
log.Info("We can successfully reach the beacon client.")
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// A simple wrapper to create a DB object to use.
|
||||
func SetupPostgresDb(dbHostname string, dbPort int, dbName string, dbUsername string, dbPassword string, driverName string) (sql.Database, error) {
|
||||
log.Debug("Resolving Driver Type")
|
||||
@ -85,7 +58,7 @@ func BootApplication(dbHostname string, dbPort int, dbName string, dbUsername st
|
||||
log.Info("Booting the Application")
|
||||
|
||||
log.Debug("Checking beacon Client")
|
||||
err := checkBeaconClient(bcAddress, bcPort)
|
||||
err := healthcheck.CheckBeaconClient(bcAddress, bcPort)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
38
pkg/beaconclient/healthcheck/healthcheck.go
Normal file
38
pkg/beaconclient/healthcheck/healthcheck.go
Normal file
@ -0,0 +1,38 @@
|
||||
package healthcheck
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
|
||||
)
|
||||
|
||||
var (
|
||||
bcHealthEndpoint = "/eth/v1/node/health"
|
||||
)
|
||||
|
||||
// This function will ensure that we can connect to the beacon client.
|
||||
// Keep in mind, the beacon client will allow you to connect to it but it might
|
||||
// Not allow you to make http requests. This is part of its built in logic, and you will have
|
||||
// to follow their provided guidelines. https://lighthouse-book.sigmaprime.io/api-bn.html#security
|
||||
func CheckBeaconClient(bcAddress string, bcPort int) error {
|
||||
log.Debug("Attempting to connect to the beacon client")
|
||||
bcEndpoint := "http://" + bcAddress + ":" + strconv.Itoa(bcPort) + bcHealthEndpoint
|
||||
resp, err := http.Get(bcEndpoint)
|
||||
if err != nil {
|
||||
loghelper.LogError(err).Error("Unable to get bc endpoint: ", bcEndpoint)
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
log.Error("We recieved a non 2xx status code when checking the health of the beacon node.")
|
||||
log.Error("Health Endpoint Status Code: ", resp.StatusCode)
|
||||
return fmt.Errorf("beacon Node Provided a non 2xx status code, code provided: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
log.Info("We can successfully reach the beacon client.")
|
||||
return nil
|
||||
|
||||
}
|
13
pkg/beaconclient/healthcheck/healthcheck_suite_test.go
Normal file
13
pkg/beaconclient/healthcheck/healthcheck_suite_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package healthcheck_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestHealthcheck(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Healthcheck Suite")
|
||||
}
|
28
pkg/beaconclient/healthcheck/healthcheck_test.go
Normal file
28
pkg/beaconclient/healthcheck/healthcheck_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package healthcheck_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient/healthcheck"
|
||||
)
|
||||
|
||||
var _ = Describe("Healthcheck", func() {
|
||||
var (
|
||||
bcAddress string = "localhost"
|
||||
bcPort int = 5052
|
||||
)
|
||||
Describe("Connecting to the lighthouse client", Label("integration"), func() {
|
||||
Context("When the client is running", func() {
|
||||
It("We should connect successfully", func() {
|
||||
err := healthcheck.CheckBeaconClient(bcAddress, bcPort)
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
})
|
||||
Context("When the client is running", func() {
|
||||
It("We should connect successfully", func() {
|
||||
err := healthcheck.CheckBeaconClient("blah-blah", 10)
|
||||
Expect(err).ToNot(BeNil())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user