Skip to content

Commit

Permalink
Check status code on fetching snapshot hashes (#321)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: alex.sharov <[email protected]>
  • Loading branch information
awskii and AskAlexSharov authored Oct 23, 2024
1 parent ce27540 commit f64407a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ on:

jobs:
tests:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.23'

- run: go test ./...

- run: go install github.com/pelletier/go-toml/v2/cmd/tomll@latest

- run: tomll amoy.toml
Expand All @@ -32,3 +35,4 @@ jobs:
- run: tomll webseed/gnosis.toml
- run: tomll webseed/mainnet.toml
- run: tomll webseed/sepolia.toml

47 changes: 27 additions & 20 deletions embed.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snapshothashes

import (
"context"
_ "embed"
"fmt"
"io"
Expand Down Expand Up @@ -45,7 +46,7 @@ func getURLByChain(chain string) string {
return fmt.Sprintf("https://raw.githubusercontent.com/erigontech/erigon-snapshot/%s/%s.toml", branchReference, chain)
}

func LoadSnapshots() (couldFetch bool) {
func LoadSnapshots(ctx context.Context) (fetched bool, err error) {
var (
mainnetUrl = getURLByChain("mainnet")
sepoliaUrl = getURLByChain("sepolia")
Expand All @@ -56,60 +57,66 @@ func LoadSnapshots() (couldFetch bool) {
holeskyUrl = getURLByChain("holesky")
)
var hashes []byte
var err error
// Try to fetch the latest snapshot hashes from the web
if hashes, err = fetchSnapshotHashes(mainnetUrl); err != nil {
couldFetch = false
if hashes, err = fetchSnapshotHashes(ctx, mainnetUrl); err != nil {
fetched = false
return
}
Mainnet = hashes

if hashes, err = fetchSnapshotHashes(sepoliaUrl); err != nil {
couldFetch = false
if hashes, err = fetchSnapshotHashes(ctx, sepoliaUrl); err != nil {
fetched = false
return
}
Sepolia = hashes

if hashes, err = fetchSnapshotHashes(amoyUrl); err != nil {
couldFetch = false
if hashes, err = fetchSnapshotHashes(ctx, amoyUrl); err != nil {
fetched = false
return
}
Amoy = hashes

if hashes, err = fetchSnapshotHashes(borMainnetUrl); err != nil {
couldFetch = false
if hashes, err = fetchSnapshotHashes(ctx, borMainnetUrl); err != nil {
fetched = false
return
}
BorMainnet = hashes

if hashes, err = fetchSnapshotHashes(gnosisUrl); err != nil {
couldFetch = false
if hashes, err = fetchSnapshotHashes(ctx, gnosisUrl); err != nil {
fetched = false
return
}
Gnosis = hashes

if hashes, err = fetchSnapshotHashes(chiadoUrl); err != nil {
couldFetch = false
if hashes, err = fetchSnapshotHashes(ctx, chiadoUrl); err != nil {
fetched = false
return
}
Chiado = hashes

if hashes, err = fetchSnapshotHashes(holeskyUrl); err != nil {
couldFetch = false
if hashes, err = fetchSnapshotHashes(ctx, holeskyUrl); err != nil {
fetched = false
return
}
Holesky = hashes

couldFetch = true
return
fetched = true
return fetched, nil
}

func fetchSnapshotHashes(url string) ([]byte, error) {
resp, err := http.Get(url)
func fetchSnapshotHashes(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
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("failed to fetch snapshot hashes by %q: status code %d %s", url, resp.StatusCode, resp.Status)
}
res, err := io.ReadAll(resp.Body)
if len(res) == 0 {
return nil, fmt.Errorf("empty response from %s", url)
Expand Down
13 changes: 10 additions & 3 deletions embed_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package snapshothashes

import "testing"
import (
"context"
"testing"
)

func TestFetchSnapshotHashes(t *testing.T) {
dat, err := fetchSnapshotHashes("https://raw.githubusercontent.com/erigontech/erigon-snapshot/main/mainnet.toml")
dat, err := fetchSnapshotHashes(context.Background(), "https://raw.githubusercontent.com/erigontech/erigon-snapshot/main/mainnet.toml")
if err != nil {
t.Errorf("fetchSnapshotHashes() failed: %v", err)
}
Expand All @@ -13,7 +16,11 @@ func TestFetchSnapshotHashes(t *testing.T) {
}

func TestFetchSnapshotHashesAll(t *testing.T) {
if !LoadSnapshots() {
ok, err := LoadSnapshots(context.Background())
if err != nil {
t.Errorf("LoadSnapshots() failed %s", err)
}
if !ok {
t.Errorf("LoadSnapshots() failed")
}
if len(Mainnet) == 0 {
Expand Down

0 comments on commit f64407a

Please sign in to comment.