From c8b5a42724013a9d8d4f0f5cf5188a727934d12a Mon Sep 17 00:00:00 2001 From: Hussam Date: Wed, 18 Dec 2024 10:52:41 -0600 Subject: [PATCH] Consolidate all big values to use common, add sanity checks --- common/big.go | 27 +++++++++++++++------------ consensus/blake3pow/consensus.go | 22 +--------------------- consensus/blake3pow/poem.go | 8 ++++---- consensus/consensus.go | 21 +++------------------ consensus/progpow/consensus.go | 22 +--------------------- consensus/progpow/poem.go | 10 +++++----- core/types/block.go | 1 - 7 files changed, 29 insertions(+), 82 deletions(-) diff --git a/common/big.go b/common/big.go index 571a417f9..7d6942599 100644 --- a/common/big.go +++ b/common/big.go @@ -35,11 +35,12 @@ var ( Big2 = big.NewInt(2) Big3 = big.NewInt(3) Big8 = big.NewInt(8) + Big10 = big.NewInt(10) Big32 = big.NewInt(32) 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 { @@ -87,27 +88,29 @@ 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) 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 || - 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 || + Big256 == nil || big256.Cmp(Big256) != 0 || + Big257 == nil || big257.Cmp(Big257) != 0 || + Big2e64 == nil || big2e64.Cmp(Big2e64) != 0 || + Big2e256 == nil || big2e256.Cmp(Big2e256) != 0 { // Send a message to quitCh to abort. log.Global.Error("A common value has mutated, exiting now") quitCh <- struct{}{} diff --git a/consensus/blake3pow/consensus.go b/consensus/blake3pow/consensus.go index b8c6aa492..cc10bc9f6 100644 --- a/consensus/blake3pow/consensus.go +++ b/consensus/blake3pow/consensus.go @@ -28,26 +28,6 @@ import ( // Blake3pow proof-of-work protocol constants. 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 ) // Author implements consensus.Engine, returning the header's coinbase as the @@ -592,7 +572,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..65e8e4eaf 100644 --- a/consensus/progpow/consensus.go +++ b/consensus/progpow/consensus.go @@ -29,26 +29,6 @@ import ( // Progpow proof-of-work protocol constants. 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 ) // Author implements consensus.Engine, returning the header's coinbase as the @@ -636,7 +616,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 )