From c7b8b72463b813f7eb20ba18143a3ba26e42e830 Mon Sep 17 00:00:00 2001 From: maskpp Date: Fri, 19 Jul 2024 07:54:27 +0800 Subject: [PATCH] fix(guardian-prover-health-check): upgrades relate Database operation (#17752) Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com> --- packages/guardian-prover-health-check/db.go | 28 -------------- .../guardian-prover-health-check/db/db.go | 37 +++++++++++++------ .../healthcheck.go | 2 +- .../healthchecker/config.go | 17 +++------ .../healthchecker/config_test.go | 3 +- .../healthchecker/healthchecker.go | 12 +++++- ..._checks_by_guardian_prover_address_test.go | 4 +- .../http/get_health_checks_test.go | 4 +- ...signed_block_by_guardian_prover_address.go | 1 + ...d_block_by_guardian_prover_address_test.go | 17 +++++---- ...startup_by_guardian_prover_address_test.go | 3 +- .../http/get_signed_blocks.go | 1 + ...tartups_by_guardian_prover_address_test.go | 3 +- .../http/post_health_check.go | 3 +- .../http/post_signed_block.go | 16 ++++---- .../http/post_startup.go | 3 +- .../guardian-prover-health-check/mock/db.go | 2 + .../mock/health_check_repo.go | 2 +- .../mock/signed_block_repo.go | 5 ++- .../mock/startup_repo.go | 2 +- .../repo/containers_test.go | 2 +- .../guardian-prover-health-check/repo/db.go | 17 --------- .../repo/healthcheck.go | 34 +++++++++-------- .../repo/healthcheck_test.go | 15 ++++---- .../repo/signed_block.go | 34 +++++++++-------- .../repo/signed_block_test.go | 12 +++--- .../repo/startup.go | 26 +++++++------ .../repo/startup_test.go | 15 ++++---- .../signed_block.go | 7 ++-- .../guardian-prover-health-check/startup.go | 2 +- 30 files changed, 169 insertions(+), 160 deletions(-) delete mode 100644 packages/guardian-prover-health-check/db.go delete mode 100644 packages/guardian-prover-health-check/repo/db.go diff --git a/packages/guardian-prover-health-check/db.go b/packages/guardian-prover-health-check/db.go deleted file mode 100644 index 75263f6edaa..00000000000 --- a/packages/guardian-prover-health-check/db.go +++ /dev/null @@ -1,28 +0,0 @@ -package guardianproverhealthcheck - -import ( - "database/sql" - - "github.com/cyberhorsey/errors" - "gorm.io/gorm" -) - -var ( - ErrNoDB = errors.Validation.NewWithKeyAndDetail("ERR_NO_DB", "DB is required") -) - -type DBConnectionOpts struct { - Name string - Password string - Host string - Database string - MaxIdleConns uint64 - MaxOpenConns uint64 - MaxConnLifetime uint64 - OpenFunc func(dsn string) (DB, error) -} - -type DB interface { - DB() (*sql.DB, error) - GormDB() *gorm.DB -} diff --git a/packages/guardian-prover-health-check/db/db.go b/packages/guardian-prover-health-check/db/db.go index e2d19c61ca0..a4c258c431f 100644 --- a/packages/guardian-prover-health-check/db/db.go +++ b/packages/guardian-prover-health-check/db/db.go @@ -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 @@ -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( diff --git a/packages/guardian-prover-health-check/healthcheck.go b/packages/guardian-prover-health-check/healthcheck.go index 913a0f3c66b..fb40633d936 100644 --- a/packages/guardian-prover-health-check/healthcheck.go +++ b/packages/guardian-prover-health-check/healthcheck.go @@ -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) } diff --git a/packages/guardian-prover-health-check/healthchecker/config.go b/packages/guardian-prover-health-check/healthchecker/config.go index c16c2bbbdbf..30d80f85870 100644 --- a/packages/guardian-prover-health-check/healthchecker/config.go +++ b/packages/guardian-prover-health-check/healthchecker/config.go @@ -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 @@ -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. @@ -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), @@ -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), }) diff --git a/packages/guardian-prover-health-check/healthchecker/config_test.go b/packages/guardian-prover-health-check/healthchecker/config_test.go index b7455dcfd4e..faef03712ae 100644 --- a/packages/guardian-prover-health-check/healthchecker/config_test.go +++ b/packages/guardian-prover-health-check/healthchecker/config_test.go @@ -1,6 +1,7 @@ package healthchecker import ( + "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db" "testing" "github.com/stretchr/testify/assert" @@ -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 } diff --git a/packages/guardian-prover-health-check/healthchecker/healthchecker.go b/packages/guardian-prover-health-check/healthchecker/healthchecker.go index 7c96144db0f..8f3f5827065 100644 --- a/packages/guardian-prover-health-check/healthchecker/healthchecker.go +++ b/packages/guardian-prover-health-check/healthchecker/healthchecker.go @@ -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 @@ -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 { @@ -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 diff --git a/packages/guardian-prover-health-check/http/get_health_checks_by_guardian_prover_address_test.go b/packages/guardian-prover-health-check/http/get_health_checks_by_guardian_prover_address_test.go index 4ef5151700e..4822be86b01 100644 --- a/packages/guardian-prover-health-check/http/get_health_checks_by_guardian_prover_address_test.go +++ b/packages/guardian-prover-health-check/http/get_health_checks_by_guardian_prover_address_test.go @@ -1,6 +1,7 @@ package http import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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", diff --git a/packages/guardian-prover-health-check/http/get_health_checks_test.go b/packages/guardian-prover-health-check/http/get_health_checks_test.go index 0fdf5ea3a4b..39e377a8dbe 100644 --- a/packages/guardian-prover-health-check/http/get_health_checks_test.go +++ b/packages/guardian-prover-health-check/http/get_health_checks_test.go @@ -1,6 +1,7 @@ package http import ( + "context" "net/http" "net/http/httptest" "testing" @@ -8,13 +9,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_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", diff --git a/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address.go b/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address.go index f6a0373c946..0f2a3a4de55 100644 --- a/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address.go +++ b/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address.go @@ -26,6 +26,7 @@ func (srv *Server) GetMostRecentSignedBlockByGuardianProverAddress(c echo.Contex } signedBlock, err := srv.signedBlockRepo.GetMostRecentByGuardianProverAddress( + c.Request().Context(), address, ) diff --git a/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address_test.go b/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address_test.go index 779b959b862..f793c7ba0ee 100644 --- a/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address_test.go +++ b/packages/guardian-prover-health-check/http/get_most_recent_signed_block_by_guardian_prover_address_test.go @@ -1,6 +1,7 @@ package http import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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) } diff --git a/packages/guardian-prover-health-check/http/get_most_recent_startup_by_guardian_prover_address_test.go b/packages/guardian-prover-health-check/http/get_most_recent_startup_by_guardian_prover_address_test.go index a3a48b793b1..e2556c27fce 100644 --- a/packages/guardian-prover-health-check/http/get_most_recent_startup_by_guardian_prover_address_test.go +++ b/packages/guardian-prover-health-check/http/get_most_recent_startup_by_guardian_prover_address_test.go @@ -1,6 +1,7 @@ package http import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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", diff --git a/packages/guardian-prover-health-check/http/get_signed_blocks.go b/packages/guardian-prover-health-check/http/get_signed_blocks.go index 64112f6234b..be0c6ac0950 100644 --- a/packages/guardian-prover-health-check/http/get_signed_blocks.go +++ b/packages/guardian-prover-health-check/http/get_signed_blocks.go @@ -82,6 +82,7 @@ func (srv *Server) GetSignedBlocks(c echo.Context) error { } signedBlocks, err := srv.signedBlockRepo.GetByStartingBlockID( + c.Request().Context(), guardianproverhealthcheck.GetSignedBlocksByStartingBlockIDOpts{ StartingBlockID: start, }, diff --git a/packages/guardian-prover-health-check/http/get_startups_by_guardian_prover_address_test.go b/packages/guardian-prover-health-check/http/get_startups_by_guardian_prover_address_test.go index cdb86b4c230..a483ad94a7e 100644 --- a/packages/guardian-prover-health-check/http/get_startups_by_guardian_prover_address_test.go +++ b/packages/guardian-prover-health-check/http/get_startups_by_guardian_prover_address_test.go @@ -1,6 +1,7 @@ package http import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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", diff --git a/packages/guardian-prover-health-check/http/post_health_check.go b/packages/guardian-prover-health-check/http/post_health_check.go index 860428007df..a89f7614c43 100644 --- a/packages/guardian-prover-health-check/http/post_health_check.go +++ b/packages/guardian-prover-health-check/http/post_health_check.go @@ -1,6 +1,7 @@ package http import ( + "context" "log/slog" "net/http" @@ -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(), diff --git a/packages/guardian-prover-health-check/http/post_signed_block.go b/packages/guardian-prover-health-check/http/post_signed_block.go index a7898d6eb28..e39adc331cf 100644 --- a/packages/guardian-prover-health-check/http/post_signed_block.go +++ b/packages/guardian-prover-health-check/http/post_signed_block.go @@ -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") { diff --git a/packages/guardian-prover-health-check/http/post_startup.go b/packages/guardian-prover-health-check/http/post_startup.go index c08785144c4..9c9a0d72de5 100644 --- a/packages/guardian-prover-health-check/http/post_startup.go +++ b/packages/guardian-prover-health-check/http/post_startup.go @@ -1,6 +1,7 @@ package http import ( + "context" "log/slog" "net/http" @@ -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, diff --git a/packages/guardian-prover-health-check/mock/db.go b/packages/guardian-prover-health-check/mock/db.go index 00d2111641a..32a3477a5dd 100644 --- a/packages/guardian-prover-health-check/mock/db.go +++ b/packages/guardian-prover-health-check/mock/db.go @@ -16,3 +16,5 @@ func (db *DB) DB() (*sql.DB, error) { func (db *DB) GormDB() *gorm.DB { return &gorm.DB{} } + +func (db *DB) Close() error { return nil } diff --git a/packages/guardian-prover-health-check/mock/health_check_repo.go b/packages/guardian-prover-health-check/mock/health_check_repo.go index 3bd51186260..f6aca3aa853 100644 --- a/packages/guardian-prover-health-check/mock/health_check_repo.go +++ b/packages/guardian-prover-health-check/mock/health_check_repo.go @@ -44,7 +44,7 @@ func (h *HealthCheckRepo) Get( }, nil } -func (h *HealthCheckRepo) Save(opts guardianproverhealthcheck.SaveHealthCheckOpts) error { +func (h *HealthCheckRepo) Save(ctx context.Context, opts *guardianproverhealthcheck.SaveHealthCheckOpts) error { h.healthChecks = append(h.healthChecks, &guardianproverhealthcheck.HealthCheck{ GuardianProverID: opts.GuardianProverID, Alive: opts.Alive, diff --git a/packages/guardian-prover-health-check/mock/signed_block_repo.go b/packages/guardian-prover-health-check/mock/signed_block_repo.go index 1733db41a8c..f89d800ad62 100644 --- a/packages/guardian-prover-health-check/mock/signed_block_repo.go +++ b/packages/guardian-prover-health-check/mock/signed_block_repo.go @@ -1,6 +1,7 @@ package mock import ( + "context" "errors" guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" @@ -16,7 +17,7 @@ func NewSignedBlockRepository() *SignedBlockRepo { } } -func (r *SignedBlockRepo) Save(opts guardianproverhealthcheck.SaveSignedBlockOpts) error { +func (r *SignedBlockRepo) Save(ctx context.Context, opts *guardianproverhealthcheck.SaveSignedBlockOpts) error { r.signedBlocks = append(r.signedBlocks, &guardianproverhealthcheck.SignedBlock{ GuardianProverID: opts.GuardianProverID, BlockID: opts.BlockID, @@ -30,6 +31,7 @@ func (r *SignedBlockRepo) Save(opts guardianproverhealthcheck.SaveSignedBlockOpt } func (r *SignedBlockRepo) GetByStartingBlockID( + ctx context.Context, opts guardianproverhealthcheck.GetSignedBlocksByStartingBlockIDOpts, ) ([]*guardianproverhealthcheck.SignedBlock, error) { sb := make([]*guardianproverhealthcheck.SignedBlock, 0) @@ -44,6 +46,7 @@ func (r *SignedBlockRepo) GetByStartingBlockID( } func (r *SignedBlockRepo) GetMostRecentByGuardianProverAddress( + ctx context.Context, address string, ) (*guardianproverhealthcheck.SignedBlock, error) { var b *guardianproverhealthcheck.SignedBlock diff --git a/packages/guardian-prover-health-check/mock/startup_repo.go b/packages/guardian-prover-health-check/mock/startup_repo.go index 98115b77c20..8091779772d 100644 --- a/packages/guardian-prover-health-check/mock/startup_repo.go +++ b/packages/guardian-prover-health-check/mock/startup_repo.go @@ -51,7 +51,7 @@ func (r *StartupRepo) GetMostRecentByGuardianProverAddress( return s, nil } -func (h *StartupRepo) Save(opts guardianproverhealthcheck.SaveStartupOpts) error { +func (h *StartupRepo) Save(ctx context.Context, opts *guardianproverhealthcheck.SaveStartupOpts) error { h.startups = append(h.startups, &guardianproverhealthcheck.Startup{ GuardianProverID: opts.GuardianProverID, GuardianProverAddress: opts.GuardianProverAddress, diff --git a/packages/guardian-prover-health-check/repo/containers_test.go b/packages/guardian-prover-health-check/repo/containers_test.go index 935d18fa92c..4e86abe17a4 100644 --- a/packages/guardian-prover-health-check/repo/containers_test.go +++ b/packages/guardian-prover-health-check/repo/containers_test.go @@ -20,7 +20,7 @@ var ( dbPassword = "password" ) -func testMysql(t *testing.T) (DB, func(), error) { +func testMysql(t *testing.T) (db.DB, func(), error) { req := testcontainers.ContainerRequest{ Image: "mysql:latest", ExposedPorts: []string{"3306/tcp", "33060/tcp"}, diff --git a/packages/guardian-prover-health-check/repo/db.go b/packages/guardian-prover-health-check/repo/db.go deleted file mode 100644 index 1c8a35dbca3..00000000000 --- a/packages/guardian-prover-health-check/repo/db.go +++ /dev/null @@ -1,17 +0,0 @@ -package repo - -import ( - "database/sql" - - "github.com/cyberhorsey/errors" - "gorm.io/gorm" -) - -var ( - ErrNoDB = errors.Validation.NewWithKeyAndDetail("ERR_NO_DB", "no db") -) - -type DB interface { - DB() (*sql.DB, error) - GormDB() *gorm.DB -} diff --git a/packages/guardian-prover-health-check/repo/healthcheck.go b/packages/guardian-prover-health-check/repo/healthcheck.go index c8bb184a4d4..a4cdab5bc85 100644 --- a/packages/guardian-prover-health-check/repo/healthcheck.go +++ b/packages/guardian-prover-health-check/repo/healthcheck.go @@ -5,8 +5,10 @@ import ( "net/http" "github.com/morkid/paginate" - guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" "gorm.io/gorm" + + guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" + "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db" ) var ( @@ -14,21 +16,21 @@ var ( ) type HealthCheckRepository struct { - db DB + db db.DB } -func NewHealthCheckRepository(db DB) (*HealthCheckRepository, error) { - if db == nil { - return nil, ErrNoDB +func NewHealthCheckRepository(dbHandler db.DB) (*HealthCheckRepository, error) { + if dbHandler == nil { + return nil, db.ErrNoDB } return &HealthCheckRepository{ - db: db, + db: dbHandler, }, nil } -func (r *HealthCheckRepository) startQuery() *gorm.DB { - return r.db.GormDB().Table("health_checks") +func (r *HealthCheckRepository) startQuery(ctx context.Context) *gorm.DB { + return r.db.GormDB().WithContext(ctx).Table("health_checks") } func (r *HealthCheckRepository) Get( @@ -39,7 +41,7 @@ func (r *HealthCheckRepository) Get( DefaultSize: 100, }) - reqCtx := pg.With(r.startQuery()) + reqCtx := pg.With(r.startQuery(ctx)) page := reqCtx.Request(req).Response(&[]guardianproverhealthcheck.HealthCheck{}) @@ -55,7 +57,7 @@ func (r *HealthCheckRepository) GetByGuardianProverAddress( DefaultSize: 100, }) - reqCtx := pg.With(r.startQuery().Order("created_at desc"). + reqCtx := pg.With(r.startQuery(ctx).Order("created_at desc"). Where("recovered_address = ?", address)) page := reqCtx.Request(req).Response(&[]guardianproverhealthcheck.HealthCheck{}) @@ -70,7 +72,7 @@ func (r *HealthCheckRepository) GetMostRecentByGuardianProverAddress( ) (*guardianproverhealthcheck.HealthCheck, error) { hc := &guardianproverhealthcheck.HealthCheck{} - if err := r.startQuery().Order("created_at desc"). + if err := r.startQuery(ctx).Order("created_at desc"). Where("recovered_address = ?", address).Limit(1). Scan(hc).Error; err != nil { return nil, err @@ -79,7 +81,7 @@ func (r *HealthCheckRepository) GetMostRecentByGuardianProverAddress( return hc, nil } -func (r *HealthCheckRepository) Save(opts guardianproverhealthcheck.SaveHealthCheckOpts) error { +func (r *HealthCheckRepository) Save(ctx context.Context, opts *guardianproverhealthcheck.SaveHealthCheckOpts) error { b := &guardianproverhealthcheck.HealthCheck{ Alive: opts.Alive, ExpectedAddress: opts.ExpectedAddress, @@ -89,7 +91,7 @@ func (r *HealthCheckRepository) Save(opts guardianproverhealthcheck.SaveHealthCh LatestL1Block: opts.LatestL1Block, LatestL2Block: opts.LatestL2Block, } - if err := r.startQuery().Create(b).Error; err != nil { + if err := r.startQuery(ctx).Create(b).Error; err != nil { return err } @@ -102,12 +104,12 @@ func (r *HealthCheckRepository) GetUptimeByGuardianProverAddress( ) (float64, int, error) { var count int64 - var query string = `SELECT COUNT(*) - FROM health_checks + var query string = `SELECT COUNT(*) + FROM health_checks WHERE recovered_address = ? AND created_at > NOW() - INTERVAL 1 DAY` - if err := r.db.GormDB().Raw(query, address).Scan(&count).Error; err != nil { + if err := r.db.GormDB().WithContext(ctx).Raw(query, address).Scan(&count).Error; err != nil { return 0, 0, err } diff --git a/packages/guardian-prover-health-check/repo/healthcheck_test.go b/packages/guardian-prover-health-check/repo/healthcheck_test.go index 6b84e2a2663..fff747e3217 100644 --- a/packages/guardian-prover-health-check/repo/healthcheck_test.go +++ b/packages/guardian-prover-health-check/repo/healthcheck_test.go @@ -5,26 +5,27 @@ import ( "net/http" "testing" + "gopkg.in/go-playground/assert.v1" + guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db" - "gopkg.in/go-playground/assert.v1" ) func Test_NewHealthCheckRepo(t *testing.T) { tests := []struct { name string - db DB + db db.DB wantErr error }{ { "success", - &db.DB{}, + &db.Database{}, nil, }, { "noDb", nil, - ErrNoDB, + db.ErrNoDB, }, } @@ -66,7 +67,7 @@ func TestIntegration_HealthCheck_Save(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err = healthCheckRepo.Save(tt.opts) + err = healthCheckRepo.Save(context.Background(), &tt.opts) assert.Equal(t, tt.wantErr, err) req, err := http.NewRequest(http.MethodGet, "/healtcheck", nil) @@ -90,7 +91,7 @@ func TestIntegration_HealthCheck_UptimeByGuardianProverId(t *testing.T) { assert.Equal(t, nil, err) - err = healthCheckRepo.Save(guardianproverhealthcheck.SaveHealthCheckOpts{ + err = healthCheckRepo.Save(context.Background(), &guardianproverhealthcheck.SaveHealthCheckOpts{ GuardianProverID: 1, Alive: true, ExpectedAddress: "0x123", @@ -102,7 +103,7 @@ func TestIntegration_HealthCheck_UptimeByGuardianProverId(t *testing.T) { assert.Equal(t, err, nil) - err = healthCheckRepo.Save(guardianproverhealthcheck.SaveHealthCheckOpts{ + err = healthCheckRepo.Save(context.Background(), &guardianproverhealthcheck.SaveHealthCheckOpts{ GuardianProverID: 1, Alive: true, ExpectedAddress: "0x123", diff --git a/packages/guardian-prover-health-check/repo/signed_block.go b/packages/guardian-prover-health-check/repo/signed_block.go index c5be413ee72..97e0b4ebaf6 100644 --- a/packages/guardian-prover-health-check/repo/signed_block.go +++ b/packages/guardian-prover-health-check/repo/signed_block.go @@ -1,29 +1,32 @@ package repo import ( - guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" + "context" "gorm.io/gorm" + + guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" + "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db" ) type SignedBlockRepository struct { - db DB + db db.DB } -func NewSignedBlockRepository(db DB) (*SignedBlockRepository, error) { - if db == nil { - return nil, ErrNoDB +func NewSignedBlockRepository(dbHandler db.DB) (*SignedBlockRepository, error) { + if dbHandler == nil { + return nil, db.ErrNoDB } return &SignedBlockRepository{ - db: db, + db: dbHandler, }, nil } -func (r *SignedBlockRepository) startQuery() *gorm.DB { - return r.db.GormDB().Table("signed_blocks") +func (r *SignedBlockRepository) startQuery(ctx context.Context) *gorm.DB { + return r.db.GormDB().WithContext(ctx).Table("signed_blocks") } -func (r *SignedBlockRepository) Save(opts guardianproverhealthcheck.SaveSignedBlockOpts) error { +func (r *SignedBlockRepository) Save(ctx context.Context, opts *guardianproverhealthcheck.SaveSignedBlockOpts) error { b := &guardianproverhealthcheck.SignedBlock{ GuardianProverID: opts.GuardianProverID, BlockID: opts.BlockID, @@ -31,7 +34,7 @@ func (r *SignedBlockRepository) Save(opts guardianproverhealthcheck.SaveSignedBl RecoveredAddress: opts.RecoveredAddress, Signature: opts.Signature, } - if err := r.startQuery().Create(b).Error; err != nil { + if err := r.startQuery(ctx).Create(b).Error; err != nil { return err } @@ -39,31 +42,32 @@ func (r *SignedBlockRepository) Save(opts guardianproverhealthcheck.SaveSignedBl } func (r *SignedBlockRepository) GetByStartingBlockID( + ctx context.Context, opts guardianproverhealthcheck.GetSignedBlocksByStartingBlockIDOpts, ) ([]*guardianproverhealthcheck.SignedBlock, error) { var sb []*guardianproverhealthcheck.SignedBlock - if err := r.startQuery().Where("block_id >= ?", opts.StartingBlockID).Find(&sb).Error; err != nil { + if err := r.startQuery(ctx).Where("block_id >= ?", opts.StartingBlockID).Find(&sb).Error; err != nil { return nil, err } return sb, nil } -func (r *SignedBlockRepository) GetMostRecentByGuardianProverAddress(address string) ( +func (r *SignedBlockRepository) GetMostRecentByGuardianProverAddress(ctx context.Context, address string) ( *guardianproverhealthcheck.SignedBlock, error) { q := `SELECT * FROM signed_blocks WHERE block_id = ( - SELECT MAX(block_id) - FROM signed_blocks + SELECT MAX(block_id) + FROM signed_blocks WHERE recovered_address = ? ) AND recovered_address = ?;` var b *guardianproverhealthcheck.SignedBlock - if err := r.startQuery().Raw(q, address, address).Scan(&b).Error; err != nil { + if err := r.startQuery(ctx).Raw(q, address, address).Scan(&b).Error; err != nil { return nil, err } diff --git a/packages/guardian-prover-health-check/repo/signed_block_test.go b/packages/guardian-prover-health-check/repo/signed_block_test.go index 2c94c50adc4..781699dda5c 100644 --- a/packages/guardian-prover-health-check/repo/signed_block_test.go +++ b/packages/guardian-prover-health-check/repo/signed_block_test.go @@ -1,28 +1,30 @@ package repo import ( + "context" "testing" + "gopkg.in/go-playground/assert.v1" + guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db" - "gopkg.in/go-playground/assert.v1" ) func Test_NewSignedBlockRepo(t *testing.T) { tests := []struct { name string - db DB + db db.DB wantErr error }{ { "success", - &db.DB{}, + &db.Database{}, nil, }, { "noDb", nil, - ErrNoDB, + db.ErrNoDB, }, } @@ -62,7 +64,7 @@ func TestIntegration_SignedBlock_Save(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err = SignedBlockRepo.Save(tt.opts) + err = SignedBlockRepo.Save(context.Background(), &tt.opts) assert.Equal(t, tt.wantErr, err) }) } diff --git a/packages/guardian-prover-health-check/repo/startup.go b/packages/guardian-prover-health-check/repo/startup.go index 576a2ca3842..b7560761a93 100644 --- a/packages/guardian-prover-health-check/repo/startup.go +++ b/packages/guardian-prover-health-check/repo/startup.go @@ -5,26 +5,28 @@ import ( "net/http" "github.com/morkid/paginate" - guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" "gorm.io/gorm" + + guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" + "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db" ) type StartupRepository struct { - db DB + db db.DB } -func NewStartupRepository(db DB) (*StartupRepository, error) { - if db == nil { - return nil, ErrNoDB +func NewStartupRepository(dbHandler db.DB) (*StartupRepository, error) { + if dbHandler == nil { + return nil, db.ErrNoDB } return &StartupRepository{ - db: db, + db: dbHandler, }, nil } -func (r *StartupRepository) startQuery() *gorm.DB { - return r.db.GormDB().Table("startups") +func (r *StartupRepository) startQuery(ctx context.Context) *gorm.DB { + return r.db.GormDB().WithContext(ctx).Table("startups") } func (r *StartupRepository) GetByGuardianProverAddress( @@ -36,7 +38,7 @@ func (r *StartupRepository) GetByGuardianProverAddress( DefaultSize: 100, }) - reqCtx := pg.With(r.startQuery().Order("created_at desc"). + reqCtx := pg.With(r.startQuery(ctx).Order("created_at desc"). Where("guardian_prover_address = ?", address)) page := reqCtx.Request(req).Response(&[]guardianproverhealthcheck.Startup{}) @@ -50,7 +52,7 @@ func (r *StartupRepository) GetMostRecentByGuardianProverAddress( ) (*guardianproverhealthcheck.Startup, error) { s := &guardianproverhealthcheck.Startup{} - if err := r.startQuery().Order("created_at desc"). + if err := r.startQuery(ctx).Order("created_at desc"). Where("guardian_prover_address = ?", address).Limit(1). Scan(s).Error; err != nil { return nil, err @@ -59,7 +61,7 @@ func (r *StartupRepository) GetMostRecentByGuardianProverAddress( return s, nil } -func (r *StartupRepository) Save(opts guardianproverhealthcheck.SaveStartupOpts) error { +func (r *StartupRepository) Save(ctx context.Context, opts *guardianproverhealthcheck.SaveStartupOpts) error { b := &guardianproverhealthcheck.Startup{ GuardianProverAddress: opts.GuardianProverAddress, GuardianProverID: opts.GuardianProverID, @@ -68,7 +70,7 @@ func (r *StartupRepository) Save(opts guardianproverhealthcheck.SaveStartupOpts) L1NodeVersion: opts.L1NodeVersion, L2NodeVersion: opts.L2NodeVersion, } - if err := r.startQuery().Create(b).Error; err != nil { + if err := r.startQuery(ctx).Create(b).Error; err != nil { return err } diff --git a/packages/guardian-prover-health-check/repo/startup_test.go b/packages/guardian-prover-health-check/repo/startup_test.go index 7b83a842bbe..4c4bfae33bb 100644 --- a/packages/guardian-prover-health-check/repo/startup_test.go +++ b/packages/guardian-prover-health-check/repo/startup_test.go @@ -5,26 +5,27 @@ import ( "net/http" "testing" + "gopkg.in/go-playground/assert.v1" + guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check" "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check/db" - "gopkg.in/go-playground/assert.v1" ) func Test_NewStartupRepo(t *testing.T) { tests := []struct { name string - db DB + db db.DB wantErr error }{ { "success", - &db.DB{}, + &db.Database{}, nil, }, { "noDb", nil, - ErrNoDB, + db.ErrNoDB, }, } @@ -63,7 +64,7 @@ func TestIntegration_Startup_Save(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err = startupRepo.Save(tt.opts) + err = startupRepo.Save(context.Background(), &tt.opts) assert.Equal(t, tt.wantErr, err) }) } @@ -78,7 +79,7 @@ func TestIntegration_Startup_GetByGuardianProverID(t *testing.T) { startupRepo, err := NewStartupRepository(db) assert.Equal(t, nil, err) - err = startupRepo.Save(guardianproverhealthcheck.SaveStartupOpts{ + err = startupRepo.Save(context.Background(), &guardianproverhealthcheck.SaveStartupOpts{ GuardianProverID: 1, GuardianProverAddress: "0x123", Revision: "asdf", @@ -89,7 +90,7 @@ func TestIntegration_Startup_GetByGuardianProverID(t *testing.T) { assert.Equal(t, nil, err) - err = startupRepo.Save(guardianproverhealthcheck.SaveStartupOpts{ + err = startupRepo.Save(context.Background(), &guardianproverhealthcheck.SaveStartupOpts{ GuardianProverID: 1, GuardianProverAddress: "0x123", Revision: "zxxc", diff --git a/packages/guardian-prover-health-check/signed_block.go b/packages/guardian-prover-health-check/signed_block.go index 3f040e74114..95eded73c1d 100644 --- a/packages/guardian-prover-health-check/signed_block.go +++ b/packages/guardian-prover-health-check/signed_block.go @@ -1,6 +1,7 @@ package guardianproverhealthcheck import ( + "context" "time" ) @@ -28,7 +29,7 @@ type GetSignedBlocksByStartingBlockIDOpts struct { // SignedBlockRepository defines database interaction methods to create and get // signed blocks submitted by guardian provers. type SignedBlockRepository interface { - Save(opts SaveSignedBlockOpts) error - GetByStartingBlockID(opts GetSignedBlocksByStartingBlockIDOpts) ([]*SignedBlock, error) - GetMostRecentByGuardianProverAddress(address string) (*SignedBlock, error) + Save(ctx context.Context, opts *SaveSignedBlockOpts) error + GetByStartingBlockID(ctx context.Context, opts GetSignedBlocksByStartingBlockIDOpts) ([]*SignedBlock, error) + GetMostRecentByGuardianProverAddress(ctx context.Context, address string) (*SignedBlock, error) } diff --git a/packages/guardian-prover-health-check/startup.go b/packages/guardian-prover-health-check/startup.go index 30e7db03377..831398421e1 100644 --- a/packages/guardian-prover-health-check/startup.go +++ b/packages/guardian-prover-health-check/startup.go @@ -45,5 +45,5 @@ type StartupRepository interface { ctx context.Context, address string, ) (*Startup, error) - Save(opts SaveStartupOpts) error + Save(ctx context.Context, opts *SaveStartupOpts) error }