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 relay all feature #245

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

25 changes: 19 additions & 6 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2412,12 +2412,13 @@ ApiKeys=[]
**Type:** : `object`
**Description:** ApiRelay defines the relay configuration for the API

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ----------------------------------- | ------- | --------------- | ---------- | ---------- | ----------------- |
| - [Enabled](#RPC_ApiRelay_Enabled ) | No | boolean | No | - | - |
| - [DestURI](#RPC_ApiRelay_DestURI ) | No | string | No | - | - |
| - [RPCs](#RPC_ApiRelay_RPCs ) | No | array of string | No | - | - |
| - [Rerun](#RPC_ApiRelay_Rerun ) | No | boolean | No | - | - |
| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ------------------------------------- | ------- | --------------- | ---------- | ---------- | ----------------- |
| - [Enabled](#RPC_ApiRelay_Enabled ) | No | boolean | No | - | - |
| - [DestURI](#RPC_ApiRelay_DestURI ) | No | string | No | - | - |
| - [RPCs](#RPC_ApiRelay_RPCs ) | No | array of string | No | - | - |
| - [Rerun](#RPC_ApiRelay_Rerun ) | No | boolean | No | - | - |
| - [RelayAll](#RPC_ApiRelay_RelayAll ) | No | boolean | No | - | - |

#### <a name="RPC_ApiRelay_Enabled"></a>8.30.1. `RPC.ApiRelay.Enabled`

Expand Down Expand Up @@ -2467,6 +2468,18 @@ RPCs=[]
Rerun=false
```

#### <a name="RPC_ApiRelay_RelayAll"></a>8.30.5. `RPC.ApiRelay.RelayAll`

**Type:** : `boolean`

**Default:** `false`

**Example setting the default value** (false):
```
[RPC.ApiRelay]
RelayAll=false
```

## <a name="Synchronizer"></a>9. `[Synchronizer]`

**Type:** : `object`
Expand Down
4 changes: 4 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,10 @@
"Rerun": {
"type": "boolean",
"default": false
},
"RelayAll": {
"type": "boolean",
"default": false
}
},
"additionalProperties": false,
Expand Down
35 changes: 29 additions & 6 deletions jsonrpc/api_relay_xlayer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jsonrpc

import (
"time"

"github.com/0xPolygonHermez/zkevm-node/jsonrpc/client"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/metrics"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
Expand All @@ -9,13 +11,14 @@ import (

// ApiRelayConfig is the api relay config
type ApiRelayConfig struct {
Enabled bool `mapstructure:"Enabled"`
DestURI string `mapstructure:"DestURI"`
RPCs []string `mapstructure:"RPCs"`
Rerun bool `mapstructure:"Rerun"`
Enabled bool `mapstructure:"Enabled"`
DestURI string `mapstructure:"DestURI"`
RPCs []string `mapstructure:"RPCs"`
Rerun bool `mapstructure:"Rerun"`
RelayAll bool `mapstructure:"RelayAll"`
}

func shouldRelay(localCfg ApiRelayConfig, name string) bool {
func shouldRelayByMethod(localCfg ApiRelayConfig, name string) bool {
enable := localCfg.Enabled && localCfg.DestURI != ""
contained := types.Contains(localCfg.RPCs, name)
if getApolloConfig().Enable() {
Expand Down Expand Up @@ -50,17 +53,37 @@ func getRelayDestURI(localDestURI string) string {
}

func tryRelay(localCfg ApiRelayConfig, request types.Request) (types.Response, bool) {
if shouldRelay(localCfg, request.Method) && pass(&request) {
if shouldRelay(localCfg, &request) {
destURI := getRelayDestURI(localCfg.DestURI)
ts := time.Now()
res, err := client.JSONRPCRelay(destURI, request, shouldRerun(localCfg))
if err != nil {
log.Errorf("failed to relay %v to %s: %v", request.Method, destURI, err)
metrics.RequestRelayFailCount(request.Method)
return types.Response{}, false
}
log.Infof("relayed %v to %s in %v", request.Method, destURI, time.Since(ts))

return res, true
}

return types.Response{}, false
}

func shouldRelay(localCfg ApiRelayConfig, request *types.Request) bool {
enable := localCfg.Enabled &&
localCfg.DestURI != ""
relayAll := localCfg.RelayAll
if getApolloConfig().Enable() {
getApolloConfig().RLock()
defer getApolloConfig().RUnlock()
enable = getApolloConfig().ApiRelay.Enabled &&
getApolloConfig().ApiRelay.DestURI != ""
relayAll = getApolloConfig().ApiRelay.RelayAll
}
if enable && relayAll {
return true
}

return shouldRelayByMethod(localCfg, request.Method) && pass(request)
}
1 change: 1 addition & 0 deletions jsonrpc/apollo_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (c *ApolloConfig) setApiRelayCfg(apiRelayCfg ApiRelayConfig) {
c.ApiRelay.DestURI = apiRelayCfg.DestURI
c.ApiRelay.RPCs = make([]string, len(apiRelayCfg.RPCs))
c.ApiRelay.Rerun = apiRelayCfg.Rerun
c.ApiRelay.RelayAll = apiRelayCfg.RelayAll
copy(c.ApiRelay.RPCs, apiRelayCfg.RPCs)
}

Expand Down
4 changes: 3 additions & 1 deletion pool/pool_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"sync"
"time"

"github.com/ethereum/go-ethereum/common"

"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/common"
)

const (
Expand Down Expand Up @@ -134,6 +135,7 @@ func (p *Pool) checkFreeGp(ctx context.Context, poolTx Transaction, from common.
fromToName, freeGpList := GetSpecialFreeGasList(p.cfg.FreeGasList)
info := freeGpList[fromToName[from.String()]]
if info != nil &&
poolTx.To() != nil &&
Contains(info.ToList, *poolTx.To()) &&
ContainsMethod("0x"+common.Bytes2Hex(poolTx.Data()), info.MethodSigs) {
return true, nil
Expand Down
Loading