Add fields to block tables

This commit is contained in:
Matt Krump 2017-11-02 09:36:53 -05:00
parent 4733b25f54
commit df09cf8d93
8 changed files with 111 additions and 28 deletions

View File

@ -1,9 +1,15 @@
package core
type Block struct {
Number int64
Difficulty int64
GasLimit int64
GasUsed int64
Hash string
Nonce string
Number int64
ParentHash string
Size int64
Time int64
Transactions []Transaction
UncleHash string
}

View File

@ -4,6 +4,7 @@ import (
"github.com/8thlight/vulcanizedb/core"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"strconv"
)
func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction {
@ -26,11 +27,17 @@ func GethBlockToCoreBlock(gethBlock *types.Block) core.Block {
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
}
return core.Block{
Number: gethBlock.Number().Int64(),
Difficulty: gethBlock.Difficulty().Int64(),
GasLimit: gethBlock.GasLimit().Int64(),
GasUsed: gethBlock.GasUsed().Int64(),
Hash: gethBlock.Hash().Hex(),
Nonce: strconv.FormatUint(gethBlock.Nonce(), 10),
Number: gethBlock.Number().Int64(),
ParentHash: gethBlock.ParentHash().Hex(),
Size: gethBlock.Size().Int64(),
Time: gethBlock.Time().Int64(),
Transactions: transactions,
UncleHash: gethBlock.UncleHash().Hex(),
}
}

View File

@ -8,29 +8,42 @@ import (
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"strconv"
)
var _ = Describe("Conversion of GethBlock to core.Block", func() {
It("converts basic Block metada", func() {
blockNumber := int64(1)
gasUsed := int64(100000)
difficulty := big.NewInt(1)
gasLimit := int64(100000)
gasUsed := int64(100000)
nonce := types.BlockNonce{10}
number := int64(1)
time := int64(140000000)
header := types.Header{
GasUsed: big.NewInt(gasUsed),
Number: big.NewInt(blockNumber),
Time: big.NewInt(time),
GasLimit: big.NewInt(gasLimit),
Difficulty: difficulty,
GasLimit: big.NewInt(gasLimit),
GasUsed: big.NewInt(gasUsed),
Nonce: nonce,
Number: big.NewInt(number),
ParentHash: common.Hash{64},
Time: big.NewInt(time),
UncleHash: common.Hash{128},
}
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
gethBlock := geth.GethBlockToCoreBlock(block)
Expect(gethBlock.Number).To(Equal(blockNumber))
Expect(gethBlock.GasUsed).To(Equal(gasUsed))
Expect(gethBlock.Difficulty).To(Equal(difficulty.Int64()))
Expect(gethBlock.GasLimit).To(Equal(gasLimit))
Expect(gethBlock.GasUsed).To(Equal(gasUsed))
Expect(gethBlock.Hash).To(Equal(block.Hash().Hex()))
Expect(gethBlock.Nonce).To(Equal((strconv.FormatUint(block.Nonce(), 10))))
Expect(gethBlock.Number).To(Equal(number))
Expect(gethBlock.ParentHash).To(Equal(block.ParentHash().Hex()))
Expect(gethBlock.Size).To(Equal(block.Size().Int64()))
Expect(gethBlock.Time).To(Equal(time))
Expect(gethBlock.UncleHash).To(Equal(block.UncleHash().Hex()))
})
Describe("the converted transations", func() {

View File

@ -0,0 +1,7 @@
ALTER TABLE blocks
Drop COLUMN block_difficulty,
Drop COLUMN block_hash,
drop COLUMN block_nonce,
drop COLUMN block_parenthash,
drop COLUMN block_size,
drop COLUMN uncle_hash

View File

@ -0,0 +1,7 @@
ALTER TABLE blocks
ADD COLUMN block_difficulty BIGINT,
ADD COLUMN block_hash VARCHAR(66),
ADD COLUMN block_nonce VARCHAR(20),
ADD COLUMN block_parenthash VARCHAR(66),
ADD COLUMN block_size BIGINT,
ADD COLUMN uncle_hash VARCHAR(66)

View File

@ -43,7 +43,13 @@ CREATE TABLE blocks (
block_gaslimit double precision,
block_gasused double precision,
block_time double precision,
id integer NOT NULL
id integer NOT NULL,
block_difficulty bigint,
block_hash character varying(66),
block_nonce character varying(20),
block_parenthash character varying(66),
block_size bigint,
uncle_hash character varying(66)
);

View File

@ -16,10 +16,11 @@ func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) {
}
func saveBlock(observer BlockchainDBObserver, block core.Block) int64 {
insertedBlock := observer.Db.QueryRow("Insert INTO blocks "+
"(block_number, block_gaslimit, block_gasused, block_time) "+
"VALUES ($1, $2, $3, $4) RETURNING id",
block.Number, block.GasLimit, block.GasUsed, block.Time)
insertedBlock := observer.Db.QueryRow(
"Insert INTO blocks "+
"(block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash) "+
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id",
block.Number, block.GasLimit, block.GasUsed, block.Time, block.Difficulty, block.Hash, block.Nonce, block.ParentHash, block.Size, block.UncleHash)
var blockId int64
insertedBlock.Scan(&blockId)
return blockId

View File

@ -48,37 +48,73 @@ var _ = Describe("Saving blocks to the database", func() {
blockNumber := int64(123)
gasLimit := int64(1000000)
gasUsed := int64(10)
blockHash := "x123"
blockParentHash := "x456"
blockNonce := "0x881db2ca900682e9a9"
blockTime := int64(1508981640)
block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
uncleHash := "x789"
blockSize := int64(1000)
difficulty := int64(10)
block := core.Block{
Difficulty: difficulty,
GasLimit: gasLimit,
GasUsed: gasUsed,
Hash: blockHash,
Nonce: blockNonce,
Number: blockNumber,
ParentHash: blockParentHash,
Size: blockSize,
Time: blockTime,
UncleHash: uncleHash,
}
// save the block to the database
observer := observers.BlockchainDBObserver{Db: db}
observer.NotifyBlockAdded(block)
// find the saved block
rows, err := db.Query("SELECT block_number, block_gaslimit, block_gasused, block_time FROM blocks")
rows, err := db.Query(
"SELECT block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash FROM blocks")
Expect(err).To(BeNil())
var savedBlocks []core.Block
for rows.Next() {
var blockHash string
var blockNonce string
var blockNumber int64
var blockParentHash string
var blockSize int64
var blockTime float64
var difficulty int64
var gasLimit float64
var gasUsed float64
rows.Scan(&blockNumber, &gasLimit, &gasUsed, &blockTime)
var uncleHash string
rows.Scan(&blockNumber, &gasLimit, &gasUsed, &blockTime, &difficulty, &blockHash, &blockNonce, &blockParentHash, &blockSize, &uncleHash)
savedBlock := core.Block{
GasLimit: int64(gasLimit),
GasUsed: int64(gasUsed),
Number: blockNumber,
Time: int64(blockTime),
Difficulty: difficulty,
GasLimit: int64(gasLimit),
GasUsed: int64(gasUsed),
Hash: blockHash,
Nonce: blockNonce,
Number: blockNumber,
ParentHash: blockParentHash,
Size: blockSize,
Time: int64(blockTime),
UncleHash: uncleHash,
}
savedBlocks = append(savedBlocks, savedBlock)
}
// assert against the attributes
Expect(len(savedBlocks)).To(Equal(1))
Expect(savedBlocks[0].Number).To(Equal(blockNumber))
Expect(savedBlocks[0].Difficulty).To(Equal(difficulty))
Expect(savedBlocks[0].GasLimit).To(Equal(gasLimit))
Expect(savedBlocks[0].GasUsed).To(Equal(gasUsed))
Expect(savedBlocks[0].Hash).To(Equal(blockHash))
Expect(savedBlocks[0].Nonce).To(Equal(blockNonce))
Expect(savedBlocks[0].Number).To(Equal(blockNumber))
Expect(savedBlocks[0].ParentHash).To(Equal(blockParentHash))
Expect(savedBlocks[0].Size).To(Equal(blockSize))
Expect(savedBlocks[0].Time).To(Equal(blockTime))
Expect(savedBlocks[0].UncleHash).To(Equal(uncleHash))
})
var _ = Describe("Saving transactions to the database", func() {
@ -116,11 +152,11 @@ var _ = Describe("Saving blocks to the database", func() {
var dbValue int64
rows.Scan(&dbHash, &dbNonce, &dbTo, &dbGasLimit, &dbGasPrice, &dbValue)
savedTransaction := core.Transaction{
GasLimit: dbGasLimit,
GasPrice: dbGasPrice,
Hash: dbHash,
Nonce: dbNonce,
To: dbTo,
GasLimit: dbGasLimit,
GasPrice: dbGasPrice,
Value: dbValue,
}
savedTransactions = append(savedTransactions, savedTransaction)
@ -128,11 +164,11 @@ var _ = Describe("Saving blocks to the database", func() {
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.Hash).To(Equal(txRecord.Hash))
Expect(savedTransaction.Nonce).To(Equal(nonce))
Expect(savedTransaction.To).To(Equal(to))
Expect(savedTransaction.Value).To(Equal(value))
})