Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lock to reduce the memory pressure while synching #1104

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,43 +1240,57 @@ func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) {
}
}

func setDurationLimit(ctx *cli.Context, cfg *ethconfig.Config) {
func setConsensusEngineConfig(ctx *cli.Context, cfg *ethconfig.Config) {
if cfg.ConsensusEngine == "blake3" {
// Override any default configs for hard coded networks.
switch {
case ctx.GlobalBool(ColosseumFlag.Name):
cfg.Blake3Pow.DurationLimit = params.DurationLimit
cfg.Blake3Pow.GasCeil = params.ColosseumGasCeil
case ctx.GlobalBool(GardenFlag.Name):
cfg.Blake3Pow.DurationLimit = params.GardenDurationLimit
cfg.Blake3Pow.GasCeil = params.GardenGasCeil
case ctx.GlobalBool(OrchardFlag.Name):
cfg.Blake3Pow.DurationLimit = params.OrchardDurationLimit
cfg.Blake3Pow.GasCeil = params.OrchardGasCeil
case ctx.GlobalBool(LighthouseFlag.Name):
cfg.Blake3Pow.DurationLimit = params.LighthouseDurationLimit
cfg.Blake3Pow.GasCeil = params.LighthouseGasCeil
case ctx.GlobalBool(LocalFlag.Name):
cfg.Blake3Pow.DurationLimit = params.LocalDurationLimit
cfg.Blake3Pow.GasCeil = params.LocalGasCeil
case ctx.GlobalBool(DeveloperFlag.Name):
cfg.Blake3Pow.DurationLimit = params.DurationLimit
cfg.Blake3Pow.GasCeil = params.LocalGasCeil
default:
cfg.Blake3Pow.DurationLimit = params.DurationLimit
cfg.Blake3Pow.GasCeil = params.GasCeil

}
} else {
// Override any default configs for hard coded networks.
switch {
case ctx.GlobalBool(ColosseumFlag.Name):
cfg.Progpow.DurationLimit = params.DurationLimit
cfg.Progpow.GasCeil = params.ColosseumGasCeil
case ctx.GlobalBool(GardenFlag.Name):
cfg.Progpow.DurationLimit = params.GardenDurationLimit
cfg.Progpow.GasCeil = params.GardenGasCeil
case ctx.GlobalBool(OrchardFlag.Name):
cfg.Progpow.DurationLimit = params.OrchardDurationLimit
cfg.Progpow.GasCeil = params.OrchardGasCeil
case ctx.GlobalBool(LighthouseFlag.Name):
cfg.Progpow.DurationLimit = params.LighthouseDurationLimit
cfg.Progpow.GasCeil = params.LighthouseGasCeil
case ctx.GlobalBool(LocalFlag.Name):
cfg.Progpow.DurationLimit = params.LocalDurationLimit
cfg.Progpow.GasCeil = params.LocalGasCeil
case ctx.GlobalBool(DeveloperFlag.Name):
cfg.Progpow.DurationLimit = params.DurationLimit
cfg.Progpow.GasCeil = params.LocalGasCeil
default:
cfg.Progpow.DurationLimit = params.DurationLimit
cfg.Progpow.GasCeil = params.GasCeil

}
}
Expand Down Expand Up @@ -1425,7 +1439,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
} else {
cfg.ConsensusEngine = "progpow"
}
setDurationLimit(ctx, cfg)
setConsensusEngineConfig(ctx, cfg)

setWhitelist(ctx, cfg)

Expand Down
2 changes: 2 additions & 0 deletions consensus/blake3pow/blake3pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Config struct {

DurationLimit *big.Int

GasCeil uint64

// When set, notifications sent by the remote sealer will
// be block header JSON objects instead of work package arrays.
NotifyFull bool
Expand Down
46 changes: 21 additions & 25 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/params"
"github.com/dominant-strategies/go-quai/trie"
"modernc.org/mathutil"
)

// Blake3pow proof-of-work protocol constants.
Expand Down Expand Up @@ -300,8 +301,10 @@ func (blake3pow *Blake3pow) verifyHeader(chain consensus.ChainHeaderReader, head
}
// Verify the block's gas usage and verify the base fee.
// Verify that the gas limit remains within allowed bounds
if err := misc.VerifyGaslimit(parent.GasLimit(), header.GasLimit()); err != nil {
return err
expectedGasLimit := core.CalcGasLimit(parent, blake3pow.config.GasCeil)
if expectedGasLimit != header.GasLimit() {
return fmt.Errorf("invalid gasLimit: have %d, want %d",
header.GasLimit(), expectedGasLimit)
}
// Verify the header is not malformed
if header.BaseFee() == nil {
Expand Down Expand Up @@ -331,48 +334,41 @@ func (blake3pow *Blake3pow) CalcDifficulty(chain consensus.ChainHeaderReader, pa
log.Error("Cannot CalcDifficulty for", "context", nodeCtx)
return nil
}
// algorithm:
// diff = (parent_diff +
// (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
// ) + 2^(periodCount - 2)

time := parent.Time()
///// Algorithm:
///// e = (DurationLimit - (parent.Time() - parentOfParent.Time())) * parent.Difficulty()
///// k = Floor(BinaryLog(parent.Difficulty()))/(DurationLimit*DifficultyAdjustmentFactor*AdjustmentPeriod)
///// Difficulty = Max(parent.Difficulty() + e * k, MinimumDifficulty)

if parent.Hash() == chain.Config().GenesisHash {
return parent.Difficulty()
}

parentOfParent := chain.GetHeaderByHash(parent.ParentHash())
if parentOfParent.Hash() == chain.Config().GenesisHash {
return parent.Difficulty()
}

time := parent.Time()
bigTime := new(big.Int).SetUint64(time)
bigParentTime := new(big.Int).SetUint64(parentOfParent.Time())

// holds intermediate values to make the algo easier to read & audit
x := new(big.Int)
y := new(big.Int)

// (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // duration_limit
x.Sub(bigTime, bigParentTime)
x.Sub(blake3pow.config.DurationLimit, x)
x.Mul(x, parent.Difficulty())
k, _ := mathutil.BinaryLog(new(big.Int).Set(parent.Difficulty()), 64)
gameofpointers marked this conversation as resolved.
Show resolved Hide resolved
x.Mul(x, big.NewInt(int64(k)))
x.Div(x, blake3pow.config.DurationLimit)
if parent.UncleHash() == types.EmptyUncleHash {
x.Sub(big1, x)
} else {
x.Sub(big2, x)
}
// max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
if x.Cmp(bigMinus99) < 0 {
x.Set(bigMinus99)
}
// parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
y.Div(parent.Difficulty(), params.DifficultyBoundDivisor)
x.Mul(y, x)
x.Add(parent.Difficulty(), x)
x.Div(x, big.NewInt(params.DifficultyAdjustmentFactor))
x.Div(x, params.DifficultyAdjustmentPeriod)
x.Add(x, parent.Difficulty())

// minimum difficulty can ever be (before exponential factor)
if x.Cmp(params.MinimumDifficulty) < 0 {
x.Set(params.MinimumDifficulty)
}

return x
}

Expand Down Expand Up @@ -465,7 +461,7 @@ func (blake3pow *Blake3pow) ComputePowLight(header *types.Header) (common.Hash,
// included uncles. The coinbase of each uncle block is also rewarded.
func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
// Select the correct block reward based on chain progression
blockReward := misc.CalculateReward()
blockReward := misc.CalculateReward(header)

coinbase, err := header.Coinbase().InternalAddress()
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions consensus/blake3pow/poem.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ func (blake3pow *Blake3pow) CalcOrder(header *types.Header) (*big.Int, int, erro
// PRIME
// PrimeEntropyThreshold number of zone blocks times the intrinsic logs of
// the given header determines the prime block
totalDeltaSPrime := new(big.Int).Add(header.ParentDeltaS(common.REGION_CTX), header.ParentDeltaS(common.ZONE_CTX))
totalDeltaSPrime = new(big.Int).Add(totalDeltaSPrime, intrinsicS)
primeDeltaSTarget := new(big.Int).Div(params.PrimeEntropyTarget, big2)
gameofpointers marked this conversation as resolved.
Show resolved Hide resolved
primeDeltaSTarget = new(big.Int).Mul(zoneThresholdS, primeDeltaSTarget)

primeBlockEntropyThreshold := new(big.Int).Add(zoneThresholdS, common.BitsToBigBits(params.PrimeEntropyTarget))
if intrinsicS.Cmp(primeBlockEntropyThreshold) > 0 {
if intrinsicS.Cmp(primeBlockEntropyThreshold) > 0 && totalDeltaSPrime.Cmp(primeDeltaSTarget) > 0 {
return intrinsicS, common.PRIME_CTX, nil
}

// REGION
// Compute the total accumulated entropy since the last region block
totalDeltaSRegion := new(big.Int).Add(header.ParentDeltaS(common.ZONE_CTX), intrinsicS)
regionDeltaSTarget := new(big.Int).Div(params.RegionEntropyTarget, big2)
gameofpointers marked this conversation as resolved.
Show resolved Hide resolved
regionDeltaSTarget = new(big.Int).Mul(zoneThresholdS, regionDeltaSTarget)
regionBlockEntropyThreshold := new(big.Int).Add(zoneThresholdS, common.BitsToBigBits(params.RegionEntropyTarget))
if intrinsicS.Cmp(regionBlockEntropyThreshold) > 0 {
if intrinsicS.Cmp(regionBlockEntropyThreshold) > 0 && totalDeltaSRegion.Cmp(regionDeltaSTarget) > 0 {
return intrinsicS, common.REGION_CTX, nil
}

Expand Down
11 changes: 2 additions & 9 deletions consensus/misc/basefee.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package misc
import (
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/math"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/params"
Expand Down Expand Up @@ -51,10 +50,7 @@ func calcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed() - parentGasTarget)
x := new(big.Int).Mul(parent.BaseFee(), gasUsedDelta)
y := x.Div(x, parentGasTargetBig)
baseFeeDelta := math.BigMax(
x.Div(y, baseFeeChangeDenominator),
common.Big1,
)
baseFeeDelta := math.BigMax(x.Div(y, baseFeeChangeDenominator), big.NewInt(params.GWei))

return x.Add(parent.BaseFee(), baseFeeDelta)
} else {
Expand All @@ -64,9 +60,6 @@ func calcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
y := x.Div(x, parentGasTargetBig)
baseFeeDelta := x.Div(y, baseFeeChangeDenominator)

return math.BigMax(
x.Sub(parent.BaseFee(), baseFeeDelta),
common.Big0,
)
return math.BigMax(x.Sub(parent.BaseFee(), baseFeeDelta), big.NewInt(params.GWei))
}
}
42 changes: 0 additions & 42 deletions consensus/misc/gaslimit.go

This file was deleted.

41 changes: 5 additions & 36 deletions consensus/misc/rewards.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
package misc

import (
"log"
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/types"
)

// CalculateReward calculates the coinbase rewards depending on the type of the block
// regions = # of regions
// zones = # of zones
// For each prime = Reward/3
// For each region = Reward/(3*regions*time-factor)
// For each zone = Reward/(3*regions*zones*time-factor^2)
func CalculateReward() *big.Int {
reward := big.NewInt(5e18)
reward.Mul(reward, big.NewInt(1000))
timeFactor := big.NewInt(10)
regions := big.NewInt(3)
zones := big.NewInt(3)
context := common.NodeLocation.Context()
if context == common.PRIME_CTX {
primeReward := big.NewInt(3)
primeReward.Div(reward, primeReward)
return primeReward
} else if context == common.REGION_CTX {
regionReward := big.NewInt(3)
regionReward.Mul(regionReward, regions)
regionReward.Mul(regionReward, timeFactor)
regionReward.Div(reward, regionReward)
return regionReward
} else if context == common.ZONE_CTX {
zoneReward := big.NewInt(3)
zoneReward.Mul(zoneReward, regions)
zoneReward.Mul(zoneReward, zones)
zoneReward.Mul(zoneReward, timeFactor)
zoneReward.Mul(zoneReward, timeFactor)
zoneReward.Div(reward, zoneReward)
return zoneReward
} else {
log.Fatal("unknown node context")
return nil
}
func CalculateReward(header *types.Header) *big.Int {
//// This Reward Schedule is only for Iron Age Testnet and has nothing to do
//// with the Mainnet Schedule
return new(big.Int).Mul(header.Difficulty(), big.NewInt(10e8))
gameofpointers marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading