Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
i-norden committed Mar 19, 2021
1 parent c365dad commit ec5789d
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pkg/eth/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package eth

import (
"fmt"
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
"math/big"
"time"

Expand All @@ -29,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/statediff"
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
node "github.com/ipfs/go-ipld-format"
"github.com/jmoiron/sqlx"
"github.com/multiformats/go-multihash"
Expand Down
2 changes: 1 addition & 1 deletion pkg/eth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package eth

import (
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
)

// ConvertedPayload is a custom type which packages raw ETH data for publishing to IPFS and filtering to subscribers
Expand Down
8 changes: 3 additions & 5 deletions pkg/historical/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ const (

// Config struct
type Config struct {
DBConfig postgres.Config

DB *postgres.DB
HTTPClient *rpc.Client
Frequency time.Duration
Expand Down Expand Up @@ -92,9 +90,9 @@ func NewConfig() (*Config, error) {
return nil, err
}

c.DBConfig.Init()
overrideDBConnConfig(&c.DBConfig)
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo, true)
dbConfig := postgres.NewConfig()
overrideDBConnConfig(dbConfig)
db := utils.LoadPostgres(dbConfig, c.NodeInfo, true)
c.DB = &db
return c, nil
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/postgres/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Config struct {
MaxLifetime int
}

func DbConnectionString(config Config) string {
func (config *Config) DbConnectionString() string {
if len(config.User) > 0 && len(config.Password) > 0 {
return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=disable",
config.User, config.Password, config.Hostname, config.Port, config.Name)
Expand All @@ -57,7 +57,9 @@ func DbConnectionString(config Config) string {
return fmt.Sprintf("postgresql://%s:%d/%s?sslmode=disable", config.Hostname, config.Port, config.Name)
}

func (d *Config) Init() {
// NewConfig initializes and returns a new db config
func NewConfig() *Config {
config := new(Config)
viper.BindEnv("database.name", DATABASE_NAME)
viper.BindEnv("database.hostname", DATABASE_HOSTNAME)
viper.BindEnv("database.port", DATABASE_PORT)
Expand All @@ -67,12 +69,13 @@ func (d *Config) Init() {
viper.BindEnv("database.maxOpen", DATABASE_MAX_OPEN_CONNECTIONS)
viper.BindEnv("database.maxLifetime", DATABASE_MAX_CONN_LIFETIME)

d.Name = viper.GetString("database.name")
d.Hostname = viper.GetString("database.hostname")
d.Port = viper.GetInt("database.port")
d.User = viper.GetString("database.user")
d.Password = viper.GetString("database.password")
d.MaxIdle = viper.GetInt("database.maxIdle")
d.MaxOpen = viper.GetInt("database.maxOpen")
d.MaxLifetime = viper.GetInt("database.maxLifetime")
config.Name = viper.GetString("database.name")
config.Hostname = viper.GetString("database.hostname")
config.Port = viper.GetInt("database.port")
config.User = viper.GetString("database.user")
config.Password = viper.GetString("database.password")
config.MaxIdle = viper.GetInt("database.maxIdle")
config.MaxOpen = viper.GetInt("database.maxOpen")
config.MaxLifetime = viper.GetInt("database.maxLifetime")
return config
}
7 changes: 4 additions & 3 deletions pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type DB struct {
*sqlx.DB
Node node.Info
NodeID int64
Config *Config
}

func NewDB(databaseConfig Config, node node.Info, createNode bool) (*DB, error) {
connectString := DbConnectionString(databaseConfig)
func NewDB(databaseConfig *Config, node node.Info, createNode bool) (*DB, error) {
connectString := databaseConfig.DbConnectionString()
db, connectErr := sqlx.Connect("postgres", connectString)
if connectErr != nil {
return &DB{}, ErrDBConnectionFailed(connectErr)
Expand All @@ -48,7 +49,7 @@ func NewDB(databaseConfig Config, node node.Info, createNode bool) (*DB, error)
lifetime := time.Duration(databaseConfig.MaxLifetime) * time.Second
db.SetConnMaxLifetime(lifetime)
}
pg := DB{DB: db, Node: node}
pg := DB{DB: db, Node: node, Config: databaseConfig}

if createNode {
nodeErr := pg.CreateNode(&node)
Expand Down
3 changes: 2 additions & 1 deletion pkg/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ package postgres_test

import (
"fmt"
"github.com/vulcanize/ipld-eth-indexer/pkg/shared"
"strings"

"github.com/vulcanize/ipld-eth-indexer/pkg/shared"

"math/big"

_ "github.com/lib/pq"
Expand Down
9 changes: 4 additions & 5 deletions pkg/resync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ type Config struct {
ResetValidation bool // If true, resync will reset the validation level to 0 for the given range

// DB info
DB *postgres.DB
DBConfig postgres.Config
DB *postgres.DB

HTTPClient *rpc.Client // Ethereum rpc client
NodeInfo node.Info // Info for the associated node
Expand Down Expand Up @@ -110,9 +109,9 @@ func NewConfig() (*Config, error) {
return nil, err
}

c.DBConfig.Init()
overrideDBConnConfig(&c.DBConfig)
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo, true)
dbConfig := postgres.NewConfig()
overrideDBConnConfig(dbConfig)
db := utils.LoadPostgres(dbConfig, c.NodeInfo, true)
c.DB = &db
return c, nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/shared/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package shared

import (
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/vulcanize/ipld-eth-indexer/pkg/postgres"
"os"
"strconv"

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/vulcanize/ipld-eth-indexer/pkg/node"
"github.com/vulcanize/ipld-eth-indexer/pkg/postgres"
)

// SetupDB is use to setup a db for watcher tests
Expand All @@ -35,7 +35,7 @@ func SetupDBWithNode(node node.Info) (*postgres.DB, error) {
return postgres.NewDB(getTestConfig(), node, true)
}

func getTestConfig() postgres.Config {
func getTestConfig() *postgres.Config {
// get connection to test database from environment variables
hostname := os.Getenv(postgres.DATABASE_HOSTNAME)
if hostname == "" {
Expand All @@ -56,7 +56,7 @@ func getTestConfig() postgres.Config {
user := os.Getenv(postgres.DATABASE_USER)
password := os.Getenv(postgres.DATABASE_PASSWORD)

return postgres.Config{
return &postgres.Config{
Hostname: hostname,
Name: name,
Port: port,
Expand Down
7 changes: 3 additions & 4 deletions pkg/sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (
// Config struct
type Config struct {
DB *postgres.DB
DBConfig postgres.Config
Workers int64
WSClient *rpc.Client
NodeInfo node.Info
Expand All @@ -65,9 +64,9 @@ func NewConfig() (*Config, error) {
return nil, err
}

c.DBConfig.Init()
overrideDBConnConfig(&c.DBConfig)
syncDB := utils.LoadPostgres(c.DBConfig, c.NodeInfo, true)
dbConfig := postgres.NewConfig()
overrideDBConnConfig(dbConfig)
syncDB := utils.LoadPostgres(dbConfig, c.NodeInfo, true)
c.DB = &syncDB
return c, nil
}
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/vulcanize/ipld-eth-indexer/pkg/postgres"
)

func LoadPostgres(database postgres.Config, node node.Info, createNode bool) postgres.DB {
func LoadPostgres(database *postgres.Config, node node.Info, createNode bool) postgres.DB {
db, err := postgres.NewDB(database, node, createNode)
if err != nil {
logrus.Fatal("Error loading postgres: ", err)
Expand Down

0 comments on commit ec5789d

Please sign in to comment.