fork

package
v0.0.2-0...-1c7e8a7 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2021 License: Unlicense Imports: 8 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:       1,
		},
	}
	// 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    = 101
	// P9Algos is the algorithm specifications after the hard fork
	P9Algos        = make(map[string]AlgoParams)
	P9AlgosNumeric = map[int32]AlgoParams{
		5:  {5, FirstPowLimitBits, 0, IntervalBase * P9PrimeSequence[0] / IntervalDivisor},
		6:  {6, FirstPowLimitBits, 1, IntervalBase * P9PrimeSequence[1] / IntervalDivisor},
		7:  {7, FirstPowLimitBits, 2, IntervalBase * P9PrimeSequence[2] / IntervalDivisor},
		8:  {8, FirstPowLimitBits, 3, IntervalBase * P9PrimeSequence[3] / IntervalDivisor},
		9:  {9, FirstPowLimitBits, 4, IntervalBase * P9PrimeSequence[4] / IntervalDivisor},
		10: {10, FirstPowLimitBits, 5, IntervalBase * P9PrimeSequence[5] / IntervalDivisor},
		11: {11, FirstPowLimitBits, 7, IntervalBase * P9PrimeSequence[7] / IntervalDivisor},
		12: {12, FirstPowLimitBits, 6, IntervalBase * P9PrimeSequence[6] / IntervalDivisor},
		13: {13, FirstPowLimitBits, 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)
)

Functions

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 Check

func Check(err error) bool

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 Debug

func Debug(a ...interface{})

func Debugc

func Debugc(fn func() string)

func Debugf

func Debugf(format string, a ...interface{})

func Debugs

func Debugs(a interface{})

func Error

func Error(a ...interface{})

func Errorc

func Errorc(fn func() string)

func Errorf

func Errorf(format string, a ...interface{})

func Errors

func Errors(a interface{})

func Fatal

func Fatal(a ...interface{})

func Fatalc

func Fatalc(fn func() string)

func Fatalf

func Fatalf(format string, a ...interface{})

func Fatals

func Fatals(a interface{})

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 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 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

func Info

func Info(a ...interface{})

func Infoc

func Infoc(fn func() string)

func Infof

func Infof(format string, a ...interface{})

func Infos

func Infos(a interface{})

func Trace

func Trace(a ...interface{})

func Tracec

func Tracec(fn func() string)

func Tracef

func Tracef(format string, a ...interface{})

func Traces

func Traces(a interface{})

func Warn

func Warn(a ...interface{})

func Warnc

func Warnc(fn func() string)

func Warnf

func Warnf(format string, a ...interface{})

func Warns

func Warns(a interface{})

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             uint32
	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