Remove BlockRecord

This commit is contained in:
Matt Krump 2017-10-27 15:29:55 -05:00
parent 5d297584df
commit e05b1a59dd
4 changed files with 38 additions and 53 deletions

View File

@ -15,24 +15,6 @@ type Block struct {
NumberOfTransactions int
}
//Our Block to DB
func BlockToBlockRecord(block Block) *BlockRecord {
return &BlockRecord{
BlockNumber: block.Number.Int64(),
GasLimit: block.GasLimit.Int64(),
GasUsed: block.GasUsed.Int64(),
Time: block.Time.Int64(),
}
}
//DB block representation
type BlockRecord struct {
BlockNumber int64 `db:"block_number"`
GasLimit int64 `db:"block_gaslimit"`
GasUsed int64 `db:"block_gasused"`
Time int64 `db:"block_time"`
}
//Geth Block to Ours
func GethBlockToCoreBlock(gethBlock *types.Block) Block {
return Block{

View File

@ -1,24 +0,0 @@
package core
import (
"math/big"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Converting core.Block to DB record", func() {
It("Converts core.Block to BlockRecord", func() {
blockNumber := big.NewInt(1)
gasLimit := big.NewInt(100000)
gasUsed := big.NewInt(10)
blockTime := big.NewInt(1508981640)
block := Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
blockRecord := BlockToBlockRecord(block)
Expect(blockRecord.BlockNumber).To(Equal(int64(1)))
Expect(blockRecord.GasLimit).To(Equal(int64(100000)))
Expect(blockRecord.GasUsed).To(Equal(int64(10)))
})
})

View File

@ -10,10 +10,8 @@ type BlockchainDBObserver struct {
}
func (observer BlockchainDBObserver) NotifyBlockAdded(block Block) {
blockRecord := BlockToBlockRecord(block)
observer.Db.NamedExec(
"INSERT INTO blocks "+
observer.Db.MustExec("Insert INTO blocks "+
"(block_number, block_gaslimit, block_gasused, block_time) "+
"VALUES (:block_number, :block_gaslimit, :block_gasused, :block_time)", blockRecord)
//observer.Db.MustExec("Insert INTO blocks (block_number) VALUES ($1)", block.Number.Int64())
"VALUES ($1, $2, $3, $4)",
block.Number.Int64(), block.GasLimit.Int64(), block.GasUsed.Int64(), block.Time.Int64())
}

View File

@ -6,6 +6,8 @@ import (
"fmt"
"github.com/8thlight/vulcanizedb/core"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
. "github.com/onsi/ginkgo"
@ -24,8 +26,19 @@ var _ = Describe("Saving blocks to the database", func() {
var db *sqlx.DB
var err error
var gethTransaction *types.Transaction
BeforeEach(func() {
blockName := []byte("0x28f9a8d33109c87bda4a9ea890792421c710fe1c")
addr := common.BytesToAddress(blockName)
nonce := uint64(18848)
amt := big.NewInt(0)
gasLimit := big.NewInt(0)
gasPrice := big.NewInt(0)
data := []byte{}
gethTransaction = types.NewTransaction(nonce, addr, amt, gasLimit, gasPrice, data)
pgConfig := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
@ -51,25 +64,41 @@ var _ = Describe("Saving blocks to the database", func() {
})
It("inserts a block", func() {
// setup a block in memory
blockNumber := big.NewInt(1)
gasLimit := big.NewInt(1000000)
gasUsed := big.NewInt(10)
blockTime := big.NewInt(1508981640)
block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
// save the block to the database
observer := core.BlockchainDBObserver{Db: db}
observer.NotifyBlockAdded(block)
rows, err := db.Queryx("SELECT * FROM blocks")
// find the saved block
rows, err := db.Query("SELECT block_number, block_gaslimit, block_gasused, block_time FROM blocks")
Expect(err).To(BeNil())
var savedBlocks []core.BlockRecord
var savedBlocks []core.Block
for rows.Next() {
var savedBlock core.BlockRecord
rows.StructScan(&savedBlock)
var blockNumber int64
var blockTime float64
var gasLimit float64
var gasUsed float64
rows.Scan(&blockNumber, &gasLimit, &gasUsed, &blockTime)
savedBlock := core.Block{
GasUsed: big.NewInt(int64(gasUsed)),
GasLimit: big.NewInt(int64(gasLimit)),
Number: big.NewInt(blockNumber),
Time: big.NewInt(int64(blockTime)),
}
savedBlocks = append(savedBlocks, savedBlock)
}
// assert against the attributes
Expect(len(savedBlocks)).To(Equal(1))
Expect(savedBlocks[0].BlockNumber)
Expect(savedBlocks[0].Number.Int64()).To(Equal(blockNumber.Int64()))
Expect(savedBlocks[0].GasLimit.Int64()).To(Equal(gasLimit.Int64()))
Expect(savedBlocks[0].GasUsed.Int64()).To(Equal(gasUsed.Int64()))
Expect(savedBlocks[0].Time).To(Equal(blockTime))
})
})