Skip to content

Commit

Permalink
Relay add ban filter (#215)
Browse files Browse the repository at this point in the history
* add filter for relay

* add pass

* check perstateTracer

* add interface check
  • Loading branch information
giskook authored Jun 5, 2024
1 parent ecd6a35 commit 3ff1237
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
68 changes: 68 additions & 0 deletions jsonrpc/api_relay_filter_xlayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package jsonrpc

import (
"encoding/json"

"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
)

type ban func(request *types.Request) bool
type banList []ban

var banListInst banList

func init() {
banListInst.register(debugTraceTransactionNotPrestateTracer)
}

func pass(request *types.Request) bool {
return !banListInst.ban(request)
}

func (c *banList) register(handler ban) {
*c = append(*c, handler)
}

func (c *banList) ban(request *types.Request) bool {
for _, handler := range *c {
if handler(request) {
return true
}
}

return false
}

// debutTraceTransactionNotPrestateTracer checks if the request is a debug_traceTransaction and the tracer is not preStateTracer
func debugTraceTransactionNotPrestateTracer(req *types.Request) bool {
if req == nil || req.Method != "debug_traceTransaction" {
return false
}

// check params passed by request match function params
var testStruct []interface{}
if err := json.Unmarshal(req.Params, &testStruct); err != nil {
return false
}
inputs := make([]interface{}, len(testStruct))
if err := json.Unmarshal(req.Params, &inputs); err != nil {
return false
}

// for debug_tranceTransaction, at least 2 param is required
if len(inputs) < 2 { // nolint:gomnd
return false
}
traceCfgMap, ok := inputs[1].(map[string]interface{})
if !ok {
return false
}
if traceCfgMap["tracer"] == nil {
return false
}
if tracer, ok := traceCfgMap["tracer"].(string); ok && tracer != "prestateTracer" {
return true
}

return false
}
2 changes: 1 addition & 1 deletion jsonrpc/api_relay_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func getRelayDestURI(localDestURI string) string {
}

func tryRelay(localCfg ApiRelayConfig, request types.Request) (types.Response, bool) {
if shouldRelay(localCfg, request.Method) {
if shouldRelay(localCfg, request.Method) && pass(&request) {
destURI := getRelayDestURI(localCfg.DestURI)
res, err := client.JSONRPCRelay(destURI, request)
if err != nil {
Expand Down

0 comments on commit 3ff1237

Please sign in to comment.