Skip to content

Commit

Permalink
fix dynamic gas price apollo config (#157)
Browse files Browse the repository at this point in the history
* fix apollo

* Update dynamic_gas_price_xlayer.go

* Update dynamic_gas_price_xlayer.go

* only for test

* Update dynamic_gas_price_xlayer.go

* add log

* fix lint
  • Loading branch information
LeoGuo621 authored Apr 2, 2024
1 parent 25267c5 commit 3e852e1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 35 deletions.
9 changes: 9 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ DisableAPIs = []
Host = "0.0.0.0"
Port = 8546
ReadLimit = 104857600
[RPC.DynamicGP]
Enabled = false
CongestionTxThreshold = 100
CheckBatches = 5
SampleNumber = 3
Percentile = 70
MaxPrice = 20000000000
MinPrice = 2000000000
UpdatePeriod = "10s"
[Synchronizer]
SyncInterval = "1s"
Expand Down
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2032,84 +2032,84 @@ Enabled=false

**Type:** : `integer`

**Default:** `0`
**Default:** `100`

**Description:** CongestionTxThreshold defines the tx threshold to measure whether there is congestion

**Example setting the default value** (0):
**Example setting the default value** (100):
```
[RPC.DynamicGP]
CongestionTxThreshold=0
CongestionTxThreshold=100
```

#### <a name="RPC_DynamicGP_CheckBatches"></a>8.24.3. `RPC.DynamicGP.CheckBatches`

**Type:** : `integer`

**Default:** `0`
**Default:** `5`

**Description:** CheckBatches defines the number of recent Batches used to sample gas price

**Example setting the default value** (0):
**Example setting the default value** (5):
```
[RPC.DynamicGP]
CheckBatches=0
CheckBatches=5
```

#### <a name="RPC_DynamicGP_SampleNumber"></a>8.24.4. `RPC.DynamicGP.SampleNumber`

**Type:** : `integer`

**Default:** `0`
**Default:** `3`

**Description:** SampleTxNumer defines the number of sampled gas prices in each batch

**Example setting the default value** (0):
**Example setting the default value** (3):
```
[RPC.DynamicGP]
SampleNumber=0
SampleNumber=3
```

#### <a name="RPC_DynamicGP_Percentile"></a>8.24.5. `RPC.DynamicGP.Percentile`

**Type:** : `integer`

**Default:** `0`
**Default:** `70`

**Description:** Percentile defines the sampling weight of all sampled gas prices

**Example setting the default value** (0):
**Example setting the default value** (70):
```
[RPC.DynamicGP]
Percentile=0
Percentile=70
```

#### <a name="RPC_DynamicGP_MaxPrice"></a>8.24.6. `RPC.DynamicGP.MaxPrice`

**Type:** : `integer`

**Default:** `0`
**Default:** `20000000000`

**Description:** MaxPrice defines the dynamic gas price upper limit

**Example setting the default value** (0):
**Example setting the default value** (20000000000):
```
[RPC.DynamicGP]
MaxPrice=0
MaxPrice=20000000000
```

#### <a name="RPC_DynamicGP_MinPrice"></a>8.24.7. `RPC.DynamicGP.MinPrice`

**Type:** : `integer`

**Default:** `0`
**Default:** `2000000000`

**Description:** MinPrice defines the dynamic gas price lower limit

**Example setting the default value** (0):
**Example setting the default value** (2000000000):
```
[RPC.DynamicGP]
MinPrice=0
MinPrice=2000000000
```

#### <a name="RPC_DynamicGP_UpdatePeriod"></a>8.24.8. `RPC.DynamicGP.UpdatePeriod`
Expand All @@ -2118,7 +2118,7 @@ MinPrice=0

**Type:** : `string`

**Default:** `"0s"`
**Default:** `"10s"`

**Description:** UpdatePeriod defines the time interval for updating dynamic gas price

Expand All @@ -2132,10 +2132,10 @@ MinPrice=0
"300ms"
```

**Example setting the default value** ("0s"):
**Example setting the default value** ("10s"):
```
[RPC.DynamicGP]
UpdatePeriod="0s"
UpdatePeriod="10s"
```

### <a name="RPC_EnableInnerTxCacheDB"></a>8.25. `RPC.EnableInnerTxCacheDB`
Expand Down
14 changes: 7 additions & 7 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -763,38 +763,38 @@
"CongestionTxThreshold": {
"type": "integer",
"description": "CongestionTxThreshold defines the tx threshold to measure whether there is congestion",
"default": 0
"default": 100
},
"CheckBatches": {
"type": "integer",
"description": "CheckBatches defines the number of recent Batches used to sample gas price",
"default": 0
"default": 5
},
"SampleNumber": {
"type": "integer",
"description": "SampleTxNumer defines the number of sampled gas prices in each batch",
"default": 0
"default": 3
},
"Percentile": {
"type": "integer",
"description": "Percentile defines the sampling weight of all sampled gas prices",
"default": 0
"default": 70
},
"MaxPrice": {
"type": "integer",
"description": "MaxPrice defines the dynamic gas price upper limit",
"default": 0
"default": 20000000000
},
"MinPrice": {
"type": "integer",
"description": "MinPrice defines the dynamic gas price lower limit",
"default": 0
"default": 2000000000
},
"UpdatePeriod": {
"type": "string",
"title": "Duration",
"description": "UpdatePeriod defines the time interval for updating dynamic gas price",
"default": "0s",
"default": "10s",
"examples": [
"1m",
"300ms"
Expand Down
26 changes: 21 additions & 5 deletions jsonrpc/dynamic_gas_price_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

// DefaultUpdatePeriod defines default value of UpdatePeriod
const DefaultUpdatePeriod = 10 * time.Second //nolint:gomnd

// DynamicGPConfig represents the configuration of the dynamic gas price
type DynamicGPConfig struct {

Expand Down Expand Up @@ -53,7 +56,11 @@ type DynamicGPManager struct {
func (e *EthEndpoints) runDynamicGPSuggester() {
ctx := context.Background()
// initialization
updateTimer := time.NewTimer(10 * time.Second) //nolint:gomnd
updateTimer := time.NewTimer(DefaultUpdatePeriod)
if e.cfg.DynamicGP.UpdatePeriod.Duration.Nanoseconds() > 0 {
log.Infof("Dynamic gas price update period is %s", e.cfg.DynamicGP.UpdatePeriod.Duration.String())
updateTimer = time.NewTimer(e.cfg.DynamicGP.UpdatePeriod.Duration)
}
for {
select {
case <-ctx.Done():
Expand All @@ -65,9 +72,17 @@ func (e *EthEndpoints) runDynamicGPSuggester() {
e.cfg.DynamicGP = getApolloConfig().DynamicGP
getApolloConfig().RUnlock()
}
log.Info("Dynamic gas price update period is ", e.cfg.DynamicGP.UpdatePeriod.Duration.String())
e.calcDynamicGP(ctx)
updateTimer.Reset(e.cfg.DynamicGP.UpdatePeriod.Duration)
period := e.cfg.DynamicGP.UpdatePeriod.Duration
if period.Nanoseconds() <= 0 {
log.Warn("Dynamic gas price update period is less than or equal to 0. Set it to DefaultUpdatePeriod.")
period = DefaultUpdatePeriod
}
if e.cfg.DynamicGP.Enabled {
log.Info("Starting calculate dynamic gas price...")
e.calcDynamicGP(ctx)
}
log.Infof("Dynamic gas price update period is %s", period.String())
updateTimer.Reset(period)
}
}
}
Expand Down Expand Up @@ -95,6 +110,7 @@ func (e *EthEndpoints) calcDynamicGP(ctx context.Context) {
}

if !isCongested {
log.Info("there is no congestion for L2")
gasPrices, err := e.pool.GetGasPrices(ctx)
if err != nil {
log.Errorf("failed to get raw gas prices when it is not congested: ", err)
Expand All @@ -107,7 +123,7 @@ func (e *EthEndpoints) calcDynamicGP(ctx context.Context) {
return
}

log.Debug("there is congestion for L2")
log.Warn("there is congestion for L2")

e.dgpMan.fetchLock.Lock()
defer e.dgpMan.fetchLock.Unlock()
Expand Down
3 changes: 2 additions & 1 deletion jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func NewEthEndpoints(cfg Config, chainID uint64, p types.PoolInterface, s types.
e := &EthEndpoints{cfg: cfg, chainID: chainID, pool: p, state: s, etherman: etherman, storage: storage}
e.dgpMan = DynamicGPManager{
lastL2BatchNumber: 0,
lastPrice: new(big.Int).SetUint64(cfg.DynamicGP.MinPrice),
// initialize default lastPrice to 1 GWei
lastPrice: new(big.Int).SetUint64(1000000000), //nolint:gomnd
}
go e.runDynamicGPSuggester()
s.RegisterNewL2BlockEventHandler(e.onNewL2Block)
Expand Down

0 comments on commit 3e852e1

Please sign in to comment.