Refactor postgres to be initialized with config
This commit is contained in:
parent
aa52088ba7
commit
58fbeb32fd
@ -10,7 +10,6 @@ import (
|
|||||||
"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"
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -23,12 +22,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
|
||||||
connectString := config.DbConnectionString(cfg.Database)
|
repository := repositories.NewPostgres(cfg.Database)
|
||||||
db, err := sqlx.Connect("postgres", connectString)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error connecting to DB: %v\n", err)
|
|
||||||
}
|
|
||||||
repository := repositories.NewPostgres(db)
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"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"
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -26,12 +25,7 @@ func main() {
|
|||||||
fmt.Println("Client Path ", cfg.Client.IPCPath)
|
fmt.Println("Client Path ", cfg.Client.IPCPath)
|
||||||
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
|
||||||
loggingObserver := observers.BlockchainLoggingObserver{}
|
loggingObserver := observers.BlockchainLoggingObserver{}
|
||||||
connectString := config.DbConnectionString(cfg.Database)
|
repository := repositories.NewPostgres(cfg.Database)
|
||||||
db, err := sqlx.Connect("postgres", connectString)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error connecting to DB: %v\n", err)
|
|
||||||
}
|
|
||||||
repository := repositories.NewPostgres(db)
|
|
||||||
dbObserver := observers.NewBlockchainDbObserver(repository)
|
dbObserver := observers.NewBlockchainDbObserver(repository)
|
||||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
||||||
loggingObserver,
|
loggingObserver,
|
||||||
|
@ -2,11 +2,13 @@ package repositories
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/8thlight/vulcanizedb/pkg/config"
|
||||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
@ -16,6 +18,15 @@ type Postgres struct {
|
|||||||
Db *sqlx.DB
|
Db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewPostgres(databaseConfig config.Database) Postgres {
|
||||||
|
connectString := config.DbConnectionString(databaseConfig)
|
||||||
|
db, err := sqlx.Connect("postgres", connectString)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error connecting to DB: %v\n", err)
|
||||||
|
}
|
||||||
|
return Postgres{Db: db}
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrDBInsertFailed = errors.New("postgres: insert failed")
|
ErrDBInsertFailed = errors.New("postgres: insert failed")
|
||||||
)
|
)
|
||||||
@ -40,10 +51,6 @@ func (repository Postgres) MissingBlockNumbers(startingBlockNumber int64, highes
|
|||||||
return numbers
|
return numbers
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPostgres(db *sqlx.DB) Postgres {
|
|
||||||
return Postgres{Db: db}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repository Postgres) FindBlockByNumber(blockNumber int64) *core.Block {
|
func (repository Postgres) FindBlockByNumber(blockNumber int64) *core.Block {
|
||||||
blockRows, _ := repository.Db.Query(
|
blockRows, _ := repository.Db.Query(
|
||||||
`SELECT id, block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash FROM blocks`)
|
`SELECT id, block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash FROM blocks`)
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/8thlight/vulcanizedb/pkg/config"
|
"github.com/8thlight/vulcanizedb/pkg/config"
|
||||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||||
|
"github.com/8thlight/vulcanizedb/pkg/repositories/testing"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
@ -225,7 +226,6 @@ var _ = Describe("Repositories", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("does not commit block if block is invalid", func() {
|
It("does not commit block if block is invalid", func() {
|
||||||
|
|
||||||
//badNonce violates db Nonce field length
|
//badNonce violates db Nonce field length
|
||||||
badNonce := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
badNonce := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
||||||
badBlock := core.Block{
|
badBlock := core.Block{
|
||||||
@ -234,10 +234,7 @@ var _ = Describe("Repositories", func() {
|
|||||||
Transactions: []core.Transaction{},
|
Transactions: []core.Transaction{},
|
||||||
}
|
}
|
||||||
cfg, _ := config.NewConfig("private")
|
cfg, _ := config.NewConfig("private")
|
||||||
pgConfig := config.DbConnectionString(cfg.Database)
|
repository := repositories.NewPostgres(cfg.Database)
|
||||||
db, _ := sqlx.Connect("postgres", pgConfig)
|
|
||||||
Expect(db).ShouldNot(BeNil())
|
|
||||||
repository := repositories.NewPostgres(db)
|
|
||||||
|
|
||||||
err := repository.CreateBlock(badBlock)
|
err := repository.CreateBlock(badBlock)
|
||||||
savedBlock := repository.FindBlockByNumber(123)
|
savedBlock := repository.FindBlockByNumber(123)
|
||||||
@ -247,19 +244,15 @@ var _ = Describe("Repositories", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
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))
|
||||||
badTransaction := core.Transaction{To: badHash}
|
badTransaction := core.Transaction{To: badHash}
|
||||||
cfg, _ := config.NewConfig("private")
|
|
||||||
pgConfig := config.DbConnectionString(cfg.Database)
|
|
||||||
block := core.Block{
|
block := core.Block{
|
||||||
Number: 123,
|
Number: 123,
|
||||||
Transactions: []core.Transaction{badTransaction},
|
Transactions: []core.Transaction{badTransaction},
|
||||||
}
|
}
|
||||||
db, _ := sqlx.Connect("postgres", pgConfig)
|
cfg, _ := config.NewConfig("private")
|
||||||
Expect(db).ShouldNot(BeNil())
|
repository := repositories.NewPostgres(cfg.Database)
|
||||||
repository := repositories.NewPostgres(db)
|
|
||||||
|
|
||||||
err := repository.CreateBlock(block)
|
err := repository.CreateBlock(block)
|
||||||
savedBlock := repository.FindBlockByNumber(123)
|
savedBlock := repository.FindBlockByNumber(123)
|
||||||
@ -270,11 +263,9 @@ var _ = Describe("Repositories", func() {
|
|||||||
|
|
||||||
AssertRepositoryBehavior(func() repositories.Repository {
|
AssertRepositoryBehavior(func() repositories.Repository {
|
||||||
cfg, _ := config.NewConfig("private")
|
cfg, _ := config.NewConfig("private")
|
||||||
pgConfig := config.DbConnectionString(cfg.Database)
|
repository := repositories.NewPostgres(cfg.Database)
|
||||||
db, _ := sqlx.Connect("postgres", pgConfig)
|
testing.ClearData(repository)
|
||||||
db.MustExec("DELETE FROM transactions")
|
return repository
|
||||||
db.MustExec("DELETE FROM blocks")
|
|
||||||
return repositories.NewPostgres(db)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
8
pkg/repositories/testing/helpers.go
Normal file
8
pkg/repositories/testing/helpers.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package testing
|
||||||
|
|
||||||
|
import "github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||||
|
|
||||||
|
func ClearData(postgres repositories.Postgres) {
|
||||||
|
postgres.Db.MustExec("DELETE FROM transactions")
|
||||||
|
postgres.Db.MustExec("DELETE FROM blocks")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user