Add Block Miner

This commit is contained in:
Matt Krump 2017-12-27 10:50:56 -06:00
parent 7e5e12f488
commit c992186846
8 changed files with 23 additions and 7 deletions

View File

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

View File

@ -0,0 +1,2 @@
ALTER TABLE blocks
ADD COLUMN block_miner VARCHAR(42);

View File

@ -51,7 +51,8 @@ CREATE TABLE blocks (
block_size bigint,
uncle_hash character varying(66),
node_id integer NOT NULL,
is_final boolean
is_final boolean,
block_miner character varying(42)
);

View File

@ -7,6 +7,7 @@ type Block struct {
Hash string
Nonce string
Number int64
Miner string
ParentHash string
Size int64
Time int64

View File

@ -28,6 +28,7 @@ func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block
Hash: gethBlock.Hash().Hex(),
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
Number: gethBlock.Number().Int64(),
Miner: gethBlock.Coinbase().Hex(),
ParentHash: gethBlock.ParentHash().Hex(),
Size: gethBlock.Size().Int64(),
Time: gethBlock.Time().Int64(),

View File

@ -21,10 +21,11 @@ func (client *FakeGethClient) TransactionSender(ctx context.Context, tx *types.T
var _ = Describe("Conversion of GethBlock to core.Block", func() {
It("converts basic Block metada", func() {
It("converts basic Block metadata", func() {
difficulty := big.NewInt(1)
gasLimit := int64(100000)
gasUsed := int64(100000)
miner := common.HexToAddress("0x0000000000000000000000000000000000000123")
nonce := types.BlockNonce{10}
number := int64(1)
time := int64(140000000)
@ -33,6 +34,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
Difficulty: difficulty,
GasLimit: big.NewInt(gasLimit),
GasUsed: big.NewInt(gasUsed),
Coinbase: miner,
Nonce: nonce,
Number: big.NewInt(number),
ParentHash: common.Hash{64},
@ -45,6 +47,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
Expect(gethBlock.Difficulty).To(Equal(difficulty.Int64()))
Expect(gethBlock.GasLimit).To(Equal(gasLimit))
Expect(gethBlock.Miner).To(Equal(miner.Hex()))
Expect(gethBlock.GasUsed).To(Equal(gasUsed))
Expect(gethBlock.Hash).To(Equal(block.Hash().Hex()))
Expect(gethBlock.Nonce).To(Equal(hexutil.Encode(header.Nonce[:])))

View File

@ -198,7 +198,8 @@ func (repository Postgres) FindBlockByNumber(blockNumber int64) (core.Block, err
block_parenthash,
block_size,
uncle_hash,
is_final
is_final,
block_miner
FROM blocks
WHERE node_id = $1 AND block_number = $2`, repository.nodeId, blockNumber)
savedBlock, err := repository.loadBlock(blockRows)
@ -256,10 +257,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)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
(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)
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).
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).
Scan(&blockId)
if err != nil {
tx.Rollback()
@ -305,6 +306,7 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) {
var blockHash string
var blockNonce string
var blockNumber int64
var blockMiner string
var blockParentHash string
var blockSize int64
var blockTime float64
@ -313,7 +315,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)
err := blockRows.Scan(&blockId, &blockNumber, &gasLimit, &gasUsed, &blockTime, &difficulty, &blockHash, &blockNonce, &blockParentHash, &blockSize, &uncleHash, &isFinal, &blockMiner)
if err != nil {
return core.Block{}, err
}
@ -336,6 +338,7 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) {
Hash: blockHash,
Nonce: blockNonce,
Number: blockNumber,
Miner: blockMiner,
ParentHash: blockParentHash,
Size: blockSize,
Time: int64(blockTime),

View File

@ -61,6 +61,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
blockHash := "x123"
blockParentHash := "x456"
blockNonce := "0x881db2ca900682e9a9"
miner := "x123"
blockTime := int64(1508981640)
uncleHash := "x789"
blockSize := int64(1000)
@ -71,6 +72,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
GasUsed: gasUsed,
Hash: blockHash,
Nonce: blockNonce,
Miner: miner,
Number: blockNumber,
ParentHash: blockParentHash,
Size: blockSize,
@ -87,6 +89,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
Expect(savedBlock.GasUsed).To(Equal(gasUsed))
Expect(savedBlock.Hash).To(Equal(blockHash))
Expect(savedBlock.Nonce).To(Equal(blockNonce))
Expect(savedBlock.Miner).To(Equal(miner))
Expect(savedBlock.Number).To(Equal(blockNumber))
Expect(savedBlock.ParentHash).To(Equal(blockParentHash))
Expect(savedBlock.Size).To(Equal(blockSize))