-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fast endpoint implementation
- Loading branch information
Showing
11 changed files
with
267 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package webfile | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
var ErrRequest = fmt.Errorf("request failed") | ||
|
||
//https://raw.githubusercontent.com/flashbots/dowg/main/builder-registrations.json | ||
|
||
type Fetcher struct { | ||
url string | ||
cl http.Client | ||
} | ||
|
||
func NewFetcher(url string) *Fetcher { | ||
return &Fetcher{url: url, cl: http.Client{}} | ||
} | ||
|
||
func (f *Fetcher) Fetch(ctx context.Context) ([]byte, error) { | ||
//execute http request and load bytes | ||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, f.url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := f.cl.Do(httpReq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bts, err := io.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode >= 400 { | ||
return nil, fmt.Errorf("err: %w status code %d", ErrRequest, resp.StatusCode) | ||
} | ||
return bts, nil | ||
} |
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,21 @@ | ||
package webfile | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestFetch(t *testing.T) { | ||
f := Fetcher{ | ||
url: "https://raw.githubusercontent.com/flashbots/dowg/main/builder-registrations.json", | ||
cl: http.Client{}, | ||
} | ||
bts, err := f.Fetch(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(string(bts)) | ||
} |
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,76 @@ | ||
package application | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"strings" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
type BuilderInfo struct { | ||
Name string `json:"name"` | ||
RPC string `json:"rpc"` | ||
SupportedApis []string `json:"supported-apis"` | ||
} | ||
type Fetcher interface { | ||
Fetch(ctx context.Context) ([]byte, error) | ||
} | ||
type BuilderInfoService struct { | ||
fetcher Fetcher | ||
builderInfos []BuilderInfo | ||
} | ||
|
||
func StartBuilderInfoService(ctx context.Context, fetcher Fetcher, fetchInterval time.Duration) (*BuilderInfoService, error) { | ||
bis := BuilderInfoService{ | ||
fetcher: fetcher, | ||
} | ||
if fetcher != nil { | ||
err := bis.fetchBuilderInfo(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
go bis.syncLoop(fetchInterval) | ||
|
||
} | ||
return &bis, nil | ||
} | ||
func (bis *BuilderInfoService) Builders() []BuilderInfo { | ||
return bis.builderInfos | ||
} | ||
|
||
func (bis *BuilderInfoService) BuilderNames() []string { | ||
var names = make([]string, 0, len(bis.builderInfos)) | ||
for _, builderInfo := range bis.builderInfos { | ||
names = append(names, strings.ToLower(builderInfo.Name)) | ||
} | ||
return names | ||
} | ||
|
||
func (bis *BuilderInfoService) syncLoop(fetchInterval time.Duration) { | ||
ticker := time.NewTicker(fetchInterval) | ||
for range ticker.C { | ||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
err := bis.fetchBuilderInfo(ctx) | ||
if err != nil { | ||
//TODO: probably panic on multiple consequent errors, though it's not critical in nature | ||
log.Error("failed to fetch builder info", "err", err) | ||
} | ||
cancel() | ||
} | ||
} | ||
|
||
func (bis *BuilderInfoService) fetchBuilderInfo(ctx context.Context) error { | ||
bts, err := bis.fetcher.Fetch(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
var builderInfos []BuilderInfo | ||
err = json.Unmarshal(bts, &builderInfos) | ||
if err != nil { | ||
return err | ||
} | ||
bis.builderInfos = builderInfos | ||
return nil | ||
} |
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
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
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
Oops, something went wrong.