-
Notifications
You must be signed in to change notification settings - Fork 22
/
embed.go
125 lines (104 loc) · 2.61 KB
/
embed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package snapshothashes
import (
"context"
_ "embed"
"fmt"
"io"
"net/http"
"os"
_ "github.com/erigontech/erigon-snapshot/webseed"
)
var branchReference = getBranchReference()
func getBranchReference() string {
v, _ := os.LookupEnv("SNAPS_GIT_BRANCH")
if v != "" {
return v
}
return "main"
}
//go:embed mainnet.toml
var Mainnet []byte
//go:embed sepolia.toml
var Sepolia []byte
//go:embed amoy.toml
var Amoy []byte
//go:embed bor-mainnet.toml
var BorMainnet []byte
//go:embed gnosis.toml
var Gnosis []byte
//go:embed chiado.toml
var Chiado []byte
//go:embed holesky.toml
var Holesky []byte
func getURLByChain(chain string) string {
return fmt.Sprintf("https://raw.githubusercontent.com/erigontech/erigon-snapshot/%s/%s.toml", branchReference, chain)
}
func LoadSnapshots(ctx context.Context) (fetched bool, err error) {
var (
mainnetUrl = getURLByChain("mainnet")
sepoliaUrl = getURLByChain("sepolia")
amoyUrl = getURLByChain("amoy")
borMainnetUrl = getURLByChain("bor-mainnet")
gnosisUrl = getURLByChain("gnosis")
chiadoUrl = getURLByChain("chiado")
holeskyUrl = getURLByChain("holesky")
)
var hashes []byte
// Try to fetch the latest snapshot hashes from the web
if hashes, err = fetchSnapshotHashes(ctx, mainnetUrl); err != nil {
fetched = false
return
}
Mainnet = hashes
if hashes, err = fetchSnapshotHashes(ctx, sepoliaUrl); err != nil {
fetched = false
return
}
Sepolia = hashes
if hashes, err = fetchSnapshotHashes(ctx, amoyUrl); err != nil {
fetched = false
return
}
Amoy = hashes
if hashes, err = fetchSnapshotHashes(ctx, borMainnetUrl); err != nil {
fetched = false
return
}
BorMainnet = hashes
if hashes, err = fetchSnapshotHashes(ctx, gnosisUrl); err != nil {
fetched = false
return
}
Gnosis = hashes
if hashes, err = fetchSnapshotHashes(ctx, chiadoUrl); err != nil {
fetched = false
return
}
Chiado = hashes
if hashes, err = fetchSnapshotHashes(ctx, holeskyUrl); err != nil {
fetched = false
return
}
Holesky = hashes
fetched = true
return fetched, nil
}
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)
}
return res, err
}