From cb4e745464dbe5cc58e54e6e9ad50f19bbc9cb84 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Wed, 27 Dec 2017 12:10:08 -0600 Subject: [PATCH] Add extra data field --- .../1514397430_add_extra_data.down.sql | 2 ++ db/migrations/1514397430_add_extra_data.up.sql | 2 ++ db/schema.sql | 3 ++- pkg/core/block.go | 5 +++-- pkg/geth/geth_block_to_core_block.go | 1 + pkg/geth/geth_block_to_core_block_test.go | 3 +++ pkg/repositories/postgres.go | 17 ++++++++++------- pkg/repositories/testing/helpers.go | 3 +++ 8 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 db/migrations/1514397430_add_extra_data.down.sql create mode 100644 db/migrations/1514397430_add_extra_data.up.sql diff --git a/db/migrations/1514397430_add_extra_data.down.sql b/db/migrations/1514397430_add_extra_data.down.sql new file mode 100644 index 00000000..0da3cdeb --- /dev/null +++ b/db/migrations/1514397430_add_extra_data.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE blocks + DROP COLUMN block_extra_data; \ No newline at end of file diff --git a/db/migrations/1514397430_add_extra_data.up.sql b/db/migrations/1514397430_add_extra_data.up.sql new file mode 100644 index 00000000..71839271 --- /dev/null +++ b/db/migrations/1514397430_add_extra_data.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE blocks + ADD COLUMN block_extra_data VARCHAR; \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index f6a08bf5..e4bbce3f 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -52,7 +52,8 @@ CREATE TABLE blocks ( uncle_hash character varying(66), node_id integer NOT NULL, is_final boolean, - block_miner character varying(42) + block_miner character varying(42), + block_extra_data character varying ); diff --git a/pkg/core/block.go b/pkg/core/block.go index 1d58fe8e..a8300a50 100644 --- a/pkg/core/block.go +++ b/pkg/core/block.go @@ -2,16 +2,17 @@ package core type Block struct { Difficulty int64 + ExtraData string GasLimit int64 GasUsed int64 Hash string + IsFinal bool + Miner string Nonce string Number int64 - Miner string ParentHash string Size int64 Time int64 Transactions []Transaction UncleHash string - IsFinal bool } diff --git a/pkg/geth/geth_block_to_core_block.go b/pkg/geth/geth_block_to_core_block.go index 05f1d111..e9895cbd 100644 --- a/pkg/geth/geth_block_to_core_block.go +++ b/pkg/geth/geth_block_to_core_block.go @@ -26,6 +26,7 @@ func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block GasLimit: gethBlock.GasLimit().Int64(), GasUsed: gethBlock.GasUsed().Int64(), Hash: gethBlock.Hash().Hex(), + ExtraData: hexutil.Encode(gethBlock.Extra()), Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]), Number: gethBlock.Number().Int64(), Miner: gethBlock.Coinbase().Hex(), diff --git a/pkg/geth/geth_block_to_core_block_test.go b/pkg/geth/geth_block_to_core_block_test.go index 11e30336..065e6d55 100644 --- a/pkg/geth/geth_block_to_core_block_test.go +++ b/pkg/geth/geth_block_to_core_block_test.go @@ -26,6 +26,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() { gasLimit := int64(100000) gasUsed := int64(100000) miner := common.HexToAddress("0x0000000000000000000000000000000000000123") + extraData, _ := hexutil.Decode("0xe4b883e5bda9e7a59ee4bb99e9b1bc") nonce := types.BlockNonce{10} number := int64(1) time := int64(140000000) @@ -34,6 +35,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() { Difficulty: difficulty, GasLimit: big.NewInt(gasLimit), GasUsed: big.NewInt(gasUsed), + Extra: extraData, Coinbase: miner, Nonce: nonce, Number: big.NewInt(number), @@ -53,6 +55,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() { 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.ExtraData).To(Equal(hexutil.Encode(block.Extra()))) Expect(gethBlock.Size).To(Equal(block.Size().Int64())) Expect(gethBlock.Time).To(Equal(time)) Expect(gethBlock.UncleHash).To(Equal(block.UncleHash().Hex())) diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index 663da189..ed7446ae 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -199,7 +199,8 @@ func (repository Postgres) FindBlockByNumber(blockNumber int64) (core.Block, err block_size, uncle_hash, is_final, - block_miner + block_miner, + block_extra_data FROM blocks WHERE node_id = $1 AND block_number = $2`, repository.nodeId, blockNumber) savedBlock, err := repository.loadBlock(blockRows) @@ -257,10 +258,10 @@ func (repository Postgres) insertBlock(block core.Block) error { tx, _ := repository.Db.BeginTx(context.Background(), nil) err := tx.QueryRow( `INSERT INTO blocks - (node_id, block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash, is_final, block_miner) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) + (node_id, block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash, is_final, block_miner, block_extra_data) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING id `, - repository.nodeId, block.Number, block.GasLimit, block.GasUsed, block.Time, block.Difficulty, block.Hash, block.Nonce, block.ParentHash, block.Size, block.UncleHash, block.IsFinal, block.Miner). + repository.nodeId, block.Number, block.GasLimit, block.GasUsed, block.Time, block.Difficulty, block.Hash, block.Nonce, block.ParentHash, block.Size, block.UncleHash, block.IsFinal, block.Miner, block.ExtraData). Scan(&blockId) if err != nil { tx.Rollback() @@ -307,6 +308,7 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) { var blockNonce string var blockNumber int64 var blockMiner string + var blockExtraData string var blockParentHash string var blockSize int64 var blockTime float64 @@ -315,7 +317,7 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) { var gasUsed float64 var uncleHash string var isFinal bool - err := blockRows.Scan(&blockId, &blockNumber, &gasLimit, &gasUsed, &blockTime, &difficulty, &blockHash, &blockNonce, &blockParentHash, &blockSize, &uncleHash, &isFinal, &blockMiner) + err := blockRows.Scan(&blockId, &blockNumber, &gasLimit, &gasUsed, &blockTime, &difficulty, &blockHash, &blockNonce, &blockParentHash, &blockSize, &uncleHash, &isFinal, &blockMiner, &blockExtraData) if err != nil { return core.Block{}, err } @@ -333,18 +335,19 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) { transactions := repository.loadTransactions(transactionRows) return core.Block{ Difficulty: difficulty, + ExtraData: blockExtraData, GasLimit: int64(gasLimit), GasUsed: int64(gasUsed), Hash: blockHash, + IsFinal: isFinal, + Miner: blockMiner, Nonce: blockNonce, Number: blockNumber, - Miner: blockMiner, ParentHash: blockParentHash, Size: blockSize, Time: int64(blockTime), Transactions: transactions, UncleHash: uncleHash, - IsFinal: isFinal, }, nil } diff --git a/pkg/repositories/testing/helpers.go b/pkg/repositories/testing/helpers.go index e6bc6aa1..7accb32b 100644 --- a/pkg/repositories/testing/helpers.go +++ b/pkg/repositories/testing/helpers.go @@ -62,6 +62,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. blockParentHash := "x456" blockNonce := "0x881db2ca900682e9a9" miner := "x123" + extraData := "xextraData" blockTime := int64(1508981640) uncleHash := "x789" blockSize := int64(1000) @@ -71,6 +72,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. GasLimit: gasLimit, GasUsed: gasUsed, Hash: blockHash, + ExtraData: extraData, Nonce: blockNonce, Miner: miner, Number: blockNumber, @@ -90,6 +92,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories. Expect(savedBlock.Hash).To(Equal(blockHash)) Expect(savedBlock.Nonce).To(Equal(blockNonce)) Expect(savedBlock.Miner).To(Equal(miner)) + Expect(savedBlock.ExtraData).To(Equal(extraData)) Expect(savedBlock.Number).To(Equal(blockNumber)) Expect(savedBlock.ParentHash).To(Equal(blockParentHash)) Expect(savedBlock.Size).To(Equal(blockSize))