Merge pull request #85 from 8thlight/remove-logfatal

Remove log.Fatal from pkg
This commit is contained in:
ericmeyer 2017-12-04 10:07:36 -06:00 committed by GitHub
commit f46891f732
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/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/history"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
func main() {
@ -16,9 +15,8 @@ func main() {
startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from")
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
repository := repositories.NewPostgres(config.Database)
repository := cmd.LoadPostgres(config.Database)
numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, int64(*startingBlockNumber))
fmt.Printf("Populated %d blocks", numberOfBlocksCreated)
}

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"log"
"github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
func LoadConfig(environment string) config.Config {
@ -13,3 +14,11 @@ func LoadConfig(environment string) config.Config {
}
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
import (
"log"
"os"
"fmt"
@ -50,8 +49,9 @@ func parseConfigFile(filePath string) (*Config, error) {
if err != nil {
return nil, err
} else {
if _, err := toml.DecodeFile(filePath, &cfg); err != nil {
log.Fatal(err)
_, err := toml.DecodeFile(filePath, &cfg)
if err != nil {
return nil, err
}
return &cfg, err
}

View File

@ -2,7 +2,6 @@ package repositories
import (
"database/sql"
"log"
"context"
@ -19,16 +18,17 @@ type Postgres struct {
}
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)
db, err := sqlx.Connect("postgres", connectString)
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 {
@ -42,11 +42,8 @@ func (repository Postgres) CreateWatchedContract(contract core.WatchedContract)
func (repository Postgres) IsWatchedContract(contractHash string) 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)
if err != nil && err != sql.ErrNoRows {
log.Fatalf("error checking if row exists %v", err)
}
return exists
}

View File

@ -26,7 +26,7 @@ var _ = Describe("Postgres repository", func() {
testing.AssertRepositoryBehavior(func() repositories.Repository {
cfg, _ := config.NewConfig("private")
repository := repositories.NewPostgres(cfg.Database)
repository, _ := repositories.NewPostgres(cfg.Database)
testing.ClearData(repository)
return repository
})
@ -40,7 +40,7 @@ var _ = Describe("Postgres repository", func() {
Transactions: []core.Transaction{},
}
cfg, _ := config.NewConfig("private")
repository := repositories.NewPostgres(cfg.Database)
repository, _ := repositories.NewPostgres(cfg.Database)
err := repository.CreateBlock(badBlock)
savedBlock := repository.FindBlockByNumber(123)
@ -49,6 +49,12 @@ var _ = Describe("Postgres repository", func() {
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() {
//badHash violates db To field length
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
@ -58,7 +64,7 @@ var _ = Describe("Postgres repository", func() {
Transactions: []core.Transaction{badTransaction},
}
cfg, _ := config.NewConfig("private")
repository := repositories.NewPostgres(cfg.Database)
repository, _ := repositories.NewPostgres(cfg.Database)
err := repository.CreateBlock(block)
savedBlock := repository.FindBlockByNumber(123)