github.com/aquanetwork/aquachain@v1.7.8/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.IsHF(8, height) {
    45  		return 3 // argon2id (1,256mb,1)
    46  	}
    47  	if c.IsHF(5, height) {
    48  		return 2 // argon2id (1,1kb,1)
    49  	}
    50  	return 1 // ethash
    51  }
    52  
    53  // IsHF returns whether num is either equal to the hf block or greater.
    54  func (c *ChainConfig) IsHF(hf int, num *big.Int) bool {
    55  	if c.HF[hf] == nil {
    56  		return false
    57  	}
    58  	return isForked(c.HF[hf], num)
    59  }
    60  
    61  func (f ForkMap) Sorted() (hfs []int) {
    62  	for i := 0; i < KnownHF; i++ {
    63  		if f[i] != nil {
    64  			hfs = append(hfs, i)
    65  		}
    66  	}
    67  	return hfs
    68  }
    69  
    70  // UseHF returns the highest hf that is activated
    71  func (c *ChainConfig) UseHF(height *big.Int) int {
    72  	hfs := c.HF.Sorted()
    73  	active := 0
    74  	for _, hf := range hfs {
    75  		if c.IsHF(hf, height) {
    76  			active = hf
    77  		}
    78  	}
    79  	return active
    80  }
    81  
    82  // GetHF returns the height of input hf, can be nil.
    83  func (c *ChainConfig) GetHF(hf int) *big.Int {
    84  	if c.HF[hf] == nil {
    85  		return nil
    86  	}
    87  	return new(big.Int).Set(c.HF[hf])
    88  }
    89  
    90  // NextHF returns the next scheduled hard fork block number
    91  func (c *ChainConfig) NextHF(cur *big.Int) *big.Int {
    92  	for i := KnownHF; i > 0; i-- {
    93  		if c.HF[i] == nil {
    94  			continue
    95  		}
    96  		return new(big.Int).Set(c.HF[i])
    97  	}
    98  	return nil
    99  
   100  }