|
|
@@ -1,6 +1,8 @@
|
|
|
package rpc
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
+
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
)
|
|
|
@@ -11,10 +13,10 @@ type BlockRes struct {
|
|
|
BlockNumber *hexnum `json:"number"`
|
|
|
BlockHash *hexdata `json:"hash"`
|
|
|
ParentHash *hexdata `json:"parentHash"`
|
|
|
- Nonce *hexnum `json:"nonce"`
|
|
|
+ Nonce *hexdata `json:"nonce"`
|
|
|
Sha3Uncles *hexdata `json:"sha3Uncles"`
|
|
|
LogsBloom *hexdata `json:"logsBloom"`
|
|
|
- TransactionRoot *hexdata `json:"transactionRoot"`
|
|
|
+ TransactionRoot *hexdata `json:"transactionsRoot"`
|
|
|
StateRoot *hexdata `json:"stateRoot"`
|
|
|
Miner *hexdata `json:"miner"`
|
|
|
Difficulty *hexnum `json:"difficulty"`
|
|
|
@@ -29,11 +31,104 @@ type BlockRes struct {
|
|
|
Uncles []*hexdata `json:"uncles"`
|
|
|
}
|
|
|
|
|
|
+func (b *BlockRes) MarshalJSON() ([]byte, error) {
|
|
|
+ if b.fullTx {
|
|
|
+ var ext struct {
|
|
|
+ BlockNumber *hexnum `json:"number"`
|
|
|
+ BlockHash *hexdata `json:"hash"`
|
|
|
+ ParentHash *hexdata `json:"parentHash"`
|
|
|
+ Nonce *hexdata `json:"nonce"`
|
|
|
+ Sha3Uncles *hexdata `json:"sha3Uncles"`
|
|
|
+ LogsBloom *hexdata `json:"logsBloom"`
|
|
|
+ TransactionRoot *hexdata `json:"transactionsRoot"`
|
|
|
+ StateRoot *hexdata `json:"stateRoot"`
|
|
|
+ Miner *hexdata `json:"miner"`
|
|
|
+ Difficulty *hexnum `json:"difficulty"`
|
|
|
+ TotalDifficulty *hexnum `json:"totalDifficulty"`
|
|
|
+ Size *hexnum `json:"size"`
|
|
|
+ ExtraData *hexdata `json:"extraData"`
|
|
|
+ GasLimit *hexnum `json:"gasLimit"`
|
|
|
+ MinGasPrice *hexnum `json:"minGasPrice"`
|
|
|
+ GasUsed *hexnum `json:"gasUsed"`
|
|
|
+ UnixTimestamp *hexnum `json:"timestamp"`
|
|
|
+ Transactions []*TransactionRes `json:"transactions"`
|
|
|
+ Uncles []*hexdata `json:"uncles"`
|
|
|
+ }
|
|
|
+
|
|
|
+ ext.BlockNumber = b.BlockNumber
|
|
|
+ ext.BlockHash = b.BlockHash
|
|
|
+ ext.ParentHash = b.ParentHash
|
|
|
+ ext.Nonce = b.Nonce
|
|
|
+ ext.Sha3Uncles = b.Sha3Uncles
|
|
|
+ ext.LogsBloom = b.LogsBloom
|
|
|
+ ext.TransactionRoot = b.TransactionRoot
|
|
|
+ ext.StateRoot = b.StateRoot
|
|
|
+ ext.Miner = b.Miner
|
|
|
+ ext.Difficulty = b.Difficulty
|
|
|
+ ext.TotalDifficulty = b.TotalDifficulty
|
|
|
+ ext.Size = b.Size
|
|
|
+ ext.ExtraData = b.ExtraData
|
|
|
+ ext.GasLimit = b.GasLimit
|
|
|
+ ext.MinGasPrice = b.MinGasPrice
|
|
|
+ ext.GasUsed = b.GasUsed
|
|
|
+ ext.UnixTimestamp = b.UnixTimestamp
|
|
|
+ ext.Transactions = b.Transactions
|
|
|
+ ext.Uncles = b.Uncles
|
|
|
+ return json.Marshal(ext)
|
|
|
+ } else {
|
|
|
+ var ext struct {
|
|
|
+ BlockNumber *hexnum `json:"number"`
|
|
|
+ BlockHash *hexdata `json:"hash"`
|
|
|
+ ParentHash *hexdata `json:"parentHash"`
|
|
|
+ Nonce *hexdata `json:"nonce"`
|
|
|
+ Sha3Uncles *hexdata `json:"sha3Uncles"`
|
|
|
+ LogsBloom *hexdata `json:"logsBloom"`
|
|
|
+ TransactionRoot *hexdata `json:"transactionsRoot"`
|
|
|
+ StateRoot *hexdata `json:"stateRoot"`
|
|
|
+ Miner *hexdata `json:"miner"`
|
|
|
+ Difficulty *hexnum `json:"difficulty"`
|
|
|
+ TotalDifficulty *hexnum `json:"totalDifficulty"`
|
|
|
+ Size *hexnum `json:"size"`
|
|
|
+ ExtraData *hexdata `json:"extraData"`
|
|
|
+ GasLimit *hexnum `json:"gasLimit"`
|
|
|
+ MinGasPrice *hexnum `json:"minGasPrice"`
|
|
|
+ GasUsed *hexnum `json:"gasUsed"`
|
|
|
+ UnixTimestamp *hexnum `json:"timestamp"`
|
|
|
+ Transactions []*hexdata `json:"transactions"`
|
|
|
+ Uncles []*hexdata `json:"uncles"`
|
|
|
+ }
|
|
|
+
|
|
|
+ ext.BlockNumber = b.BlockNumber
|
|
|
+ ext.BlockHash = b.BlockHash
|
|
|
+ ext.ParentHash = b.ParentHash
|
|
|
+ ext.Nonce = b.Nonce
|
|
|
+ ext.Sha3Uncles = b.Sha3Uncles
|
|
|
+ ext.LogsBloom = b.LogsBloom
|
|
|
+ ext.TransactionRoot = b.TransactionRoot
|
|
|
+ ext.StateRoot = b.StateRoot
|
|
|
+ ext.Miner = b.Miner
|
|
|
+ ext.Difficulty = b.Difficulty
|
|
|
+ ext.TotalDifficulty = b.TotalDifficulty
|
|
|
+ ext.Size = b.Size
|
|
|
+ ext.ExtraData = b.ExtraData
|
|
|
+ ext.GasLimit = b.GasLimit
|
|
|
+ ext.MinGasPrice = b.MinGasPrice
|
|
|
+ ext.GasUsed = b.GasUsed
|
|
|
+ ext.UnixTimestamp = b.UnixTimestamp
|
|
|
+ ext.Transactions = make([]*hexdata, len(b.Transactions))
|
|
|
+ for i, tx := range b.Transactions {
|
|
|
+ ext.Transactions[i] = tx.Hash
|
|
|
+ }
|
|
|
+ ext.Uncles = b.Uncles
|
|
|
+ return json.Marshal(ext)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
|
|
|
// TODO respect fullTx flag
|
|
|
|
|
|
if block == nil {
|
|
|
- return &BlockRes{}
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
res := new(BlockRes)
|
|
|
@@ -41,7 +136,7 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
|
|
|
res.BlockNumber = newHexNum(block.Number())
|
|
|
res.BlockHash = newHexData(block.Hash())
|
|
|
res.ParentHash = newHexData(block.ParentHash())
|
|
|
- res.Nonce = newHexNum(block.Header().Nonce)
|
|
|
+ res.Nonce = newHexData(block.Nonce())
|
|
|
res.Sha3Uncles = newHexData(block.Header().UncleHash)
|
|
|
res.LogsBloom = newHexData(block.Bloom())
|
|
|
res.TransactionRoot = newHexData(block.Header().TxHash)
|
|
|
@@ -49,7 +144,7 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
|
|
|
res.Miner = newHexData(block.Header().Coinbase)
|
|
|
res.Difficulty = newHexNum(block.Difficulty())
|
|
|
res.TotalDifficulty = newHexNum(block.Td)
|
|
|
- res.Size = newHexNum(block.Size())
|
|
|
+ res.Size = newHexNum(block.Size().Int64())
|
|
|
res.ExtraData = newHexData(block.Header().Extra)
|
|
|
res.GasLimit = newHexNum(block.GasLimit())
|
|
|
// res.MinGasPrice =
|