ipld-eth-server/pkg/datastore/postgres/postgres_test.go

145 lines
4.6 KiB
Go
Raw Normal View History

package postgres_test
2017-11-13 21:42:07 +00:00
import (
"fmt"
"strings"
"math/big"
2017-11-13 21:42:07 +00:00
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/test_config"
2017-11-13 21:42:07 +00:00
)
var _ = Describe("Postgres DB", func() {
var sqlxdb *sqlx.DB
2017-11-13 21:42:07 +00:00
It("connects to the database", func() {
var err error
pgConfig := config.DbConnectionString(test_config.DBConfig)
2018-05-02 16:17:02 +00:00
sqlxdb, err = sqlx.Connect("postgres", pgConfig)
2018-05-02 16:17:02 +00:00
2017-11-13 21:42:07 +00:00
Expect(err).Should(BeNil())
Expect(sqlxdb).ShouldNot(BeNil())
2017-11-13 21:42:07 +00:00
})
It("serializes big.Int to db", func() {
// postgres driver doesn't support go big.Int type
// various casts in golang uint64, int64, overflow for
// transaction value (in wei) even though
// postgres numeric can handle an arbitrary
// sized int, so use string representation of big.Int
// and cast on insert
pgConnectString := config.DbConnectionString(test_config.DBConfig)
db, err := sqlx.Connect("postgres", pgConnectString)
Expect(err).NotTo(HaveOccurred())
bi := new(big.Int)
bi.SetString("34940183920000000000", 10)
Expect(bi.String()).To(Equal("34940183920000000000"))
defer db.Exec(`DROP TABLE IF EXISTS example`)
_, err = db.Exec("CREATE TABLE example ( id INTEGER, data NUMERIC )")
Expect(err).ToNot(HaveOccurred())
sqlStatement := `
INSERT INTO example (id, data)
VALUES (1, cast($1 AS NUMERIC))`
_, err = db.Exec(sqlStatement, bi.String())
Expect(err).ToNot(HaveOccurred())
var data string
err = db.QueryRow(`SELECT data FROM example WHERE id = 1`).Scan(&data)
Expect(err).ToNot(HaveOccurred())
Expect(bi.String()).To(Equal(data))
actual := new(big.Int)
actual.SetString(data, 10)
Expect(actual).To(Equal(bi))
})
2017-11-13 21:42:07 +00:00
It("does not commit block if block is invalid", func() {
//badNonce violates db Nonce field length
badNonce := fmt.Sprintf("x %s", strings.Repeat("1", 100))
badBlock := core.Block{
Number: 123,
Nonce: badNonce,
Transactions: []core.Transaction{},
}
node := core.Node{GenesisBlock: "GENESIS", NetworkID: 1, ID: "x123", ClientName: "geth"}
db := test_config.NewTestDB(node)
test_config.CleanTestDB(db)
blocksRepository := repositories.NewBlockRepository(db)
2017-11-13 21:42:07 +00:00
2018-05-02 16:17:02 +00:00
_, err1 := blocksRepository.CreateOrUpdateBlock(badBlock)
2017-11-13 21:42:07 +00:00
Expect(err1).To(HaveOccurred())
2018-05-02 16:17:02 +00:00
savedBlock, err2 := blocksRepository.GetBlock(123)
Expect(err2).To(HaveOccurred())
Expect(savedBlock).To(BeZero())
2017-11-13 21:42:07 +00:00
})
It("throws error when can't connect to the database", func() {
invalidDatabase := config.Database{}
node := core.Node{GenesisBlock: "GENESIS", NetworkID: 1, ID: "x123", ClientName: "geth"}
2018-05-02 16:17:02 +00:00
_, err := postgres.NewDB(invalidDatabase, node)
2018-05-02 16:17:02 +00:00
Expect(err).To(Equal(postgres.ErrDBConnectionFailed))
})
It("throws error when can't create node", func() {
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
node := core.Node{GenesisBlock: badHash, NetworkID: 1, ID: "x123", ClientName: "geth"}
_, err := postgres.NewDB(test_config.DBConfig, node)
Expect(err).To(Equal(postgres.ErrUnableToSetNode))
})
It("does not commit log if log is invalid", func() {
//badTxHash violates db tx_hash field length
badTxHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
badLog := core.Log{
Address: "x123",
BlockNumber: 1,
TxHash: badTxHash,
}
node := core.Node{GenesisBlock: "GENESIS", NetworkID: 1, ID: "x123", ClientName: "geth"}
db, _ := postgres.NewDB(test_config.DBConfig, node)
logRepository := repositories.LogRepository{DB: db}
2018-05-02 16:17:02 +00:00
err := logRepository.CreateLogs([]core.Log{badLog}, 123)
Expect(err).ToNot(BeNil())
2018-05-02 16:17:02 +00:00
savedBlock := logRepository.GetLogs("x123", 1)
Expect(savedBlock).To(BeNil())
})
2017-11-13 21:42:07 +00:00
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))
badTransaction := core.Transaction{To: badHash}
block := core.Block{
Number: 123,
Transactions: []core.Transaction{badTransaction},
}
node := core.Node{GenesisBlock: "GENESIS", NetworkID: 1, ID: "x123", ClientName: "geth"}
db, _ := postgres.NewDB(test_config.DBConfig, node)
blockRepository := repositories.NewBlockRepository(db)
2017-11-13 21:42:07 +00:00
2018-05-02 16:17:02 +00:00
_, err1 := blockRepository.CreateOrUpdateBlock(block)
2017-11-13 21:42:07 +00:00
Expect(err1).To(HaveOccurred())
2018-05-02 16:17:02 +00:00
savedBlock, err2 := blockRepository.GetBlock(123)
Expect(err2).To(HaveOccurred())
Expect(savedBlock).To(BeZero())
2017-11-13 21:42:07 +00:00
})
})