From 3e50c8176db95aa5156fc1ae73e33ee6fc2b2cf5 Mon Sep 17 00:00:00 2001 From: KamiD <44460798+KamiD@users.noreply.github.com> Date: Tue, 30 Mar 2021 15:20:38 +0800 Subject: [PATCH] merge BlockNonce (#785) --- x/evm/watcher/types.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/x/evm/watcher/types.go b/x/evm/watcher/types.go index b508e4deec..83e8dbc936 100644 --- a/x/evm/watcher/types.go +++ b/x/evm/watcher/types.go @@ -1,6 +1,7 @@ package watcher import ( + "encoding/binary" "encoding/json" "math/big" "strconv" @@ -157,11 +158,38 @@ type MsgBlock struct { block string } +// 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. +type BlockNonce [8]byte + +// EncodeNonce converts the given integer to a block nonce. +func EncodeNonce(i uint64) BlockNonce { + var n BlockNonce + binary.BigEndian.PutUint64(n[:], i) + return n +} + +// Uint64 returns the integer value of a block nonce. +func (n BlockNonce) Uint64() uint64 { + return binary.BigEndian.Uint64(n[:]) +} + +// MarshalText encodes n as a hex string with 0x prefix. +func (n BlockNonce) MarshalText() ([]byte, error) { + return hexutil.Bytes(n[:]).MarshalText() +} + +// UnmarshalText implements encoding.TextUnmarshaler. +func (n *BlockNonce) UnmarshalText(input []byte) error { + return hexutil.UnmarshalFixedText("BlockNonce", input, n[:]) +} + type EthBlock struct { Number hexutil.Uint64 `json:"number"` Hash common.Hash `json:"hash"` ParentHash common.Hash `json:"parentHash"` - Nonce uint64 `json:"nonce"` + Nonce BlockNonce `json:"nonce"` Sha3Uncles common.Hash `json:"sha3Uncles"` LogsBloom ethtypes.Bloom `json:"logsBloom"` TransactionsRoot common.Hash `json:"transactionsRoot"` @@ -185,7 +213,7 @@ func NewMsgBlock(height uint64, blockBloom ethtypes.Bloom, blockHash common.Hash Number: hexutil.Uint64(height), Hash: blockHash, ParentHash: common.BytesToHash(header.LastBlockId.Hash), - Nonce: 0, + Nonce: BlockNonce{}, Sha3Uncles: common.Hash{}, LogsBloom: blockBloom, TransactionsRoot: common.BytesToHash(header.DataHash),