diff --git a/common/big.go b/common/big.go index db6a04c9d..4a1925b9d 100644 --- a/common/big.go +++ b/common/big.go @@ -35,14 +35,15 @@ var ( Big2 = big.NewInt(2) Big3 = big.NewInt(3) Big8 = big.NewInt(8) + Big10 = big.NewInt(10) Big32 = big.NewInt(32) Big99 = big.NewInt(99) Big100 = big.NewInt(100) Big101 = big.NewInt(101) Big256 = big.NewInt(256) Big257 = big.NewInt(257) - Big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) Big2e64 = new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0)) + Big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) ) func BigBitsToBits(original *big.Int) *big.Int { @@ -90,33 +91,35 @@ func SanityCheck(quitCh chan struct{}) { big2 := big.NewInt(2) big3 := big.NewInt(3) big8 := big.NewInt(8) + big10 := big.NewInt(10) big32 := big.NewInt(32) big99 := big.NewInt(99) big100 := big.NewInt(100) big101 := big.NewInt(101) big256 := big.NewInt(256) big257 := big.NewInt(257) - big2e256 := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) big2e64 := new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0)) + big2e256 := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) go func(quitCh chan struct{}) { for { time.Sleep(1 * time.Minute) // Verify that none of the values have mutated. - if big0.Cmp(Big0) != 0 || - big1.Cmp(Big1) != 0 || - big2.Cmp(Big2) != 0 || - big3.Cmp(Big3) != 0 || - big8.Cmp(Big8) != 0 || - big32.Cmp(Big32) != 0 || - big99.Cmp(Big99) != 0 || - big100.Cmp(Big100) != 0 || - big101.Cmp(Big101) != 0 || - big256.Cmp(Big256) != 0 || - big257.Cmp(Big257) != 0 || - big2e256.Cmp(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))) != 0 || - big2e64.Cmp(new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))) != 0 { + if big0 != nil || big0.Cmp(Big0) != 0 || + big1 == nil || big1.Cmp(Big1) != 0 || + big2 == nil || big2.Cmp(Big2) != 0 || + big3 == nil || big3.Cmp(Big3) != 0 || + big8 == nil || big8.Cmp(Big8) != 0 || + big10 == nil || big10.Cmp(Big10) != 0 || + big32 == nil || big32.Cmp(Big32) != 0 || + big99 == nil || big99.Cmp(Big99) != 0 || + big100 == nil || big100.Cmp(Big100) != 0 || + big101 == nil || big101.Cmp(Big101) != 0 || + big256 == nil || big256.Cmp(Big256) != 0 || + big257 == nil || big257.Cmp(Big257) != 0 || + big2e64 == nil || big2e64.Cmp(new(big.Int).Exp(big.NewInt(2), big.NewInt(64), big.NewInt(0))) != 0 || + big2e256 == nil || big2e256.Cmp(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))) != 0 { // Send a message to quitCh to abort. log.Global.Fatal("A common value has mutated, exiting now") quitCh <- struct{}{} diff --git a/consensus/blake3pow/consensus.go b/consensus/blake3pow/consensus.go index b8c6aa492..f75f00a9e 100644 --- a/consensus/blake3pow/consensus.go +++ b/consensus/blake3pow/consensus.go @@ -29,25 +29,9 @@ import ( var ( allowedFutureBlockTimeSeconds = int64(15) // Max seconds from current time allowed for blocks, before they're considered future blocks - ContextTimeFactor = big10 ZoneBlockReward = big.NewInt(5e+18) - RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, big3) - PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, big3) -) - -// Some useful constants to avoid constant memory allocs for them. -var ( - expDiffPeriod = big.NewInt(100000) - big0 = big.NewInt(0) - big1 = big.NewInt(1) - big2 = big.NewInt(2) - big3 = big.NewInt(3) - big8 = big.NewInt(8) - big9 = big.NewInt(9) - big10 = big.NewInt(10) - big32 = big.NewInt(32) - bigMinus99 = big.NewInt(-99) - big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256 + RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, common.Big3) + PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, common.Big3) ) // Author implements consensus.Engine, returning the header's coinbase as the @@ -592,7 +576,7 @@ func (blake3pow *Blake3pow) verifySeal(header *types.WorkObjectHeader) error { return consensus.ErrInvalidDifficulty } - target := new(big.Int).Div(big2e256, header.Difficulty()) + target := new(big.Int).Div(common.Big2e256, header.Difficulty()) if new(big.Int).SetBytes(header.Hash().Bytes()).Cmp(target) > 0 { return consensus.ErrInvalidPoW } diff --git a/consensus/blake3pow/poem.go b/consensus/blake3pow/poem.go index 149ea94b2..626e3f04c 100644 --- a/consensus/blake3pow/poem.go +++ b/consensus/blake3pow/poem.go @@ -23,7 +23,7 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types } nodeCtx := blake3pow.config.NodeLocation.Context() if header.NumberU64(nodeCtx) == 0 { - return big0, common.PRIME_CTX, nil + return big.NewInt(0), common.PRIME_CTX, nil } expansionNum := header.ExpansionNumber() @@ -31,7 +31,7 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types // Verify the seal and get the powHash for the given header err := blake3pow.verifySeal(header.WorkObjectHeader()) if err != nil { - return big0, -1, err + return big.NewInt(0), -1, err } // Get entropy reduction of this header @@ -59,7 +59,7 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types totalDeltaEntropyRegion := new(big.Int).Add(header.ParentDeltaEntropy(common.ZONE_CTX), intrinsicEntropy) regionDeltaEntropyTarget := new(big.Int).Mul(zoneThresholdEntropy, params.RegionEntropyTarget(expansionNum)) - regionDeltaEntropyTarget = new(big.Int).Div(regionDeltaEntropyTarget, big2) + regionDeltaEntropyTarget = new(big.Int).Div(regionDeltaEntropyTarget, common.Big2) regionBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.RegionEntropyTarget(expansionNum))) if intrinsicEntropy.Cmp(regionBlockEntropyThreshold) > 0 && totalDeltaEntropyRegion.Cmp(regionDeltaEntropyTarget) > 0 { @@ -75,7 +75,7 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types // IntrinsicLogEntropy returns the logarithm of the intrinsic entropy reduction of a PoW hash func (blake3pow *Blake3pow) IntrinsicLogEntropy(powHash common.Hash) *big.Int { x := new(big.Int).SetBytes(powHash.Bytes()) - d := new(big.Int).Div(big2e256, x) + d := new(big.Int).Div(common.Big2e256, x) c, m := mathutil.BinaryLog(d, consensus.MantBits) bigBits := new(big.Int).Mul(big.NewInt(int64(c)), new(big.Int).Exp(big.NewInt(2), big.NewInt(consensus.MantBits), nil)) bigBits = new(big.Int).Add(bigBits, m) diff --git a/consensus/consensus.go b/consensus/consensus.go index b546c1bc1..d376d9bb3 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -24,8 +24,8 @@ import ( "github.com/dominant-strategies/go-quai/common" "github.com/dominant-strategies/go-quai/core/state" "github.com/dominant-strategies/go-quai/core/types" - "github.com/dominant-strategies/go-quai/ethdb" "github.com/dominant-strategies/go-quai/crypto/multiset" + "github.com/dominant-strategies/go-quai/ethdb" "github.com/dominant-strategies/go-quai/params" ) @@ -35,21 +35,6 @@ const ( MantBits = 64 ) -// Some useful constants to avoid constant memory allocs for them. -var ( - ExpDiffPeriod = big.NewInt(100000) - Big0 = big.NewInt(0) - Big1 = big.NewInt(1) - Big2 = big.NewInt(2) - Big3 = big.NewInt(3) - Big8 = big.NewInt(8) - Big9 = big.NewInt(9) - Big10 = big.NewInt(10) - Big32 = big.NewInt(32) - BigMinus99 = big.NewInt(-99) - Big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256 -) - // Various error messages to mark blocks invalid. These should be private to // prevent engine specific errors from being referenced in the remainder of the // codebase, inherently breaking if the engine is swapped out. Please put common @@ -259,8 +244,8 @@ func CalcWorkShareThreshold(workShare *types.WorkObjectHeader, workShareThreshol return nil, ErrInvalidThresholdDiff } diff := workShare.Difficulty() - diffTarget := new(big.Int).Div(Big2e256, diff) - workShareTarget := new(big.Int).Exp(Big2, big.NewInt(int64(workShareThresholdDiff)), nil) + diffTarget := new(big.Int).Div(common.Big2e256, diff) + workShareTarget := new(big.Int).Exp(common.Big2, big.NewInt(int64(workShareThresholdDiff)), nil) return workShareTarget.Mul(diffTarget, workShareTarget), nil } diff --git a/consensus/progpow/consensus.go b/consensus/progpow/consensus.go index 756446993..4f1ba58fd 100644 --- a/consensus/progpow/consensus.go +++ b/consensus/progpow/consensus.go @@ -30,25 +30,9 @@ import ( var ( allowedFutureBlockTimeSeconds = int64(15) // Max seconds from current time allowed for blocks, before they're considered future blocks - ContextTimeFactor = big10 ZoneBlockReward = big.NewInt(5e+18) - RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, big3) - PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, big3) -) - -// Some useful constants to avoid constant memory allocs for them. -var ( - expDiffPeriod = big.NewInt(100000) - big0 = big.NewInt(0) - big1 = big.NewInt(1) - big2 = big.NewInt(2) - big3 = big.NewInt(3) - big8 = big.NewInt(8) - big9 = big.NewInt(9) - big10 = big.NewInt(10) - big32 = big.NewInt(32) - bigMinus99 = big.NewInt(-99) - big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) // 2^256 + RegionBlockReward = new(big.Int).Mul(ZoneBlockReward, common.Big3) + PrimeBlockReward = new(big.Int).Mul(RegionBlockReward, common.Big3) ) // Author implements consensus.Engine, returning the header's coinbase as the @@ -636,7 +620,7 @@ func (progpow *Progpow) verifySeal(header *types.WorkObjectHeader) (common.Hash, if err != nil { return common.Hash{}, err } - target := new(big.Int).Div(big2e256, header.Difficulty()) + target := new(big.Int).Div(common.Big2e256, header.Difficulty()) if new(big.Int).SetBytes(powHash.Bytes()).Cmp(target) > 0 { return powHash, consensus.ErrInvalidPoW } diff --git a/consensus/progpow/poem.go b/consensus/progpow/poem.go index 6d535a2da..ed7bbdcc4 100644 --- a/consensus/progpow/poem.go +++ b/consensus/progpow/poem.go @@ -23,14 +23,14 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor nodeCtx := progpow.config.NodeLocation.Context() // Except for the slice [0,0] have to check if the header hash is the genesis hash if header.NumberU64(nodeCtx) == 0 { - return big0, common.PRIME_CTX, nil + return big.NewInt(0), common.PRIME_CTX, nil } expansionNum := header.ExpansionNumber() // Verify the seal and get the powHash for the given header powHash, err := progpow.verifySeal(header.WorkObjectHeader()) if err != nil { - return big0, -1, err + return big.NewInt(0), -1, err } // Get entropy reduction of this header @@ -45,7 +45,7 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor totalDeltaEntropyPrime = new(big.Int).Add(totalDeltaEntropyPrime, intrinsicEntropy) primeDeltaEntropyTarget := new(big.Int).Mul(params.PrimeEntropyTarget(expansionNum), zoneThresholdEntropy) - primeDeltaEntropyTarget = new(big.Int).Div(primeDeltaEntropyTarget, big2) + primeDeltaEntropyTarget = new(big.Int).Div(primeDeltaEntropyTarget, common.Big2) primeBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.PrimeEntropyTarget(expansionNum))) if intrinsicEntropy.Cmp(primeBlockEntropyThreshold) > 0 && totalDeltaEntropyPrime.Cmp(primeDeltaEntropyTarget) > 0 { @@ -58,7 +58,7 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor totalDeltaSRegion := new(big.Int).Add(header.ParentDeltaEntropy(common.ZONE_CTX), intrinsicEntropy) regionDeltaSTarget := new(big.Int).Mul(zoneThresholdEntropy, params.RegionEntropyTarget(expansionNum)) - regionDeltaSTarget = new(big.Int).Div(regionDeltaSTarget, big2) + regionDeltaSTarget = new(big.Int).Div(regionDeltaSTarget, common.Big2) regionBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.RegionEntropyTarget(expansionNum))) if intrinsicEntropy.Cmp(regionBlockEntropyThreshold) > 0 && totalDeltaSRegion.Cmp(regionDeltaSTarget) > 0 { @@ -74,7 +74,7 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor // IntrinsicLogEntropy returns the logarithm of the intrinsic entropy reduction of a PoW hash func (progpow *Progpow) IntrinsicLogEntropy(powHash common.Hash) *big.Int { x := new(big.Int).SetBytes(powHash.Bytes()) - d := new(big.Int).Div(big2e256, x) + d := new(big.Int).Div(common.Big2e256, x) c, m := mathutil.BinaryLog(d, consensus.MantBits) bigBits := new(big.Int).Mul(big.NewInt(int64(c)), new(big.Int).Exp(big.NewInt(2), big.NewInt(consensus.MantBits), nil)) bigBits = new(big.Int).Add(bigBits, m) diff --git a/core/types/block.go b/core/types/block.go index 70457e654..2945f2c8b 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -44,7 +44,6 @@ var ( EmptyUncleHash = RlpHash([]*Header(nil)) EmptyBodyHash = common.HexToHash("51e1b9c1426a03bf73da3d98d9f384a49ded6a4d705dcdf25433915c3306826c") EmptyHash = common.Hash{} - big2e256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil) // 2^256 hasher = blake3.New(32, nil) hasherMu sync.RWMutex )