From c2ff4a510b5fbe52dc432421d83ed0e2363f7d80 Mon Sep 17 00:00:00 2001 From: svin Date: Thu, 14 Mar 2024 20:26:56 +0200 Subject: [PATCH] Create parity_rpc client that replace `input` -> `data` field --- relay/internal/networks/amb/amb.go | 3 +- .../parity/parity_rpc/parity_rpc.go | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 relay/pkg/ethclients/parity/parity_rpc/parity_rpc.go diff --git a/relay/internal/networks/amb/amb.go b/relay/internal/networks/amb/amb.go index 8d86e7e5..23da6da4 100644 --- a/relay/internal/networks/amb/amb.go +++ b/relay/internal/networks/amb/amb.go @@ -10,6 +10,7 @@ import ( nc "github.com/ambrosus/ambrosus-bridge/relay/internal/networks/common" "github.com/ambrosus/ambrosus-bridge/relay/internal/networks/events" "github.com/ambrosus/ambrosus-bridge/relay/pkg/ethclients/parity" + "github.com/ambrosus/ambrosus-bridge/relay/pkg/ethclients/parity/parity_rpc" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rpc" "github.com/rs/zerolog" @@ -34,7 +35,7 @@ func New(cfg *config.Network, sideBridgeName string, eventsApi events.Events, ba // /////////////////// origin := nc.GetAmbrosusOrigin() - rpcHTTPClient, err := rpc.DialHTTP(cfg.HttpURL) + rpcHTTPClient, err := parity_rpc.DialHTTP(cfg.HttpURL) if err != nil { return nil, fmt.Errorf("dial http: %w", err) } diff --git a/relay/pkg/ethclients/parity/parity_rpc/parity_rpc.go b/relay/pkg/ethclients/parity/parity_rpc/parity_rpc.go new file mode 100644 index 00000000..a8bb90fd --- /dev/null +++ b/relay/pkg/ethclients/parity/parity_rpc/parity_rpc.go @@ -0,0 +1,39 @@ +package parity_rpc + +import ( + "bytes" + "io" + "net/http" + "time" + + "github.com/ethereum/go-ethereum/rpc" +) + +// DialHTTP creates a new RPC client that connects to an RPC server over HTTP with default rate limiter options. +func DialHTTP(endpoint string) (*rpc.Client, error) { + httpClient := new(http.Client) + httpClient.Transport = &ReplaceInputToDataTransporter{roundTripperWrap: http.DefaultTransport} + return rpc.DialHTTPWithClient(endpoint, httpClient) +} + +// DialHTTPWithLimitOptions creates a new RPC client that connects to an RPC server over HTTP with specified rate limiter options. +func DialHTTPWithLimitOptions(endpoint string, limitPeriod time.Duration, requestCount int) (*rpc.Client, error) { + httpClient := new(http.Client) + httpClient.Transport = &ReplaceInputToDataTransporter{roundTripperWrap: http.DefaultTransport} + return rpc.DialHTTPWithClient(endpoint, httpClient) +} + +type ReplaceInputToDataTransporter struct { + roundTripperWrap http.RoundTripper +} + +func (c *ReplaceInputToDataTransporter) RoundTrip(r *http.Request) (*http.Response, error) { + bodyBytes, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + bodyBytes = bytes.Replace(bodyBytes, []byte(`"input"`), []byte(`"data"`), -1) + r.Body = io.NopCloser(bytes.NewReader(bodyBytes)) + r.ContentLength = int64(len(bodyBytes)) + return c.roundTripperWrap.RoundTrip(r) +}