fork

package
v1.9.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2021 License: Unlicense Imports: 7 Imported by: 0

Documentation

Overview

Package fork handles tracking the hard fork status and is used to determine which consensus rules apply on a block

Index

Constants

View Source
const (
	Scrypt  = "scrypt"
	SHA256d = "sha256d"
)

Variables

View Source
var (
	AlgoSlices []AlgoSpecs
	// AlgoVers is the lookup for pre hardfork
	//
	AlgoVers = map[int32]string{
		2:   SHA256d,
		514: Scrypt,
	}
	// Algos are the specifications identifying the algorithm used in the
	// block proof
	Algos = map[string]AlgoParams{
		AlgoVers[2]: {
			Version: 2,
			MinBits: MainPowLimitBits,
		},
		AlgoVers[514]: {
			Version: 514,
			MinBits: MainPowLimitBits,
			AlgoID:  1,
		},
	}
	// FirstPowLimit is
	FirstPowLimit = func() big.Int {
		mplb, _ := hex.DecodeString(
			"0fffff0000000000000000000000000000000000000000000000000000000000",
		)
		return *big.NewInt(0).SetBytes(mplb)
	}()
	// FirstPowLimitBits is
	FirstPowLimitBits = BigToCompact(&FirstPowLimit)

	// IsTestnet is set at startup here to be accessible to all other libraries
	IsTestnet bool
	// List is the list of existing hard forks and when they activate
	List = []HardForks{
		{
			Number:             0,
			Name:               "Halcyon days",
			ActivationHeight:   0,
			Algos:              Algos,
			AlgoVers:           AlgoVers,
			TargetTimePerBlock: 300,
			AveragingInterval:  10,
			TestnetStart:       0,
		},
		{
			Number:             1,
			Name:               "Plan 9 from Crypto Space",
			ActivationHeight:   2500000,
			Algos:              P9Algos,
			AlgoVers:           P9AlgoVers,
			TargetTimePerBlock: 36,
			AveragingInterval:  3600,
			TestnetStart:       0,
		},
	}
	// P9AlgoVers is the lookup for after 1st hardfork
	P9AlgoVers = make(map[int32]string)

	P9PrimeSequence = []int{2, 3, 5, 7, 11, 13, 17, 19, 23}
	IntervalDivisor = 1
	IntervalBase    = 9
	// P9Algos is the algorithm specifications after the hard fork
	P9Algos        = make(map[string]AlgoParams)
	P9AlgosNumeric = map[int32]AlgoParams{
		5:  {5, p9PowLimitBits, 0, IntervalBase * P9PrimeSequence[0] / IntervalDivisor},
		6:  {6, p9PowLimitBits, 1, IntervalBase * P9PrimeSequence[1] / IntervalDivisor},
		7:  {7, p9PowLimitBits, 2, IntervalBase * P9PrimeSequence[2] / IntervalDivisor},
		8:  {8, p9PowLimitBits, 3, IntervalBase * P9PrimeSequence[3] / IntervalDivisor},
		9:  {9, p9PowLimitBits, 4, IntervalBase * P9PrimeSequence[4] / IntervalDivisor},
		10: {10, p9PowLimitBits, 5, IntervalBase * P9PrimeSequence[5] / IntervalDivisor},
		11: {11, p9PowLimitBits, 7, IntervalBase * P9PrimeSequence[7] / IntervalDivisor},
		12: {12, p9PowLimitBits, 6, IntervalBase * P9PrimeSequence[6] / IntervalDivisor},
		13: {13, p9PowLimitBits, 8, IntervalBase * P9PrimeSequence[8] / IntervalDivisor},
	}

	P9Average float64

	// SecondPowLimit is
	SecondPowLimit = func() big.Int {
		mplb, _ := hex.DecodeString(

			"0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
		)
		return *big.NewInt(0).SetBytes(mplb)
	}()
	SecondPowLimitBits = BigToCompact(&SecondPowLimit)
	MainPowLimit       = func() big.Int {
		mplb, _ := hex.DecodeString(
			"00000fffff000000000000000000000000000000000000000000000000000000",
		)
		return *big.NewInt(0).SetBytes(mplb)
	}()
	MainPowLimitBits = BigToCompact(&MainPowLimit)
)
View Source
var F, E, W, I, D, T logg.LevelPrinter = logg.GetLogPrinterSet(subsystem)

Functions

func AlgoVerIterator

func AlgoVerIterator(height int32) (next func(), curr func() int32, more func() bool)

AlgoVerIterator returns a next and more function to use in a for loop to iterate over block versions at current height

func BigToCompact

func BigToCompact(n *big.Int) uint32

BigToCompact converts a whole number N to a compact representation using an unsigned 32-bit number. The compact representation only provides 23 bits of precision, so values larger than (2^23 - 1) only encode the most significant digits of the number. See CompactToBig for details.

func CompactToBig

func CompactToBig(compact uint32) *big.Int

CompactToBig converts a compact representation of a whole number N to an unsigned 32-bit number. The representation is similar to IEEE754 floating point numbers. Like IEEE754 floating point, there are three basic components: the sign, the exponent, and the mantissa. They are broken out as follows:

  • the most significant 8 bits represent the unsigned base 256 exponent
  • bit 23 (the 24th bit) represents the sign bit
  • the least significant 23 bits represent the mantissa ------------------------------------------------- | Exponent | Sign | Mantissa | ------------------------------------------------- | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] | -------------------------------------------------

The formula to calculate N is:

N = (-1^sign) * mantissa * 256^(exponent-3)

This compact form is only used in bitcoin to encode unsigned 256-bit numbers which represent difficulty targets, thus there really is not a need for a sign bit, but it is implemented here to stay consistent with bitcoind.

func GetAlgoID

func GetAlgoID(algoname string, height int32) uint32

GetAlgoID returns the 'algo_id' which in pre-hardfork is not the same as the block version number, but is afterwards

func GetAlgoName

func GetAlgoName(algoVer int32, height int32) (name string)

GetAlgoName returns the string identifier of an algorithm depending on hard fork activation status

func GetAlgoVer

func GetAlgoVer(name string, height int32) (version int32)

GetAlgoVer returns the version number for a given algorithm (by string name) at a given height. If "random" is given, a random number is taken from the system secure random source (for randomised cpu mining)

func GetAlgoVerSlice

func GetAlgoVerSlice(height int32) (o []int32)

func GetAlgos

func GetAlgos(height int32) (o map[string]AlgoParams)

GetAlgos returns the map of names and algorithm parameters

func GetAveragingInterval

func GetAveragingInterval(height int32) (r int32)

GetAveragingInterval returns the active block interval target based on hard fork status

func GetCurrent

func GetCurrent(height int32) (curr int)

GetCurrent returns the hardfork number code

func GetMinBits

func GetMinBits(algoname string, height int32) (mb uint32)

GetMinBits returns the minimum diff bits based on height and testnet

func GetMinDiff

func GetMinDiff(algoname string, height int32) (md *big.Int)

GetMinDiff returns the minimum difficulty in uint256 form

func GetNumAlgos

func GetNumAlgos(height int32) (numAlgos int)

GetNumAlgos returns the number of algos at a given height

func GetRandomVersion

func GetRandomVersion(height int32) int32

GetRandomVersion returns a random version relevant to the current hard fork state and height

func GetTargetTimePerBlock

func GetTargetTimePerBlock(height int32) (r int64)

GetTargetTimePerBlock returns the active block interval target based on hard fork status

Types

type AlgoParams

type AlgoParams struct {
	Version         int32
	MinBits         uint32
	AlgoID          uint32
	VersionInterval int
}

AlgoParams are the identifying block version number and their minimum target bits

type AlgoSpec

type AlgoSpec struct {
	Version int32
	Name    string
}

type AlgoSpecs

type AlgoSpecs []AlgoSpec

func (AlgoSpecs) Len

func (a AlgoSpecs) Len() int

func (AlgoSpecs) Less

func (a AlgoSpecs) Less(i, j int) bool

func (AlgoSpecs) Swap

func (a AlgoSpecs) Swap(i, j int)

type HardForks

type HardForks struct {
	Number             int
	ActivationHeight   int32
	Name               string
	Algos              map[string]AlgoParams
	AlgoVers           map[int32]string
	TargetTimePerBlock int32
	AveragingInterval  int32
	TestnetStart       int32
}

HardForks is the details related to a hard fork, number, name and activation height

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL