From df09cf8d93cf63ee395cd65b9b99ca5b42a505f5 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Thu, 2 Nov 2017 09:36:53 -0500 Subject: [PATCH 1/2] Add fields to block tables --- core/block.go | 8 ++- geth/geth_block_to_core_block.go | 9 ++- geth/geth_block_to_core_block_test.go | 29 ++++++--- .../1509633481_add_blocks_columns.down.sql | 7 +++ .../1509633481_add_blocks_columns.up.sql | 7 +++ migrations/schema.sql | 8 ++- observers/blockchain_db_observer.go | 9 +-- observers/blockchain_db_observer_test.go | 62 +++++++++++++++---- 8 files changed, 111 insertions(+), 28 deletions(-) create mode 100644 migrations/1509633481_add_blocks_columns.down.sql create mode 100644 migrations/1509633481_add_blocks_columns.up.sql diff --git a/core/block.go b/core/block.go index e0329ccd..12c0d2a2 100644 --- a/core/block.go +++ b/core/block.go @@ -1,9 +1,15 @@ package core type Block struct { - Number int64 + Difficulty int64 GasLimit int64 GasUsed int64 + Hash string + Nonce string + Number int64 + ParentHash string + Size int64 Time int64 Transactions []Transaction + UncleHash string } diff --git a/geth/geth_block_to_core_block.go b/geth/geth_block_to_core_block.go index ff82086a..256c3f67 100644 --- a/geth/geth_block_to_core_block.go +++ b/geth/geth_block_to_core_block.go @@ -4,6 +4,7 @@ import ( "github.com/8thlight/vulcanizedb/core" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "strconv" ) func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction { @@ -26,11 +27,17 @@ func GethBlockToCoreBlock(gethBlock *types.Block) core.Block { transactions = append(transactions, gethTransToCoreTrans(gethTransaction)) } return core.Block{ - Number: gethBlock.Number().Int64(), + Difficulty: gethBlock.Difficulty().Int64(), GasLimit: gethBlock.GasLimit().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(), Transactions: transactions, + UncleHash: gethBlock.UncleHash().Hex(), } } diff --git a/geth/geth_block_to_core_block_test.go b/geth/geth_block_to_core_block_test.go index 3fe5de0e..3eabce16 100644 --- a/geth/geth_block_to_core_block_test.go +++ b/geth/geth_block_to_core_block_test.go @@ -8,29 +8,42 @@ import ( "github.com/ethereum/go-ethereum/core/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "strconv" ) var _ = Describe("Conversion of GethBlock to core.Block", func() { It("converts basic Block metada", func() { - blockNumber := int64(1) - gasUsed := int64(100000) + difficulty := big.NewInt(1) gasLimit := int64(100000) + gasUsed := int64(100000) + nonce := types.BlockNonce{10} + number := int64(1) time := int64(140000000) header := types.Header{ - GasUsed: big.NewInt(gasUsed), - Number: big.NewInt(blockNumber), - Time: big.NewInt(time), - GasLimit: big.NewInt(gasLimit), + Difficulty: difficulty, + 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{}) gethBlock := geth.GethBlockToCoreBlock(block) - Expect(gethBlock.Number).To(Equal(blockNumber)) - Expect(gethBlock.GasUsed).To(Equal(gasUsed)) + Expect(gethBlock.Difficulty).To(Equal(difficulty.Int64())) 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.UncleHash).To(Equal(block.UncleHash().Hex())) }) Describe("the converted transations", func() { diff --git a/migrations/1509633481_add_blocks_columns.down.sql b/migrations/1509633481_add_blocks_columns.down.sql new file mode 100644 index 00000000..74f2f396 --- /dev/null +++ b/migrations/1509633481_add_blocks_columns.down.sql @@ -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 \ No newline at end of file diff --git a/migrations/1509633481_add_blocks_columns.up.sql b/migrations/1509633481_add_blocks_columns.up.sql new file mode 100644 index 00000000..d373b00f --- /dev/null +++ b/migrations/1509633481_add_blocks_columns.up.sql @@ -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) diff --git a/migrations/schema.sql b/migrations/schema.sql index 83dd2c59..2f6b40ab 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -43,7 +43,13 @@ CREATE TABLE blocks ( block_gaslimit double precision, block_gasused 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) ); diff --git a/observers/blockchain_db_observer.go b/observers/blockchain_db_observer.go index 338c8061..0c1d0fdb 100644 --- a/observers/blockchain_db_observer.go +++ b/observers/blockchain_db_observer.go @@ -16,10 +16,11 @@ func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) { } func saveBlock(observer BlockchainDBObserver, block core.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, block.GasLimit, block.GasUsed, block.Time) + insertedBlock := observer.Db.QueryRow( + "Insert INTO blocks "+ + "(block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash) "+ + "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 insertedBlock.Scan(&blockId) return blockId diff --git a/observers/blockchain_db_observer_test.go b/observers/blockchain_db_observer_test.go index b22eb261..8c0b3748 100644 --- a/observers/blockchain_db_observer_test.go +++ b/observers/blockchain_db_observer_test.go @@ -48,37 +48,73 @@ var _ = Describe("Saving blocks to the database", func() { blockNumber := int64(123) gasLimit := int64(1000000) gasUsed := int64(10) + blockHash := "x123" + blockParentHash := "x456" + blockNonce := "0x881db2ca900682e9a9" 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 observer := observers.BlockchainDBObserver{Db: db} observer.NotifyBlockAdded(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()) var savedBlocks []core.Block for rows.Next() { + var blockHash string + var blockNonce string var blockNumber int64 + var blockParentHash string + var blockSize int64 var blockTime float64 + var difficulty int64 var gasLimit 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{ - GasLimit: int64(gasLimit), - GasUsed: int64(gasUsed), - Number: blockNumber, - Time: int64(blockTime), + Difficulty: difficulty, + GasLimit: int64(gasLimit), + GasUsed: int64(gasUsed), + Hash: blockHash, + Nonce: blockNonce, + Number: blockNumber, + ParentHash: blockParentHash, + Size: blockSize, + Time: int64(blockTime), + UncleHash: uncleHash, } savedBlocks = append(savedBlocks, savedBlock) } // assert against the attributes 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].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].UncleHash).To(Equal(uncleHash)) }) var _ = Describe("Saving transactions to the database", func() { @@ -116,11 +152,11 @@ var _ = Describe("Saving blocks to the database", func() { var dbValue int64 rows.Scan(&dbHash, &dbNonce, &dbTo, &dbGasLimit, &dbGasPrice, &dbValue) savedTransaction := core.Transaction{ + GasLimit: dbGasLimit, + GasPrice: dbGasPrice, Hash: dbHash, Nonce: dbNonce, To: dbTo, - GasLimit: dbGasLimit, - GasPrice: dbGasPrice, Value: dbValue, } savedTransactions = append(savedTransactions, savedTransaction) @@ -128,11 +164,11 @@ var _ = Describe("Saving blocks to the database", func() { Expect(len(savedTransactions)).To(Equal(1)) 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.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)) }) From 946eb238613336ee20c935b64e856f3f4e7aadf1 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Thu, 2 Nov 2017 16:43:07 -0500 Subject: [PATCH 2/2] mend --- geth/geth_block_to_core_block.go | 4 ++-- geth/geth_block_to_core_block_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/geth/geth_block_to_core_block.go b/geth/geth_block_to_core_block.go index 256c3f67..58166075 100644 --- a/geth/geth_block_to_core_block.go +++ b/geth/geth_block_to_core_block.go @@ -3,8 +3,8 @@ package geth import ( "github.com/8thlight/vulcanizedb/core" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" - "strconv" ) func gethTransToCoreTrans(transaction *types.Transaction) core.Transaction { @@ -31,7 +31,7 @@ func GethBlockToCoreBlock(gethBlock *types.Block) core.Block { GasLimit: gethBlock.GasLimit().Int64(), GasUsed: gethBlock.GasUsed().Int64(), Hash: gethBlock.Hash().Hex(), - Nonce: strconv.FormatUint(gethBlock.Nonce(), 10), + Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]), Number: gethBlock.Number().Int64(), ParentHash: gethBlock.ParentHash().Hex(), Size: gethBlock.Size().Int64(), diff --git a/geth/geth_block_to_core_block_test.go b/geth/geth_block_to_core_block_test.go index 3eabce16..444f03e6 100644 --- a/geth/geth_block_to_core_block_test.go +++ b/geth/geth_block_to_core_block_test.go @@ -5,10 +5,10 @@ import ( "github.com/8thlight/vulcanizedb/geth" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "strconv" ) var _ = Describe("Conversion of GethBlock to core.Block", func() { @@ -38,7 +38,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() { 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.Nonce).To(Equal(hexutil.Encode(header.Nonce[:]))) Expect(gethBlock.Number).To(Equal(number)) Expect(gethBlock.ParentHash).To(Equal(block.ParentHash().Hex())) Expect(gethBlock.Size).To(Equal(block.Size().Int64()))