Remove fatal from pkg

* Functions in pkg should return errors
 * If aborting is desired, that behavior should be left to the consumer
This commit is contained in:
Eric Meyer 2017-12-04 09:53:36 -06:00
parent 7a11d3c50f
commit 655d1b1d6f
8 changed files with 31 additions and 26 deletions

View File

@ -8,7 +8,6 @@ import (
"github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/history" "github.com/8thlight/vulcanizedb/pkg/history"
"github.com/8thlight/vulcanizedb/pkg/repositories"
) )
func main() { func main() {
@ -16,9 +15,8 @@ func main() {
startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from") startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from")
flag.Parse() flag.Parse()
config := cmd.LoadConfig(*environment) config := cmd.LoadConfig(*environment)
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
repository := repositories.NewPostgres(config.Database) repository := cmd.LoadPostgres(config.Database)
numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, int64(*startingBlockNumber)) numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, int64(*startingBlockNumber))
fmt.Printf("Populated %d blocks", numberOfBlocksCreated) fmt.Printf("Populated %d blocks", numberOfBlocksCreated)
} }

View File

@ -10,15 +10,13 @@ import (
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/observers" "github.com/8thlight/vulcanizedb/pkg/observers"
"github.com/8thlight/vulcanizedb/pkg/repositories"
) )
func main() { func main() {
environment := flag.String("environment", "", "Environment name") environment := flag.String("environment", "", "Environment name")
flag.Parse() flag.Parse()
config := cmd.LoadConfig(*environment) config := cmd.LoadConfig(*environment)
repository := cmd.LoadPostgres(config.Database)
repository := repositories.NewPostgres(config.Database)
fmt.Printf("Creating Geth Blockchain to: %s\n", config.Client.IPCPath) fmt.Printf("Creating Geth Blockchain to: %s\n", config.Client.IPCPath)
listener := blockchain_listener.NewBlockchainListener( listener := blockchain_listener.NewBlockchainListener(
geth.NewGethBlockchain(config.Client.IPCPath), geth.NewGethBlockchain(config.Client.IPCPath),

View File

@ -9,7 +9,6 @@ import (
"github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/repositories"
"github.com/8thlight/vulcanizedb/pkg/watched_contracts" "github.com/8thlight/vulcanizedb/pkg/watched_contracts"
) )
@ -18,9 +17,8 @@ func main() {
contractHash := flag.String("contract-hash", "", "Contract hash to show summary") contractHash := flag.String("contract-hash", "", "Contract hash to show summary")
flag.Parse() flag.Parse()
config := cmd.LoadConfig(*environment) config := cmd.LoadConfig(*environment)
blockchain := geth.NewGethBlockchain(config.Client.IPCPath) blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
repository := repositories.NewPostgres(config.Database) repository := cmd.LoadPostgres(config.Database)
contractSummary, err := watched_contracts.NewSummary(blockchain, repository, *contractHash) contractSummary, err := watched_contracts.NewSummary(blockchain, repository, *contractHash)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)

View File

@ -5,7 +5,6 @@ import (
"github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
) )
func main() { func main() {
@ -13,6 +12,6 @@ func main() {
contractHash := flag.String("contract-hash", "", "contract-hash=x1234") contractHash := flag.String("contract-hash", "", "contract-hash=x1234")
flag.Parse() flag.Parse()
config := cmd.LoadConfig(*environment) config := cmd.LoadConfig(*environment)
repository := repositories.NewPostgres(config.Database) repository := cmd.LoadPostgres(config.Database)
repository.CreateWatchedContract(core.WatchedContract{Hash: *contractHash}) repository.CreateWatchedContract(core.WatchedContract{Hash: *contractHash})
} }

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"github.com/8thlight/vulcanizedb/pkg/config" "github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/repositories"
) )
func LoadConfig(environment string) config.Config { func LoadConfig(environment string) config.Config {
@ -13,3 +14,11 @@ func LoadConfig(environment string) config.Config {
} }
return *cfg return *cfg
} }
func LoadPostgres(database config.Database) repositories.Postgres {
repository, err := repositories.NewPostgres(database)
if err != nil {
log.Fatalf("Error loading postgres\n%v", err)
}
return repository
}

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"log"
"os" "os"
"fmt" "fmt"
@ -50,8 +49,9 @@ func parseConfigFile(filePath string) (*Config, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else { } else {
if _, err := toml.DecodeFile(filePath, &cfg); err != nil { _, err := toml.DecodeFile(filePath, &cfg)
log.Fatal(err) if err != nil {
return nil, err
} }
return &cfg, err return &cfg, err
} }

View File

@ -2,7 +2,6 @@ package repositories
import ( import (
"database/sql" "database/sql"
"log"
"context" "context"
@ -19,16 +18,17 @@ type Postgres struct {
} }
var ( var (
ErrDBInsertFailed = errors.New("postgres: insert failed") ErrDBInsertFailed = errors.New("postgres: insert failed")
ErrDBConnectionFailed = errors.New("postgres: db connection failed")
) )
func NewPostgres(databaseConfig config.Database) Postgres { func NewPostgres(databaseConfig config.Database) (Postgres, error) {
connectString := config.DbConnectionString(databaseConfig) connectString := config.DbConnectionString(databaseConfig)
db, err := sqlx.Connect("postgres", connectString) db, err := sqlx.Connect("postgres", connectString)
if err != nil { if err != nil {
log.Fatalf("Error connecting to DB: %v\n", err) return Postgres{}, ErrDBConnectionFailed
} }
return Postgres{Db: db} return Postgres{Db: db}, nil
} }
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error { func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
@ -42,11 +42,8 @@ func (repository Postgres) CreateWatchedContract(contract core.WatchedContract)
func (repository Postgres) IsWatchedContract(contractHash string) bool { func (repository Postgres) IsWatchedContract(contractHash string) bool {
var exists bool var exists bool
err := repository.Db.QueryRow( repository.Db.QueryRow(
`SELECT exists(SELECT 1 FROM watched_contracts WHERE contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists) `SELECT exists(SELECT 1 FROM watched_contracts WHERE contract_hash=$1) FROM watched_contracts`, contractHash).Scan(&exists)
if err != nil && err != sql.ErrNoRows {
log.Fatalf("error checking if row exists %v", err)
}
return exists return exists
} }

View File

@ -26,7 +26,7 @@ var _ = Describe("Postgres repository", func() {
testing.AssertRepositoryBehavior(func() repositories.Repository { testing.AssertRepositoryBehavior(func() repositories.Repository {
cfg, _ := config.NewConfig("private") cfg, _ := config.NewConfig("private")
repository := repositories.NewPostgres(cfg.Database) repository, _ := repositories.NewPostgres(cfg.Database)
testing.ClearData(repository) testing.ClearData(repository)
return repository return repository
}) })
@ -40,7 +40,7 @@ var _ = Describe("Postgres repository", func() {
Transactions: []core.Transaction{}, Transactions: []core.Transaction{},
} }
cfg, _ := config.NewConfig("private") cfg, _ := config.NewConfig("private")
repository := repositories.NewPostgres(cfg.Database) repository, _ := repositories.NewPostgres(cfg.Database)
err := repository.CreateBlock(badBlock) err := repository.CreateBlock(badBlock)
savedBlock := repository.FindBlockByNumber(123) savedBlock := repository.FindBlockByNumber(123)
@ -49,6 +49,12 @@ var _ = Describe("Postgres repository", func() {
Expect(savedBlock).To(BeNil()) Expect(savedBlock).To(BeNil())
}) })
It("throws error when can't connect to the database", func() {
invalidDatabase := config.Database{}
_, err := repositories.NewPostgres(invalidDatabase)
Expect(err).To(Equal(repositories.ErrDBConnectionFailed))
})
It("does not commit block or transactions if transaction is invalid", func() { It("does not commit block or transactions if transaction is invalid", func() {
//badHash violates db To field length //badHash violates db To field length
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100)) badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
@ -58,7 +64,7 @@ var _ = Describe("Postgres repository", func() {
Transactions: []core.Transaction{badTransaction}, Transactions: []core.Transaction{badTransaction},
} }
cfg, _ := config.NewConfig("private") cfg, _ := config.NewConfig("private")
repository := repositories.NewPostgres(cfg.Database) repository, _ := repositories.NewPostgres(cfg.Database)
err := repository.CreateBlock(block) err := repository.CreateBlock(block)
savedBlock := repository.FindBlockByNumber(123) savedBlock := repository.FindBlockByNumber(123)