diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-03-22 08:36:22 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-22 08:36:22 +0800 |
commit | 06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462 (patch) | |
tree | d764690055f2ee7d880bcce2e413a3665fc1e7e9 /core/types/block.go | |
parent | 1dfd65f6d047407585845dfe9be2ff52a8b9f3ab (diff) | |
parent | 8cf08e4b25c4cd0e0955598342394f34feecca0c (diff) | |
download | go-tangerine-06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462.tar go-tangerine-06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462.tar.gz go-tangerine-06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462.tar.bz2 go-tangerine-06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462.tar.lz go-tangerine-06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462.tar.xz go-tangerine-06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462.tar.zst go-tangerine-06d6685eb58f6cb0eda4d7adc8ae91e5cdb88462.zip |
Merge pull request #3756 from fjl/core-types-gencodec
core/types: use gencodec for JSON marshaling code
Diffstat (limited to 'core/types/block.go')
-rw-r--r-- | core/types/block.go | 127 |
1 files changed, 27 insertions, 100 deletions
diff --git a/core/types/block.go b/core/types/block.go index 1dae87962..b699ba686 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -19,8 +19,6 @@ package types import ( "encoding/binary" - "encoding/json" - "errors" "fmt" "io" "math/big" @@ -39,12 +37,6 @@ var ( EmptyUncleHash = CalcUncleHash(nil) ) -var ( - errMissingHeaderMixDigest = errors.New("missing mixHash in JSON block header") - errMissingHeaderFields = errors.New("missing required JSON block header fields") - errBadNonceSize = errors.New("invalid block nonce size, want 8 bytes") -) - // A BlockNonce is a 64-bit hash which proves (combined with the // mix-hash) that a sufficient amount of computation has been carried // out on a block. @@ -72,41 +64,35 @@ func (n *BlockNonce) UnmarshalText(input []byte) error { return hexutil.UnmarshalFixedText("BlockNonce", input, n[:]) } +//go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go + // Header represents a block header in the Ethereum blockchain. type Header struct { - ParentHash common.Hash // Hash to the previous block - UncleHash common.Hash // Uncles of this block - Coinbase common.Address // The coin base address - Root common.Hash // Block Trie state - TxHash common.Hash // Tx sha - ReceiptHash common.Hash // Receipt sha - Bloom Bloom // Bloom - Difficulty *big.Int // Difficulty for the current block - Number *big.Int // The block number - GasLimit *big.Int // Gas limit - GasUsed *big.Int // Gas used - Time *big.Int // Creation time - Extra []byte // Extra data - MixDigest common.Hash // for quick difficulty verification - Nonce BlockNonce -} - -type jsonHeader struct { - ParentHash *common.Hash `json:"parentHash"` - UncleHash *common.Hash `json:"sha3Uncles"` - Coinbase *common.Address `json:"miner"` - Root *common.Hash `json:"stateRoot"` - TxHash *common.Hash `json:"transactionsRoot"` - ReceiptHash *common.Hash `json:"receiptsRoot"` - Bloom *Bloom `json:"logsBloom"` - Difficulty *hexutil.Big `json:"difficulty"` - Number *hexutil.Big `json:"number"` - GasLimit *hexutil.Big `json:"gasLimit"` - GasUsed *hexutil.Big `json:"gasUsed"` - Time *hexutil.Big `json:"timestamp"` - Extra *hexutil.Bytes `json:"extraData"` - MixDigest *common.Hash `json:"mixHash"` - Nonce *BlockNonce `json:"nonce"` + ParentHash common.Hash `json:"parentHash"` + UncleHash common.Hash `json:"sha3Uncles"` + Coinbase common.Address `json:"miner"` + Root common.Hash `json:"stateRoot"` + TxHash common.Hash `json:"transactionsRoot"` + ReceiptHash common.Hash `json:"receiptsRoot"` + Bloom Bloom `json:"logsBloom"` + Difficulty *big.Int `json:"difficulty"` + Number *big.Int `json:"number"` + GasLimit *big.Int `json:"gasLimit"` + GasUsed *big.Int `json:"gasUsed"` + Time *big.Int `json:"timestamp"` + Extra []byte `json:"extraData"` + MixDigest common.Hash `json:"mixHash"` + Nonce BlockNonce `json:"nonce"` +} + +// field type overrides for gencodec +type headerMarshaling struct { + Difficulty *hexutil.Big + Number *hexutil.Big + GasLimit *hexutil.Big + GasUsed *hexutil.Big + Time *hexutil.Big + Extra hexutil.Bytes } // Hash returns the block hash of the header, which is simply the keccak256 hash of its @@ -134,65 +120,6 @@ func (h *Header) HashNoNonce() common.Hash { }) } -// MarshalJSON encodes headers into the web3 RPC response block format. -func (h *Header) MarshalJSON() ([]byte, error) { - return json.Marshal(&jsonHeader{ - ParentHash: &h.ParentHash, - UncleHash: &h.UncleHash, - Coinbase: &h.Coinbase, - Root: &h.Root, - TxHash: &h.TxHash, - ReceiptHash: &h.ReceiptHash, - Bloom: &h.Bloom, - Difficulty: (*hexutil.Big)(h.Difficulty), - Number: (*hexutil.Big)(h.Number), - GasLimit: (*hexutil.Big)(h.GasLimit), - GasUsed: (*hexutil.Big)(h.GasUsed), - Time: (*hexutil.Big)(h.Time), - Extra: (*hexutil.Bytes)(&h.Extra), - MixDigest: &h.MixDigest, - Nonce: &h.Nonce, - }) -} - -// UnmarshalJSON decodes headers from the web3 RPC response block format. -func (h *Header) UnmarshalJSON(input []byte) error { - var dec jsonHeader - if err := json.Unmarshal(input, &dec); err != nil { - return err - } - // Ensure that all fields are set. MixDigest is checked separately because - // it is a recent addition to the spec (as of August 2016) and older RPC server - // implementations might not provide it. - if dec.MixDigest == nil { - return errMissingHeaderMixDigest - } - if dec.ParentHash == nil || dec.UncleHash == nil || dec.Coinbase == nil || - dec.Root == nil || dec.TxHash == nil || dec.ReceiptHash == nil || - dec.Bloom == nil || dec.Difficulty == nil || dec.Number == nil || - dec.GasLimit == nil || dec.GasUsed == nil || dec.Time == nil || - dec.Extra == nil || dec.Nonce == nil { - return errMissingHeaderFields - } - // Assign all values. - h.ParentHash = *dec.ParentHash - h.UncleHash = *dec.UncleHash - h.Coinbase = *dec.Coinbase - h.Root = *dec.Root - h.TxHash = *dec.TxHash - h.ReceiptHash = *dec.ReceiptHash - h.Bloom = *dec.Bloom - h.Difficulty = (*big.Int)(dec.Difficulty) - h.Number = (*big.Int)(dec.Number) - h.GasLimit = (*big.Int)(dec.GasLimit) - h.GasUsed = (*big.Int)(dec.GasUsed) - h.Time = (*big.Int)(dec.Time) - h.Extra = *dec.Extra - h.MixDigest = *dec.MixDigest - h.Nonce = *dec.Nonce - return nil -} - func rlpHash(x interface{}) (h common.Hash) { hw := sha3.NewKeccak256() rlp.Encode(hw, x) |