Skip to content

Commit

Permalink
feat: v0.0.0-alpha.0
Browse files Browse the repository at this point in the history
  • Loading branch information
notwedtm committed Dec 31, 2023
0 parents commit c469fd1
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Trustless Engineering Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SOL Shotty

![](./docs/sol-shotty.png)

SOL Shotty is a Solana RPC proxy with a little something "extra".

When running sol-shotty, it will take any RPC request and "shotgun blast" them at every RPC provider configured, only returning the result from the first to respond.

## Why would I want this?

When you need to ensure your transaction goes through *no matter what*, you can send the same request to many different providers and let the validators sort out which one got there first.

All RPC providers have bad days, sol-shotty helps get faster responses when a single RPC provider is degraded, or even fully down. No reconfiguration required!

# Quick Start

Edit the `config.yaml` file to include the endpoints you wish to use. Some providers use Cloudflare to provide anycast routing when they only have infrastructure in a few locations.

A lot of providers allow you to sign up with just a web3 wallet, if you have multiple you might be able to sign up multiple times.

Make sure you have `golang` installed and then run `go run ./cmd/sol-shotty`

sol-shotty will now be listening on `http://127.0.0.1:420` and can be used anywhere an RPC URL is accepted.

# Development

This tool was a quick rip of some internal tooling we've built to increase reliability in [solan.ai](https://solan.ai) RPC requests.

While we will update it from time to time when we can, we *highly encourage* PR's if you see a bug or would like additional functionality that isn't here.

If you'd like a feature that isn't here, donations greatly encourage us to prioritize those requests.

Donations are accepted at `23Q5e33JnmKWACqmmYW1owRwfs7ToD4SuCTzfDGxMcfA`.
58 changes: 58 additions & 0 deletions cmd/sol-shotty/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"net/http"

"github.com/trustless-engineering/sol-shotty/pkg"
"github.com/trustless-engineering/sol-shotty/pkg/utils"
)

func proxy(w http.ResponseWriter, req *http.Request) {
endpoints, err := utils.LoadEndpoints()

if err != nil {
fmt.Printf("Error loading endpoints: %v\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

// Shotgun the request to all endpoints
response, err := pkg.Shotgun(endpoints, req)
if err != nil {
fmt.Printf("Error shotgunning request: %v\n", err)
http.Error(w, "Bad Gateway", http.StatusBadGateway)
return
}

w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, solana-client")

// Copy the response headers back to the client
for key, values := range response.Result.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}

// Add CORS headers
w.Header().Set("Access-Control-Allow-Origin", "*")

w.WriteHeader(response.Result.StatusCode)

// Write the response body to the client's response writer
_, err = w.Write(response.Body)
if err != nil {
fmt.Printf("Error writing response: %v\n", err)
return
}

fmt.Printf("Successful response from %s in %dms\n", response.Endpoint, response.RTT)
}

func main() {
fmt.Printf("Loading the shotty...\n")
http.HandleFunc("/", proxy)

http.ListenAndServe(":420", nil)
}
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
endpoints:
- https://api.mainnet-beta.solana.com
use_cluster_nodes: false
Binary file added docs/sol-shotty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/trustless-engineering/sol-shotty

go 1.21.1

require gopkg.in/yaml.v2 v2.4.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
81 changes: 81 additions & 0 deletions pkg/shotgun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package pkg

import (
"encoding/json"
"io"
"net/http"
"time"
)

type SuccessResponse struct {
Result *http.Response `json:"result"`
Endpoint string `json:"endpoint"`
RTT int `json:"rtt"`
Body []byte `json:"-"`
}

func Shotgun(endpoints []string, mainRequest *http.Request) (SuccessResponse, error) {
successCh := make(chan SuccessResponse, 1) // Make successCh buffered with capacity 1

for _, endpoint := range endpoints {
go makeRequest(endpoint, mainRequest, successCh)
}

successValue := <-successCh
return successValue, nil
}

func makeRequest(endpoint string, req *http.Request, successCh chan SuccessResponse) {
// Create a new request with the same method and body as the original request
newReq, err := http.NewRequest(req.Method, endpoint, req.Body)
if err != nil {
return
}

// Copy headers from the original request to the new request
newReq.Header = make(http.Header)
for key, values := range req.Header {
newReq.Header[key] = values
}

// Send request
client := &http.Client{}

startTime := time.Now()
resp, err := client.Do(newReq)
endTime := time.Now()

if err != nil {
return
}

defer resp.Body.Close()

// Check for a successful response (status code 2xx)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}

var jsonData map[string]interface{}
err = json.Unmarshal(body, &jsonData)
if err != nil {
return
}

// Reject responses with an "error" key
if _, ok := jsonData["error"]; ok {
return
}

successCh <- SuccessResponse{
Result: resp,
Endpoint: endpoint,
RTT: int(endTime.Sub(startTime).Milliseconds()),
Body: body,
}

return
}
}
29 changes: 29 additions & 0 deletions pkg/utils/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package utils

import (
"os"

"gopkg.in/yaml.v2"
)

type Config struct {
Endpoints []string `yaml:"endpoints"`
UseClusterNodes bool `yaml:"use_cluster_nodes"`
}

func LoadConfig() (*Config, error) {
// Read the YAML file
yamlFile, err := os.ReadFile("config.yaml")
if err != nil {
return nil, err
}

// Parse the YAML file into the Config struct
var config Config
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
return nil, err
}

return &config, nil
}
90 changes: 90 additions & 0 deletions pkg/utils/endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package utils

import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)

type ClusterMember struct {
Pubkey string `json:"pubkey"`
Gossip string `json:"gossip"`
RPC string `json:"rpc"`
TPU string `json:"tpu"`
Version string `json:"version"`
}

func GetClusterEndpoints() ([]string, error) {
endpoint := "https://api.mainnet-beta.solana.com"
jsonBody := []byte(`{ "jsonrpc": "2.0", "id": 1, "method": "getClusterNodes"}`)

bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, endpoint, bodyReader)

if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)

if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status: %s", resp.Status)
}

var response map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&response)

if err != nil {
return nil, err
}

result, ok := response["result"].([]interface{})

if !ok {
return nil, fmt.Errorf("unexpected response structure")
}

var clusterEndpoints []string
for _, rawMember := range result {
member, ok := rawMember.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unexpected entity structure")
}
rpc, ok := member["rpc"].(string)
if !ok {
continue
}
clusterEndpoints = append(clusterEndpoints, "http://"+rpc)
}

return clusterEndpoints, nil
}

func LoadEndpoints() ([]string, error) {
config, err := LoadConfig()
if err != nil {
return nil, err
}

endpoints := config.Endpoints

if config.UseClusterNodes {
clusterEndpoints, err := GetClusterEndpoints()
if err != nil {
log.Print("error loading cluster endpoints")
}

endpoints = append(endpoints, clusterEndpoints...)
}
return endpoints, nil
}

0 comments on commit c469fd1

Please sign in to comment.