Add extra data field

This commit is contained in:
Matt Krump 2017-12-27 12:10:08 -06:00
parent c992186846
commit cb4e745464
8 changed files with 26 additions and 10 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE blocks
DROP COLUMN block_extra_data;

View File

@ -0,0 +1,2 @@
ALTER TABLE blocks
ADD COLUMN block_extra_data VARCHAR;

View File

@ -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
);

View File

@ -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
}

View File

@ -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(),

View File

@ -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()))

View File

@ -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
}

View File

@ -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))