2017-11-02 19:37:07 +00:00
|
|
|
package observers_test
|
2017-10-25 15:57:59 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-02 22:37:27 +00:00
|
|
|
"runtime"
|
|
|
|
|
2017-11-01 15:17:01 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/config"
|
2017-10-25 15:57:59 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/core"
|
2017-11-02 19:37:07 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/observers"
|
2017-10-25 15:57:59 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
var (
|
|
|
|
_, filename, _, _ = runtime.Caller(0)
|
|
|
|
)
|
|
|
|
|
2017-10-25 15:57:59 +00:00
|
|
|
var _ = Describe("Saving blocks to the database", func() {
|
|
|
|
|
|
|
|
var db *sqlx.DB
|
|
|
|
var err error
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2017-11-02 22:37:27 +00:00
|
|
|
cfg := config.NewConfig("private")
|
|
|
|
pgConfig := config.DbConnectionString(cfg.Database)
|
2017-10-25 15:57:59 +00:00
|
|
|
db, err = sqlx.Connect("postgres", pgConfig)
|
2017-10-31 13:58:04 +00:00
|
|
|
db.MustExec("DELETE FROM transactions")
|
2017-10-31 15:36:37 +00:00
|
|
|
db.MustExec("DELETE FROM blocks")
|
2017-10-25 15:57:59 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 15:17:01 +00:00
|
|
|
AfterEach(func() {
|
|
|
|
db.Close()
|
|
|
|
})
|
|
|
|
|
2017-10-25 15:57:59 +00:00
|
|
|
It("implements the observer interface", func() {
|
2017-11-02 19:37:07 +00:00
|
|
|
var observer core.BlockchainObserver = observers.BlockchainDBObserver{Db: db}
|
2017-10-25 15:57:59 +00:00
|
|
|
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() {
|
2017-10-27 20:29:55 +00:00
|
|
|
// setup a block in memory
|
2017-10-31 17:51:05 +00:00
|
|
|
blockNumber := int64(123)
|
|
|
|
gasLimit := int64(1000000)
|
|
|
|
gasUsed := int64(10)
|
|
|
|
blockTime := int64(1508981640)
|
2017-10-25 22:24:38 +00:00
|
|
|
block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
|
2017-10-25 15:57:59 +00:00
|
|
|
|
2017-10-27 20:29:55 +00:00
|
|
|
// save the block to the database
|
2017-11-02 19:37:07 +00:00
|
|
|
observer := observers.BlockchainDBObserver{Db: db}
|
2017-10-25 15:57:59 +00:00
|
|
|
observer.NotifyBlockAdded(block)
|
|
|
|
|
2017-10-27 20:29:55 +00:00
|
|
|
// find the saved block
|
|
|
|
rows, err := db.Query("SELECT block_number, block_gaslimit, block_gasused, block_time FROM blocks")
|
2017-10-25 15:57:59 +00:00
|
|
|
Expect(err).To(BeNil())
|
2017-10-27 20:29:55 +00:00
|
|
|
var savedBlocks []core.Block
|
2017-10-25 15:57:59 +00:00
|
|
|
for rows.Next() {
|
2017-10-27 20:29:55 +00:00
|
|
|
var blockNumber int64
|
|
|
|
var blockTime float64
|
|
|
|
var gasLimit float64
|
|
|
|
var gasUsed float64
|
|
|
|
rows.Scan(&blockNumber, &gasLimit, &gasUsed, &blockTime)
|
|
|
|
savedBlock := core.Block{
|
2017-10-31 17:51:05 +00:00
|
|
|
GasLimit: int64(gasLimit),
|
|
|
|
GasUsed: int64(gasUsed),
|
|
|
|
Number: blockNumber,
|
|
|
|
Time: int64(blockTime),
|
2017-10-27 20:29:55 +00:00
|
|
|
}
|
2017-10-25 15:57:59 +00:00
|
|
|
savedBlocks = append(savedBlocks, savedBlock)
|
|
|
|
}
|
2017-10-27 20:29:55 +00:00
|
|
|
// assert against the attributes
|
2017-10-25 15:57:59 +00:00
|
|
|
Expect(len(savedBlocks)).To(Equal(1))
|
2017-10-31 17:51:05 +00:00
|
|
|
Expect(savedBlocks[0].Number).To(Equal(blockNumber))
|
|
|
|
Expect(savedBlocks[0].GasLimit).To(Equal(gasLimit))
|
|
|
|
Expect(savedBlocks[0].GasUsed).To(Equal(gasUsed))
|
2017-10-27 20:29:55 +00:00
|
|
|
Expect(savedBlocks[0].Time).To(Equal(blockTime))
|
2017-10-25 15:57:59 +00:00
|
|
|
})
|
|
|
|
|
2017-10-31 13:58:04 +00:00
|
|
|
var _ = Describe("Saving transactions to the database", func() {
|
|
|
|
|
|
|
|
It("inserts a transaction", func() {
|
|
|
|
gasLimit := int64(5000)
|
|
|
|
gasPrice := int64(3)
|
|
|
|
nonce := uint64(10000)
|
|
|
|
to := "1234567890"
|
|
|
|
value := int64(10)
|
|
|
|
|
|
|
|
txRecord := core.Transaction{
|
|
|
|
Hash: "x1234",
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
GasLimit: gasLimit,
|
|
|
|
Nonce: nonce,
|
|
|
|
To: to,
|
|
|
|
Value: value,
|
|
|
|
}
|
2017-10-31 17:51:05 +00:00
|
|
|
block := core.Block{Transactions: []core.Transaction{txRecord}}
|
2017-10-31 13:58:04 +00:00
|
|
|
|
2017-11-02 19:37:07 +00:00
|
|
|
observer := observers.BlockchainDBObserver{Db: db}
|
2017-10-31 13:58:04 +00:00
|
|
|
observer.NotifyBlockAdded(block)
|
|
|
|
|
|
|
|
rows, err := db.Query("SELECT tx_hash, tx_nonce, tx_to, tx_gaslimit, tx_gasprice, tx_value FROM transactions")
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
|
|
|
var savedTransactions []core.Transaction
|
|
|
|
for rows.Next() {
|
|
|
|
var dbHash string
|
|
|
|
var dbNonce uint64
|
|
|
|
var dbTo string
|
|
|
|
var dbGasLimit int64
|
|
|
|
var dbGasPrice int64
|
|
|
|
var dbValue int64
|
|
|
|
rows.Scan(&dbHash, &dbNonce, &dbTo, &dbGasLimit, &dbGasPrice, &dbValue)
|
|
|
|
savedTransaction := core.Transaction{
|
|
|
|
Hash: dbHash,
|
|
|
|
Nonce: dbNonce,
|
|
|
|
To: dbTo,
|
|
|
|
GasLimit: dbGasLimit,
|
|
|
|
GasPrice: dbGasPrice,
|
|
|
|
Value: dbValue,
|
|
|
|
}
|
|
|
|
savedTransactions = append(savedTransactions, savedTransaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(len(savedTransactions)).To(Equal(1))
|
|
|
|
savedTransaction := savedTransactions[0]
|
|
|
|
Expect(savedTransaction.Hash).To(Equal(txRecord.Hash))
|
|
|
|
Expect(savedTransaction.To).To(Equal(to))
|
|
|
|
Expect(savedTransaction.Nonce).To(Equal(nonce))
|
|
|
|
Expect(savedTransaction.GasLimit).To(Equal(gasLimit))
|
|
|
|
Expect(savedTransaction.GasPrice).To(Equal(gasPrice))
|
|
|
|
Expect(savedTransaction.Value).To(Equal(value))
|
|
|
|
})
|
2017-10-31 15:36:37 +00:00
|
|
|
|
|
|
|
It("associates the transaction with the block", func() {
|
|
|
|
txRecord := core.Transaction{}
|
|
|
|
block := core.Block{
|
|
|
|
Transactions: []core.Transaction{txRecord},
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:37:07 +00:00
|
|
|
observer := observers.BlockchainDBObserver{Db: db}
|
2017-10-31 15:36:37 +00:00
|
|
|
observer.NotifyBlockAdded(block)
|
|
|
|
|
|
|
|
blockRows, err := db.Query("SELECT id FROM blocks")
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
|
|
|
var actualBlockIds []int64
|
|
|
|
for blockRows.Next() {
|
|
|
|
var actualBlockId int64
|
|
|
|
blockRows.Scan(&actualBlockId)
|
|
|
|
actualBlockIds = append(actualBlockIds, actualBlockId)
|
|
|
|
}
|
|
|
|
|
|
|
|
transactionRows, err := db.Query("SELECT block_id FROM transactions")
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
|
|
|
var transactionBlockIds []int64
|
|
|
|
for transactionRows.Next() {
|
|
|
|
var transactionBlockId int64
|
|
|
|
transactionRows.Scan(&transactionBlockId)
|
|
|
|
transactionBlockIds = append(transactionBlockIds, transactionBlockId)
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(len(actualBlockIds)).To(Equal(1))
|
|
|
|
Expect(len(transactionBlockIds)).To(Equal(1))
|
|
|
|
Expect(transactionBlockIds[0]).To(Equal(actualBlockIds[0]))
|
|
|
|
})
|
2017-10-31 13:58:04 +00:00
|
|
|
})
|
|
|
|
|
2017-10-25 15:57:59 +00:00
|
|
|
})
|