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 ec48786 commit c074d6b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 81 deletions.
33 changes: 18 additions & 15 deletions common/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}{}
Expand Down
22 changes: 3 additions & 19 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
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: 3 additions & 19 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
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 c074d6b

Please sign in to comment.