Refactor block conversion (#38)
Move more of block conversion out of observer
This commit is contained in:
parent
963cc83299
commit
c961e85099
@ -1,16 +1,14 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
type Block struct {
|
||||
Number *big.Int
|
||||
GasLimit *big.Int
|
||||
GasUsed *big.Int
|
||||
Time *big.Int
|
||||
Number int64
|
||||
GasLimit int64
|
||||
GasUsed int64
|
||||
Time int64
|
||||
Transactions []Transaction
|
||||
}
|
||||
|
||||
@ -20,10 +18,10 @@ func GethBlockToCoreBlock(gethBlock *types.Block) Block {
|
||||
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
|
||||
}
|
||||
return Block{
|
||||
Number: gethBlock.Number(),
|
||||
GasLimit: gethBlock.GasLimit(),
|
||||
GasUsed: gethBlock.GasUsed(),
|
||||
Time: gethBlock.Time(),
|
||||
Number: gethBlock.Number().Int64(),
|
||||
GasLimit: gethBlock.GasLimit().Int64(),
|
||||
GasUsed: gethBlock.GasUsed().Int64(),
|
||||
Time: gethBlock.Time().Int64(),
|
||||
Transactions: transactions,
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ func saveBlock(observer BlockchainDBObserver, block 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.Int64(), block.GasLimit.Int64(), block.GasUsed.Int64(), block.Time.Int64())
|
||||
block.Number, block.GasLimit, block.GasUsed, block.Time)
|
||||
var blockId int64
|
||||
insertedBlock.Scan(&blockId)
|
||||
return blockId
|
||||
|
@ -1,8 +1,6 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"fmt"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
@ -53,10 +51,10 @@ 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)
|
||||
blockNumber := int64(123)
|
||||
gasLimit := int64(1000000)
|
||||
gasUsed := int64(10)
|
||||
blockTime := int64(1508981640)
|
||||
block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
|
||||
|
||||
// save the block to the database
|
||||
@ -74,18 +72,18 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
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)),
|
||||
GasLimit: int64(gasLimit),
|
||||
GasUsed: int64(gasUsed),
|
||||
Number: blockNumber,
|
||||
Time: int64(blockTime),
|
||||
}
|
||||
savedBlocks = append(savedBlocks, savedBlock)
|
||||
}
|
||||
// assert against the attributes
|
||||
Expect(len(savedBlocks)).To(Equal(1))
|
||||
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].Number).To(Equal(blockNumber))
|
||||
Expect(savedBlocks[0].GasLimit).To(Equal(gasLimit))
|
||||
Expect(savedBlocks[0].GasUsed).To(Equal(gasUsed))
|
||||
Expect(savedBlocks[0].Time).To(Equal(blockTime))
|
||||
})
|
||||
|
||||
@ -106,10 +104,7 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
To: to,
|
||||
Value: value,
|
||||
}
|
||||
blockNumber := big.NewInt(1)
|
||||
gasUsed := big.NewInt(10)
|
||||
blockTime := big.NewInt(1508981640)
|
||||
block := core.Block{Number: blockNumber, GasLimit: big.NewInt(gasLimit), GasUsed: gasUsed, Time: blockTime, Transactions: []core.Transaction{txRecord}}
|
||||
block := core.Block{Transactions: []core.Transaction{txRecord}}
|
||||
|
||||
observer := core.BlockchainDBObserver{Db: db}
|
||||
observer.NotifyBlockAdded(block)
|
||||
@ -148,17 +143,8 @@ var _ = Describe("Saving blocks to the database", func() {
|
||||
})
|
||||
|
||||
It("associates the transaction with the block", func() {
|
||||
gasLimit := int64(5000)
|
||||
|
||||
txRecord := core.Transaction{}
|
||||
blockNumber := big.NewInt(1)
|
||||
gasUsed := big.NewInt(10)
|
||||
blockTime := big.NewInt(1508981640)
|
||||
block := core.Block{
|
||||
Number: blockNumber,
|
||||
GasLimit: big.NewInt(gasLimit),
|
||||
GasUsed: gasUsed,
|
||||
Time: blockTime,
|
||||
Transactions: []core.Transaction{txRecord},
|
||||
}
|
||||
|
||||
|
@ -12,5 +12,5 @@ func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block Block
|
||||
"\tTime: %v\n"+
|
||||
"\tGas Limit: %d\n"+
|
||||
"\tGas Used: %d\n"+
|
||||
"\tNumber of Transactions %d\n", block.Number, time.Unix(block.Time.Int64(), 0), block.GasLimit, block.GasUsed, len(block.Transactions))
|
||||
"\tNumber of Transactions %d\n", block.Number, time.Unix(block.Time, 0), block.GasLimit, block.GasUsed, len(block.Transactions))
|
||||
}
|
||||
|
@ -13,12 +13,17 @@ import (
|
||||
var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
|
||||
It("converts basic Block metada", func() {
|
||||
blockNumber := big.NewInt(1)
|
||||
gasUsed := big.NewInt(100000)
|
||||
gasLimit := big.NewInt(100000)
|
||||
time := big.NewInt(140000000)
|
||||
blockNumber := int64(1)
|
||||
gasUsed := int64(100000)
|
||||
gasLimit := int64(100000)
|
||||
time := int64(140000000)
|
||||
|
||||
header := types.Header{Number: blockNumber, GasUsed: gasUsed, Time: time, GasLimit: gasLimit}
|
||||
header := types.Header{
|
||||
GasUsed: big.NewInt(gasUsed),
|
||||
Number: big.NewInt(blockNumber),
|
||||
Time: big.NewInt(time),
|
||||
GasLimit: big.NewInt(gasLimit),
|
||||
}
|
||||
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
|
||||
gethBlock := core.GethBlockToCoreBlock(block)
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/core"
|
||||
"github.com/8thlight/vulcanizedb/fakes"
|
||||
|
||||
@ -44,10 +42,10 @@ var _ = Describe("The fake blockchain", func() {
|
||||
blockchainObserver := &fakes.BlockchainObserver{}
|
||||
blockchain.RegisterObserver(blockchainObserver)
|
||||
|
||||
blockchain.AddBlock(core.Block{Number: big.NewInt(123)})
|
||||
blockchain.AddBlock(core.Block{Number: int64(123)})
|
||||
|
||||
Expect(blockchainObserver.LastAddedBlock().Number).ShouldNot(BeNil())
|
||||
Expect(blockchainObserver.LastAddedBlock().Number).Should(Equal(big.NewInt(123)))
|
||||
Expect(blockchainObserver.LastAddedBlock().Number).Should(Equal(int64(123)))
|
||||
})
|
||||
|
||||
})
|
||||
|
@ -2,7 +2,6 @@ package integration_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
@ -44,7 +43,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
|
||||
firstBlock := <-addedBlock
|
||||
Expect(firstBlock).ShouldNot(BeNil())
|
||||
secondBlock := <-addedBlock
|
||||
Expect(firstBlock.Number.Add(firstBlock.Number, big.NewInt(1))).Should(Equal(secondBlock.Number))
|
||||
Expect(firstBlock.Number + 1).Should(Equal(secondBlock.Number))
|
||||
|
||||
close(done)
|
||||
}, 10)
|
||||
|
Loading…
Reference in New Issue
Block a user