github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/config/hardfork.go (about)

     1  package config
     2  
     3  //go:generate stringer -type=Hardfork -linecomment
     4  
     5  // Hardfork represents the application hard-fork identifier.
     6  type Hardfork byte
     7  
     8  // HFDefault is a default value of Hardfork enum. It's a special constant
     9  // aimed to denote the node code enabled by default starting from the
    10  // genesis block. HFDefault is not a hard-fork, but this constant can be used for
    11  // convenient hard-forks comparison and to refer to the default hard-fork-less
    12  // node behaviour.
    13  const HFDefault Hardfork = 0 // Default
    14  
    15  const (
    16  	// HFAspidochelone represents hard-fork introduced in #2469 (ported from
    17  	// https://github.com/neo-project/neo/pull/2712) and #2519 (ported from
    18  	// https://github.com/neo-project/neo/pull/2749).
    19  	HFAspidochelone Hardfork = 1 << iota // Aspidochelone
    20  	// HFBasilisk represents hard-fork introduced in #3056 (ported from
    21  	// https://github.com/neo-project/neo/pull/2881), #3080 (ported from
    22  	// https://github.com/neo-project/neo/pull/2883) and #3085 (ported from
    23  	// https://github.com/neo-project/neo/pull/2810).
    24  	HFBasilisk // Basilisk
    25  	// HFCockatrice represents hard-fork introduced in #3402 (ported from
    26  	// https://github.com/neo-project/neo/pull/2942), #3301 (ported from
    27  	// https://github.com/neo-project/neo/pull/2925) and #3362 (ported from
    28  	// https://github.com/neo-project/neo/pull/3154).
    29  	HFCockatrice // Cockatrice
    30  	// hfLast denotes the end of hardforks enum. Consider adding new hardforks
    31  	// before hfLast.
    32  	hfLast
    33  )
    34  
    35  // Hardforks represents the ordered slice of all possible hardforks.
    36  var Hardforks []Hardfork
    37  
    38  // hardforks holds a map of Hardfork string representation to its type.
    39  var hardforks map[string]Hardfork
    40  
    41  func init() {
    42  	for i := HFAspidochelone; i < hfLast; i = i << 1 {
    43  		Hardforks = append(Hardforks, i)
    44  	}
    45  	hardforks = make(map[string]Hardfork, len(Hardforks))
    46  	for _, hf := range Hardforks {
    47  		hardforks[hf.String()] = hf
    48  	}
    49  }
    50  
    51  // Cmp returns the result of hardforks comparison. It returns:
    52  //
    53  //	-1 if hf <  other
    54  //	 0 if hf == other
    55  //	+1 if hf >  other
    56  func (hf Hardfork) Cmp(other Hardfork) int {
    57  	switch {
    58  	case hf == other:
    59  		return 0
    60  	case hf < other:
    61  		return -1
    62  	default:
    63  		return 1
    64  	}
    65  }
    66  
    67  // Prev returns the previous hardfork for the given one. Calling Prev for the default hardfork is a no-op.
    68  func (hf Hardfork) Prev() Hardfork {
    69  	if hf == HFDefault {
    70  		panic("unexpected call to Prev for the default hardfork")
    71  	}
    72  	return hf >> 1
    73  }
    74  
    75  // IsHardforkValid denotes whether the provided string represents a valid
    76  // Hardfork name.
    77  func IsHardforkValid(s string) bool {
    78  	_, ok := hardforks[s]
    79  	return ok
    80  }
    81  
    82  // LatestHardfork returns latest known hardfork.
    83  func LatestHardfork() Hardfork {
    84  	return hfLast >> 1
    85  }