diff --git a/application_component.md b/application_component.md index 0dbc67d..cfd9e4b 100644 --- a/application_component.md +++ b/application_component.md @@ -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. diff --git a/internal/boot/boot.go b/internal/boot/boot.go index 46e3b02..86bd891 100644 --- a/internal/boot/boot.go +++ b/internal/boot/boot.go @@ -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{} + 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 } diff --git a/pkg/beaconclient/healthcheck/healthcheck.go b/pkg/beaconclient/healthcheck/healthcheck.go new file mode 100644 index 0000000..2481bfc --- /dev/null +++ b/pkg/beaconclient/healthcheck/healthcheck.go @@ -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 + +} diff --git a/pkg/beaconclient/healthcheck/healthcheck_suite_test.go b/pkg/beaconclient/healthcheck/healthcheck_suite_test.go new file mode 100644 index 0000000..510e24f --- /dev/null +++ b/pkg/beaconclient/healthcheck/healthcheck_suite_test.go @@ -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") +} diff --git a/pkg/beaconclient/healthcheck/healthcheck_test.go b/pkg/beaconclient/healthcheck/healthcheck_test.go new file mode 100644 index 0000000..f6f406a --- /dev/null +++ b/pkg/beaconclient/healthcheck/healthcheck_test.go @@ -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()) + }) + }) + }) +})