Skip to content

Commit

Permalink
Controller for statelimit adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Aug 23, 2024
1 parent 2636960 commit 1f33c90
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func (blake3pow *Blake3pow) verifyHeader(chain consensus.ChainHeaderReader, head
return fmt.Errorf("invalid stateUsed: have %d, stateLimit %d", header.StateUsed(), header.StateLimit())
}
// Verify the stateLimit is correct based on the parent header.
expectedStateLimit := misc.CalcStateLimit()
expectedStateLimit := misc.CalcStateLimit(parent, params.StateCeil)
if header.StateLimit() != expectedStateLimit {
return fmt.Errorf("invalid stateLimit: have %v, want %v, parentStateLimit %v", expectedStateLimit, header.StateLimit(), parent.StateLimit())
}
Expand Down
42 changes: 40 additions & 2 deletions consensus/misc/statefee.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
package misc

func CalcStateLimit() uint64 {
return 0
import (
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/params"
)

func CalcStateLimit(parent *types.WorkObject, stateCeil uint64) uint64 {
// No Gas for TimeToStartTx days worth of zone blocks, this gives enough time to
// onboard new miners into the slice
if parent.NumberU64(common.ZONE_CTX) < params.TimeToStartTx {
return 0
}

// If parent gas is zero and we have passed the 5 day threshold, we can set the first block gas limit to min gas limit
if parent.StateLimit() == 0 {
return params.MinGasLimit
}

parentStateLimit := parent.StateLimit()

delta := parentStateLimit/params.StateLimitBoundDivisor - 1
limit := parentStateLimit

var desiredLimit uint64
percentStateUsed := parent.StateUsed() * 100 / parent.StateLimit()
if percentStateUsed > params.PercentStateUsedThreshold {
desiredLimit = stateCeil
if limit+delta > desiredLimit {
return desiredLimit
} else {
return limit + delta
}
} else {
desiredLimit = params.MinGasLimit
if limit-delta/2 < desiredLimit {
return desiredLimit
} else {
return limit - delta/2
}
}
}
2 changes: 1 addition & 1 deletion consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (progpow *Progpow) verifyHeader(chain consensus.ChainHeaderReader, header,
return fmt.Errorf("invalid stateUsed: have %d, stateLimit %d", header.StateUsed(), header.StateLimit())
}
// Verify the StateLimit is correct based on the parent header.
expectedStateLimit := misc.CalcStateLimit()
expectedStateLimit := misc.CalcStateLimit(parent, params.StateCeil)
if header.StateLimit() != expectedStateLimit {
return fmt.Errorf("invalid StateLimit: have %d, want %d, parentStateLimit %d", expectedStateLimit, header.StateLimit(), parent.StateLimit())
}
Expand Down
2 changes: 1 addition & 1 deletion core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ func (w *worker) prepareWork(genParams *generateParams, wo *types.WorkObject) (*
if nodeCtx == common.ZONE_CTX && w.hc.ProcessingState() {
newWo.Header().SetExtra(w.extra)
newWo.Header().SetBaseFee(misc.CalcBaseFee(w.chainConfig, parent))
newWo.Header().SetStateLimit(misc.CalcStateLimit())
newWo.Header().SetStateLimit(misc.CalcStateLimit(parent, w.config.GasCeil))
if w.isRunning() {
if w.coinbase.Equal(common.Zero) {
w.logger.Error("Refusing to mine without etherbase")
Expand Down
4 changes: 4 additions & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const (
MinGasLimit uint64 = 40000000 // Minimum the gas limit may ever be.
GenesisGasLimit uint64 = 5000000 // Gas limit of the Genesis block.

StateCeil uint64 = 40000000 // Maximum the StateCeil may ever be
StateLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations.
PercentStateUsedThreshold uint64 = 90 // Percent Gas used threshold at which the gas limit adjusts

MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.
CallValueTransferGas uint64 = 9000 // Paid for CALL when the value transfer is non-zero.
Expand Down

0 comments on commit 1f33c90

Please sign in to comment.