forked from cerc-io/ipld-eth-server
Add extra data field
This commit is contained in:
parent
c992186846
commit
cb4e745464
2
db/migrations/1514397430_add_extra_data.down.sql
Normal file
2
db/migrations/1514397430_add_extra_data.down.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE blocks
|
||||||
|
DROP COLUMN block_extra_data;
|
2
db/migrations/1514397430_add_extra_data.up.sql
Normal file
2
db/migrations/1514397430_add_extra_data.up.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE blocks
|
||||||
|
ADD COLUMN block_extra_data VARCHAR;
|
@ -52,7 +52,8 @@ CREATE TABLE blocks (
|
|||||||
uncle_hash character varying(66),
|
uncle_hash character varying(66),
|
||||||
node_id integer NOT NULL,
|
node_id integer NOT NULL,
|
||||||
is_final boolean,
|
is_final boolean,
|
||||||
block_miner character varying(42)
|
block_miner character varying(42),
|
||||||
|
block_extra_data character varying
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,16 +2,17 @@ package core
|
|||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
Difficulty int64
|
Difficulty int64
|
||||||
|
ExtraData string
|
||||||
GasLimit int64
|
GasLimit int64
|
||||||
GasUsed int64
|
GasUsed int64
|
||||||
Hash string
|
Hash string
|
||||||
|
IsFinal bool
|
||||||
|
Miner string
|
||||||
Nonce string
|
Nonce string
|
||||||
Number int64
|
Number int64
|
||||||
Miner string
|
|
||||||
ParentHash string
|
ParentHash string
|
||||||
Size int64
|
Size int64
|
||||||
Time int64
|
Time int64
|
||||||
Transactions []Transaction
|
Transactions []Transaction
|
||||||
UncleHash string
|
UncleHash string
|
||||||
IsFinal bool
|
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block
|
|||||||
GasLimit: gethBlock.GasLimit().Int64(),
|
GasLimit: gethBlock.GasLimit().Int64(),
|
||||||
GasUsed: gethBlock.GasUsed().Int64(),
|
GasUsed: gethBlock.GasUsed().Int64(),
|
||||||
Hash: gethBlock.Hash().Hex(),
|
Hash: gethBlock.Hash().Hex(),
|
||||||
|
ExtraData: hexutil.Encode(gethBlock.Extra()),
|
||||||
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
|
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
|
||||||
Number: gethBlock.Number().Int64(),
|
Number: gethBlock.Number().Int64(),
|
||||||
Miner: gethBlock.Coinbase().Hex(),
|
Miner: gethBlock.Coinbase().Hex(),
|
||||||
|
@ -26,6 +26,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
|||||||
gasLimit := int64(100000)
|
gasLimit := int64(100000)
|
||||||
gasUsed := int64(100000)
|
gasUsed := int64(100000)
|
||||||
miner := common.HexToAddress("0x0000000000000000000000000000000000000123")
|
miner := common.HexToAddress("0x0000000000000000000000000000000000000123")
|
||||||
|
extraData, _ := hexutil.Decode("0xe4b883e5bda9e7a59ee4bb99e9b1bc")
|
||||||
nonce := types.BlockNonce{10}
|
nonce := types.BlockNonce{10}
|
||||||
number := int64(1)
|
number := int64(1)
|
||||||
time := int64(140000000)
|
time := int64(140000000)
|
||||||
@ -34,6 +35,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
|||||||
Difficulty: difficulty,
|
Difficulty: difficulty,
|
||||||
GasLimit: big.NewInt(gasLimit),
|
GasLimit: big.NewInt(gasLimit),
|
||||||
GasUsed: big.NewInt(gasUsed),
|
GasUsed: big.NewInt(gasUsed),
|
||||||
|
Extra: extraData,
|
||||||
Coinbase: miner,
|
Coinbase: miner,
|
||||||
Nonce: nonce,
|
Nonce: nonce,
|
||||||
Number: big.NewInt(number),
|
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.Nonce).To(Equal(hexutil.Encode(header.Nonce[:])))
|
||||||
Expect(gethBlock.Number).To(Equal(number))
|
Expect(gethBlock.Number).To(Equal(number))
|
||||||
Expect(gethBlock.ParentHash).To(Equal(block.ParentHash().Hex()))
|
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.Size).To(Equal(block.Size().Int64()))
|
||||||
Expect(gethBlock.Time).To(Equal(time))
|
Expect(gethBlock.Time).To(Equal(time))
|
||||||
Expect(gethBlock.UncleHash).To(Equal(block.UncleHash().Hex()))
|
Expect(gethBlock.UncleHash).To(Equal(block.UncleHash().Hex()))
|
||||||
|
@ -199,7 +199,8 @@ func (repository Postgres) FindBlockByNumber(blockNumber int64) (core.Block, err
|
|||||||
block_size,
|
block_size,
|
||||||
uncle_hash,
|
uncle_hash,
|
||||||
is_final,
|
is_final,
|
||||||
block_miner
|
block_miner,
|
||||||
|
block_extra_data
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE node_id = $1 AND block_number = $2`, repository.nodeId, blockNumber)
|
WHERE node_id = $1 AND block_number = $2`, repository.nodeId, blockNumber)
|
||||||
savedBlock, err := repository.loadBlock(blockRows)
|
savedBlock, err := repository.loadBlock(blockRows)
|
||||||
@ -257,10 +258,10 @@ func (repository Postgres) insertBlock(block core.Block) error {
|
|||||||
tx, _ := repository.Db.BeginTx(context.Background(), nil)
|
tx, _ := repository.Db.BeginTx(context.Background(), nil)
|
||||||
err := tx.QueryRow(
|
err := tx.QueryRow(
|
||||||
`INSERT INTO blocks
|
`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)
|
(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)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||||||
RETURNING id `,
|
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)
|
Scan(&blockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
@ -307,6 +308,7 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) {
|
|||||||
var blockNonce string
|
var blockNonce string
|
||||||
var blockNumber int64
|
var blockNumber int64
|
||||||
var blockMiner string
|
var blockMiner string
|
||||||
|
var blockExtraData string
|
||||||
var blockParentHash string
|
var blockParentHash string
|
||||||
var blockSize int64
|
var blockSize int64
|
||||||
var blockTime float64
|
var blockTime float64
|
||||||
@ -315,7 +317,7 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) {
|
|||||||
var gasUsed float64
|
var gasUsed float64
|
||||||
var uncleHash string
|
var uncleHash string
|
||||||
var isFinal bool
|
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 {
|
if err != nil {
|
||||||
return core.Block{}, err
|
return core.Block{}, err
|
||||||
}
|
}
|
||||||
@ -333,18 +335,19 @@ func (repository Postgres) loadBlock(blockRows *sql.Row) (core.Block, error) {
|
|||||||
transactions := repository.loadTransactions(transactionRows)
|
transactions := repository.loadTransactions(transactionRows)
|
||||||
return core.Block{
|
return core.Block{
|
||||||
Difficulty: difficulty,
|
Difficulty: difficulty,
|
||||||
|
ExtraData: blockExtraData,
|
||||||
GasLimit: int64(gasLimit),
|
GasLimit: int64(gasLimit),
|
||||||
GasUsed: int64(gasUsed),
|
GasUsed: int64(gasUsed),
|
||||||
Hash: blockHash,
|
Hash: blockHash,
|
||||||
|
IsFinal: isFinal,
|
||||||
|
Miner: blockMiner,
|
||||||
Nonce: blockNonce,
|
Nonce: blockNonce,
|
||||||
Number: blockNumber,
|
Number: blockNumber,
|
||||||
Miner: blockMiner,
|
|
||||||
ParentHash: blockParentHash,
|
ParentHash: blockParentHash,
|
||||||
Size: blockSize,
|
Size: blockSize,
|
||||||
Time: int64(blockTime),
|
Time: int64(blockTime),
|
||||||
Transactions: transactions,
|
Transactions: transactions,
|
||||||
UncleHash: uncleHash,
|
UncleHash: uncleHash,
|
||||||
IsFinal: isFinal,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,6 +62,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
|
|||||||
blockParentHash := "x456"
|
blockParentHash := "x456"
|
||||||
blockNonce := "0x881db2ca900682e9a9"
|
blockNonce := "0x881db2ca900682e9a9"
|
||||||
miner := "x123"
|
miner := "x123"
|
||||||
|
extraData := "xextraData"
|
||||||
blockTime := int64(1508981640)
|
blockTime := int64(1508981640)
|
||||||
uncleHash := "x789"
|
uncleHash := "x789"
|
||||||
blockSize := int64(1000)
|
blockSize := int64(1000)
|
||||||
@ -71,6 +72,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
|
|||||||
GasLimit: gasLimit,
|
GasLimit: gasLimit,
|
||||||
GasUsed: gasUsed,
|
GasUsed: gasUsed,
|
||||||
Hash: blockHash,
|
Hash: blockHash,
|
||||||
|
ExtraData: extraData,
|
||||||
Nonce: blockNonce,
|
Nonce: blockNonce,
|
||||||
Miner: miner,
|
Miner: miner,
|
||||||
Number: blockNumber,
|
Number: blockNumber,
|
||||||
@ -90,6 +92,7 @@ func AssertRepositoryBehavior(buildRepository func(node core.Node) repositories.
|
|||||||
Expect(savedBlock.Hash).To(Equal(blockHash))
|
Expect(savedBlock.Hash).To(Equal(blockHash))
|
||||||
Expect(savedBlock.Nonce).To(Equal(blockNonce))
|
Expect(savedBlock.Nonce).To(Equal(blockNonce))
|
||||||
Expect(savedBlock.Miner).To(Equal(miner))
|
Expect(savedBlock.Miner).To(Equal(miner))
|
||||||
|
Expect(savedBlock.ExtraData).To(Equal(extraData))
|
||||||
Expect(savedBlock.Number).To(Equal(blockNumber))
|
Expect(savedBlock.Number).To(Equal(blockNumber))
|
||||||
Expect(savedBlock.ParentHash).To(Equal(blockParentHash))
|
Expect(savedBlock.ParentHash).To(Equal(blockParentHash))
|
||||||
Expect(savedBlock.Size).To(Equal(blockSize))
|
Expect(savedBlock.Size).To(Equal(blockSize))
|
||||||
|
Loading…
Reference in New Issue
Block a user