Added fields for: Time, Gas Limit, and Gas Used

This commit is contained in:
Matt Krump 2017-10-25 17:24:38 -05:00
parent f97c75828a
commit 598a58e3b2
9 changed files with 99 additions and 22 deletions

View File

@ -1,8 +1,45 @@
package core
import "math/big"
import (
"math/big"
"github.com/ethereum/go-ethereum/core/types"
)
//Our block representation
type Block struct {
Number *big.Int
GasLimit *big.Int
GasUsed *big.Int
Time *big.Int
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{
Number: gethBlock.Number(),
GasLimit: gethBlock.GasLimit(),
GasUsed: gethBlock.GasUsed(),
Time: gethBlock.Time(),
NumberOfTransactions: len(gethBlock.Transactions()),
}
}

24
core/block_test.go Normal file
View File

@ -0,0 +1,24 @@
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

@ -5,15 +5,15 @@ import (
_ "github.com/lib/pq"
)
type BlockRecord struct {
BlockNumber int64 `db:"block_number"`
}
type BlockchainDBObserver struct {
Db *sqlx.DB
}
func (observer BlockchainDBObserver) NotifyBlockAdded(block Block) {
observer.Db.NamedExec("INSERT INTO blocks (block_number) VALUES (:block_number)", &BlockRecord{BlockNumber: block.Number.Int64()})
blockRecord := BlockToBlockRecord(block)
observer.Db.NamedExec(
"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())
}

View File

@ -51,7 +51,11 @@ var _ = Describe("Saving blocks to the database", func() {
})
It("inserts a block", func() {
block := core.Block{Number: big.NewInt(1)}
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}
observer := core.BlockchainDBObserver{Db: db}
observer.NotifyBlockAdded(block)
@ -64,9 +68,8 @@ var _ = Describe("Saving blocks to the database", func() {
rows.StructScan(&savedBlock)
savedBlocks = append(savedBlocks, savedBlock)
}
Expect(len(savedBlocks)).To(Equal(1))
Expect(savedBlocks[0].BlockNumber).To(Equal(int64(1)))
Expect(savedBlocks[0].BlockNumber)
})
})

View File

@ -1,9 +1,16 @@
package core
import "fmt"
import (
"fmt"
"time"
)
type BlockchainLoggingObserver struct{}
func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block Block) {
fmt.Printf("New block was added: %d, %d\n", block.Number, block.NumberOfTransactions)
fmt.Printf("New block was added: %d\n"+
"\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, block.NumberOfTransactions)
}

View File

@ -2,11 +2,12 @@ package core
import (
"fmt"
"reflect"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/net/context"
"reflect"
)
type GethBlockchain struct {
@ -24,20 +25,13 @@ func NewGethBlockchain(ipcPath string) *GethBlockchain {
return &blockchain
}
func (blockchain GethBlockchain) notifyObservers(getBlock *types.Block) {
block := convertBlock(getBlock)
func (blockchain GethBlockchain) notifyObservers(gethBlock *types.Block) {
block := GethBlockToCoreBlock(gethBlock)
for _, observer := range blockchain.observers {
observer.NotifyBlockAdded(block)
}
}
func convertBlock(gethBlock *types.Block) Block {
return Block{
Number: gethBlock.Number(),
NumberOfTransactions: len(gethBlock.Transactions()),
}
}
func (blockchain *GethBlockchain) RegisterObserver(observer BlockchainObserver) {
fmt.Printf("Registering observer: %v\n", reflect.TypeOf(observer))
blockchain.observers = append(blockchain.observers, observer)

View File

@ -0,0 +1,5 @@
ALTER TABLE blocks
DROP COLUMN block_gaslimit,
DROP COLUMN block_gasused,
DROP COLUMN block_time;

View File

@ -0,0 +1,4 @@
ALTER TABLE blocks
ADD COLUMN block_gaslimit DOUBLE PRECISION,
ADD COLUMN block_gasused DOUBLE PRECISION,
ADD COLUMN block_time DOUBLE PRECISION;

View File

@ -39,7 +39,10 @@ SET default_with_oids = false;
--
CREATE TABLE blocks (
block_number bigint
block_number bigint,
block_gaslimit double precision,
block_gasused double precision,
block_time double precision
);