gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/params/hf.go (about)

     1  // Copyright 2018 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package params
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"strings"
    23  )
    24  
    25  type ForkMap map[int]*big.Int
    26  
    27  func (f ForkMap) String() (s string) {
    28  	for i := 0; i <= KnownHF; i++ {
    29  		if f[i] == nil {
    30  			continue
    31  		}
    32  		s = fmt.Sprintf("%s %v:%v", s, i, f[i].Int64())
    33  	}
    34  	return strings.TrimSpace(s)
    35  }
    36  
    37  // HeaderVersion is not stored in db, or rlp encoded, or sent over the network.
    38  type HeaderVersion byte
    39  
    40  func (c *ChainConfig) GetBlockVersion(height *big.Int) HeaderVersion {
    41  	if height == nil {
    42  		panic("GetBlockVersion: got nil height")
    43  	}
    44  	if c == EthnetChainConfig {
    45  		return 1
    46  	}
    47  	if c.IsHF(9, height) {
    48  		return 4 // argon2id-C
    49  	}
    50  	if c.IsHF(8, height) {
    51  		return 3 // argon2id-B
    52  	}
    53  	if c.IsHF(5, height) {
    54  		return 2 // argon2id
    55  	}
    56  	return 1 // ethash
    57  }
    58  
    59  // IsHF returns whether num is either equal to the hf block or greater.
    60  func (c *ChainConfig) IsHF(hf int, num *big.Int) bool {
    61  	if c.HF[hf] == nil {
    62  		return false
    63  	}
    64  	return isForked(c.HF[hf], num)
    65  }
    66  
    67  func (f ForkMap) Sorted() (hfs []int) {
    68  	for i := 0; i < KnownHF; i++ {
    69  		if f[i] != nil {
    70  			hfs = append(hfs, i)
    71  		}
    72  	}
    73  	return hfs
    74  }
    75  
    76  // UseHF returns the highest hf that is activated
    77  func (c *ChainConfig) UseHF(height *big.Int) int {
    78  	hfs := c.HF.Sorted()
    79  	active := 0
    80  	for _, hf := range hfs {
    81  		if c.IsHF(hf, height) {
    82  			active = hf
    83  		}
    84  	}
    85  	return active
    86  }
    87  
    88  // GetHF returns the height of input hf, can be nil.
    89  func (c *ChainConfig) GetHF(hf int) *big.Int {
    90  	if c.HF[hf] == nil {
    91  		return nil
    92  	}
    93  	return new(big.Int).Set(c.HF[hf])
    94  }
    95  
    96  // NextHF returns the next scheduled hard fork block number
    97  func (c *ChainConfig) NextHF(cur *big.Int) *big.Int {
    98  	for i := KnownHF; i > 0; i-- {
    99  		if c.HF[i] == nil {
   100  			continue
   101  		}
   102  		return new(big.Int).Set(c.HF[i])
   103  	}
   104  	return nil
   105  
   106  }