Skip to content

Commit

Permalink
wip: add provider db
Browse files Browse the repository at this point in the history
  • Loading branch information
MonikaCat committed Jan 3, 2024
1 parent bc6c10f commit 403701f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 42 deletions.
113 changes: 75 additions & 38 deletions database/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "net/url"

type Config struct {
URL string `yaml:"url"`
ProviderURL string `yaml:"provider_url"`
MaxOpenConnections int `yaml:"max_open_connections"`
MaxIdleConnections int `yaml:"max_idle_connections"`
PartitionSize int64 `yaml:"partition_size"`
Expand All @@ -14,13 +15,81 @@ type Config struct {
SSLKey string `yaml:"ssl_key"`
}

func (c *Config) getURL() *url.URL {
parsedURL, err := url.Parse(c.URL)
if err != nil {
panic(err)
}
return parsedURL
}

func (c *Config) getProviderURL() *url.URL {
parsedProviderURL, err := url.Parse(c.ProviderURL)
if err != nil {
panic(err)
}
return parsedProviderURL
}

func (c *Config) GetUser() string {
return c.getURL().User.Username()
}

func (c *Config) GetProviderUser() string {
return c.getProviderURL().User.Username()
}

func (c *Config) GetPassword() string {
password, _ := c.getURL().User.Password()
return password
}

func (c *Config) GetProviderPassword() string {
providerPassword, _ := c.getProviderURL().User.Password()
return providerPassword
}

func (c *Config) GetHost() string {
return c.getURL().Host
}

func (c *Config) GetProviderHost() string {
return c.getProviderURL().Host
}

func (c *Config) GetPort() string {
return c.getURL().Port()
}

func (c *Config) GetProviderPort() string {
return c.getProviderURL().Port()
}

func (c *Config) GetSchema() string {
return c.getURL().Query().Get("search_path")
}

func (c *Config) GetProviderSchema() string {
return c.getProviderURL().Query().Get("search_path")
}

func (c *Config) GetSSLMode() string {
return c.getURL().Query().Get("sslmode")
}

func (c *Config) GetProviderSSLMode() string {
return c.getProviderURL().Query().Get("sslmode")
}

func NewDatabaseConfig(
url, sslModeEnable, sslRootCert, sslCert, sslKey string,
url, providerURL string,
maxOpenConnections int, maxIdleConnections int,
partitionSize int64, batchSize int64,
sslModeEnable, sslRootCert, sslCert, sslKey string,
) Config {
return Config{
URL: url,
ProviderURL: providerURL,
MaxOpenConnections: maxOpenConnections,
MaxIdleConnections: maxIdleConnections,
PartitionSize: partitionSize,
Expand Down Expand Up @@ -77,50 +146,18 @@ func (c Config) WithSSLKey(sslKey string) Config {
return c
}

func (c Config) getURL() *url.URL {
parsedURL, err := url.Parse(c.URL)
if err != nil {
panic(err)
}
return parsedURL
}

func (c Config) GetUser() string {
return c.getURL().User.Username()
}

func (c Config) GetPassword() string {
password, _ := c.getURL().User.Password()
return password
}

func (c Config) GetHost() string {
return c.getURL().Host
}

func (c Config) GetPort() string {
return c.getURL().Port()
}

func (c Config) GetSchema() string {
return c.getURL().Query().Get("search_path")
}

func (c Config) GetSSLMode() string {
return c.getURL().Query().Get("sslmode")
}

// DefaultDatabaseConfig returns the default instance of Config
func DefaultDatabaseConfig() Config {
return NewDatabaseConfig(
"postgresql://user:password@localhost:5432/database-name?sslmode=disable&search_path=public",
"false",
"",
"",
"",
"postgresql://user:password@localhost:5432/provider-database-name?sslmode=disable&search_path=public",
1,
1,
100000,
1000,
"",
"",
"",
"",
)
}
29 changes: 25 additions & 4 deletions database/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,27 @@ func Builder(ctx *database.Context) (database.Database, error) {
return nil, err
}

providerDBURI := utils.GetEnvOr(env.ProviderDatabaseURI, ctx.Cfg.ProviderURL)

providerPostgresDb, err := sqlx.Open("postgres", providerDBURI)
if err != nil {
return nil, err
}

// Set max open connections
postgresDb.SetMaxOpenConns(ctx.Cfg.MaxOpenConnections)
postgresDb.SetMaxIdleConns(ctx.Cfg.MaxIdleConnections)

providerPostgresDb.SetMaxIdleConns(ctx.Cfg.MaxIdleConnections)
providerPostgresDb.SetMaxIdleConns(ctx.Cfg.MaxIdleConnections)

return &Database{
Cdc: ctx.EncodingConfig.Codec,
Amino: ctx.EncodingConfig.Amino,

SQL: postgresDb,
Logger: ctx.Logger,
SQL: postgresDb,
ProviderSQL: providerPostgresDb,
Logger: ctx.Logger,
}, nil
}

Expand All @@ -63,8 +74,9 @@ type Database struct {
Cdc codec.Codec
Amino *codec.LegacyAmino

SQL *sqlx.DB
Logger logging.Logger
SQL *sqlx.DB
ProviderSQL *sqlx.DB
Logger logging.Logger
}

// CreatePartitionIfNotExists creates a new partition having the given partition id if not existing
Expand Down Expand Up @@ -141,6 +153,15 @@ VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT DO NOTHING`
return err
}

// SaveBlock implements database.Database
// func (db *Database) GetProvidersValidator(consensusAddress string) error {
// sqlStatement := `
// SELECT self_delegate_address FROM validator_info where consensus_address = '%s'`

// _, err := db.ProviderSQL.Exec(sqlStatement, consensusAddress)
// return err
// }

// GetTotalBlocks implements database.Database
func (db *Database) GetTotalBlocks() int64 {
var blockCount int64
Expand Down
1 change: 1 addition & 0 deletions types/env/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
DatabaseSSLRootCert = "JUNO_DATABASE_SSL_ROOT_CERT"
DatabaseSSLCert = "JUNO_DATABASE_SSL_CERT"
DatabaseSSLKey = "JUNO_DATABASE_SSL_KEY"
ProviderDatabaseURI = "JUNO_PROVIDER_DATABASE_URL"
)

0 comments on commit 403701f

Please sign in to comment.