Skip to content

Commit e99fc7e

Browse files
authored
Parameterize by CDN source; add R2 source support (#544)
1 parent 9ba6d1e commit e99fc7e

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

embed.go

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,59 +31,72 @@ var Chiado []byte
3131
//go:embed holesky.toml
3232
var Holesky []byte
3333

34-
func getURLByChain(chain, branch string) string {
35-
return fmt.Sprintf("https://raw.githubusercontent.com/erigontech/erigon-snapshot/%s/%s.toml", branch, chain)
34+
type SnapshotSource int
35+
36+
const (
37+
Github SnapshotSource = 0
38+
R2 SnapshotSource = 1
39+
)
40+
41+
func getURLByChain(source SnapshotSource, chain, branch string) string {
42+
if source == Github {
43+
return fmt.Sprintf("https://raw.githubusercontent.com/erigontech/erigon-snapshot/%s/%s.toml", branch, chain)
44+
} else if source == R2 {
45+
return fmt.Sprintf("https://erigon-snapshots.erigon.network/%s/%s.toml", branch, chain)
46+
}
47+
48+
panic(fmt.Sprintf("unknown snapshot source: %d", source))
3649
}
3750

38-
func LoadSnapshots(ctx context.Context, branch string) (fetched bool, err error) {
51+
func LoadSnapshots(ctx context.Context, source SnapshotSource, branch string) (fetched bool, err error) {
3952
var (
40-
mainnetUrl = getURLByChain("mainnet", branch)
41-
sepoliaUrl = getURLByChain("sepolia", branch)
42-
amoyUrl = getURLByChain("amoy", branch)
43-
borMainnetUrl = getURLByChain("bor-mainnet", branch)
44-
gnosisUrl = getURLByChain("gnosis", branch)
45-
chiadoUrl = getURLByChain("chiado", branch)
46-
holeskyUrl = getURLByChain("holesky", branch)
53+
mainnetUrl = getURLByChain(source, "mainnet", branch)
54+
sepoliaUrl = getURLByChain(source, "sepolia", branch)
55+
amoyUrl = getURLByChain(source, "amoy", branch)
56+
borMainnetUrl = getURLByChain(source, "bor-mainnet", branch)
57+
gnosisUrl = getURLByChain(source, "gnosis", branch)
58+
chiadoUrl = getURLByChain(source, "chiado", branch)
59+
holeskyUrl = getURLByChain(source, "holesky", branch)
4760
)
4861
var hashes []byte
4962
// Try to fetch the latest snapshot hashes from the web
50-
if hashes, err = fetchSnapshotHashes(ctx, mainnetUrl); err != nil {
63+
if hashes, err = fetchSnapshotHashes(ctx, source, mainnetUrl); err != nil {
5164
fetched = false
5265
return
5366
}
5467
Mainnet = hashes
5568

56-
if hashes, err = fetchSnapshotHashes(ctx, sepoliaUrl); err != nil {
69+
if hashes, err = fetchSnapshotHashes(ctx, source, sepoliaUrl); err != nil {
5770
fetched = false
5871
return
5972
}
6073
Sepolia = hashes
6174

62-
if hashes, err = fetchSnapshotHashes(ctx, amoyUrl); err != nil {
75+
if hashes, err = fetchSnapshotHashes(ctx, source, amoyUrl); err != nil {
6376
fetched = false
6477
return
6578
}
6679
Amoy = hashes
6780

68-
if hashes, err = fetchSnapshotHashes(ctx, borMainnetUrl); err != nil {
81+
if hashes, err = fetchSnapshotHashes(ctx, source, borMainnetUrl); err != nil {
6982
fetched = false
7083
return
7184
}
7285
BorMainnet = hashes
7386

74-
if hashes, err = fetchSnapshotHashes(ctx, gnosisUrl); err != nil {
87+
if hashes, err = fetchSnapshotHashes(ctx, source, gnosisUrl); err != nil {
7588
fetched = false
7689
return
7790
}
7891
Gnosis = hashes
7992

80-
if hashes, err = fetchSnapshotHashes(ctx, chiadoUrl); err != nil {
93+
if hashes, err = fetchSnapshotHashes(ctx, source, chiadoUrl); err != nil {
8194
fetched = false
8295
return
8396
}
8497
Chiado = hashes
8598

86-
if hashes, err = fetchSnapshotHashes(ctx, holeskyUrl); err != nil {
99+
if hashes, err = fetchSnapshotHashes(ctx, source, holeskyUrl); err != nil {
87100
fetched = false
88101
return
89102
}
@@ -93,11 +106,14 @@ func LoadSnapshots(ctx context.Context, branch string) (fetched bool, err error)
93106
return fetched, nil
94107
}
95108

96-
func fetchSnapshotHashes(ctx context.Context, url string) ([]byte, error) {
109+
func fetchSnapshotHashes(ctx context.Context, source SnapshotSource, url string) ([]byte, error) {
97110
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
98111
if err != nil {
99112
return nil, err
100113
}
114+
if source == R2 {
115+
insertCloudflareHeaders(req)
116+
}
101117
resp, err := http.DefaultClient.Do(req)
102118
if err != nil {
103119
return nil, err
@@ -112,3 +128,14 @@ func fetchSnapshotHashes(ctx context.Context, url string) ([]byte, error) {
112128
}
113129
return res, err
114130
}
131+
132+
// TODO: this was taken originally from erigon repo, downloader.go; we need to decide on a unique place to store such headers
133+
var cloudflareHeaders = http.Header{
134+
"lsjdjwcush6jbnjj3jnjscoscisoc5s": []string{"I%OSJDNFKE783DDHHJD873EFSIVNI7384R78SSJBJBCCJBC32JABBJCBJK45"},
135+
}
136+
137+
func insertCloudflareHeaders(req *http.Request) {
138+
for key, value := range cloudflareHeaders {
139+
req.Header[key] = value
140+
}
141+
}

embed_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func TestFetchSnapshotHashes(t *testing.T) {
9-
dat, err := fetchSnapshotHashes(context.Background(), "https://raw.githubusercontent.com/erigontech/erigon-snapshot/main/mainnet.toml")
9+
dat, err := fetchSnapshotHashes(context.Background(), Github, "https://raw.githubusercontent.com/erigontech/erigon-snapshot/main/mainnet.toml")
1010
if err != nil {
1111
t.Errorf("fetchSnapshotHashes() failed: %v", err)
1212
}
@@ -16,7 +16,7 @@ func TestFetchSnapshotHashes(t *testing.T) {
1616
}
1717

1818
func TestFetchSnapshotHashesAll(t *testing.T) {
19-
ok, err := LoadSnapshots(context.Background(), "main")
19+
ok, err := LoadSnapshots(context.Background(), Github, "main")
2020
if err != nil {
2121
t.Errorf("LoadSnapshots() failed %s", err)
2222
}

0 commit comments

Comments
 (0)