gitee.com/liu-zhao234568/cntest@v1.0.0/core/forkid/forkid.go (about)

     1  // Copyright 2019 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 forkid implements EIP-2124 (https://eips.ethereum.org/EIPS/eip-2124).
    18  package forkid
    19  
    20  import (
    21  	"encoding/binary"
    22  	"errors"
    23  	"hash/crc32"
    24  	"math"
    25  	"math/big"
    26  	"reflect"
    27  	"strings"
    28  
    29  	"gitee.com/liu-zhao234568/cntest/common"
    30  	"gitee.com/liu-zhao234568/cntest/core/types"
    31  	"gitee.com/liu-zhao234568/cntest/log"
    32  	"gitee.com/liu-zhao234568/cntest/params"
    33  )
    34  
    35  var (
    36  	// ErrRemoteStale is returned by the validator if a remote fork checksum is a
    37  	// subset of our already applied forks, but the announced next fork block is
    38  	// not on our already passed chain.
    39  	ErrRemoteStale = errors.New("remote needs update")
    40  
    41  	// ErrLocalIncompatibleOrStale is returned by the validator if a remote fork
    42  	// checksum does not match any local checksum variation, signalling that the
    43  	// two chains have diverged in the past at some point (possibly at genesis).
    44  	ErrLocalIncompatibleOrStale = errors.New("local incompatible or needs update")
    45  )
    46  
    47  // Blockchain defines all necessary method to build a forkID.
    48  type Blockchain interface {
    49  	// Config retrieves the chain's fork configuration.
    50  	Config() *params.ChainConfig
    51  
    52  	// Genesis retrieves the chain's genesis block.
    53  	Genesis() *types.Block
    54  
    55  	// CurrentHeader retrieves the current head header of the canonical chain.
    56  	CurrentHeader() *types.Header
    57  }
    58  
    59  // ID is a fork identifier as defined by EIP-2124.
    60  type ID struct {
    61  	Hash [4]byte // CRC32 checksum of the genesis block and passed fork block numbers
    62  	Next uint64  // Block number of the next upcoming fork, or 0 if no forks are known
    63  }
    64  
    65  // Filter is a fork id filter to validate a remotely advertised ID.
    66  type Filter func(id ID) error
    67  
    68  // NewID calculates the Ethereum fork ID from the chain config, genesis hash, and head.
    69  func NewID(config *params.ChainConfig, genesis common.Hash, head uint64) ID {
    70  	// Calculate the starting checksum from the genesis hash
    71  	hash := crc32.ChecksumIEEE(genesis[:])
    72  
    73  	// Calculate the current fork checksum and the next fork block
    74  	var next uint64
    75  	for _, fork := range gatherForks(config) {
    76  		if fork <= head {
    77  			// Fork already passed, checksum the previous hash and the fork number
    78  			hash = checksumUpdate(hash, fork)
    79  			continue
    80  		}
    81  		next = fork
    82  		break
    83  	}
    84  	return ID{Hash: checksumToBytes(hash), Next: next}
    85  }
    86  
    87  // NewIDWithChain calculates the Ethereum fork ID from an existing chain instance.
    88  func NewIDWithChain(chain Blockchain) ID {
    89  	return NewID(
    90  		chain.Config(),
    91  		chain.Genesis().Hash(),
    92  		chain.CurrentHeader().Number.Uint64(),
    93  	)
    94  }
    95  
    96  // NewFilter creates a filter that returns if a fork ID should be rejected or not
    97  // based on the local chain's status.
    98  func NewFilter(chain Blockchain) Filter {
    99  	return newFilter(
   100  		chain.Config(),
   101  		chain.Genesis().Hash(),
   102  		func() uint64 {
   103  			return chain.CurrentHeader().Number.Uint64()
   104  		},
   105  	)
   106  }
   107  
   108  // NewStaticFilter creates a filter at block zero.
   109  func NewStaticFilter(config *params.ChainConfig, genesis common.Hash) Filter {
   110  	head := func() uint64 { return 0 }
   111  	return newFilter(config, genesis, head)
   112  }
   113  
   114  // newFilter is the internal version of NewFilter, taking closures as its arguments
   115  // instead of a chain. The reason is to allow testing it without having to simulate
   116  // an entire blockchain.
   117  func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() uint64) Filter {
   118  	// Calculate the all the valid fork hash and fork next combos
   119  	var (
   120  		forks = gatherForks(config)
   121  		sums  = make([][4]byte, len(forks)+1) // 0th is the genesis
   122  	)
   123  	hash := crc32.ChecksumIEEE(genesis[:])
   124  	sums[0] = checksumToBytes(hash)
   125  	for i, fork := range forks {
   126  		hash = checksumUpdate(hash, fork)
   127  		sums[i+1] = checksumToBytes(hash)
   128  	}
   129  	// Add two sentries to simplify the fork checks and don't require special
   130  	// casing the last one.
   131  	forks = append(forks, math.MaxUint64) // Last fork will never be passed
   132  
   133  	// Create a validator that will filter out incompatible chains
   134  	return func(id ID) error {
   135  		// Run the fork checksum validation ruleset:
   136  		//   1. If local and remote FORK_CSUM matches, compare local head to FORK_NEXT.
   137  		//        The two nodes are in the same fork state currently. They might know
   138  		//        of differing future forks, but that's not relevant until the fork
   139  		//        triggers (might be postponed, nodes might be updated to match).
   140  		//      1a. A remotely announced but remotely not passed block is already passed
   141  		//          locally, disconnect, since the chains are incompatible.
   142  		//      1b. No remotely announced fork; or not yet passed locally, connect.
   143  		//   2. If the remote FORK_CSUM is a subset of the local past forks and the
   144  		//      remote FORK_NEXT matches with the locally following fork block number,
   145  		//      connect.
   146  		//        Remote node is currently syncing. It might eventually diverge from
   147  		//        us, but at this current point in time we don't have enough information.
   148  		//   3. If the remote FORK_CSUM is a superset of the local past forks and can
   149  		//      be completed with locally known future forks, connect.
   150  		//        Local node is currently syncing. It might eventually diverge from
   151  		//        the remote, but at this current point in time we don't have enough
   152  		//        information.
   153  		//   4. Reject in all other cases.
   154  		head := headfn()
   155  		for i, fork := range forks {
   156  			// If our head is beyond this fork, continue to the next (we have a dummy
   157  			// fork of maxuint64 as the last item to always fail this check eventually).
   158  			if head >= fork {
   159  				continue
   160  			}
   161  			// Found the first unpassed fork block, check if our current state matches
   162  			// the remote checksum (rule #1).
   163  			if sums[i] == id.Hash {
   164  				// Fork checksum matched, check if a remote future fork block already passed
   165  				// locally without the local node being aware of it (rule #1a).
   166  				if id.Next > 0 && head >= id.Next {
   167  					return ErrLocalIncompatibleOrStale
   168  				}
   169  				// Haven't passed locally a remote-only fork, accept the connection (rule #1b).
   170  				return nil
   171  			}
   172  			// The local and remote nodes are in different forks currently, check if the
   173  			// remote checksum is a subset of our local forks (rule #2).
   174  			for j := 0; j < i; j++ {
   175  				if sums[j] == id.Hash {
   176  					// Remote checksum is a subset, validate based on the announced next fork
   177  					if forks[j] != id.Next {
   178  						return ErrRemoteStale
   179  					}
   180  					return nil
   181  				}
   182  			}
   183  			// Remote chain is not a subset of our local one, check if it's a superset by
   184  			// any chance, signalling that we're simply out of sync (rule #3).
   185  			for j := i + 1; j < len(sums); j++ {
   186  				if sums[j] == id.Hash {
   187  					// Yay, remote checksum is a superset, ignore upcoming forks
   188  					return nil
   189  				}
   190  			}
   191  			// No exact, subset or superset match. We are on differing chains, reject.
   192  			return ErrLocalIncompatibleOrStale
   193  		}
   194  		log.Error("Impossible fork ID validation", "id", id)
   195  		return nil // Something's very wrong, accept rather than reject
   196  	}
   197  }
   198  
   199  // checksumUpdate calculates the next IEEE CRC32 checksum based on the previous
   200  // one and a fork block number (equivalent to CRC32(original-blob || fork)).
   201  func checksumUpdate(hash uint32, fork uint64) uint32 {
   202  	var blob [8]byte
   203  	binary.BigEndian.PutUint64(blob[:], fork)
   204  	return crc32.Update(hash, crc32.IEEETable, blob[:])
   205  }
   206  
   207  // checksumToBytes converts a uint32 checksum into a [4]byte array.
   208  func checksumToBytes(hash uint32) [4]byte {
   209  	var blob [4]byte
   210  	binary.BigEndian.PutUint32(blob[:], hash)
   211  	return blob
   212  }
   213  
   214  // gatherForks gathers all the known forks and creates a sorted list out of them.
   215  func gatherForks(config *params.ChainConfig) []uint64 {
   216  	// Gather all the fork block numbers via reflection
   217  	kind := reflect.TypeOf(params.ChainConfig{})
   218  	conf := reflect.ValueOf(config).Elem()
   219  
   220  	var forks []uint64
   221  	for i := 0; i < kind.NumField(); i++ {
   222  		// Fetch the next field and skip non-fork rules
   223  		field := kind.Field(i)
   224  		if !strings.HasSuffix(field.Name, "Block") {
   225  			continue
   226  		}
   227  		if field.Type != reflect.TypeOf(new(big.Int)) {
   228  			continue
   229  		}
   230  		// Extract the fork rule block number and aggregate it
   231  		rule := conf.Field(i).Interface().(*big.Int)
   232  		if rule != nil {
   233  			forks = append(forks, rule.Uint64())
   234  		}
   235  	}
   236  	// Sort the fork block numbers to permit chronological XOR
   237  	for i := 0; i < len(forks); i++ {
   238  		for j := i + 1; j < len(forks); j++ {
   239  			if forks[i] > forks[j] {
   240  				forks[i], forks[j] = forks[j], forks[i]
   241  			}
   242  		}
   243  	}
   244  	// Deduplicate block numbers applying multiple forks
   245  	for i := 1; i < len(forks); i++ {
   246  		if forks[i] == forks[i-1] {
   247  			forks = append(forks[:i], forks[i+1:]...)
   248  			i--
   249  		}
   250  	}
   251  	// Skip any forks in block 0, that's the genesis ruleset
   252  	if len(forks) > 0 && forks[0] == 0 {
   253  		forks = forks[1:]
   254  	}
   255  	return forks
   256  }