diff --git a/core/block.go b/core/block.go index ae0647fa..c5247602 100644 --- a/core/block.go +++ b/core/block.go @@ -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()), + } +} diff --git a/core/block_test.go b/core/block_test.go new file mode 100644 index 00000000..ac622baf --- /dev/null +++ b/core/block_test.go @@ -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))) + + }) +}) diff --git a/core/blockchain_db_observer.go b/core/blockchain_db_observer.go index 51622592..3fb72cb2 100644 --- a/core/blockchain_db_observer.go +++ b/core/blockchain_db_observer.go @@ -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()) } diff --git a/core/blockchain_db_observer_test.go b/core/blockchain_db_observer_test.go index 83824107..ad6ff0c1 100644 --- a/core/blockchain_db_observer_test.go +++ b/core/blockchain_db_observer_test.go @@ -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) }) }) diff --git a/core/blockchain_logging_observer.go b/core/blockchain_logging_observer.go index 9602b634..1abb2547 100644 --- a/core/blockchain_logging_observer.go +++ b/core/blockchain_logging_observer.go @@ -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) } diff --git a/core/geth_blockchain.go b/core/geth_blockchain.go index 84a6ca1b..be61c4f3 100644 --- a/core/geth_blockchain.go +++ b/core/geth_blockchain.go @@ -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) diff --git a/migrations/1508965325_add_columns_to_blocks.down.sql b/migrations/1508965325_add_columns_to_blocks.down.sql new file mode 100644 index 00000000..7624ef13 --- /dev/null +++ b/migrations/1508965325_add_columns_to_blocks.down.sql @@ -0,0 +1,5 @@ +ALTER TABLE blocks + DROP COLUMN block_gaslimit, + DROP COLUMN block_gasused, + DROP COLUMN block_time; + diff --git a/migrations/1508965325_add_columns_to_blocks.up.sql b/migrations/1508965325_add_columns_to_blocks.up.sql new file mode 100644 index 00000000..65c6262e --- /dev/null +++ b/migrations/1508965325_add_columns_to_blocks.up.sql @@ -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; diff --git a/migrations/schema.sql b/migrations/schema.sql index 890148f3..bc17a417 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -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 );