Skip to content

Commit

Permalink
added egp value for eth transfer. Fix zkevm_EstimateFee endpoint. Rem…
Browse files Browse the repository at this point in the history
…ove use minGasPriceAllowed on egp calculation (0xPolygonHermez#3041)

Co-authored-by: tclemos <[email protected]>
  • Loading branch information
agnusmor and tclemos authored Jan 15, 2024
1 parent 021a399 commit fe3decc
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 82 deletions.
8 changes: 8 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ func Test_Defaults(t *testing.T) {
path: "Pool.EffectiveGasPrice.FinalDeviationPct",
expectedValue: uint64(10),
},
{
path: "Pool.EffectiveGasPrice.EthTransferGasPrice",
expectedValue: uint64(0),
},
{
path: "Pool.EffectiveGasPrice.EthTransferL1GasPriceFactor",
expectedValue: float64(0),
},
{
path: "Pool.DB.User",
expectedValue: "pool_user",
Expand Down
2 changes: 2 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ GlobalQueue = 1024
NetProfit = 1
BreakEvenFactor = 1.1
FinalDeviationPct = 10
EthTransferGasPrice = 0
EthTransferL1GasPriceFactor = 0
L2GasPriceSuggesterFactor = 0.5
[Pool.DB]
User = "pool_user"
Expand Down
2 changes: 2 additions & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ GlobalQueue = 1024
NetProfit = 1
BreakEvenFactor = 1.1
FinalDeviationPct = 10
EthTransferGasPrice = 0
EthTransferL1GasPriceFactor = 0
L2GasPriceSuggesterFactor = 0.5
[Pool.DB]
User = "pool_user"
Expand Down
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

54 changes: 43 additions & 11 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -720,16 +720,18 @@ GlobalQueue=1024
**Type:** : `object`
**Description:** EffectiveGasPrice is the config for the effective gas price calculation

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| --------------------------------------------------------------------------------- | ------- | ------- | ---------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| - [Enabled](#Pool_EffectiveGasPrice_Enabled ) | No | boolean | No | - | Enabled is a flag to enable/disable the effective gas price |
| - [L1GasPriceFactor](#Pool_EffectiveGasPrice_L1GasPriceFactor ) | No | number | No | - | L1GasPriceFactor is the percentage of the L1 gas price that will be used as the L2 min gas price |
| - [ByteGasCost](#Pool_EffectiveGasPrice_ByteGasCost ) | No | integer | No | - | ByteGasCost is the gas cost per byte that is not 0 |
| - [ZeroByteGasCost](#Pool_EffectiveGasPrice_ZeroByteGasCost ) | No | integer | No | - | ZeroByteGasCost is the gas cost per byte that is 0 |
| - [NetProfit](#Pool_EffectiveGasPrice_NetProfit ) | No | number | No | - | NetProfit is the profit margin to apply to the calculated breakEvenGasPrice |
| - [BreakEvenFactor](#Pool_EffectiveGasPrice_BreakEvenFactor ) | No | number | No | - | BreakEvenFactor is the factor to apply to the calculated breakevenGasPrice when comparing it with the gasPriceSigned of a tx |
| - [FinalDeviationPct](#Pool_EffectiveGasPrice_FinalDeviationPct ) | No | integer | No | - | FinalDeviationPct is the max allowed deviation percentage BreakEvenGasPrice on re-calculation |
| - [L2GasPriceSuggesterFactor](#Pool_EffectiveGasPrice_L2GasPriceSuggesterFactor ) | No | number | No | - | L2GasPriceSuggesterFactor is the factor to apply to L1 gas price to get the suggested L2 gas price used in the<br />calculations when the effective gas price is disabled (testing/metrics purposes) |
| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ------------------------------------------------------------------------------------- | ------- | ------- | ---------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| - [Enabled](#Pool_EffectiveGasPrice_Enabled ) | No | boolean | No | - | Enabled is a flag to enable/disable the effective gas price |
| - [L1GasPriceFactor](#Pool_EffectiveGasPrice_L1GasPriceFactor ) | No | number | No | - | L1GasPriceFactor is the percentage of the L1 gas price that will be used as the L2 min gas price |
| - [ByteGasCost](#Pool_EffectiveGasPrice_ByteGasCost ) | No | integer | No | - | ByteGasCost is the gas cost per byte that is not 0 |
| - [ZeroByteGasCost](#Pool_EffectiveGasPrice_ZeroByteGasCost ) | No | integer | No | - | ZeroByteGasCost is the gas cost per byte that is 0 |
| - [NetProfit](#Pool_EffectiveGasPrice_NetProfit ) | No | number | No | - | NetProfit is the profit margin to apply to the calculated breakEvenGasPrice |
| - [BreakEvenFactor](#Pool_EffectiveGasPrice_BreakEvenFactor ) | No | number | No | - | BreakEvenFactor is the factor to apply to the calculated breakevenGasPrice when comparing it with the gasPriceSigned of a tx |
| - [FinalDeviationPct](#Pool_EffectiveGasPrice_FinalDeviationPct ) | No | integer | No | - | FinalDeviationPct is the max allowed deviation percentage BreakEvenGasPrice on re-calculation |
| - [EthTransferGasPrice](#Pool_EffectiveGasPrice_EthTransferGasPrice ) | No | integer | No | - | EthTransferGasPrice is the fixed gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)<br />Only one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error |
| - [EthTransferL1GasPriceFactor](#Pool_EffectiveGasPrice_EthTransferL1GasPriceFactor ) | No | number | No | - | EthTransferL1GasPriceFactor is the percentage of L1 gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)<br />Only one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error |
| - [L2GasPriceSuggesterFactor](#Pool_EffectiveGasPrice_L2GasPriceSuggesterFactor ) | No | number | No | - | L2GasPriceSuggesterFactor is the factor to apply to L1 gas price to get the suggested L2 gas price used in the<br />calculations when the effective gas price is disabled (testing/metrics purposes) |

#### <a name="Pool_EffectiveGasPrice_Enabled"></a>7.11.1. `Pool.EffectiveGasPrice.Enabled`

Expand Down Expand Up @@ -829,7 +831,37 @@ BreakEvenFactor=1.1
FinalDeviationPct=10
```

#### <a name="Pool_EffectiveGasPrice_L2GasPriceSuggesterFactor"></a>7.11.8. `Pool.EffectiveGasPrice.L2GasPriceSuggesterFactor`
#### <a name="Pool_EffectiveGasPrice_EthTransferGasPrice"></a>7.11.8. `Pool.EffectiveGasPrice.EthTransferGasPrice`

**Type:** : `integer`

**Default:** `0`

**Description:** EthTransferGasPrice is the fixed gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)
Only one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error

**Example setting the default value** (0):
```
[Pool.EffectiveGasPrice]
EthTransferGasPrice=0
```

#### <a name="Pool_EffectiveGasPrice_EthTransferL1GasPriceFactor"></a>7.11.9. `Pool.EffectiveGasPrice.EthTransferL1GasPriceFactor`

**Type:** : `number`

**Default:** `0`

**Description:** EthTransferL1GasPriceFactor is the percentage of L1 gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)
Only one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error

**Example setting the default value** (0):
```
[Pool.EffectiveGasPrice]
EthTransferL1GasPriceFactor=0
```

#### <a name="Pool_EffectiveGasPrice_L2GasPriceSuggesterFactor"></a>7.11.10. `Pool.EffectiveGasPrice.L2GasPriceSuggesterFactor`

**Type:** : `number`

Expand Down
10 changes: 10 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@
"description": "FinalDeviationPct is the max allowed deviation percentage BreakEvenGasPrice on re-calculation",
"default": 10
},
"EthTransferGasPrice": {
"type": "integer",
"description": "EthTransferGasPrice is the fixed gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)\nOnly one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error",
"default": 0
},
"EthTransferL1GasPriceFactor": {
"type": "number",
"description": "EthTransferL1GasPriceFactor is the percentage of L1 gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)\nOnly one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error",
"default": 0
},
"L2GasPriceSuggesterFactor": {
"type": "number",
"description": "L2GasPriceSuggesterFactor is the factor to apply to L1 gas price to get the suggested L2 gas price used in the\ncalculations when the effective gas price is disabled (testing/metrics purposes)",
Expand Down
15 changes: 12 additions & 3 deletions jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ func (z *ZKEVMEndpoints) EstimateFee(arg *types.TxArgs, blockArg *types.BlockNum
}

txGasPrice := new(big.Int).SetUint64(gasPrices.L2GasPrice) // by default we assume the tx gas price is the current L2 gas price
txEGPPct := state.MaxEffectivePercentage
egpEnabled := z.pool.EffectiveGasPriceEnabled()

if egpEnabled {
Expand All @@ -476,11 +477,19 @@ func (z *ZKEVMEndpoints) EstimateFee(arg *types.TxArgs, blockArg *types.BlockNum
}

if txEGP.Cmp(txGasPrice) == -1 { // txEGP < txGasPrice
txGasPrice = txEGP
// We need to "round" the final effectiveGasPrice to a 256 fraction of the txGasPrice
txEGPPct, err = z.pool.CalculateEffectiveGasPricePercentage(txGasPrice, txEGP)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to calculate effective gas price percentage", err, false)
}
// txGasPriceFraction = txGasPrice/256
txGasPriceFraction := new(big.Int).Div(txGasPrice, new(big.Int).SetUint64(256)) //nolint:gomnd
// txGasPrice = txGasPriceFraction*(txEGPPct+1)
txGasPrice = new(big.Int).Mul(txGasPriceFraction, new(big.Int).SetUint64(uint64(txEGPPct+1)))
}

log.Infof("[EstimateFee] gasPrice: %d, effectiveGasPrice: %d, l2GasPrice: %d, len: %d, gas: %d, l1GasPrice: %d",
txGasPrice, txEGP, gasPrices.L2GasPrice, len(rawTx), gasEstimation, gasPrices.L1GasPrice)
log.Infof("[EstimateFee] finalGasPrice: %d, effectiveGasPrice: %d, egpPct: %d, l2GasPrice: %d, len: %d, gas: %d, l1GasPrice: %d",
txGasPrice, txEGP, txEGPPct, gasPrices.L2GasPrice, len(rawTx), gasEstimation, gasPrices.L1GasPrice)
}

fee := new(big.Int).Mul(txGasPrice, new(big.Int).SetUint64(gasEstimation))
Expand Down
28 changes: 28 additions & 0 deletions jsonrpc/mocks/mock_pool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type PoolInterface interface {
GetTransactionByHash(ctx context.Context, hash common.Hash) (*pool.Transaction, error)
GetTransactionByL2Hash(ctx context.Context, hash common.Hash) (*pool.Transaction, error)
CalculateEffectiveGasPrice(rawTx []byte, txGasPrice *big.Int, txGasUsed uint64, l1GasPrice uint64, l2GasPrice uint64) (*big.Int, error)
CalculateEffectiveGasPricePercentage(gasPrice *big.Int, effectiveGasPrice *big.Int) (uint8, error)
EffectiveGasPriceEnabled() bool
}

Expand Down
8 changes: 8 additions & 0 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ type EffectiveGasPriceCfg struct {
// FinalDeviationPct is the max allowed deviation percentage BreakEvenGasPrice on re-calculation
FinalDeviationPct uint64 `mapstructure:"FinalDeviationPct"`

// EthTransferGasPrice is the fixed gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)
// Only one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error
EthTransferGasPrice uint64 `mapstructure:"EthTransferGasPrice"`

// EthTransferL1GasPriceFactor is the percentage of L1 gas price returned as effective gas price for txs tha are ETH transfers (0 means disabled)
// Only one of EthTransferGasPrice or EthTransferL1GasPriceFactor params can be different than 0. If both params are set to 0, the sequencer will halt and log an error
EthTransferL1GasPriceFactor float64 `mapstructure:"EthTransferL1GasPriceFactor"`

// L2GasPriceSuggesterFactor is the factor to apply to L1 gas price to get the suggested L2 gas price used in the
// calculations when the effective gas price is disabled (testing/metrics purposes)
L2GasPriceSuggesterFactor float64 `mapstructure:"L2GasPriceSuggesterFactor"`
Expand Down
Loading

0 comments on commit fe3decc

Please sign in to comment.