2017-11-13 21:42:07 +00:00
|
|
|
package repositories_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-12-19 20:14:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
2017-11-13 21:42:07 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/config"
|
|
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
|
|
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
|
|
|
"github.com/8thlight/vulcanizedb/pkg/repositories/testing"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2017-12-19 20:14:41 +00:00
|
|
|
func init() {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
2017-11-13 21:42:07 +00:00
|
|
|
var _ = Describe("Postgres repository", func() {
|
|
|
|
|
|
|
|
It("connects to the database", func() {
|
|
|
|
cfg, _ := config.NewConfig("private")
|
|
|
|
pgConfig := config.DbConnectionString(cfg.Database)
|
|
|
|
db, err := sqlx.Connect("postgres", pgConfig)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(db).ShouldNot(BeNil())
|
|
|
|
})
|
|
|
|
|
2017-12-07 19:32:16 +00:00
|
|
|
testing.AssertRepositoryBehavior(func(node core.Node) repositories.Repository {
|
2017-11-13 21:42:07 +00:00
|
|
|
cfg, _ := config.NewConfig("private")
|
2017-12-07 19:32:16 +00:00
|
|
|
repository, _ := repositories.NewPostgres(cfg.Database, node)
|
2017-11-13 21:42:07 +00:00
|
|
|
testing.ClearData(repository)
|
|
|
|
return repository
|
|
|
|
})
|
|
|
|
|
|
|
|
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{},
|
|
|
|
}
|
|
|
|
cfg, _ := config.NewConfig("private")
|
2017-12-07 19:32:16 +00:00
|
|
|
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1}
|
|
|
|
repository, _ := repositories.NewPostgres(cfg.Database, node)
|
2017-11-13 21:42:07 +00:00
|
|
|
|
2017-12-19 20:14:41 +00:00
|
|
|
err1 := repository.CreateOrUpdateBlock(badBlock)
|
2017-12-13 16:51:11 +00:00
|
|
|
savedBlock, err2 := repository.FindBlockByNumber(123)
|
2017-11-13 21:42:07 +00:00
|
|
|
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err1).To(HaveOccurred())
|
|
|
|
Expect(err2).To(HaveOccurred())
|
|
|
|
Expect(savedBlock).To(BeZero())
|
2017-11-13 21:42:07 +00:00
|
|
|
})
|
|
|
|
|
2017-12-04 15:53:36 +00:00
|
|
|
It("throws error when can't connect to the database", func() {
|
|
|
|
invalidDatabase := config.Database{}
|
2017-12-07 19:32:16 +00:00
|
|
|
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1}
|
|
|
|
_, err := repositories.NewPostgres(invalidDatabase, node)
|
2017-12-04 15:53:36 +00:00
|
|
|
Expect(err).To(Equal(repositories.ErrDBConnectionFailed))
|
|
|
|
})
|
|
|
|
|
2017-12-07 19:32:16 +00:00
|
|
|
It("throws error when can't create node", func() {
|
|
|
|
cfg, _ := config.NewConfig("private")
|
|
|
|
badHash := fmt.Sprintf("x %s", strings.Repeat("1", 100))
|
|
|
|
node := core.Node{GenesisBlock: badHash, NetworkId: 1}
|
|
|
|
_, err := repositories.NewPostgres(cfg.Database, node)
|
|
|
|
Expect(err).To(Equal(repositories.ErrUnableToSetNode))
|
|
|
|
})
|
|
|
|
|
2017-12-12 21:55:26 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
cfg, _ := config.NewConfig("private")
|
|
|
|
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1}
|
|
|
|
repository, _ := repositories.NewPostgres(cfg.Database, node)
|
|
|
|
|
|
|
|
err := repository.CreateLogs([]core.Log{badLog})
|
|
|
|
savedBlock := repository.FindLogs("x123", 1)
|
|
|
|
|
|
|
|
Expect(err).ToNot(BeNil())
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
cfg, _ := config.NewConfig("private")
|
2017-12-07 19:32:16 +00:00
|
|
|
node := core.Node{GenesisBlock: "GENESIS", NetworkId: 1}
|
|
|
|
repository, _ := repositories.NewPostgres(cfg.Database, node)
|
2017-11-13 21:42:07 +00:00
|
|
|
|
2017-12-19 20:14:41 +00:00
|
|
|
err1 := repository.CreateOrUpdateBlock(block)
|
2017-12-13 16:51:11 +00:00
|
|
|
savedBlock, err2 := repository.FindBlockByNumber(123)
|
2017-11-13 21:42:07 +00:00
|
|
|
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err1).To(HaveOccurred())
|
|
|
|
Expect(err2).To(HaveOccurred())
|
|
|
|
Expect(savedBlock).To(BeZero())
|
2017-11-13 21:42:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|