Skip to content

Commit

Permalink
add gas price dynamic adjust in follower mode (#34)
Browse files Browse the repository at this point in the history
* add dynamic adjust for follower

* modify the config

* fix lint and schema

* modify the comment

* modify the comment

* make doc
  • Loading branch information
lyh169 authored Dec 4, 2023
1 parent 9e945f8 commit 6b55630
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 77 deletions.
18 changes: 18 additions & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ MaxGasPriceWei = 0
#GasPriceUsdt = 0.0001
#L2CoinId = 7184

## the follower strategy config demo that can dynamic adjust the factor through get the L1 and L2 coin price from kafka
#Type = "follower"
#UpdatePeriod = "10s"
#Factor = 1
#DefaultGasPriceWei = 1000000000
#KafkaURL = "127.0.0.1:9092"
#Topic = "middle_coinPrice_push"
#GroupID = "web3_okbc_explorerchainprice"
## just in SASL_SSL mode
#Username = ""
#Password = ""
#RootCAPath = "only-4096-ca-cert"
#L1CoinId = 15756
#L2CoinId = 7184
#DefaultL2CoinPrice = 40
#DefaultL1CoinPrice = 1600
#EnableFollowerAdjustByL2L1Price = true # dynamic adjust the factor through the L1 and L2 coins price in follower strategy

[MTClient]
URI = "x1-prover:50061"

Expand Down
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

95 changes: 69 additions & 26 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2516,28 +2516,31 @@ GenesisBlockNum=0
**Type:** : `object`
**Description:** Configuration of the gas price suggester service

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ------------------------------------------------------------------------------ | ------- | ------- | ---------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| - [Type](#L2GasPriceSuggester_Type ) | No | string | No | - | - |
| - [DefaultGasPriceWei](#L2GasPriceSuggester_DefaultGasPriceWei ) | No | integer | No | - | DefaultGasPriceWei is used to set the gas price to be used by the default gas pricer or as minimim gas price by the follower gas pricer. |
| - [MaxGasPriceWei](#L2GasPriceSuggester_MaxGasPriceWei ) | No | integer | No | - | MaxGasPriceWei is used to limit the gas price returned by the follower gas pricer to a maximum value. It is ignored if 0. |
| - [MaxPrice](#L2GasPriceSuggester_MaxPrice ) | No | object | No | - | - |
| - [IgnorePrice](#L2GasPriceSuggester_IgnorePrice ) | No | object | No | - | - |
| - [CheckBlocks](#L2GasPriceSuggester_CheckBlocks ) | No | integer | No | - | - |
| - [Percentile](#L2GasPriceSuggester_Percentile ) | No | integer | No | - | - |
| - [UpdatePeriod](#L2GasPriceSuggester_UpdatePeriod ) | No | string | No | - | Duration |
| - [CleanHistoryPeriod](#L2GasPriceSuggester_CleanHistoryPeriod ) | No | string | No | - | Duration |
| - [CleanHistoryTimeRetention](#L2GasPriceSuggester_CleanHistoryTimeRetention ) | No | string | No | - | Duration |
| - [KafkaURL](#L2GasPriceSuggester_KafkaURL ) | No | string | No | - | - |
| - [Topic](#L2GasPriceSuggester_Topic ) | No | string | No | - | - |
| - [GroupID](#L2GasPriceSuggester_GroupID ) | No | string | No | - | - |
| - [Username](#L2GasPriceSuggester_Username ) | No | string | No | - | - |
| - [Password](#L2GasPriceSuggester_Password ) | No | string | No | - | - |
| - [RootCAPath](#L2GasPriceSuggester_RootCAPath ) | No | string | No | - | - |
| - [L2CoinId](#L2GasPriceSuggester_L2CoinId ) | No | integer | No | - | - |
| - [DefaultL2CoinPrice](#L2GasPriceSuggester_DefaultL2CoinPrice ) | No | number | No | - | DefaultL2CoinPrice is the native token's coin price |
| - [GasPriceUsdt](#L2GasPriceSuggester_GasPriceUsdt ) | No | number | No | - | - |
| - [Factor](#L2GasPriceSuggester_Factor ) | No | number | No | - | - |
| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ------------------------------------------------------------------------------------------ | ------- | ------- | ---------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| - [Type](#L2GasPriceSuggester_Type ) | No | string | No | - | - |
| - [DefaultGasPriceWei](#L2GasPriceSuggester_DefaultGasPriceWei ) | No | integer | No | - | DefaultGasPriceWei is used to set the gas price to be used by the default gas pricer or as minimim gas price by the follower gas pricer. |
| - [MaxGasPriceWei](#L2GasPriceSuggester_MaxGasPriceWei ) | No | integer | No | - | MaxGasPriceWei is used to limit the gas price returned by the follower gas pricer to a maximum value. It is ignored if 0. |
| - [MaxPrice](#L2GasPriceSuggester_MaxPrice ) | No | object | No | - | - |
| - [IgnorePrice](#L2GasPriceSuggester_IgnorePrice ) | No | object | No | - | - |
| - [CheckBlocks](#L2GasPriceSuggester_CheckBlocks ) | No | integer | No | - | - |
| - [Percentile](#L2GasPriceSuggester_Percentile ) | No | integer | No | - | - |
| - [UpdatePeriod](#L2GasPriceSuggester_UpdatePeriod ) | No | string | No | - | Duration |
| - [CleanHistoryPeriod](#L2GasPriceSuggester_CleanHistoryPeriod ) | No | string | No | - | Duration |
| - [CleanHistoryTimeRetention](#L2GasPriceSuggester_CleanHistoryTimeRetention ) | No | string | No | - | Duration |
| - [KafkaURL](#L2GasPriceSuggester_KafkaURL ) | No | string | No | - | - |
| - [Topic](#L2GasPriceSuggester_Topic ) | No | string | No | - | - |
| - [GroupID](#L2GasPriceSuggester_GroupID ) | No | string | No | - | - |
| - [Username](#L2GasPriceSuggester_Username ) | No | string | No | - | - |
| - [Password](#L2GasPriceSuggester_Password ) | No | string | No | - | - |
| - [RootCAPath](#L2GasPriceSuggester_RootCAPath ) | No | string | No | - | - |
| - [L1CoinId](#L2GasPriceSuggester_L1CoinId ) | No | integer | No | - | - |
| - [L2CoinId](#L2GasPriceSuggester_L2CoinId ) | No | integer | No | - | - |
| - [DefaultL1CoinPrice](#L2GasPriceSuggester_DefaultL1CoinPrice ) | No | number | No | - | DefaultL1CoinPrice is the L1 token's coin price |
| - [DefaultL2CoinPrice](#L2GasPriceSuggester_DefaultL2CoinPrice ) | No | number | No | - | DefaultL2CoinPrice is the native token's coin price |
| - [GasPriceUsdt](#L2GasPriceSuggester_GasPriceUsdt ) | No | number | No | - | - |
| - [EnableFollowerAdjustByL2L1Price](#L2GasPriceSuggester_EnableFollowerAdjustByL2L1Price ) | No | boolean | No | - | EnableFollowerAdjustByL2L1Price is dynamic adjust the factor through the L1 and L2 coins price in follower strategy |
| - [Factor](#L2GasPriceSuggester_Factor ) | No | number | No | - | - |

### <a name="L2GasPriceSuggester_Type"></a>14.1. `L2GasPriceSuggester.Type`

Expand Down Expand Up @@ -2755,7 +2758,19 @@ Password=""
RootCAPath=""
```

### <a name="L2GasPriceSuggester_L2CoinId"></a>14.17. `L2GasPriceSuggester.L2CoinId`
### <a name="L2GasPriceSuggester_L1CoinId"></a>14.17. `L2GasPriceSuggester.L1CoinId`

**Type:** : `integer`

**Default:** `0`

**Example setting the default value** (0):
```
[L2GasPriceSuggester]
L1CoinId=0
```

### <a name="L2GasPriceSuggester_L2CoinId"></a>14.18. `L2GasPriceSuggester.L2CoinId`

**Type:** : `integer`

Expand All @@ -2767,7 +2782,21 @@ RootCAPath=""
L2CoinId=0
```

### <a name="L2GasPriceSuggester_DefaultL2CoinPrice"></a>14.18. `L2GasPriceSuggester.DefaultL2CoinPrice`
### <a name="L2GasPriceSuggester_DefaultL1CoinPrice"></a>14.19. `L2GasPriceSuggester.DefaultL1CoinPrice`

**Type:** : `number`

**Default:** `0`

**Description:** DefaultL1CoinPrice is the L1 token's coin price

**Example setting the default value** (0):
```
[L2GasPriceSuggester]
DefaultL1CoinPrice=0
```

### <a name="L2GasPriceSuggester_DefaultL2CoinPrice"></a>14.20. `L2GasPriceSuggester.DefaultL2CoinPrice`

**Type:** : `number`

Expand All @@ -2781,7 +2810,7 @@ L2CoinId=0
DefaultL2CoinPrice=0
```

### <a name="L2GasPriceSuggester_GasPriceUsdt"></a>14.19. `L2GasPriceSuggester.GasPriceUsdt`
### <a name="L2GasPriceSuggester_GasPriceUsdt"></a>14.21. `L2GasPriceSuggester.GasPriceUsdt`

**Type:** : `number`

Expand All @@ -2793,7 +2822,21 @@ DefaultL2CoinPrice=0
GasPriceUsdt=0
```

### <a name="L2GasPriceSuggester_Factor"></a>14.20. `L2GasPriceSuggester.Factor`
### <a name="L2GasPriceSuggester_EnableFollowerAdjustByL2L1Price"></a>14.22. `L2GasPriceSuggester.EnableFollowerAdjustByL2L1Price`

**Type:** : `boolean`

**Default:** `false`

**Description:** EnableFollowerAdjustByL2L1Price is dynamic adjust the factor through the L1 and L2 coins price in follower strategy

**Example setting the default value** (false):
```
[L2GasPriceSuggester]
EnableFollowerAdjustByL2L1Price=false
```

### <a name="L2GasPriceSuggester_Factor"></a>14.23. `L2GasPriceSuggester.Factor`

**Type:** : `number`

Expand Down
14 changes: 14 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1109,10 +1109,19 @@
"type": "string",
"default": ""
},
"L1CoinId": {
"type": "integer",
"default": 0
},
"L2CoinId": {
"type": "integer",
"default": 0
},
"DefaultL1CoinPrice": {
"type": "number",
"description": "DefaultL1CoinPrice is the L1 token's coin price",
"default": 0
},
"DefaultL2CoinPrice": {
"type": "number",
"description": "DefaultL2CoinPrice is the native token's coin price",
Expand All @@ -1122,6 +1131,11 @@
"type": "number",
"default": 0
},
"EnableFollowerAdjustByL2L1Price": {
"type": "boolean",
"description": "EnableFollowerAdjustByL2L1Price is dynamic adjust the factor through the L1 and L2 coins price in follower strategy",
"default": false
},
"Factor": {
"type": "number",
"default": 0.15
Expand Down
6 changes: 6 additions & 0 deletions gasprice/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ type Config struct {
Username string `mapstructure:"Username"`
Password string `mapstructure:"Password"`
RootCAPath string `mapstructure:"RootCAPath"`
L1CoinId int `mapstructure:"L1CoinId"`
L2CoinId int `mapstructure:"L2CoinId"`
// DefaultL1CoinPrice is the L1 token's coin price
DefaultL1CoinPrice float64 `mapstructure:"DefaultL1CoinPrice"`
// DefaultL2CoinPrice is the native token's coin price
DefaultL2CoinPrice float64 `mapstructure:"DefaultL2CoinPrice"`
GasPriceUsdt float64 `mapstructure:"GasPriceUsdt"`

// EnableFollowerAdjustByL2L1Price is dynamic adjust the factor through the L1 and L2 coins price in follower strategy
EnableFollowerAdjustByL2L1Price bool `mapstructure:"EnableFollowerAdjustByL2L1Price"`

Factor float64 `mapstructure:"Factor"`
}
6 changes: 3 additions & 3 deletions gasprice/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

const (
// OKBWei OKB wei
OKBWei = 1e18
minOKBWei = 1e-18
OKBWei = 1e18
minCoinPrice = 1e-18
)

// FixedGasPrice struct
Expand Down Expand Up @@ -49,7 +49,7 @@ func (f *FixedGasPrice) UpdateGasPriceAvg() {
}

l2CoinPrice := f.ratePrc.GetL2CoinPrice()
if l2CoinPrice < minOKBWei {
if l2CoinPrice < minCoinPrice {
log.Warn("the L2 native coin price too small...")
return
}
Expand Down
23 changes: 19 additions & 4 deletions gasprice/follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

// FollowerGasPrice struct.
type FollowerGasPrice struct {
cfg Config
pool poolInterface
ctx context.Context
eth ethermanInterface
cfg Config
pool poolInterface
ctx context.Context
eth ethermanInterface
kafkaPrc *KafkaProcessor
}

// newFollowerGasPriceSuggester inits l2 follower gas price suggester which is based on the l1 gas price.
Expand All @@ -26,6 +27,9 @@ func newFollowerGasPriceSuggester(ctx context.Context, cfg Config, pool poolInte
ctx: ctx,
eth: ethMan,
}
if cfg.EnableFollowerAdjustByL2L1Price {
gps.kafkaPrc = newKafkaProcessor(cfg, ctx)
}
gps.UpdateGasPriceAvg()
return gps
}
Expand All @@ -44,6 +48,17 @@ func (f *FollowerGasPrice) UpdateGasPriceAvg() {
factor := big.NewFloat(0).SetFloat64(f.cfg.Factor)
res := new(big.Float).Mul(factor, big.NewFloat(0).SetInt(l1GasPrice))

// convert the eth gas price to okb gas price
if f.cfg.EnableFollowerAdjustByL2L1Price {
l1CoinPrice, l2CoinPrice := f.kafkaPrc.GetL1L2CoinPrice()
if l1CoinPrice < minCoinPrice || l2CoinPrice < minCoinPrice {
log.Warn("the L1 or L2 native coin price too small...")
return
}
res = new(big.Float).Mul(big.NewFloat(0).SetFloat64(l1CoinPrice/l2CoinPrice), res)
log.Debug("L2 pre gas price value: ", res.String(), ". L1 coin price: ", l1CoinPrice, ". L2 coin price: ", l2CoinPrice)
}

// Store l2 gasPrice calculated
result := new(big.Int)
res.Int(result)
Expand Down
Loading

0 comments on commit 6b55630

Please sign in to comment.