github.com/amazechain/amc@v0.1.3/internal/avm/types/block.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/binary"
     5  	"encoding/json"
     6  	"errors"
     7  	"github.com/amazechain/amc/common/hexutil"
     8  	"github.com/amazechain/amc/common/types"
     9  	"github.com/amazechain/amc/internal/avm/common"
    10  	"math/big"
    11  	"reflect"
    12  )
    13  
    14  // var EmptyUncleHash = rlpHash([]*Header(nil))
    15  var EmptyUncleHash = types.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
    16  
    17  type BlockNonce [8]byte
    18  
    19  // EncodeNonce converts the given integer to a block nonce.
    20  func EncodeNonce(i uint64) BlockNonce {
    21  	var n BlockNonce
    22  	binary.BigEndian.PutUint64(n[:], i)
    23  	return n
    24  }
    25  
    26  // Uint64 returns the integer value of a block nonce.
    27  func (n BlockNonce) Uint64() uint64 {
    28  	return binary.BigEndian.Uint64(n[:])
    29  }
    30  
    31  // MarshalText encodes n as a hex string with 0x prefix.
    32  func (n BlockNonce) MarshalText() ([]byte, error) {
    33  	return hexutil.Bytes(n[:]).MarshalText()
    34  }
    35  
    36  // UnmarshalText implements encoding.TextUnmarshaler.
    37  func (n *BlockNonce) UnmarshalText(input []byte) error {
    38  	return hexutil.UnmarshalFixedText("BlockNonce", input, n[:])
    39  }
    40  
    41  type Header struct {
    42  	ParentHash  common.Hash    `json:"parentHash"       gencodec:"required"`
    43  	UncleHash   common.Hash    `json:"sha3Uncles"       gencodec:"required"`
    44  	Coinbase    common.Address `json:"miner"`
    45  	Root        common.Hash    `json:"stateRoot"        gencodec:"required"`
    46  	TxHash      common.Hash    `json:"transactionsRoot" gencodec:"required"`
    47  	ReceiptHash common.Hash    `json:"receiptsRoot"     gencodec:"required"`
    48  	Bloom       Bloom          `json:"logsBloom"        gencodec:"required"`
    49  	Difficulty  *big.Int       `json:"difficulty"       gencodec:"required"`
    50  	Number      *big.Int       `json:"number"           gencodec:"required"`
    51  	GasLimit    uint64         `json:"gasLimit"         gencodec:"required"`
    52  	GasUsed     uint64         `json:"gasUsed"          gencodec:"required"`
    53  	Time        uint64         `json:"timestamp"        gencodec:"required"`
    54  	Extra       []byte         `json:"extraData"        gencodec:"required"`
    55  	MixDigest   common.Hash    `json:"mixHash"`
    56  	Nonce       BlockNonce     `json:"nonce"`
    57  
    58  	// BaseFee was added by EIP-1559 and is ignored in legacy headers.
    59  	BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
    60  
    61  	/*
    62  		TODO (MariusVanDerWijden) Add this field once needed
    63  		// Random was added during the merge and contains the BeaconState randomness
    64  		Random common.Hash `json:"random" rlp:"optional"`
    65  	*/
    66  }
    67  
    68  // Hash returns the block hash of the header, which is simply the keccak256 hash of its
    69  // RLP encoding.
    70  func (h *Header) Hash() common.Hash {
    71  	return rlpHash(h)
    72  }
    73  
    74  var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size())
    75  
    76  // Size returns the approximate memory used by all internal contents. It is used
    77  // to approximate and limit the memory consumption of various caches.
    78  func (h *Header) Size() common.StorageSize {
    79  	var baseFeeBits int
    80  	if h.BaseFee != nil {
    81  		baseFeeBits = h.BaseFee.BitLen()
    82  	}
    83  	return headerSize + common.StorageSize(len(h.Extra)+(h.Difficulty.BitLen()+h.Number.BitLen()+baseFeeBits)/8)
    84  }
    85  
    86  // MarshalJSON marshals as JSON.
    87  func (h Header) MarshalJSON() ([]byte, error) {
    88  	type Header struct {
    89  		ParentHash  common.Hash    `json:"parentHash"       gencodec:"required"`
    90  		UncleHash   common.Hash    `json:"sha3Uncles"       gencodec:"required"`
    91  		Coinbase    common.Address `json:"miner"`
    92  		Root        common.Hash    `json:"stateRoot"        gencodec:"required"`
    93  		TxHash      common.Hash    `json:"transactionsRoot" gencodec:"required"`
    94  		ReceiptHash common.Hash    `json:"receiptsRoot"     gencodec:"required"`
    95  		Bloom       Bloom          `json:"logsBloom"        gencodec:"required"`
    96  		Difficulty  *hexutil.Big   `json:"difficulty"       gencodec:"required"`
    97  		Number      *hexutil.Big   `json:"number"           gencodec:"required"`
    98  		GasLimit    hexutil.Uint64 `json:"gasLimit"         gencodec:"required"`
    99  		GasUsed     hexutil.Uint64 `json:"gasUsed"          gencodec:"required"`
   100  		Time        hexutil.Uint64 `json:"timestamp"        gencodec:"required"`
   101  		Extra       hexutil.Bytes  `json:"extraData"        gencodec:"required"`
   102  		MixDigest   common.Hash    `json:"mixHash"`
   103  		Nonce       BlockNonce     `json:"nonce"`
   104  		BaseFee     *hexutil.Big   `json:"baseFeePerGas" rlp:"optional"`
   105  		Hash        common.Hash    `json:"hash"`
   106  	}
   107  	var enc Header
   108  	enc.ParentHash = h.ParentHash
   109  	enc.UncleHash = h.UncleHash
   110  	enc.Coinbase = h.Coinbase
   111  	enc.Root = h.Root
   112  	enc.TxHash = h.TxHash
   113  	enc.ReceiptHash = h.ReceiptHash
   114  	enc.Bloom = h.Bloom
   115  	enc.Difficulty = (*hexutil.Big)(h.Difficulty)
   116  	enc.Number = (*hexutil.Big)(h.Number)
   117  	enc.GasLimit = hexutil.Uint64(h.GasLimit)
   118  	enc.GasUsed = hexutil.Uint64(h.GasUsed)
   119  	enc.Time = hexutil.Uint64(h.Time)
   120  	enc.Extra = h.Extra
   121  	enc.MixDigest = h.MixDigest
   122  	enc.Nonce = h.Nonce
   123  	enc.BaseFee = (*hexutil.Big)(h.BaseFee)
   124  	enc.Hash = h.Hash()
   125  	return json.Marshal(&enc)
   126  }
   127  
   128  // UnmarshalJSON unmarshals from JSON.
   129  func (h *Header) UnmarshalJSON(input []byte) error {
   130  	type Header struct {
   131  		ParentHash  *common.Hash    `json:"parentHash"       gencodec:"required"`
   132  		UncleHash   *common.Hash    `json:"sha3Uncles"       gencodec:"required"`
   133  		Coinbase    *common.Address `json:"miner"`
   134  		Root        *common.Hash    `json:"stateRoot"        gencodec:"required"`
   135  		TxHash      *common.Hash    `json:"transactionsRoot" gencodec:"required"`
   136  		ReceiptHash *common.Hash    `json:"receiptsRoot"     gencodec:"required"`
   137  		Bloom       *Bloom          `json:"logsBloom"        gencodec:"required"`
   138  		Difficulty  *hexutil.Big    `json:"difficulty"       gencodec:"required"`
   139  		Number      *hexutil.Big    `json:"number"           gencodec:"required"`
   140  		GasLimit    *hexutil.Uint64 `json:"gasLimit"         gencodec:"required"`
   141  		GasUsed     *hexutil.Uint64 `json:"gasUsed"          gencodec:"required"`
   142  		Time        *hexutil.Uint64 `json:"timestamp"        gencodec:"required"`
   143  		Extra       *hexutil.Bytes  `json:"extraData"        gencodec:"required"`
   144  		MixDigest   *common.Hash    `json:"mixHash"`
   145  		Nonce       *BlockNonce     `json:"nonce"`
   146  		BaseFee     *hexutil.Big    `json:"baseFeePerGas" rlp:"optional"`
   147  	}
   148  	var dec Header
   149  	if err := json.Unmarshal(input, &dec); err != nil {
   150  		return err
   151  	}
   152  	if dec.ParentHash == nil {
   153  		return errors.New("missing required field 'parentHash' for Header")
   154  	}
   155  	h.ParentHash = *dec.ParentHash
   156  	if dec.UncleHash == nil {
   157  		return errors.New("missing required field 'sha3Uncles' for Header")
   158  	}
   159  	h.UncleHash = *dec.UncleHash
   160  	if dec.Coinbase != nil {
   161  		h.Coinbase = *dec.Coinbase
   162  	}
   163  	if dec.Root == nil {
   164  		return errors.New("missing required field 'stateRoot' for Header")
   165  	}
   166  	h.Root = *dec.Root
   167  	if dec.TxHash == nil {
   168  		return errors.New("missing required field 'transactionsRoot' for Header")
   169  	}
   170  	h.TxHash = *dec.TxHash
   171  	if dec.ReceiptHash == nil {
   172  		return errors.New("missing required field 'receiptsRoot' for Header")
   173  	}
   174  	h.ReceiptHash = *dec.ReceiptHash
   175  	if dec.Bloom == nil {
   176  		return errors.New("missing required field 'logsBloom' for Header")
   177  	}
   178  	h.Bloom = *dec.Bloom
   179  	if dec.Difficulty == nil {
   180  		return errors.New("missing required field 'difficulty' for Header")
   181  	}
   182  	h.Difficulty = (*big.Int)(dec.Difficulty)
   183  	if dec.Number == nil {
   184  		return errors.New("missing required field 'number' for Header")
   185  	}
   186  	h.Number = (*big.Int)(dec.Number)
   187  	if dec.GasLimit == nil {
   188  		return errors.New("missing required field 'gasLimit' for Header")
   189  	}
   190  	h.GasLimit = uint64(*dec.GasLimit)
   191  	if dec.GasUsed == nil {
   192  		return errors.New("missing required field 'gasUsed' for Header")
   193  	}
   194  	h.GasUsed = uint64(*dec.GasUsed)
   195  	if dec.Time == nil {
   196  		return errors.New("missing required field 'timestamp' for Header")
   197  	}
   198  	h.Time = uint64(*dec.Time)
   199  	if dec.Extra == nil {
   200  		return errors.New("missing required field 'extraData' for Header")
   201  	}
   202  	h.Extra = *dec.Extra
   203  	if dec.MixDigest != nil {
   204  		h.MixDigest = *dec.MixDigest
   205  	}
   206  	if dec.Nonce != nil {
   207  		h.Nonce = *dec.Nonce
   208  	}
   209  	if dec.BaseFee != nil {
   210  		h.BaseFee = (*big.Int)(dec.BaseFee)
   211  	}
   212  	return nil
   213  }