Skip to content

Commit

Permalink
Multi-Chain RPC Proxy w/ Retries (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: Nero <trajan0x@users.noreply.github.com>
  • Loading branch information
trajan0x and trajan0x authored Aug 26, 2022
1 parent 2fcf58b commit 9ee1191
Show file tree
Hide file tree
Showing 33 changed files with 3,615 additions and 298 deletions.
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:
tools: 'tools/**'
core: 'core/**'
ethergo: 'ethergo/**'
services/omnirpc: 'services/omnirpc/**'
- name: Check For Solidity Changes
id: filter_solidity
uses: tj-actions/changed-files@v26.1
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ use (
./core
./ethergo
./scribe
./services/omnirpc
./tools
)
1 change: 1 addition & 0 deletions services/omnirpc/Makefile
37 changes: 37 additions & 0 deletions services/omnirpc/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"fmt"
"github.com/synapsecns/sanguine/serivces/omnirpc/rpcmap"
"gopkg.in/yaml.v2"
"os"
)

// UnmarshallConfig unmarshalls an rpc config from an input.
func UnmarshallConfig(input string) (*rpcmap.RPCMap, error) {
var rawMap map[int][]string
err := yaml.Unmarshal([]byte(input), &rawMap)
if err != nil {
return nil, fmt.Errorf("could not unmarshall rpc map: %w", err)
}
return rpcmap.NewRPCMapFromMap(rawMap), nil
}

// UnmarshallConfigFromFile gets a config from a file.
func UnmarshallConfigFromFile(file string) (*rpcmap.RPCMap, error) {
//nolint: gosec
contents, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("could not read file: %w", err)
}

return UnmarshallConfig(string(contents))
}

// MarshallFromMap marshalls a config from an rpc map.
func MarshallFromMap(rpcMap *rpcmap.RPCMap) string {
// errors are impossible here
output, _ := yaml.Marshal(rpcMap.RawMap())

return string(output)
}
40 changes: 40 additions & 0 deletions services/omnirpc/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config_test

import (
"github.com/Flaque/filet"
. "github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/serivces/omnirpc/config"
"golang.org/x/exp/slices"
"testing"
)

func TestUnmarshallMarshall(t *testing.T) {
rpcMap, err := config.UnmarshallConfig(testYaml)
Nil(t, err)

True(t, slices.Contains(rpcMap.ChainID(1), "https://1.com"))
True(t, slices.Contains(rpcMap.ChainID(1), "https://1.test.com"))
True(t, slices.Contains(rpcMap.ChainID(2), "https://2.test.com"))

newMap, err := config.UnmarshallConfig(config.MarshallFromMap(rpcMap))
Nil(t, err)

Equal(t, rpcMap.RawMap(), newMap.RawMap())
}

func TestFileUnmarshall(t *testing.T) {
resConfig := filet.TmpFile(t, "", testYaml)
rpcMap, err := config.UnmarshallConfigFromFile(resConfig.Name())
Nil(t, err)

True(t, slices.Contains(rpcMap.ChainID(2), "https://2.com"))
}

const testYaml = `
---
1:
- "https://1.com"
- "https://1.test.com"
2:
- "https://2.com"
- "https://2.test.com"`
2 changes: 2 additions & 0 deletions services/omnirpc/config/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package config allows you to pass in a custom config for which rpcs to hit
package config
6 changes: 3 additions & 3 deletions tools/rpc/display.go → services/omnirpc/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"fmt"
"github.com/jftuga/ellipsis"
"github.com/olekukonko/tablewriter"
"github.com/synapsecns/sanguine/tools/rpc/internal"
"github.com/synapsecns/sanguine/serivces/omnirpc/latency"
"os"
"sort"
)

// DisplayLatency displays latency results in a cli.
func DisplayLatency(lat []internal.LatencyResult) {
func DisplayLatency(lat []latency.Result) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"URL", "Latency", "HasError (if any)"})

sort.Slice(lat, func(i, j int) bool {
return lat[i].Latency > lat[j].Latency
return lat[i].Latency < lat[j].Latency
})

for _, latencyResult := range lat {
Expand Down
40 changes: 40 additions & 0 deletions services/omnirpc/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"github.com/phayes/freeport"
"github.com/urfave/cli/v2"
"os"
"path/filepath"
)

var chainIDFlag = &cli.IntFlag{
Name: "chain-id",
Usage: "Chain id you'd like to select an rpc for",
Required: true,
}

var portFlag = &cli.IntFlag{
Name: "port",
Usage: "port to run the omniproxy on",
// default to an open port
Value: freeport.GetPort(),
}

// set the default dir to the users home path/omnirpc.yaml.
func init() {
// user must set manually if this errors anyway
homeDir, _ := os.UserHomeDir()
defaultConfig := filepath.Join(homeDir, "omnirpc.yaml")
outputFlag.Value = defaultConfig
configFlag.Value = defaultConfig
}

var outputFlag = &cli.StringFlag{
Name: "output",
Usage: "path to output the new config file",
}

var configFlag = &cli.StringFlag{
Name: "config",
Usage: "path to output the new config file",
}
Loading

0 comments on commit 9ee1191

Please sign in to comment.