Skip to content

Commit

Permalink
fix(guardian-prover-health-check): upgrades relate Database operation (
Browse files Browse the repository at this point in the history
…#17752)

Co-authored-by: jeff <[email protected]>
  • Loading branch information
mask-pp and cyberhorsey authored Jul 18, 2024
1 parent 42331f4 commit c7b8b72
Show file tree
Hide file tree
Showing 30 changed files with 169 additions and 160 deletions.
28 changes: 0 additions & 28 deletions packages/guardian-prover-health-check/db.go

This file was deleted.

37 changes: 26 additions & 11 deletions packages/guardian-prover-health-check/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,42 @@ import (
"gorm.io/gorm"
)

type DB struct {
var (
ErrNoDB = errors.Validation.NewWithKeyAndDetail("ERR_NO_DB", "no db")
)

type DB interface {
DB() (*sql.DB, error)
GormDB() *gorm.DB
Close() error
}

type Database struct {
gormdb *gorm.DB
}

func (db *DB) DB() (*sql.DB, error) {
func (db *Database) DB() (*sql.DB, error) {
return db.gormdb.DB()
}

func (db *DB) GormDB() *gorm.DB {
func (db *Database) GormDB() *gorm.DB {
return db.gormdb
}

func New(gormdb *gorm.DB) *DB {
return &DB{
gormdb: gormdb,
func (db *Database) Close() error {
sqldb, err := db.gormdb.DB()
if err != nil {
return err
}

return sqldb.Close()
}

var (
ErrNoDB = errors.Validation.NewWithKeyAndDetail("ERR_NO_DB", "DB is required")
)
func New(gormdb *gorm.DB) *Database {
return &Database{
gormdb: gormdb,
}
}

type DBConnectionOpts struct {
Name string
Expand All @@ -39,10 +54,10 @@ type DBConnectionOpts struct {
MaxIdleConns uint64
MaxOpenConns uint64
MaxConnLifetime uint64
OpenFunc func(dsn string) (*DB, error)
OpenFunc func(dsn string) (*Database, error)
}

func OpenDBConnection(opts DBConnectionOpts) (*DB, error) {
func OpenDBConnection(opts DBConnectionOpts) (*Database, error) {
dsn := ""
if opts.Password == "" {
dsn = fmt.Sprintf(
Expand Down
2 changes: 1 addition & 1 deletion packages/guardian-prover-health-check/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ type HealthCheckRepository interface {
req *http.Request,
address string,
) (*HealthCheck, error)
Save(opts SaveHealthCheckOpts) error
Save(ctx context.Context, opts *SaveHealthCheckOpts) error
GetUptimeByGuardianProverAddress(ctx context.Context, address string) (float64, int, error)
}
17 changes: 6 additions & 11 deletions packages/guardian-prover-health-check/healthchecker/config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package healthchecker

import (
"database/sql"
"strings"

"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/cmd/flags"
"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db"
"github.com/urfave/cli/v2"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

type DB interface {
DB() (*sql.DB, error)
GormDB() *gorm.DB
}
"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/cmd/flags"
"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db"
)

type Config struct {
// db configs
Expand All @@ -32,7 +27,7 @@ type Config struct {
GuardianProverContractAddress string
L1RPCUrl string
L2RPCUrl string
OpenDBFunc func() (DB, error)
OpenDBFunc func() (db.DB, error)
}

// NewConfigFromCliContext creates a new config instance from command line flags.
Expand All @@ -50,7 +45,7 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
L1RPCUrl: c.String(flags.L1RPCUrl.Name),
L2RPCUrl: c.String(flags.L2RPCUrl.Name),
HTTPPort: c.Uint64(flags.HTTPPort.Name),
OpenDBFunc: func() (DB, error) {
OpenDBFunc: func() (db.DB, error) {
return db.OpenDBConnection(db.DBConnectionOpts{
Name: c.String(flags.DatabaseUsername.Name),
Password: c.String(flags.DatabasePassword.Name),
Expand All @@ -59,7 +54,7 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
MaxIdleConns: c.Uint64(flags.DatabaseMaxIdleConns.Name),
MaxOpenConns: c.Uint64(flags.DatabaseMaxOpenConns.Name),
MaxConnLifetime: c.Uint64(flags.DatabaseConnMaxLifetime.Name),
OpenFunc: func(dsn string) (*db.DB, error) {
OpenFunc: func(dsn string) (*db.Database, error) {
gormDB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package healthchecker

import (
"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestNewConfigFromCliContext(t *testing.T) {
assert.Equal(t, uint64(30), c.DatabaseMaxConnLifetime)
assert.Equal(t, uint64(1000), c.HTTPPort)

c.OpenDBFunc = func() (DB, error) {
c.OpenDBFunc = func() (db.DB, error) {
return &mock.DB{}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ import (
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/urfave/cli/v2"

guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check"
"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/bindings/guardianprover"
"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db"
hchttp "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/http"
"github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/repo"
"github.com/urfave/cli/v2"
)

type HealthChecker struct {
db db.DB

ctx context.Context
cancelCtx context.CancelFunc
healthCheckRepo guardianproverhealthcheck.HealthCheckRepository
Expand All @@ -42,6 +46,11 @@ func (h *HealthChecker) Close(ctx context.Context) {
if err := h.httpSrv.Shutdown(ctx); err != nil {
slog.Error("error encountered shutting down http server", "error", err)
}

// close db
if err := h.db.Close(); err != nil {
slog.Error("error encountered closing db", "error", err)
}
}

func (h *HealthChecker) InitFromCli(ctx context.Context, c *cli.Context) error {
Expand Down Expand Up @@ -143,6 +152,7 @@ func InitFromConfig(ctx context.Context, h *HealthChecker, cfg *Config) (err err
return err
}

h.db = db
h.guardianProvers = guardianProvers
h.numGuardians = numGuardians.Uint64()
h.healthCheckRepo = healthCheckRepo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -9,13 +10,14 @@ import (
"github.com/cyberhorsey/webutils/testutils"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"

guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check"
)

func Test_GetHealthChecksByGuardianProverID(t *testing.T) {
srv := newTestServer("")

err := srv.healthCheckRepo.Save(guardianproverhealthcheck.SaveHealthCheckOpts{
err := srv.healthCheckRepo.Save(context.Background(), &guardianproverhealthcheck.SaveHealthCheckOpts{
GuardianProverID: 1,
Alive: true,
ExpectedAddress: "0x123",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package http

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/cyberhorsey/webutils/testutils"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"

guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check"
)

func Test_GetHealthChecks(t *testing.T) {
srv := newTestServer("")

err := srv.healthCheckRepo.Save(guardianproverhealthcheck.SaveHealthCheckOpts{
err := srv.healthCheckRepo.Save(context.Background(), &guardianproverhealthcheck.SaveHealthCheckOpts{
GuardianProverID: 1,
Alive: true,
ExpectedAddress: "0x123",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (srv *Server) GetMostRecentSignedBlockByGuardianProverAddress(c echo.Contex
}

signedBlock, err := srv.signedBlockRepo.GetMostRecentByGuardianProverAddress(
c.Request().Context(),
address,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -16,13 +17,15 @@ func Test_GetMostRecentSignedBlockByGuardianProverAddress(t *testing.T) {
srv := newTestServer("")

for i := 0; i < 10; i++ {
err := srv.signedBlockRepo.Save(guardianproverhealthcheck.SaveSignedBlockOpts{
GuardianProverID: 1,
RecoveredAddress: "0x123",
BlockID: uint64(i),
BlockHash: "0x123",
Signature: "0x123",
})
err := srv.signedBlockRepo.Save(
context.Background(),
&guardianproverhealthcheck.SaveSignedBlockOpts{
GuardianProverID: 1,
RecoveredAddress: "0x123",
BlockID: uint64(i),
BlockHash: "0x123",
Signature: "0x123",
})

assert.Nil(t, err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -16,7 +17,7 @@ func Test_GetMostRecentStartupByGuardianProverAddress(t *testing.T) {
srv := newTestServer("")

for i := 0; i < 5; i++ {
err := srv.startupRepo.Save(guardianproverhealthcheck.SaveStartupOpts{
err := srv.startupRepo.Save(context.Background(), &guardianproverhealthcheck.SaveStartupOpts{
GuardianProverID: 1,
GuardianProverAddress: "0x123",
Revision: "asdf",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (srv *Server) GetSignedBlocks(c echo.Context) error {
}

signedBlocks, err := srv.signedBlockRepo.GetByStartingBlockID(
c.Request().Context(),
guardianproverhealthcheck.GetSignedBlocksByStartingBlockIDOpts{
StartingBlockID: start,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -15,7 +16,7 @@ import (
func Test_GetStartupsByGuardianProverAddress(t *testing.T) {
srv := newTestServer("")

err := srv.startupRepo.Save(guardianproverhealthcheck.SaveStartupOpts{
err := srv.startupRepo.Save(context.Background(), &guardianproverhealthcheck.SaveStartupOpts{
GuardianProverID: 1,
GuardianProverAddress: "0x123",
Revision: "asdf",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"log/slog"
"net/http"

Expand Down Expand Up @@ -62,7 +63,7 @@ func (srv *Server) PostHealthCheck(c echo.Context) error {
// expected address and recovered address will be the same until we have an auth
// mechanism which will allow us to store health checks that ecrecover to an unexpected
// address.
if err := srv.healthCheckRepo.Save(guardianproverhealthcheck.SaveHealthCheckOpts{
if err := srv.healthCheckRepo.Save(context.Background(), &guardianproverhealthcheck.SaveHealthCheckOpts{
GuardianProverID: recoveredGuardianProver.ID.Uint64(),
Alive: true,
ExpectedAddress: recoveredGuardianProver.Address.Hex(),
Expand Down
16 changes: 9 additions & 7 deletions packages/guardian-prover-health-check/http/post_signed_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ func (srv *Server) PostSignedBlock(c echo.Context) error {
}

// otherwise, we can store it in the database.
if err := srv.signedBlockRepo.Save(guardianproverhealthcheck.SaveSignedBlockOpts{
GuardianProverID: recoveredGuardianProver.ID.Uint64(),
BlockID: req.BlockID,
BlockHash: req.BlockHash,
Signature: req.Signature,
RecoveredAddress: recoveredGuardianProver.Address.Hex(),
}); err != nil {
if err := srv.signedBlockRepo.Save(
c.Request().Context(),
&guardianproverhealthcheck.SaveSignedBlockOpts{
GuardianProverID: recoveredGuardianProver.ID.Uint64(),
BlockID: req.BlockID,
BlockHash: req.BlockHash,
Signature: req.Signature,
RecoveredAddress: recoveredGuardianProver.Address.Hex(),
}); err != nil {
// if its a duplicate entry, we just return empty response with
// status 200 instead of an error.
if strings.Contains(err.Error(), "Duplicate entry") {
Expand Down
3 changes: 2 additions & 1 deletion packages/guardian-prover-health-check/http/post_startup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"log/slog"
"net/http"

Expand Down Expand Up @@ -61,7 +62,7 @@ func (srv *Server) PostStartup(c echo.Context) error {
// expected address and recovered address will be the same until we have an auth
// mechanism which will allow us to store health checks that ecrecover to an unexpected
// address.
if err := srv.startupRepo.Save(guardianproverhealthcheck.SaveStartupOpts{
if err := srv.startupRepo.Save(context.Background(), &guardianproverhealthcheck.SaveStartupOpts{
GuardianProverID: recoveredGuardianProver.ID.Uint64(),
GuardianVersion: req.GuardianVersion,
L1NodeVersion: req.L1NodeVersion,
Expand Down
Loading

0 comments on commit c7b8b72

Please sign in to comment.