diff --git a/packages/relayer/api/api.go b/packages/relayer/api/api.go index ab7b3c1b0e8..ebff088719b 100644 --- a/packages/relayer/api/api.go +++ b/packages/relayer/api/api.go @@ -2,7 +2,6 @@ package api import ( "context" - "database/sql" "fmt" "log/slog" nethttp "net/http" @@ -17,14 +16,8 @@ import ( "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/repo" "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/utils" "github.com/urfave/cli/v2" - "gorm.io/gorm" ) -type DB interface { - DB() (*sql.DB, error) - GormDB() *gorm.DB -} - type API struct { srv *http.Server httpPort uint64 diff --git a/packages/relayer/api/config.go b/packages/relayer/api/config.go index 9c25d25e5a5..22b320cecc3 100644 --- a/packages/relayer/api/config.go +++ b/packages/relayer/api/config.go @@ -28,7 +28,7 @@ type Config struct { ProcessingFeeMultiplier float64 DestTaikoAddress common.Address HTTPPort uint64 - OpenDBFunc func() (DB, error) + OpenDBFunc func() (db.DB, error) } // NewConfigFromCliContext creates a new config instance from command line flags. @@ -47,7 +47,7 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) { DestRPCUrl: c.String(flags.DestRPCUrl.Name), ProcessingFeeMultiplier: c.Float64(flags.ProcessingFeeMultiplier.Name), DestTaikoAddress: common.HexToAddress(c.String(flags.DestTaikoAddress.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), @@ -56,7 +56,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.DB, error) { gormDB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) diff --git a/packages/relayer/api/config_test.go b/packages/relayer/api/config_test.go index 0e07a99668d..9176b3a8856 100644 --- a/packages/relayer/api/config_test.go +++ b/packages/relayer/api/config_test.go @@ -4,9 +4,11 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/urfave/cli/v2" + "github.com/taikoxyz/taiko-mono/packages/relayer/cmd/flags" + "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/db" "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/mock" - "github.com/urfave/cli/v2" ) var ( @@ -47,7 +49,7 @@ func TestNewConfigFromCliContext(t *testing.T) { assert.Equal(t, "destRpcUrl", c.DestRPCUrl) assert.Equal(t, destTaikoAddress, c.DestTaikoAddress.Hex()) - c.OpenDBFunc = func() (DB, error) { + c.OpenDBFunc = func() (db.DB, error) { return &mock.DB{}, nil } diff --git a/packages/relayer/indexer/config.go b/packages/relayer/indexer/config.go index a765fda5b0c..3b8a6ac4704 100644 --- a/packages/relayer/indexer/config.go +++ b/packages/relayer/indexer/config.go @@ -103,7 +103,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.DB, error) { gormDB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) diff --git a/packages/relayer/pkg/db/db.go b/packages/relayer/pkg/db/db.go index e2d19c61ca0..f8887021584 100644 --- a/packages/relayer/pkg/db/db.go +++ b/packages/relayer/pkg/db/db.go @@ -1,36 +1,41 @@ package db import ( - "database/sql" "fmt" "time" + "database/sql" "github.com/cyberhorsey/errors" "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 +} + +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{ +func New(gormdb *gorm.DB) DB { + return &Database{ gormdb: gormdb, } } -var ( - ErrNoDB = errors.Validation.NewWithKeyAndDetail("ERR_NO_DB", "DB is required") -) - type DBConnectionOpts struct { Name string Password string @@ -39,10 +44,10 @@ type DBConnectionOpts struct { MaxIdleConns uint64 MaxOpenConns uint64 MaxConnLifetime uint64 - OpenFunc func(dsn string) (*DB, error) + OpenFunc func(dsn string) (DB, error) } -func OpenDBConnection(opts DBConnectionOpts) (*DB, error) { +func OpenDBConnection(opts DBConnectionOpts) (DB, error) { dsn := "" if opts.Password == "" { dsn = fmt.Sprintf( diff --git a/packages/relayer/pkg/repo/containers_test.go b/packages/relayer/pkg/repo/containers_test.go index 67f186da3bc..c18e06f4c72 100644 --- a/packages/relayer/pkg/repo/containers_test.go +++ b/packages/relayer/pkg/repo/containers_test.go @@ -6,12 +6,13 @@ import ( "testing" "github.com/pressly/goose/v3" - "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/db" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" + + "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/db" ) var ( @@ -20,7 +21,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/relayer/pkg/repo/db.go b/packages/relayer/pkg/repo/db.go deleted file mode 100644 index 1c8a35dbca3..00000000000 --- a/packages/relayer/pkg/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/relayer/pkg/repo/event.go b/packages/relayer/pkg/repo/event.go index 6a2b5d47f1a..80d9e149d09 100644 --- a/packages/relayer/pkg/repo/event.go +++ b/packages/relayer/pkg/repo/event.go @@ -9,22 +9,24 @@ import ( "github.com/morkid/paginate" "github.com/pkg/errors" - "github.com/taikoxyz/taiko-mono/packages/relayer" "gorm.io/datatypes" "gorm.io/gorm" + + "github.com/taikoxyz/taiko-mono/packages/relayer" + "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/db" ) type EventRepository struct { - db DB + db db.DB } -func NewEventRepository(db DB) (*EventRepository, error) { - if db == nil { - return nil, ErrNoDB +func NewEventRepository(dbHandler db.DB) (*EventRepository, error) { + if dbHandler == nil { + return nil, db.ErrNoDB } return &EventRepository{ - db: db, + db: dbHandler, }, nil } diff --git a/packages/relayer/pkg/repo/event_test.go b/packages/relayer/pkg/repo/event_test.go index f20880a16f6..b363466ef4a 100644 --- a/packages/relayer/pkg/repo/event_test.go +++ b/packages/relayer/pkg/repo/event_test.go @@ -10,10 +10,11 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/morkid/paginate" - "github.com/taikoxyz/taiko-mono/packages/relayer" - "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/db" "gopkg.in/go-playground/assert.v1" "gorm.io/datatypes" + + "github.com/taikoxyz/taiko-mono/packages/relayer" + "github.com/taikoxyz/taiko-mono/packages/relayer/pkg/db" ) var testMsgHash = "0x1" @@ -85,18 +86,18 @@ var testEvents = []relayer.Event{ func Test_NewEventRepo(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, }, } diff --git a/packages/relayer/processor/config.go b/packages/relayer/processor/config.go index 89d62981d94..cfd86e7ac21 100644 --- a/packages/relayer/processor/config.go +++ b/packages/relayer/processor/config.go @@ -187,7 +187,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.DB, error) { gormDB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) diff --git a/packages/relayer/watchdog/config.go b/packages/relayer/watchdog/config.go index cbb91160b3d..0b2a14c041a 100644 --- a/packages/relayer/watchdog/config.go +++ b/packages/relayer/watchdog/config.go @@ -103,7 +103,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.DB, error) { gormDB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), })