Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pending stat #187

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

97 changes: 97 additions & 0 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ SecretKey=""
| - [FreeGasAddress](#Pool_FreeGasAddress ) | No | array of string | No | - | XLayer config<br />FreeGasAddress is the default free gas address |
| - [FreeClaimGasLimit](#Pool_FreeClaimGasLimit ) | No | integer | No | - | FreeClaimGasLimit is the max gas allowed use to do a free claim |
| - [BridgeClaimMethodSigs](#Pool_BridgeClaimMethodSigs ) | No | array of string | No | - | BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method |
| - [PendingStat](#Pool_PendingStat ) | No | object | No | - | PendingStat is the configuration for the pending statistics |

### <a name="Pool_IntervalToRefreshBlockedAddresses"></a>7.1. `Pool.IntervalToRefreshBlockedAddresses`

Expand Down Expand Up @@ -1248,6 +1249,102 @@ FreeClaimGasLimit=150000
**Type:** : `array of string`
**Description:** BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method

### <a name="Pool_PendingStat"></a>7.18. `[Pool.PendingStat]`

**Type:** : `object`
**Description:** PendingStat is the configuration for the pending statistics

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| --------------------------------------------------- | ------- | ------- | ---------- | ---------- | ----------------- |
| - [Enable](#Pool_PendingStat_Enable ) | No | boolean | No | - | - |
| - [Interval](#Pool_PendingStat_Interval ) | No | string | No | - | Duration |
| - [StaleInterval](#Pool_PendingStat_StaleInterval ) | No | string | No | - | Duration |
| - [CacheInternal](#Pool_PendingStat_CacheInternal ) | No | string | No | - | Duration |

#### <a name="Pool_PendingStat_Enable"></a>7.18.1. `Pool.PendingStat.Enable`

**Type:** : `boolean`

**Default:** `false`

**Example setting the default value** (false):
```
[Pool.PendingStat]
Enable=false
```

#### <a name="Pool_PendingStat_Interval"></a>7.18.2. `Pool.PendingStat.Interval`

**Title:** Duration

**Type:** : `string`

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

**Examples:**

```json
"1m"
```

```json
"300ms"
```

**Example setting the default value** ("0s"):
```
[Pool.PendingStat]
Interval="0s"
```

#### <a name="Pool_PendingStat_StaleInterval"></a>7.18.3. `Pool.PendingStat.StaleInterval`

**Title:** Duration

**Type:** : `string`

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

**Examples:**

```json
"1m"
```

```json
"300ms"
```

**Example setting the default value** ("0s"):
```
[Pool.PendingStat]
StaleInterval="0s"
```

#### <a name="Pool_PendingStat_CacheInternal"></a>7.18.4. `Pool.PendingStat.CacheInternal`

**Title:** Duration

**Type:** : `string`

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

**Examples:**

```json
"1m"
```

```json
"300ms"
```

**Example setting the default value** ("0s"):
```
[Pool.PendingStat]
CacheInternal="0s"
```

## <a name="RPC"></a>8. `[RPC]`

**Type:** : `object`
Expand Down
38 changes: 38 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,44 @@
},
"type": "array",
"description": "BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method"
},
"PendingStat": {
"properties": {
"Enable": {
"type": "boolean",
"default": false
},
"Interval": {
"type": "string",
"title": "Duration",
"default": "0s",
"examples": [
"1m",
"300ms"
]
},
"StaleInterval": {
"type": "string",
"title": "Duration",
"default": "0s",
"examples": [
"1m",
"300ms"
]
},
"CacheInternal": {
"type": "string",
"title": "Duration",
"default": "0s",
"examples": [
"1m",
"300ms"
]
}
},
"additionalProperties": false,
"type": "object",
"description": "PendingStat is the configuration for the pending statistics"
}
},
"additionalProperties": false,
Expand Down
18 changes: 15 additions & 3 deletions jsonrpc/dynamic_gas_price_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
zktypes "github.com/0xPolygonHermez/zkevm-node/config/types"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/metrics"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/ethereum/go-ethereum/core/types"
)

Expand Down Expand Up @@ -212,9 +213,20 @@ func (e *EthEndpoints) getL2BatchTxsTips(ctx context.Context, l2BlockNumber uint
}

func (e *EthEndpoints) isCongested(ctx context.Context) (bool, error) {
txCount, err := e.pool.CountPendingTransactions(ctx)
if err != nil {
return false, err
var txCount uint64
if e.pool != nil && e.pool.IsPendingStatEnabled(ctx) {
stat := pool.GetPendingStat()
if stat.Total < stat.SkipNonce+stat.BalanceIssue+stat.ErrorNonce {
txCount = 0
} else {
txCount = stat.Total - stat.SkipNonce - stat.BalanceIssue - stat.ErrorNonce
}
} else {
cnt, err := e.pool.CountPendingTransactions(ctx)
if err != nil {
return false, err
}
txCount = cnt
}
if txCount >= e.cfg.DynamicGP.CongestionTxThreshold {
return true, nil
Expand Down
9 changes: 9 additions & 0 deletions jsonrpc/endpoints_eth_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/metrics"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -397,3 +398,11 @@ func (e *EthEndpoints) getMinPriceFromSequencerNode() (interface{}, types.Error)
}
return gasPrice, nil
}

// GetPendingStat returns the pending stat
func (e *EthEndpoints) GetPendingStat() (interface{}, types.Error) {
if e.isDisabled("eth_getPendingStat") || (e.pool != nil && !e.pool.IsPendingStatEnabled(context.Background())) {
return RPCErrorResponse(types.DefaultErrorCode, "not supported yet", nil, true)
}
return pool.GetPendingStat(), nil
}
23 changes: 23 additions & 0 deletions jsonrpc/mocks/mock_pool_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,26 @@ func (_m *PoolMock) GetMinSuggestedGasPriceWithDelta(ctx context.Context, delta

return r0, r1
}

// IsPendingStatEnabled provides a mock function with given fields: ctx
func (_m *PoolMock) IsPendingStatEnabled(ctx context.Context) bool {
ret := _m.Called(ctx)

if len(ret) == 0 {
panic("no return value specified for IsPendingStatEnabled")
}

var r0 bool
if rf, ok := ret.Get(0).(func(context.Context) bool); ok {
return rf(ctx)
}
if rf, ok := ret.Get(0).(func(context.Context) bool); ok {
r0 = rf(ctx)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(bool)
}
}

return r0
}
1 change: 1 addition & 0 deletions jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type PoolInterface interface {
AddInnerTx(ctx context.Context, txHash common.Hash, innerTx []byte) error
GetInnerTx(ctx context.Context, txHash common.Hash) (string, error)
GetMinSuggestedGasPriceWithDelta(ctx context.Context, delta time.Duration) (uint64, error)
IsPendingStatEnabled(ctx context.Context) bool
}

// StateInterface gathers the methods required to interact with the state.
Expand Down
13 changes: 13 additions & 0 deletions pool/apollo_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type apolloConfig struct {
AccountQueue uint64
EnableWhitelist bool
BridgeClaimMethods []string
EnablePendingStat bool

sync.RWMutex
}
Expand Down Expand Up @@ -56,6 +57,7 @@ func (c *apolloConfig) setBridgeClaimMethods(bridgeClaimMethods []string) {
// AccountQueue
// FreeGasAddress
// EnableWhitelist
// EnablePendingStat
func UpdateConfig(apolloConfig Config) {
getApolloConfig().Lock()
getApolloConfig().EnableApollo = true
Expand All @@ -64,6 +66,7 @@ func UpdateConfig(apolloConfig Config) {
getApolloConfig().setFreeGasAddresses(apolloConfig.FreeGasAddress)
getApolloConfig().EnableWhitelist = apolloConfig.EnableWhitelist
getApolloConfig().setBridgeClaimMethods(apolloConfig.BridgeClaimMethodSigs)
getApolloConfig().EnablePendingStat = apolloConfig.PendingStat.Enable
getApolloConfig().Unlock()
}

Expand Down Expand Up @@ -122,3 +125,13 @@ func getEnableWhitelist(enableWhitelist bool) bool {

return enableWhitelist
}

func getEnablePendingStat(enablePendingStat bool) bool {
if getApolloConfig().enable() {
getApolloConfig().RLock()
defer getApolloConfig().RUnlock()
return getApolloConfig().EnablePendingStat
}

return enablePendingStat
}
3 changes: 3 additions & 0 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type Config struct {
FreeClaimGasLimit uint64 `mapstructure:"FreeClaimGasLimit"`
// BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method
BridgeClaimMethodSigs []string `mapstructure:"BridgeClaimMethodSigs"`

// PendingStat is the configuration for the pending statistics
PendingStat PendingStatCfg `mapstructure:"PendingStat"`
}

// EffectiveGasPriceCfg contains the configuration properties for the effective gas price
Expand Down
6 changes: 6 additions & 0 deletions pool/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type storage interface {
GetEarliestProcessedTx(ctx context.Context) (common.Hash, error)
AddInnerTx(ctx context.Context, txHash common.Hash, innerTx []byte) error
GetInnerTx(ctx context.Context, txHash common.Hash) (string, error)
GetPendingFromAndMinNonceBefore(ctx context.Context, timeDuration time.Duration) ([]common.Address, []uint64, error)
LockStat(ctx context.Context, timeDuration time.Duration) (bool, error)
UnLockStat(ctx context.Context) error
UpdateStatAndUnlock(ctx context.Context, totoal, skip, balanceIssue, nonceIssue uint64) error
GetStat(ctx context.Context) (uint64, uint64, uint64, uint64, error)
CountTransactionsByFromStatusAndNonce(ctx context.Context, from common.Address, nonce uint64, status ...TxStatus) (uint64, error)
}

type stateInterface interface {
Expand Down
Loading
Loading