forked from cerc-io/ipld-eth-server
Refactor DB observer to depend on repository
This commit is contained in:
parent
26b21e8eea
commit
24a8fcbadb
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/8thlight/vulcanizedb/core"
|
"github.com/8thlight/vulcanizedb/core"
|
||||||
"github.com/8thlight/vulcanizedb/geth"
|
"github.com/8thlight/vulcanizedb/geth"
|
||||||
"github.com/8thlight/vulcanizedb/observers"
|
"github.com/8thlight/vulcanizedb/observers"
|
||||||
|
"github.com/8thlight/vulcanizedb/repositories"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
do "gopkg.in/godo.v2"
|
do "gopkg.in/godo.v2"
|
||||||
)
|
)
|
||||||
@ -31,7 +32,8 @@ func startBlockchainListener(cfg config.Config) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error connecting to DB: %v\n", err)
|
log.Fatalf("Error connecting to DB: %v\n", err)
|
||||||
}
|
}
|
||||||
dbObserver := (observers.BlockchainDBObserver{Db: db})
|
repository := repositories.NewPostgres(db)
|
||||||
|
dbObserver := observers.NewBlockchainDbObserver(repository)
|
||||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
||||||
loggingObserver,
|
loggingObserver,
|
||||||
dbObserver,
|
dbObserver,
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
var _ = Describe("Blockchain listeners", func() {
|
var _ = Describe("Blockchain listeners", func() {
|
||||||
|
|
||||||
It("starts with no blocks", func(done Done) {
|
It("starts with no blocks", func(done Done) {
|
||||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
observer := fakes.NewFakeBlockchainObserver()
|
||||||
blockchain := &fakes.Blockchain{}
|
blockchain := &fakes.Blockchain{}
|
||||||
|
|
||||||
blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||||
@ -21,7 +21,7 @@ var _ = Describe("Blockchain listeners", func() {
|
|||||||
}, 1)
|
}, 1)
|
||||||
|
|
||||||
It("sees when one block was added", func(done Done) {
|
It("sees when one block was added", func(done Done) {
|
||||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
observer := fakes.NewFakeBlockchainObserver()
|
||||||
blockchain := &fakes.Blockchain{}
|
blockchain := &fakes.Blockchain{}
|
||||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||||
go listener.Start()
|
go listener.Start()
|
||||||
@ -37,7 +37,7 @@ var _ = Describe("Blockchain listeners", func() {
|
|||||||
}, 1)
|
}, 1)
|
||||||
|
|
||||||
It("sees a second block", func(done Done) {
|
It("sees a second block", func(done Done) {
|
||||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
observer := fakes.NewFakeBlockchainObserver()
|
||||||
blockchain := &fakes.Blockchain{}
|
blockchain := &fakes.Blockchain{}
|
||||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||||
go listener.Start()
|
go listener.Start()
|
||||||
@ -55,7 +55,7 @@ var _ = Describe("Blockchain listeners", func() {
|
|||||||
}, 1)
|
}, 1)
|
||||||
|
|
||||||
It("stops listening", func(done Done) {
|
It("stops listening", func(done Done) {
|
||||||
observer := fakes.NewFakeBlockchainObserverTwo()
|
observer := fakes.NewFakeBlockchainObserver()
|
||||||
blockchain := &fakes.Blockchain{}
|
blockchain := &fakes.Blockchain{}
|
||||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||||
go listener.Start()
|
go listener.Start()
|
||||||
|
@ -11,7 +11,7 @@ func (observer *BlockchainObserver) LastBlock() core.Block {
|
|||||||
return observer.CurrentBlocks[len(observer.CurrentBlocks)-1]
|
return observer.CurrentBlocks[len(observer.CurrentBlocks)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFakeBlockchainObserverTwo() *BlockchainObserver {
|
func NewFakeBlockchainObserver() *BlockchainObserver {
|
||||||
return &BlockchainObserver{
|
return &BlockchainObserver{
|
||||||
WasNotified: make(chan bool),
|
WasNotified: make(chan bool),
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
|
|||||||
var observer *fakes.BlockchainObserver
|
var observer *fakes.BlockchainObserver
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
observer = fakes.NewFakeBlockchainObserverTwo()
|
observer = fakes.NewFakeBlockchainObserver()
|
||||||
blockchain := geth.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
|
blockchain := geth.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
|
||||||
observers := []core.BlockchainObserver{observer}
|
observers := []core.BlockchainObserver{observer}
|
||||||
listener = blockchain_listener.NewBlockchainListener(blockchain, observers)
|
listener = blockchain_listener.NewBlockchainListener(blockchain, observers)
|
||||||
|
@ -3,13 +3,16 @@ package observers
|
|||||||
import (
|
import (
|
||||||
"github.com/8thlight/vulcanizedb/core"
|
"github.com/8thlight/vulcanizedb/core"
|
||||||
"github.com/8thlight/vulcanizedb/repositories"
|
"github.com/8thlight/vulcanizedb/repositories"
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlockchainDBObserver struct {
|
type BlockchainDbObserver struct {
|
||||||
Db *sqlx.DB
|
repository repositories.Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) {
|
func NewBlockchainDbObserver(repository repositories.Repository) BlockchainDbObserver {
|
||||||
repositories.NewPostgres(observer.Db).CreateBlock(block)
|
return BlockchainDbObserver{repository: repository}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (observer BlockchainDbObserver) NotifyBlockAdded(block core.Block) {
|
||||||
|
observer.repository.CreateBlock(block)
|
||||||
}
|
}
|
||||||
|
@ -1,41 +1,23 @@
|
|||||||
package observers_test
|
package observers_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/8thlight/vulcanizedb/config"
|
|
||||||
"github.com/8thlight/vulcanizedb/core"
|
"github.com/8thlight/vulcanizedb/core"
|
||||||
"github.com/8thlight/vulcanizedb/observers"
|
"github.com/8thlight/vulcanizedb/observers"
|
||||||
"github.com/8thlight/vulcanizedb/repositories"
|
"github.com/8thlight/vulcanizedb/repositories"
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
_ "github.com/lib/pq"
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
_, filename, _, _ = runtime.Caller(0)
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ = Describe("Saving blocks to the database", func() {
|
var _ = Describe("Saving blocks to the database", func() {
|
||||||
|
|
||||||
var db *sqlx.DB
|
var repository *repositories.InMemory
|
||||||
var err error
|
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
cfg := config.NewConfig("private")
|
repository = repositories.NewInMemory()
|
||||||
pgConfig := config.DbConnectionString(cfg.Database)
|
|
||||||
db, err = sqlx.Connect("postgres", pgConfig)
|
|
||||||
db.MustExec("DELETE FROM transactions")
|
|
||||||
db.MustExec("DELETE FROM blocks")
|
|
||||||
})
|
|
||||||
|
|
||||||
AfterEach(func() {
|
|
||||||
db.Close()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
It("implements the observer interface", func() {
|
It("implements the observer interface", func() {
|
||||||
var observer core.BlockchainObserver = observers.BlockchainDBObserver{Db: db}
|
var observer core.BlockchainObserver = observers.NewBlockchainDbObserver(repository)
|
||||||
Expect(observer).NotTo(BeNil())
|
Expect(observer).NotTo(BeNil())
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -45,10 +27,9 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
Transactions: []core.Transaction{{}},
|
Transactions: []core.Transaction{{}},
|
||||||
}
|
}
|
||||||
|
|
||||||
observer := observers.BlockchainDBObserver{Db: db}
|
observer := observers.NewBlockchainDbObserver(repository)
|
||||||
observer.NotifyBlockAdded(block)
|
observer.NotifyBlockAdded(block)
|
||||||
|
|
||||||
repository := repositories.NewPostgres(db)
|
|
||||||
savedBlock := repository.FindBlockByNumber(123)
|
savedBlock := repository.FindBlockByNumber(123)
|
||||||
Expect(savedBlock).NotTo(BeNil())
|
Expect(savedBlock).NotTo(BeNil())
|
||||||
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
||||||
|
7
repositories/repository.go
Normal file
7
repositories/repository.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import "github.com/8thlight/vulcanizedb/core"
|
||||||
|
|
||||||
|
type Repository interface {
|
||||||
|
CreateBlock(block core.Block)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user