forked from cerc-io/ipld-eth-server
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
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
Number *big.Int
|
Number int64
|
||||||
GasLimit *big.Int
|
GasLimit int64
|
||||||
GasUsed *big.Int
|
GasUsed int64
|
||||||
Time *big.Int
|
Time int64
|
||||||
Transactions []Transaction
|
Transactions []Transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,10 +18,10 @@ func GethBlockToCoreBlock(gethBlock *types.Block) Block {
|
|||||||
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
|
transactions = append(transactions, gethTransToCoreTrans(gethTransaction))
|
||||||
}
|
}
|
||||||
return Block{
|
return Block{
|
||||||
Number: gethBlock.Number(),
|
Number: gethBlock.Number().Int64(),
|
||||||
GasLimit: gethBlock.GasLimit(),
|
GasLimit: gethBlock.GasLimit().Int64(),
|
||||||
GasUsed: gethBlock.GasUsed(),
|
GasUsed: gethBlock.GasUsed().Int64(),
|
||||||
Time: gethBlock.Time(),
|
Time: gethBlock.Time().Int64(),
|
||||||
Transactions: transactions,
|
Transactions: transactions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ func saveBlock(observer BlockchainDBObserver, block Block) int64 {
|
|||||||
insertedBlock := observer.Db.QueryRow("Insert INTO blocks "+
|
insertedBlock := observer.Db.QueryRow("Insert INTO blocks "+
|
||||||
"(block_number, block_gaslimit, block_gasused, block_time) "+
|
"(block_number, block_gaslimit, block_gasused, block_time) "+
|
||||||
"VALUES ($1, $2, $3, $4) RETURNING id",
|
"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
|
var blockId int64
|
||||||
insertedBlock.Scan(&blockId)
|
insertedBlock.Scan(&blockId)
|
||||||
return blockId
|
return blockId
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/8thlight/vulcanizedb/core"
|
"github.com/8thlight/vulcanizedb/core"
|
||||||
@ -53,10 +51,10 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
|
|
||||||
It("inserts a block", func() {
|
It("inserts a block", func() {
|
||||||
// setup a block in memory
|
// setup a block in memory
|
||||||
blockNumber := big.NewInt(1)
|
blockNumber := int64(123)
|
||||||
gasLimit := big.NewInt(1000000)
|
gasLimit := int64(1000000)
|
||||||
gasUsed := big.NewInt(10)
|
gasUsed := int64(10)
|
||||||
blockTime := big.NewInt(1508981640)
|
blockTime := int64(1508981640)
|
||||||
block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
|
block := core.Block{Number: blockNumber, GasLimit: gasLimit, GasUsed: gasUsed, Time: blockTime}
|
||||||
|
|
||||||
// save the block to the database
|
// save the block to the database
|
||||||
@ -74,18 +72,18 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
var gasUsed float64
|
var gasUsed float64
|
||||||
rows.Scan(&blockNumber, &gasLimit, &gasUsed, &blockTime)
|
rows.Scan(&blockNumber, &gasLimit, &gasUsed, &blockTime)
|
||||||
savedBlock := core.Block{
|
savedBlock := core.Block{
|
||||||
GasUsed: big.NewInt(int64(gasUsed)),
|
GasLimit: int64(gasLimit),
|
||||||
GasLimit: big.NewInt(int64(gasLimit)),
|
GasUsed: int64(gasUsed),
|
||||||
Number: big.NewInt(blockNumber),
|
Number: blockNumber,
|
||||||
Time: big.NewInt(int64(blockTime)),
|
Time: int64(blockTime),
|
||||||
}
|
}
|
||||||
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.Int64()).To(Equal(blockNumber.Int64()))
|
Expect(savedBlocks[0].Number).To(Equal(blockNumber))
|
||||||
Expect(savedBlocks[0].GasLimit.Int64()).To(Equal(gasLimit.Int64()))
|
Expect(savedBlocks[0].GasLimit).To(Equal(gasLimit))
|
||||||
Expect(savedBlocks[0].GasUsed.Int64()).To(Equal(gasUsed.Int64()))
|
Expect(savedBlocks[0].GasUsed).To(Equal(gasUsed))
|
||||||
Expect(savedBlocks[0].Time).To(Equal(blockTime))
|
Expect(savedBlocks[0].Time).To(Equal(blockTime))
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -106,10 +104,7 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
To: to,
|
To: to,
|
||||||
Value: value,
|
Value: value,
|
||||||
}
|
}
|
||||||
blockNumber := big.NewInt(1)
|
block := core.Block{Transactions: []core.Transaction{txRecord}}
|
||||||
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}}
|
|
||||||
|
|
||||||
observer := core.BlockchainDBObserver{Db: db}
|
observer := core.BlockchainDBObserver{Db: db}
|
||||||
observer.NotifyBlockAdded(block)
|
observer.NotifyBlockAdded(block)
|
||||||
@ -148,17 +143,8 @@ var _ = Describe("Saving blocks to the database", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("associates the transaction with the block", func() {
|
It("associates the transaction with the block", func() {
|
||||||
gasLimit := int64(5000)
|
|
||||||
|
|
||||||
txRecord := core.Transaction{}
|
txRecord := core.Transaction{}
|
||||||
blockNumber := big.NewInt(1)
|
|
||||||
gasUsed := big.NewInt(10)
|
|
||||||
blockTime := big.NewInt(1508981640)
|
|
||||||
block := core.Block{
|
block := core.Block{
|
||||||
Number: blockNumber,
|
|
||||||
GasLimit: big.NewInt(gasLimit),
|
|
||||||
GasUsed: gasUsed,
|
|
||||||
Time: blockTime,
|
|
||||||
Transactions: []core.Transaction{txRecord},
|
Transactions: []core.Transaction{txRecord},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,5 +12,5 @@ func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block Block
|
|||||||
"\tTime: %v\n"+
|
"\tTime: %v\n"+
|
||||||
"\tGas Limit: %d\n"+
|
"\tGas Limit: %d\n"+
|
||||||
"\tGas Used: %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() {
|
var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||||
|
|
||||||
It("converts basic Block metada", func() {
|
It("converts basic Block metada", func() {
|
||||||
blockNumber := big.NewInt(1)
|
blockNumber := int64(1)
|
||||||
gasUsed := big.NewInt(100000)
|
gasUsed := int64(100000)
|
||||||
gasLimit := big.NewInt(100000)
|
gasLimit := int64(100000)
|
||||||
time := big.NewInt(140000000)
|
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{})
|
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
|
||||||
gethBlock := core.GethBlockToCoreBlock(block)
|
gethBlock := core.GethBlockToCoreBlock(block)
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/8thlight/vulcanizedb/core"
|
"github.com/8thlight/vulcanizedb/core"
|
||||||
"github.com/8thlight/vulcanizedb/fakes"
|
"github.com/8thlight/vulcanizedb/fakes"
|
||||||
|
|
||||||
@ -44,10 +42,10 @@ var _ = Describe("The fake blockchain", func() {
|
|||||||
blockchainObserver := &fakes.BlockchainObserver{}
|
blockchainObserver := &fakes.BlockchainObserver{}
|
||||||
blockchain.RegisterObserver(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).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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -44,7 +43,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
|
|||||||
firstBlock := <-addedBlock
|
firstBlock := <-addedBlock
|
||||||
Expect(firstBlock).ShouldNot(BeNil())
|
Expect(firstBlock).ShouldNot(BeNil())
|
||||||
secondBlock := <-addedBlock
|
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)
|
close(done)
|
||||||
}, 10)
|
}, 10)
|
||||||
|
Loading…
Reference in New Issue
Block a user