@@ -31,59 +31,72 @@ var Chiado []byte
3131//go:embed holesky.toml
3232var 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+ }
0 commit comments