Skip to content

Commit

Permalink
Cherry pick effective gas prices fixes (0xPolygonHermez#2738, 0xPolyg…
Browse files Browse the repository at this point in the history
…onHermez#2741) to develop branch (0xPolygonHermez#2766)

* Fix GlobalQueue/AccountQueue pool config (0xPolygonHermez#2738)

* Fix missing pool.Config parameter when creating a new Sequencer (0xPolygonHermez#2741)

* add gasprice update log

* log finalizer L1/L2 gas prices

* fix passing pool.Config to new sequencer

* remove temporary logs
  • Loading branch information
agnusmor authored Nov 8, 2023
1 parent 930cab8 commit be1818d
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func createSequencer(cfg config.Config, pool *pool.Pool, st *state.State, eventL
log.Fatal(err)
}

seq, err := sequencer.New(cfg.Sequencer, cfg.State.Batch, pool, st, etherman, eventLog)
seq, err := sequencer.New(cfg.Sequencer, cfg.State.Batch, cfg.Pool, pool, st, etherman, eventLog)
if err != nil {
log.Fatal(err)
}
Expand Down
1 change: 0 additions & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Level = "info"
Outputs = ["stderr"]
[State]
AccountQueue = 64
[State.DB]
User = "state_user"
Password = "state_password"
Expand Down
3 changes: 2 additions & 1 deletion config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Level = "debug"
Outputs = ["stderr"]

[State]
AccountQueue = 64
[State.DB]
User = "state_user"
Password = "state_password"
Expand Down Expand Up @@ -37,6 +36,8 @@ MaxTxDataBytesSize=100000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
AccountQueue = 64
GlobalQueue = 1024
[Pool.EffectiveGasPrice]
Enabled = false
L1GasPriceFactor = 0.25
Expand Down
3 changes: 2 additions & 1 deletion config/environments/mainnet/node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Level = "info"
Outputs = ["stderr"]

[State]
AccountQueue = 64
[State.DB]
User = "state_user"
Password = "state_password"
Expand Down Expand Up @@ -32,6 +31,8 @@ MaxTxDataBytesSize=100000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
AccountQueue = 64
GlobalQueue = 1024
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
4 changes: 2 additions & 2 deletions pool/effectivegasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (e *EffectiveGasPrice) CalculateBreakEvenGasPrice(rawTx []byte, txGasPrice
// Calculate BreakEvenGasPrice
totalTxPrice := (txGasUsed * l2MinGasPrice) +
((constBytesTx+txNonZeroBytes)*e.cfg.ByteGasCost+txZeroBytes*e.cfg.ZeroByteGasCost)*l1GasPrice
breakEvenGasPrice := big.NewInt(0).SetUint64(uint64(float64(totalTxPrice/txGasUsed) * e.cfg.NetProfit))
breakEvenGasPrice := new(big.Int).SetUint64(uint64(float64(totalTxPrice/txGasUsed) * e.cfg.NetProfit))

return breakEvenGasPrice, nil
}
Expand All @@ -89,7 +89,7 @@ func (e *EffectiveGasPrice) CalculateEffectiveGasPrice(rawTx []byte, txGasPrice
ratioPriority := new(big.Float).SetFloat64(1.0)

if bfTxGasPrice.Cmp(bfL2GasPrice) == 1 {
//ratioPriority := (txGasPrice / l2GasPrice)
//ratioPriority = (txGasPrice / l2GasPrice)
ratioPriority = new(big.Float).Quo(bfTxGasPrice, bfL2GasPrice)
}

Expand Down
17 changes: 10 additions & 7 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,13 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
if firstTxProcess {
// Get L1 gas price and store in txTracker to make it consistent during the lifespan of the transaction
tx.L1GasPrice, tx.L2GasPrice = f.dbManager.GetL1AndL2GasPrice()
log.Infof("tx.L1GasPrice = %d, tx.L2GasPrice = %d", tx.L1GasPrice, tx.L2GasPrice)
// Save values for later logging
tx.EGPLog.GasUsedFirst = tx.BatchResources.ZKCounters.CumulativeGasUsed
tx.EGPLog.GasPrice.Set(tx.GasPrice)
tx.EGPLog.L1GasPrice = tx.L1GasPrice
tx.EGPLog.L2GasPrice = tx.L2GasPrice

// Calculate EffectiveGasPrice
egp, err := f.effectiveGasPrice.CalculateEffectiveGasPrice(tx.RawTx, tx.GasPrice, tx.BatchResources.ZKCounters.CumulativeGasUsed, tx.L1GasPrice, tx.L2GasPrice)
if err != nil {
Expand All @@ -592,12 +599,8 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
} else {
tx.EffectiveGasPrice.Set(egp)

// Save initial values for later logging
// Save first EffectiveGasPrice for later logging
tx.EGPLog.ValueFirst.Set(tx.EffectiveGasPrice)
tx.EGPLog.GasUsedFirst = tx.BatchResources.ZKCounters.CumulativeGasUsed
tx.EGPLog.GasPrice.Set(tx.GasPrice)
tx.EGPLog.L1GasPrice = tx.L1GasPrice
tx.EGPLog.L2GasPrice = tx.L2GasPrice

// If EffectiveGasPrice >= tx.GasPrice, we process the tx with tx.GasPrice
if tx.EffectiveGasPrice.Cmp(tx.GasPrice) >= 0 {
Expand Down Expand Up @@ -747,9 +750,9 @@ func (f *finalizer) handleProcessTransactionResponse(ctx context.Context, tx *Tx
tx.EGPLog.ValueFinal.Set(tx.EffectiveGasPrice)

// Log here the results of EGP calculation
log.Infof("egp-log: final: %d, first: %d, second: %d, percentage: %d, deviation: %d, maxDeviation: %d, gasUsed1: %d, gasUsed2: %d, gasPrice: %d, l1GasPrice: %d, l2GasPrice: %d, reprocess: %t, gasPriceOC: %t, balanceOC: %t, enabled: %t, txSize: %d, txHash: %s",
log.Infof("egp-log: final: %d, first: %d, second: %d, percentage: %d, deviation: %d, maxDeviation: %d, gasUsed1: %d, gasUsed2: %d, gasPrice: %d, l1GasPrice: %d, l2GasPrice: %d, reprocess: %t, gasPriceOC: %t, balanceOC: %t, enabled: %t, txSize: %d, txHash: %s, error: %s",
tx.EGPLog.ValueFinal, tx.EGPLog.ValueFirst, tx.EGPLog.ValueSecond, tx.EGPLog.Percentage, tx.EGPLog.FinalDeviation, tx.EGPLog.MaxDeviation, tx.EGPLog.GasUsedFirst, tx.EGPLog.GasUsedSecond,
tx.EGPLog.GasPrice, tx.EGPLog.L1GasPrice, tx.EGPLog.L2GasPrice, tx.EGPLog.Reprocess, tx.EGPLog.GasPriceOC, tx.EGPLog.BalanceOC, egpEnabled, len(tx.RawTx), tx.HashStr)
tx.EGPLog.GasPrice, tx.EGPLog.L1GasPrice, tx.EGPLog.L2GasPrice, tx.EGPLog.Reprocess, tx.EGPLog.GasPriceOC, tx.EGPLog.BalanceOC, egpEnabled, len(tx.RawTx), tx.HashStr, tx.EGPLog.Error)

txToStore := transactionToStore{
hash: tx.Hash,
Expand Down
3 changes: 2 additions & 1 deletion sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ClosingSignalCh struct {
}

// New init sequencer
func New(cfg Config, batchCfg state.BatchConfig, txPool txPool, state stateInterface, etherman etherman, eventLog *event.EventLog) (*Sequencer, error) {
func New(cfg Config, batchCfg state.BatchConfig, poolCfg pool.Config, txPool txPool, state stateInterface, etherman etherman, eventLog *event.EventLog) (*Sequencer, error) {
addr, err := etherman.TrustedSequencer()
if err != nil {
return nil, fmt.Errorf("failed to get trusted sequencer address, err: %v", err)
Expand All @@ -52,6 +52,7 @@ func New(cfg Config, batchCfg state.BatchConfig, txPool txPool, state stateInter
sequencer := &Sequencer{
cfg: cfg,
batchCfg: batchCfg,
poolCfg: poolCfg,
pool: txPool,
state: state,
etherman: etherman,
Expand Down
3 changes: 2 additions & 1 deletion test/config/debug.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Level = "debug"
Outputs = ["stderr"]

[State]
AccountQueue = 64
[State.DB]
User = "state_user"
Password = "state_password"
Expand Down Expand Up @@ -37,6 +36,8 @@ MaxTxDataBytesSize=30000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
AccountQueue = 64
GlobalQueue = 1024
[Pool.EffectiveGasPrice]
Enabled = false
L1GasPriceFactor = 0.25
Expand Down
3 changes: 2 additions & 1 deletion test/config/test.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Level = "debug"
Outputs = ["stderr"]

[State]
AccountQueue = 64
[State.DB]
User = "state_user"
Password = "state_password"
Expand Down Expand Up @@ -37,6 +36,8 @@ MaxTxDataBytesSize=100000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
AccountQueue = 64
GlobalQueue = 1024
[Pool.EffectiveGasPrice]
Enabled = false
L1GasPriceFactor = 0.25
Expand Down

0 comments on commit be1818d

Please sign in to comment.