Add BlockchainDBObserver

* First code that interacts
This commit is contained in:
Eric Meyer 2017-10-25 10:57:59 -05:00
parent 2e4d0b9bb1
commit 0a16e402bb
6 changed files with 129 additions and 1 deletions

View File

@ -1,5 +1,16 @@
dist: trusty
sudo: required
language: go
go:
- 1.9
services:
- postgresql
before_script:
- go get -u -d github.com/mattes/migrate/cli github.com/lib/pq
- go build -tags 'postgres' -o /usr/local/bin/migrate github.com/mattes/migrate/cli
- psql -c 'create database vulcanize;' -U postgres
- migrate -database 'postgresql://localhost:5432/vulcanize?sslmode=disable' -path ./migrations up
script:
- go test -v ./core/...
notifications:
email: false

14
Gopkg.lock generated
View File

@ -19,6 +19,18 @@
revision = "817915b46b97fd7bb80e8ab6b69f01a53ac3eebf"
version = "v1.6.0"
[[projects]]
branch = "master"
name = "github.com/jmoiron/sqlx"
packages = [".","reflectx"]
revision = "3379e5993990b1f927fc8db926485e6f6becf2d2"
[[projects]]
branch = "master"
name = "github.com/lib/pq"
packages = [".","oid"]
revision = "b609790bd85edf8e9ab7e0f8912750a786177bcf"
[[projects]]
name = "github.com/onsi/ginkgo"
packages = [".","config","internal/codelocation","internal/containernode","internal/failer","internal/leafnodes","internal/remote","internal/spec","internal/spec_iterator","internal/specrunner","internal/suite","internal/testingtproxy","internal/writer","reporters","reporters/stenographer","reporters/stenographer/support/go-colorable","reporters/stenographer/support/go-isatty","types"]
@ -88,6 +100,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "1f89d2d001b1d5929ecd9f83d34d67edc4c1ae8baea497f4b5b76d9f6cc7f8d5"
inputs-digest = "28c66c62724453afdc526c6cf9de955240b487fb81b027fefbe7ed4e1545caec"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -28,3 +28,7 @@
[[constraint]]
branch = "master"
name = "github.com/jmoiron/sqlx"
[[constraint]]
branch = "master"
name = "github.com/lib/pq"

View File

@ -0,0 +1,19 @@
package core
import (
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
type BlockRecord struct {
BlockNumber int64 `db:"block_number"`
}
type BlockchainDBObserver struct {
Db *sqlx.DB
}
func (observer BlockchainDBObserver) NotifyBlockAdded(block Block) {
observer.Db.NamedExec("INSERT INTO blocks (block_number) VALUES (:block_number)", &BlockRecord{BlockNumber: block.Number.Int64()})
//observer.Db.MustExec("Insert INTO blocks (block_number) VALUES ($1)", block.Number.Int64())
}

View File

@ -0,0 +1,72 @@
package core_test
import (
"math/big"
"fmt"
"github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
dbname = "vulcanize"
)
var _ = Describe("Saving blocks to the database", func() {
var db *sqlx.DB
var err error
BeforeEach(func() {
pgConfig := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err = sqlx.Connect("postgres", pgConfig)
db.MustExec("DELETE FROM blocks")
})
It("implements the observer interface", func() {
var observer core.BlockchainObserver = core.BlockchainDBObserver{Db: db}
Expect(observer).NotTo(BeNil())
})
It("connects to the database", func() {
Expect(err).Should(BeNil())
Expect(db).ShouldNot(BeNil())
})
It("starts with no blocks", func() {
var count int
queryError := db.Get(&count, "SELECT COUNT(*) FROM blocks")
Expect(queryError).Should(BeNil())
Expect(count).Should(Equal(0))
})
It("inserts a block", func() {
block := core.Block{Number: big.NewInt(1)}
observer := core.BlockchainDBObserver{Db: db}
observer.NotifyBlockAdded(block)
rows, err := db.Queryx("SELECT * FROM blocks")
Expect(err).To(BeNil())
var savedBlocks []core.BlockRecord
for rows.Next() {
var savedBlock core.BlockRecord
rows.StructScan(&savedBlock)
savedBlocks = append(savedBlocks, savedBlock)
}
Expect(len(savedBlocks)).To(Equal(1))
Expect(savedBlocks[0].BlockNumber).To(Equal(int64(1)))
})
})

10
main.go
View File

@ -2,7 +2,11 @@ package main
import (
"flag"
"log"
"github.com/8thlight/vulcanizedb/core"
"github.com/jmoiron/sqlx"
)
func main() {
@ -11,5 +15,11 @@ func main() {
var blockchain core.Blockchain = core.NewGethBlockchain(*ipcPath)
blockchain.RegisterObserver(core.BlockchainLoggingObserver{})
pgConfig := "host=localhost port=5432 dbname=vulcanize sslmode=disable"
db, err := sqlx.Connect("postgres", pgConfig)
if err != nil {
log.Fatalf("Error connecting to DB: %v\n", err)
}
blockchain.RegisterObserver(core.BlockchainDBObserver{Db: db})
blockchain.SubscribeToEvents()
}