Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(relayer): delete duplicate DB interface #17687

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions packages/relayer/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"context"
"database/sql"
"fmt"
"log/slog"
nethttp "net/http"
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/relayer/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
Expand All @@ -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),
})
Expand Down
6 changes: 4 additions & 2 deletions packages/relayer/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/indexer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
29 changes: 17 additions & 12 deletions packages/relayer/pkg/db/db.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions packages/relayer/pkg/repo/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"},
Expand Down
17 changes: 0 additions & 17 deletions packages/relayer/pkg/repo/db.go

This file was deleted.

14 changes: 8 additions & 6 deletions packages/relayer/pkg/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 6 additions & 5 deletions packages/relayer/pkg/repo/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
}

Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/processor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/watchdog/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down
Loading