Skip to content

Commit

Permalink
Consolidate all big values to use common, add sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Dec 18, 2024
1 parent f006891 commit c8b5a42
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 82 deletions.
27 changes: 15 additions & 12 deletions common/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}{}
Expand Down
22 changes: 1 addition & 21 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions consensus/blake3pow/poem.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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()

// 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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
21 changes: 3 additions & 18 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 1 addition & 21 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/progpow/poem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit c8b5a42

Please sign in to comment.