github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/core/types/block.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package types contains data types related to Ethereum consensus.
    18  package types
    19  
    20  import (
    21  	"encoding/binary"
    22  	"fmt"
    23  	"io"
    24  	"math/big"
    25  	"sort"
    26  	"sync/atomic"
    27  	"time"
    28  
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/common/hexutil"
    31  	"github.com/ethereum/go-ethereum/crypto/sha3"
    32  	"github.com/ethereum/go-ethereum/rlp"
    33  )
    34  
    35  var (
    36  	EmptyRootHash  = DeriveSha(Transactions{})
    37  	EmptyUncleHash = CalcUncleHash(nil)
    38  )
    39  
    40  // A BlockNonce is a 64-bit hash which proves (combined with the
    41  // mix-hash) that a sufficient amount of computation has been carried
    42  // out on a block.
    43  type BlockNonce [8]byte
    44  
    45  // EncodeNonce converts the given integer to a block nonce.
    46  func EncodeNonce(i uint64) BlockNonce {
    47  	var n BlockNonce
    48  	binary.BigEndian.PutUint64(n[:], i)
    49  	return n
    50  }
    51  
    52  // Uint64 returns the integer value of a block nonce.
    53  func (n BlockNonce) Uint64() uint64 {
    54  	return binary.BigEndian.Uint64(n[:])
    55  }
    56  
    57  // MarshalText encodes n as a hex string with 0x prefix.
    58  func (n BlockNonce) MarshalText() ([]byte, error) {
    59  	return hexutil.Bytes(n[:]).MarshalText()
    60  }
    61  
    62  // UnmarshalText implements encoding.TextUnmarshaler.
    63  func (n *BlockNonce) UnmarshalText(input []byte) error {
    64  	return hexutil.UnmarshalFixedText("BlockNonce", input, n[:])
    65  }
    66  
    67  //go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go
    68  
    69  // Header represents a block header in the Ethereum blockchain.
    70  type Header struct {
    71  	ParentHash  common.Hash    `json:"parentHash"       gencodec:"required"`
    72  	UncleHash   common.Hash    `json:"sha3Uncles"       gencodec:"required"`
    73  	Coinbase    common.Address `json:"miner"            gencodec:"required"`
    74  	Root        common.Hash    `json:"stateRoot"        gencodec:"required"`
    75  	TxHash      common.Hash    `json:"transactionsRoot" gencodec:"required"`
    76  	ReceiptHash common.Hash    `json:"receiptsRoot"     gencodec:"required"`
    77  	Bloom       Bloom          `json:"logsBloom"        gencodec:"required"`
    78  	Difficulty  *big.Int       `json:"difficulty"       gencodec:"required"`
    79  	Number      *big.Int       `json:"number"           gencodec:"required"`
    80  	GasLimit    *big.Int       `json:"gasLimit"         gencodec:"required"`
    81  	GasUsed     *big.Int       `json:"gasUsed"          gencodec:"required"`
    82  	Time        *big.Int       `json:"timestamp"        gencodec:"required"`
    83  	Extra       []byte         `json:"extraData"        gencodec:"required"`
    84  	MixDigest   common.Hash    `json:"mixHash"          gencodec:"required"`
    85  	Nonce       BlockNonce     `json:"nonce"            gencodec:"required"`
    86  }
    87  
    88  // field type overrides for gencodec
    89  type headerMarshaling struct {
    90  	Difficulty *hexutil.Big
    91  	Number     *hexutil.Big
    92  	GasLimit   *hexutil.Big
    93  	GasUsed    *hexutil.Big
    94  	Time       *hexutil.Big
    95  	Extra      hexutil.Bytes
    96  	Hash       common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON
    97  }
    98  
    99  // Hash returns the block hash of the header, which is simply the keccak256 hash of its
   100  // RLP encoding.
   101  func (h *Header) Hash() common.Hash {
   102  	// If the mix digest is equivalent to the predefined Istanbul digest, use Istanbul
   103  	// specific hash calculation.
   104  	if h.MixDigest == PoDCDigest {
   105  		// Seal is reserved in extra-data. To prove block is signed by the proposer.
   106  		/* if istanbulHeader := PoDCFilteredHeader(h, true); istanbulHeader != nil {
   107  			return rlpHash(istanbulHeader)
   108  		} */
   109  		if podcHeader := PoDCFilteredHeader(h, true); podcHeader != nil {
   110  			return rlpHash(podcHeader)
   111  		}
   112  	}
   113  	return rlpHash(h)
   114  }
   115  
   116  // HashNoNonce returns the hash which is used as input for the proof-of-work search.
   117  func (h *Header) HashNoNonce() common.Hash {
   118  	return rlpHash([]interface{}{
   119  		h.ParentHash,
   120  		h.UncleHash,
   121  		h.Coinbase,
   122  		h.Root,
   123  		h.TxHash,
   124  		h.ReceiptHash,
   125  		h.Bloom,
   126  		h.Difficulty,
   127  		h.Number,
   128  		h.GasLimit,
   129  		h.GasUsed,
   130  		h.Time,
   131  		h.Extra,
   132  	})
   133  }
   134  
   135  func rlpHash(x interface{}) (h common.Hash) {
   136  	hw := sha3.NewKeccak256()
   137  	rlp.Encode(hw, x)
   138  	hw.Sum(h[:0])
   139  	return h
   140  }
   141  
   142  // Body is a simple (mutable, non-safe) data container for storing and moving
   143  // a block's data contents (transactions and uncles) together.
   144  type Body struct {
   145  	Transactions []*Transaction
   146  	Uncles       []*Header
   147  }
   148  
   149  // Block represents an entire block in the Ethereum blockchain.
   150  type Block struct {
   151  	header       *Header
   152  	uncles       []*Header
   153  	transactions Transactions
   154  
   155  	// caches
   156  	hash atomic.Value
   157  	size atomic.Value
   158  
   159  	// Td is used by package core to store the total difficulty
   160  	// of the chain up to and including the block.
   161  	td *big.Int
   162  
   163  	// These fields are used by package eth to track
   164  	// inter-peer block relay.
   165  	ReceivedAt   time.Time
   166  	ReceivedFrom interface{}
   167  }
   168  
   169  // DeprecatedTd is an old relic for extracting the TD of a block. It is in the
   170  // code solely to facilitate upgrading the database from the old format to the
   171  // new, after which it should be deleted. Do not use!
   172  func (b *Block) DeprecatedTd() *big.Int {
   173  	return b.td
   174  }
   175  
   176  // [deprecated by eth/63]
   177  // StorageBlock defines the RLP encoding of a Block stored in the
   178  // state database. The StorageBlock encoding contains fields that
   179  // would otherwise need to be recomputed.
   180  type StorageBlock Block
   181  
   182  // "external" block encoding. used for eth protocol, etc.
   183  type extblock struct {
   184  	Header *Header
   185  	Txs    []*Transaction
   186  	Uncles []*Header
   187  }
   188  
   189  // [deprecated by eth/63]
   190  // "storage" block encoding. used for database.
   191  type storageblock struct {
   192  	Header *Header
   193  	Txs    []*Transaction
   194  	Uncles []*Header
   195  	TD     *big.Int
   196  }
   197  
   198  // NewBlock creates a new block. The input data is copied,
   199  // changes to header and to the field values will not affect the
   200  // block.
   201  //
   202  // The values of TxHash, UncleHash, ReceiptHash and Bloom in header
   203  // are ignored and set to values derived from the given txs, uncles
   204  // and receipts.
   205  func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt) *Block {
   206  	b := &Block{header: CopyHeader(header), td: new(big.Int)}
   207  
   208  	// TODO: panic if len(txs) != len(receipts)
   209  	if len(txs) == 0 {
   210  		b.header.TxHash = EmptyRootHash
   211  	} else {
   212  		b.header.TxHash = DeriveSha(Transactions(txs))
   213  		b.transactions = make(Transactions, len(txs))
   214  		copy(b.transactions, txs)
   215  	}
   216  
   217  	if len(receipts) == 0 {
   218  		b.header.ReceiptHash = EmptyRootHash
   219  	} else {
   220  		b.header.ReceiptHash = DeriveSha(Receipts(receipts))
   221  		b.header.Bloom = CreateBloom(receipts)
   222  	}
   223  
   224  	if len(uncles) == 0 {
   225  		b.header.UncleHash = EmptyUncleHash
   226  	} else {
   227  		b.header.UncleHash = CalcUncleHash(uncles)
   228  		b.uncles = make([]*Header, len(uncles))
   229  		for i := range uncles {
   230  			b.uncles[i] = CopyHeader(uncles[i])
   231  		}
   232  	}
   233  
   234  	return b
   235  }
   236  
   237  // NewBlockWithHeader creates a block with the given header data. The
   238  // header data is copied, changes to header and to the field values
   239  // will not affect the block.
   240  func NewBlockWithHeader(header *Header) *Block {
   241  	return &Block{header: CopyHeader(header)}
   242  }
   243  
   244  // CopyHeader creates a deep copy of a block header to prevent side effects from
   245  // modifying a header variable.
   246  func CopyHeader(h *Header) *Header {
   247  	cpy := *h
   248  	if cpy.Time = new(big.Int); h.Time != nil {
   249  		cpy.Time.Set(h.Time)
   250  	}
   251  	if cpy.Difficulty = new(big.Int); h.Difficulty != nil {
   252  		cpy.Difficulty.Set(h.Difficulty)
   253  	}
   254  	if cpy.Number = new(big.Int); h.Number != nil {
   255  		cpy.Number.Set(h.Number)
   256  	}
   257  	if cpy.GasLimit = new(big.Int); h.GasLimit != nil {
   258  		cpy.GasLimit.Set(h.GasLimit)
   259  	}
   260  	if cpy.GasUsed = new(big.Int); h.GasUsed != nil {
   261  		cpy.GasUsed.Set(h.GasUsed)
   262  	}
   263  	if len(h.Extra) > 0 {
   264  		cpy.Extra = make([]byte, len(h.Extra))
   265  		copy(cpy.Extra, h.Extra)
   266  	}
   267  	return &cpy
   268  }
   269  
   270  // DecodeRLP decodes the Ethereum
   271  func (b *Block) DecodeRLP(s *rlp.Stream) error {
   272  	var eb extblock
   273  	_, size, _ := s.Kind()
   274  	if err := s.Decode(&eb); err != nil {
   275  		return err
   276  	}
   277  	b.header, b.uncles, b.transactions = eb.Header, eb.Uncles, eb.Txs
   278  	b.size.Store(common.StorageSize(rlp.ListSize(size)))
   279  	return nil
   280  }
   281  
   282  // EncodeRLP serializes b into the Ethereum RLP block format.
   283  func (b *Block) EncodeRLP(w io.Writer) error {
   284  	return rlp.Encode(w, extblock{
   285  		Header: b.header,
   286  		Txs:    b.transactions,
   287  		Uncles: b.uncles,
   288  	})
   289  }
   290  
   291  // [deprecated by eth/63]
   292  func (b *StorageBlock) DecodeRLP(s *rlp.Stream) error {
   293  	var sb storageblock
   294  	if err := s.Decode(&sb); err != nil {
   295  		return err
   296  	}
   297  	b.header, b.uncles, b.transactions, b.td = sb.Header, sb.Uncles, sb.Txs, sb.TD
   298  	return nil
   299  }
   300  
   301  // TODO: copies
   302  
   303  func (b *Block) Uncles() []*Header          { return b.uncles }
   304  func (b *Block) Transactions() Transactions { return b.transactions }
   305  
   306  func (b *Block) Transaction(hash common.Hash) *Transaction {
   307  	for _, transaction := range b.transactions {
   308  		if transaction.Hash() == hash {
   309  			return transaction
   310  		}
   311  	}
   312  	return nil
   313  }
   314  
   315  func (b *Block) Number() *big.Int     { return new(big.Int).Set(b.header.Number) }
   316  func (b *Block) GasLimit() *big.Int   { return new(big.Int).Set(b.header.GasLimit) }
   317  func (b *Block) GasUsed() *big.Int    { return new(big.Int).Set(b.header.GasUsed) }
   318  func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
   319  func (b *Block) Time() *big.Int       { return new(big.Int).Set(b.header.Time) }
   320  
   321  func (b *Block) NumberU64() uint64        { return b.header.Number.Uint64() }
   322  func (b *Block) MixDigest() common.Hash   { return b.header.MixDigest }
   323  func (b *Block) Nonce() uint64            { return binary.BigEndian.Uint64(b.header.Nonce[:]) }
   324  func (b *Block) Bloom() Bloom             { return b.header.Bloom }
   325  func (b *Block) Coinbase() common.Address { return b.header.Coinbase }
   326  func (b *Block) Root() common.Hash        { return b.header.Root }
   327  func (b *Block) ParentHash() common.Hash  { return b.header.ParentHash }
   328  func (b *Block) TxHash() common.Hash      { return b.header.TxHash }
   329  func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash }
   330  func (b *Block) UncleHash() common.Hash   { return b.header.UncleHash }
   331  func (b *Block) Extra() []byte            { return common.CopyBytes(b.header.Extra) }
   332  
   333  func (b *Block) Header() *Header { return CopyHeader(b.header) }
   334  
   335  // Body returns the non-header content of the block.
   336  func (b *Block) Body() *Body { return &Body{b.transactions, b.uncles} }
   337  
   338  func (b *Block) HashNoNonce() common.Hash {
   339  	return b.header.HashNoNonce()
   340  }
   341  
   342  func (b *Block) Size() common.StorageSize {
   343  	if size := b.size.Load(); size != nil {
   344  		return size.(common.StorageSize)
   345  	}
   346  	c := writeCounter(0)
   347  	rlp.Encode(&c, b)
   348  	b.size.Store(common.StorageSize(c))
   349  	return common.StorageSize(c)
   350  }
   351  
   352  type writeCounter common.StorageSize
   353  
   354  func (c *writeCounter) Write(b []byte) (int, error) {
   355  	*c += writeCounter(len(b))
   356  	return len(b), nil
   357  }
   358  
   359  func CalcUncleHash(uncles []*Header) common.Hash {
   360  	return rlpHash(uncles)
   361  }
   362  
   363  // WithSeal returns a new block with the data from b but the header replaced with
   364  // the sealed one.
   365  func (b *Block) WithSeal(header *Header) *Block {
   366  	cpy := *header
   367  
   368  	return &Block{
   369  		header:       &cpy,
   370  		transactions: b.transactions,
   371  		uncles:       b.uncles,
   372  	}
   373  }
   374  
   375  // WithBody returns a new block with the given transaction and uncle contents.
   376  func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
   377  	block := &Block{
   378  		header:       CopyHeader(b.header),
   379  		transactions: make([]*Transaction, len(transactions)),
   380  		uncles:       make([]*Header, len(uncles)),
   381  	}
   382  	copy(block.transactions, transactions)
   383  	for i := range uncles {
   384  		block.uncles[i] = CopyHeader(uncles[i])
   385  	}
   386  	return block
   387  }
   388  
   389  // Hash returns the keccak256 hash of b's header.
   390  // The hash is computed on the first call and cached thereafter.
   391  func (b *Block) Hash() common.Hash {
   392  	if hash := b.hash.Load(); hash != nil {
   393  		return hash.(common.Hash)
   394  	}
   395  	v := b.header.Hash()
   396  	b.hash.Store(v)
   397  	return v
   398  }
   399  
   400  func (b *Block) String() string {
   401  	str := fmt.Sprintf(`Block(#%v): Size: %v {
   402  MinerHash: %x
   403  %v
   404  Transactions:
   405  %v
   406  Uncles:
   407  %v
   408  }
   409  `, b.Number(), b.Size(), b.header.HashNoNonce(), b.header, b.transactions, b.uncles)
   410  	return str
   411  }
   412  
   413  func (h *Header) String() string {
   414  	return fmt.Sprintf(`Header(%x):
   415  [
   416  	ParentHash:	    %x
   417  	UncleHash:	    %x
   418  	Coinbase:	    %x
   419  	Root:		    %x
   420  	TxSha		    %x
   421  	ReceiptSha:	    %x
   422  	Bloom:		    %x
   423  	Difficulty:	    %v
   424  	Number:		    %v
   425  	GasLimit:	    %v
   426  	GasUsed:	    %v
   427  	Time:		    %v
   428  	Extra:		    %s
   429  	MixDigest:      %x
   430  	Nonce:		    %x
   431  ]`, h.Hash(), h.ParentHash, h.UncleHash, h.Coinbase, h.Root, h.TxHash, h.ReceiptHash, h.Bloom, h.Difficulty, h.Number, h.GasLimit, h.GasUsed, h.Time, h.Extra, h.MixDigest, h.Nonce)
   432  }
   433  
   434  type Blocks []*Block
   435  
   436  type BlockBy func(b1, b2 *Block) bool
   437  
   438  func (self BlockBy) Sort(blocks Blocks) {
   439  	bs := blockSorter{
   440  		blocks: blocks,
   441  		by:     self,
   442  	}
   443  	sort.Sort(bs)
   444  }
   445  
   446  type blockSorter struct {
   447  	blocks Blocks
   448  	by     func(b1, b2 *Block) bool
   449  }
   450  
   451  func (self blockSorter) Len() int { return len(self.blocks) }
   452  func (self blockSorter) Swap(i, j int) {
   453  	self.blocks[i], self.blocks[j] = self.blocks[j], self.blocks[i]
   454  }
   455  func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) }
   456  
   457  func Number(b1, b2 *Block) bool { return b1.header.Number.Cmp(b2.header.Number) < 0 }