Skip to content

Commit

Permalink
[HAL-18] Performing multiplication before the division to keep more p…
Browse files Browse the repository at this point in the history
…recesion in the calcorder
  • Loading branch information
gameofpointers committed Nov 15, 2024
1 parent 159f1d6 commit 0c4571d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
23 changes: 19 additions & 4 deletions consensus/blake3pow/poem.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types
// the given header determines the prime block
totalDeltaEntropyPrime := new(big.Int).Add(header.ParentDeltaEntropy(common.REGION_CTX), header.ParentDeltaEntropy(common.ZONE_CTX))
totalDeltaEntropyPrime = new(big.Int).Add(totalDeltaEntropyPrime, intrinsicEntropy)
primeDeltaEntropyTarget := new(big.Int).Div(params.PrimeEntropyTarget(expansionNum), big2)
primeDeltaEntropyTarget = new(big.Int).Mul(zoneThresholdEntropy, primeDeltaEntropyTarget)

var primeDeltaEntropyTarget *big.Int
if header.NumberU64(common.ZONE_CTX) < params.GoldenAgeForkNumberV2 {
primeDeltaEntropyTarget = new(big.Int).Div(params.PrimeEntropyTarget(expansionNum), big2)
primeDeltaEntropyTarget = new(big.Int).Mul(zoneThresholdEntropy, primeDeltaEntropyTarget)
} else {
primeDeltaEntropyTarget = new(big.Int).Mul(params.PrimeEntropyTarget(expansionNum), zoneThresholdEntropy)
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 @@ -56,8 +63,16 @@ func (blake3pow *Blake3pow) CalcOrder(chain consensus.BlockReader, header *types
// REGION
// Compute the total accumulated entropy since the last region block
totalDeltaEntropyRegion := new(big.Int).Add(header.ParentDeltaEntropy(common.ZONE_CTX), intrinsicEntropy)
regionDeltaEntropyTarget := new(big.Int).Div(params.RegionEntropyTarget(expansionNum), big2)
regionDeltaEntropyTarget = new(big.Int).Mul(zoneThresholdEntropy, regionDeltaEntropyTarget)

var regionDeltaEntropyTarget *big.Int
if header.NumberU64(common.ZONE_CTX) < params.GoldenAgeForkNumberV2 {
regionDeltaEntropyTarget = new(big.Int).Div(params.RegionEntropyTarget(expansionNum), big2)
regionDeltaEntropyTarget = new(big.Int).Mul(zoneThresholdEntropy, regionDeltaEntropyTarget)
} else {
regionDeltaEntropyTarget = new(big.Int).Mul(zoneThresholdEntropy, params.RegionEntropyTarget(expansionNum))
regionDeltaEntropyTarget = new(big.Int).Div(regionDeltaEntropyTarget, big2)
}

regionBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.RegionEntropyTarget(expansionNum)))
if intrinsicEntropy.Cmp(regionBlockEntropyThreshold) > 0 && totalDeltaEntropyRegion.Cmp(regionDeltaEntropyTarget) > 0 {
chain.AddToCalcOrderCache(header.Hash(), common.REGION_CTX, intrinsicEntropy)
Expand Down
23 changes: 19 additions & 4 deletions consensus/progpow/poem.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor
// the given header determines the prime block
totalDeltaEntropyPrime := new(big.Int).Add(header.ParentDeltaEntropy(common.REGION_CTX), header.ParentDeltaEntropy(common.ZONE_CTX))
totalDeltaEntropyPrime = new(big.Int).Add(totalDeltaEntropyPrime, intrinsicEntropy)
primeDeltaEntropyTarget := new(big.Int).Div(params.PrimeEntropyTarget(expansionNum), big2)
primeDeltaEntropyTarget = new(big.Int).Mul(zoneThresholdEntropy, primeDeltaEntropyTarget)

var primeDeltaEntropyTarget *big.Int
if header.NumberU64(common.ZONE_CTX) < params.GoldenAgeForkNumberV2 {
primeDeltaEntropyTarget = new(big.Int).Div(params.PrimeEntropyTarget(expansionNum), big2)
primeDeltaEntropyTarget = new(big.Int).Mul(zoneThresholdEntropy, primeDeltaEntropyTarget)
} else {
primeDeltaEntropyTarget = new(big.Int).Mul(params.PrimeEntropyTarget(expansionNum), zoneThresholdEntropy)
primeDeltaEntropyTarget = new(big.Int).Div(primeDeltaEntropyTarget, big2)
}

primeBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.PrimeEntropyTarget(expansionNum)))
if intrinsicEntropy.Cmp(primeBlockEntropyThreshold) > 0 && totalDeltaEntropyPrime.Cmp(primeDeltaEntropyTarget) > 0 {
Expand All @@ -55,8 +62,16 @@ func (progpow *Progpow) CalcOrder(chain consensus.BlockReader, header *types.Wor
// REGION
// Compute the total accumulated entropy since the last region block
totalDeltaSRegion := new(big.Int).Add(header.ParentDeltaEntropy(common.ZONE_CTX), intrinsicEntropy)
regionDeltaSTarget := new(big.Int).Div(params.RegionEntropyTarget(expansionNum), big2)
regionDeltaSTarget = new(big.Int).Mul(zoneThresholdEntropy, regionDeltaSTarget)

var regionDeltaSTarget *big.Int
if header.NumberU64(common.ZONE_CTX) < params.GoldenAgeForkNumberV2 {
regionDeltaSTarget = new(big.Int).Div(params.RegionEntropyTarget(expansionNum), big2)
regionDeltaSTarget = new(big.Int).Mul(zoneThresholdEntropy, regionDeltaSTarget)
} else {
regionDeltaSTarget = new(big.Int).Mul(zoneThresholdEntropy, params.RegionEntropyTarget(expansionNum))
regionDeltaSTarget = new(big.Int).Div(regionDeltaSTarget, big2)
}

regionBlockEntropyThreshold := new(big.Int).Add(zoneThresholdEntropy, common.BitsToBigBits(params.RegionEntropyTarget(expansionNum)))
if intrinsicEntropy.Cmp(regionBlockEntropyThreshold) > 0 && totalDeltaSRegion.Cmp(regionDeltaSTarget) > 0 {
chain.AddToCalcOrderCache(header.Hash(), common.REGION_CTX, intrinsicEntropy)
Expand Down

0 comments on commit 0c4571d

Please sign in to comment.