Skip to content

Commit

Permalink
Use a number check before TimeToStartEtx and use gas check after
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Oct 14, 2024
1 parent 3d4e3eb commit 2fdbf18
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
1 change: 0 additions & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ func (v *BlockValidator) ValidateState(block *types.WorkObject, statedb *state.S
// to keep the baseline gas close to the provided target, and increase it towards
// the target if the baseline gas is lower.
func CalcGasLimit(parent *types.WorkObject, gasCeil uint64) uint64 {
return params.MinGasLimit
// 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 {
Expand Down
22 changes: 16 additions & 6 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
if etxPLimit < params.ETXPLimitMin {
etxPLimit = params.ETXPLimitMin
}
minimumEtxCount := params.MinEtxCount
maximumEtxCount := params.MaxEtxCount
etxCount := 0
minimumEtxGas := header.GasLimit() / params.MinimumEtxGasDivisor // 20% of the block gas limit
maximumEtxGas := minimumEtxGas * params.MaximumEtxGasMultiplier // 40% of the block gas limit
totalEtxGas := uint64(0)
Expand Down Expand Up @@ -391,6 +394,7 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
var receipt *types.Receipt
var addReceipt bool
if tx.Type() == types.ExternalTxType {
etxCount++
startTimeEtx := time.Now()
// ETXs MUST be included in order, so popping the first from the queue must equal the first in the block
etx, err := statedb.PopETX()
Expand Down Expand Up @@ -466,12 +470,14 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
quaiCoinbaseEtxs[coinbaseAddrWithLockup] = tx.Value() // this creates a new *big.Int
}
}
// subtract the minimum tx gas from the gas pool
if err := gp.SubGas(params.TxGas); err != nil {
return nil, nil, nil, nil, 0, 0, 0, nil, err
if block.NumberU64(common.ZONE_CTX) > params.TimeToStartTx {
// subtract the minimum tx gas from the gas pool
if err := gp.SubGas(params.TxGas); err != nil {
return nil, nil, nil, nil, 0, 0, 0, nil, err
}
*usedGas += params.TxGas
totalEtxGas += params.TxGas
}
*usedGas += params.TxGas
totalEtxGas += params.TxGas
timeDelta := time.Since(startTimeEtx)
timeCoinbase += timeDelta
continue
Expand Down Expand Up @@ -620,7 +626,11 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
if etx != nil {
etxAvailable = true
}
if (etxAvailable && totalEtxGas < minimumEtxGas) || totalEtxGas > maximumEtxGas {

if block.NumberU64(common.ZONE_CTX) <= params.TimeToStartTx && (etxAvailable && etxCount < minimumEtxCount || etxCount > maximumEtxCount) {
return nil, nil, nil, nil, 0, 0, 0, nil, fmt.Errorf("total number of ETXs %d is not within the range %d to %d", etxCount, minimumEtxCount, maximumEtxCount)
}
if block.NumberU64(common.ZONE_CTX) > params.TimeToStartTx && (etxAvailable && totalEtxGas < minimumEtxGas) || totalEtxGas > maximumEtxGas {
p.logger.Errorf("prevInboundEtxs: %d, oldestIndex: %d, etxHash: %s", len(prevInboundEtxs), oldestIndex.Int64(), etx.Hash().Hex())
return nil, nil, nil, nil, 0, 0, 0, nil, fmt.Errorf("total gas used by ETXs %d is not within the range %d to %d", totalEtxGas, minimumEtxGas, maximumEtxGas)
}
Expand Down
30 changes: 24 additions & 6 deletions core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,10 @@ func (w *worker) GeneratePendingHeader(block *types.WorkObject, fill bool, txs t
work.wo.Header().SetBaseFee(big.NewInt(0))
}

if block.NumberU64(common.ZONE_CTX) < params.TimeToStartTx {
work.wo.Header().SetGasUsed(0)
}

if nodeCtx == common.ZONE_CTX && w.hc.ProcessingState() {
if !fromOrderedTransactionSet {
select {
Expand Down Expand Up @@ -1034,10 +1038,12 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
return nil, false, fmt.Errorf("invalid coinbase address %v: %v", tx.To(), err)
}
lockupByte := tx.Data()[0]
if err := env.gasPool.SubGas(params.TxGas); err != nil {
// etxs are taking more gas
w.logger.Info("Stopped the etx processing because we crossed the block gas limit processing coinbase etxs")
return nil, false, nil
if parent.NumberU64(common.ZONE_CTX) >= params.TimeToStartTx {
if err := env.gasPool.SubGas(params.TxGas); err != nil {
// etxs are taking more gas
w.logger.Info("Stopped the etx processing because we crossed the block gas limit processing coinbase etxs")
return nil, false, nil
}
}
if tx.To().IsInQiLedgerScope() {
lockup := new(big.Int).SetUint64(params.LockupByteToBlockDepth[lockupByte])
Expand Down Expand Up @@ -1077,7 +1083,9 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
}
}
gasUsed := env.wo.GasUsed()
gasUsed += params.TxGas
if parent.NumberU64(common.ZONE_CTX) >= params.TimeToStartTx {
gasUsed += params.TxGas
}
env.wo.Header().SetGasUsed(gasUsed)
env.gasUsedAfterTransaction = append(env.gasUsedAfterTransaction, gasUsed)
env.txs = append(env.txs, tx)
Expand Down Expand Up @@ -1189,12 +1197,22 @@ func (w *worker) commitTransactions(env *environment, primeTerminus *types.WorkO
}
var coalescedLogs []*types.Log
minEtxGas := gasLimit() / params.MinimumEtxGasDivisor
etxCount := 0
for {
if excludeEtx {
break
}
etxCount = 0
for _, tx := range env.txs {
if tx.Type() == types.ExternalTxType {
etxCount++
}
}
if parent.NumberU64(common.ZONE_CTX) < params.TimeToStartTx && etxCount > params.MinEtxCount {
break
}
// Add ETXs until minimum gas is used
if env.wo.GasUsed() >= minEtxGas {
if parent.NumberU64(common.ZONE_CTX) >= params.TimeToStartTx && env.wo.GasUsed() >= minEtxGas {
// included etxs more than min etx gas
break
}
Expand Down
4 changes: 3 additions & 1 deletion params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ const (
MaxAddressGrindAttempts int = 1000 // Maximum number of attempts to grind an address to a valid one
MinimumEtxGasDivisor = 5 // The divisor for the minimum gas for inbound ETXs (Block gas limit / MinimumEtxGasDivisor)
MaximumEtxGasMultiplier = 2 // Multiplied with the minimum ETX gas for inbound ETXs (Block gas limit / MinimumEtxGasDivisor) * MaximumEtxGasMultiplier
MinEtxCount = 50 // These counts are used in the case where tx is not eligible to be started
MaxEtxCount = 100

// Dynamic Expansion parameters

Expand Down Expand Up @@ -174,7 +176,7 @@ var (
OrchardDurationLimit = big.NewInt(5) // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not.
LighthouseDurationLimit = big.NewInt(7) // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not.
LocalDurationLimit = big.NewInt(1) // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not.
TimeToStartTx uint64 = 0 * BlocksPerDay
TimeToStartTx uint64 = 30
BlocksPerDay uint64 = new(big.Int).Div(big.NewInt(86400), DurationLimit).Uint64() // BlocksPerDay is the number of blocks per day assuming 12 second block time
DifficultyAdjustmentPeriod = big.NewInt(360) // This is the number of blocks over which the average has to be taken
DifficultyAdjustmentFactor int64 = 40 // This is the factor that divides the log of the change in the difficulty
Expand Down

0 comments on commit 2fdbf18

Please sign in to comment.