From 0a16e402bbb4befa6c893c2634fce8c71dadba3b Mon Sep 17 00:00:00 2001 From: Eric Meyer Date: Wed, 25 Oct 2017 10:57:59 -0500 Subject: [PATCH] Add BlockchainDBObserver * First code that interacts --- .travis.yml | 11 +++++ Gopkg.lock | 14 +++++- Gopkg.toml | 4 ++ core/blockchain_db_observer.go | 19 ++++++++ core/blockchain_db_observer_test.go | 72 +++++++++++++++++++++++++++++ main.go | 10 ++++ 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 core/blockchain_db_observer.go create mode 100644 core/blockchain_db_observer_test.go diff --git a/.travis.yml b/.travis.yml index 1d93167d..db4a5119 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Gopkg.lock b/Gopkg.lock index ecf95c80..a0c67bb3 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -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 diff --git a/Gopkg.toml b/Gopkg.toml index 14015b23..0b5b260c 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -28,3 +28,7 @@ [[constraint]] branch = "master" name = "github.com/jmoiron/sqlx" + +[[constraint]] + branch = "master" + name = "github.com/lib/pq" diff --git a/core/blockchain_db_observer.go b/core/blockchain_db_observer.go new file mode 100644 index 00000000..51622592 --- /dev/null +++ b/core/blockchain_db_observer.go @@ -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()) +} diff --git a/core/blockchain_db_observer_test.go b/core/blockchain_db_observer_test.go new file mode 100644 index 00000000..83824107 --- /dev/null +++ b/core/blockchain_db_observer_test.go @@ -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))) + }) + +}) diff --git a/main.go b/main.go index 20a6d2cc..133480f3 100644 --- a/main.go +++ b/main.go @@ -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() }