forked from erigontech/erigon-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathembed.go
134 lines (111 loc) · 2.9 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
126
127
128
129
130
131
132
133
134
package snapshothashes
import (
"context"
_ "embed"
"fmt"
"io"
"net/http"
_ "github.com/erigontech/erigon-snapshot/webseed"
)
//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
//go:embed bsc.toml
var Bsc []byte
//go:embed chapel.toml
var Chapel []byte
func getURLByChain(chain, branch string) string {
return fmt.Sprintf("https://raw.githubusercontent.com/node-real/bsc-erigon-snapshot/%s/%s.toml", branch, chain)
}
func LoadSnapshots(ctx context.Context, branch string) (fetched bool, err error) {
var (
bscUrl = getURLByChain("bsc", branch)
chapelUrl = getURLByChain("chapel", branch)
mainnetUrl = getURLByChain("mainnet", branch)
sepoliaUrl = getURLByChain("sepolia", branch)
amoyUrl = getURLByChain("amoy", branch)
borMainnetUrl = getURLByChain("bor-mainnet", branch)
gnosisUrl = getURLByChain("gnosis", branch)
chiadoUrl = getURLByChain("chiado", branch)
holeskyUrl = getURLByChain("holesky", branch)
)
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
if hashes, err = fetchSnapshotHashes(ctx, bscUrl); err != nil {
fetched = false
return
}
Bsc = hashes
if hashes, err = fetchSnapshotHashes(ctx, chapelUrl); err != nil {
fetched = false
return
}
Chapel = 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
}