forked from cerc-io/ipld-eth-server
Add fields to block tables
This commit is contained in:
parent
4733b25f54
commit
df09cf8d93
@ -1,9 +1,15 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
Number int64
|
Difficulty int64
|
||||||
GasLimit int64
|
GasLimit int64
|
||||||
GasUsed int64
|
GasUsed int64
|
||||||
|
Hash string
|
||||||
|
Nonce string
|
||||||
|
Number int64
|
||||||
|
ParentHash string
|
||||||
|
Size int64
|
||||||
Time int64
|
Time int64
|
||||||
Transactions []Transaction
|
Transactions []Transaction
|
||||||
|
UncleHash string
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/8thlight/vulcanizedb/core"
|
"github.com/8thlight/vulcanizedb/core"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction {
|
func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction {
|
||||||
@ -26,11 +27,17 @@ func GethBlockToCoreBlock(gethBlock *types.Block) core.Block {
|
|||||||
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
|
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
|
||||||
}
|
}
|
||||||
return core.Block{
|
return core.Block{
|
||||||
Number: gethBlock.Number().Int64(),
|
Difficulty: gethBlock.Difficulty().Int64(),
|
||||||
GasLimit: gethBlock.GasLimit().Int64(),
|
GasLimit: gethBlock.GasLimit().Int64(),
|
||||||
GasUsed: gethBlock.GasUsed().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(),
|
Time: gethBlock.Time().Int64(),
|
||||||
Transactions: transactions,
|
Transactions: transactions,
|
||||||
|
UncleHash: gethBlock.UncleHash().Hex(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,29 +8,42 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||||
|
|
||||||
It("converts basic Block metada", func() {
|
It("converts basic Block metada", func() {
|
||||||
blockNumber := int64(1)
|
difficulty := big.NewInt(1)
|
||||||
gasUsed := int64(100000)
|
|
||||||
gasLimit := int64(100000)
|
gasLimit := int64(100000)
|
||||||
|
gasUsed := int64(100000)
|
||||||
|
nonce := types.BlockNonce{10}
|
||||||
|
number := int64(1)
|
||||||
time := int64(140000000)
|
time := int64(140000000)
|
||||||
|
|
||||||
header := types.Header{
|
header := types.Header{
|
||||||
GasUsed: big.NewInt(gasUsed),
|
Difficulty: difficulty,
|
||||||
Number: big.NewInt(blockNumber),
|
|
||||||
Time: big.NewInt(time),
|
|
||||||
GasLimit: big.NewInt(gasLimit),
|
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{})
|
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
|
||||||
gethBlock := geth.GethBlockToCoreBlock(block)
|
gethBlock := geth.GethBlockToCoreBlock(block)
|
||||||
|
|
||||||
Expect(gethBlock.Number).To(Equal(blockNumber))
|
Expect(gethBlock.Difficulty).To(Equal(difficulty.Int64()))
|
||||||
Expect(gethBlock.GasUsed).To(Equal(gasUsed))
|
|
||||||
Expect(gethBlock.GasLimit).To(Equal(gasLimit))
|
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.Time).To(Equal(time))
|
||||||
|
Expect(gethBlock.UncleHash).To(Equal(block.UncleHash().Hex()))
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("the converted transations", func() {
|
Describe("the converted transations", func() {
|
||||||
|
7
migrations/1509633481_add_blocks_columns.down.sql
Normal file
7
migrations/1509633481_add_blocks_columns.down.sql
Normal 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
|
7
migrations/1509633481_add_blocks_columns.up.sql
Normal file
7
migrations/1509633481_add_blocks_columns.up.sql
Normal 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)
|
@ -43,7 +43,13 @@ CREATE TABLE blocks (
|
|||||||
block_gaslimit double precision,
|
block_gaslimit double precision,
|
||||||
block_gasused double precision,
|
block_gasused double precision,
|
||||||
block_time 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)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,10 +16,11 @@ func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func saveBlock(observer BlockchainDBObserver, block core.Block) int64 {
|
func saveBlock(observer BlockchainDBObserver, block core.Block) int64 {
|
||||||
insertedBlock := observer.Db.QueryRow("Insert INTO blocks "+
|
insertedBlock := observer.Db.QueryRow(
|
||||||
"(block_number, block_gaslimit, block_gasused, block_time) "+
|
"Insert INTO blocks "+
|
||||||
"VALUES ($1, $2, $3, $4) RETURNING id",
|
"(block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash) "+
|
||||||
block.Number, block.GasLimit, block.GasUsed, block.Time)
|
"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
|
var blockId int64
|
||||||
insertedBlock.Scan(&blockId)
|
insertedBlock.Scan(&blockId)
|
||||||
return blockId
|
return blockId
|
||||||
|
@ -48,37 +48,73 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
blockNumber := int64(123)
|
blockNumber := int64(123)
|
||||||
gasLimit := int64(1000000)
|
gasLimit := int64(1000000)
|
||||||
gasUsed := int64(10)
|
gasUsed := int64(10)
|
||||||
|
blockHash := "x123"
|
||||||
|
blockParentHash := "x456"
|
||||||
|
blockNonce := "0x881db2ca900682e9a9"
|
||||||
blockTime := int64(1508981640)
|
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
|
// save the block to the database
|
||||||
observer := observers.BlockchainDBObserver{Db: db}
|
observer := observers.BlockchainDBObserver{Db: db}
|
||||||
observer.NotifyBlockAdded(block)
|
observer.NotifyBlockAdded(block)
|
||||||
|
|
||||||
// find the saved 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())
|
Expect(err).To(BeNil())
|
||||||
var savedBlocks []core.Block
|
var savedBlocks []core.Block
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
|
var blockHash string
|
||||||
|
var blockNonce string
|
||||||
var blockNumber int64
|
var blockNumber int64
|
||||||
|
var blockParentHash string
|
||||||
|
var blockSize int64
|
||||||
var blockTime float64
|
var blockTime float64
|
||||||
|
var difficulty int64
|
||||||
var gasLimit float64
|
var gasLimit float64
|
||||||
var gasUsed 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{
|
savedBlock := core.Block{
|
||||||
|
Difficulty: difficulty,
|
||||||
GasLimit: int64(gasLimit),
|
GasLimit: int64(gasLimit),
|
||||||
GasUsed: int64(gasUsed),
|
GasUsed: int64(gasUsed),
|
||||||
|
Hash: blockHash,
|
||||||
|
Nonce: blockNonce,
|
||||||
Number: blockNumber,
|
Number: blockNumber,
|
||||||
|
ParentHash: blockParentHash,
|
||||||
|
Size: blockSize,
|
||||||
Time: int64(blockTime),
|
Time: int64(blockTime),
|
||||||
|
UncleHash: uncleHash,
|
||||||
}
|
}
|
||||||
savedBlocks = append(savedBlocks, savedBlock)
|
savedBlocks = append(savedBlocks, savedBlock)
|
||||||
}
|
}
|
||||||
// assert against the attributes
|
// assert against the attributes
|
||||||
Expect(len(savedBlocks)).To(Equal(1))
|
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].GasLimit).To(Equal(gasLimit))
|
||||||
Expect(savedBlocks[0].GasUsed).To(Equal(gasUsed))
|
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].Time).To(Equal(blockTime))
|
||||||
|
Expect(savedBlocks[0].UncleHash).To(Equal(uncleHash))
|
||||||
})
|
})
|
||||||
|
|
||||||
var _ = Describe("Saving transactions to the database", func() {
|
var _ = Describe("Saving transactions to the database", func() {
|
||||||
@ -116,11 +152,11 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
var dbValue int64
|
var dbValue int64
|
||||||
rows.Scan(&dbHash, &dbNonce, &dbTo, &dbGasLimit, &dbGasPrice, &dbValue)
|
rows.Scan(&dbHash, &dbNonce, &dbTo, &dbGasLimit, &dbGasPrice, &dbValue)
|
||||||
savedTransaction := core.Transaction{
|
savedTransaction := core.Transaction{
|
||||||
|
GasLimit: dbGasLimit,
|
||||||
|
GasPrice: dbGasPrice,
|
||||||
Hash: dbHash,
|
Hash: dbHash,
|
||||||
Nonce: dbNonce,
|
Nonce: dbNonce,
|
||||||
To: dbTo,
|
To: dbTo,
|
||||||
GasLimit: dbGasLimit,
|
|
||||||
GasPrice: dbGasPrice,
|
|
||||||
Value: dbValue,
|
Value: dbValue,
|
||||||
}
|
}
|
||||||
savedTransactions = append(savedTransactions, savedTransaction)
|
savedTransactions = append(savedTransactions, savedTransaction)
|
||||||
@ -128,11 +164,11 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
|
|
||||||
Expect(len(savedTransactions)).To(Equal(1))
|
Expect(len(savedTransactions)).To(Equal(1))
|
||||||
savedTransaction := savedTransactions[0]
|
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.GasLimit).To(Equal(gasLimit))
|
||||||
Expect(savedTransaction.GasPrice).To(Equal(gasPrice))
|
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))
|
Expect(savedTransaction.Value).To(Equal(value))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user