-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi-Chain RPC Proxy w/ Retries (#136)
Co-authored-by: Nero <trajan0x@users.noreply.github.com>
- Loading branch information
Showing
33 changed files
with
3,615 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ use ( | |
./core | ||
./ethergo | ||
./scribe | ||
./services/omnirpc | ||
./tools | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../make/go.Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
Oops, something went wrong.