summaryrefslogtreecommitdiff
path: root/cmd/catchupsrv/download.go
blob: 80c1cc679b5b5bc9702c8b40a053f7417f4c743c (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"net"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
	tools_network "github.com/algorand/go-algorand/tools/network"
)

const minLenBlockStr = 6 // the minimum size of a block filename (after padding with zeros) when using subfolders

var downloadFlag = flag.Bool("download", false, "Download blocks from an origin server")
var serversFlag = flag.String("servers", "", "Semicolon-separated list of origin server addresses (host:port)")
var networkFlag = flag.String("network", "", "Network ID to obtain servers via DNS SRV")
var genesisFlag = flag.String("genesis", "", "Genesis ID")
var connsFlag = flag.Int("conns", 2, "Number of connections per server")

var serverList []string
var nextBlk uint64

// padLeftZeros pad the string s with zeros on the left to the length n
func padLeftZeros(s string, n int) string {
	if len(s) < n {
		return strings.Repeat("0", n-len(s)) + s
	}
	return s
}

// blockToString converts a block number into a base-36 number
func blockToString(blk uint64) string {
	return strconv.FormatUint(blk, 36)
}

// blockToFileName converts a block number into the filename in which it will be downloaded
// namely the base-36 representation of the block number padded with zeros
// so that the length of the filename is at least minLenBlockStr
func blockToFileName(blk uint64) string {
	return padLeftZeros(blockToString(blk), minLenBlockStr)
}

// stringToBlock converts a base-36 string into a block number
func stringToBlock(s string) (uint64, error) {
	blk, err := strconv.ParseUint(s, 36, 64)
	if err != nil {
		return 0, fmt.Errorf("invalid block string \"%s\": %v", s, err)
	}
	return blk, nil
}

// blockToPath converts a block number into the full path in which it will be downloaded
// Examples:
// - for block `bcdef`, the path is `0b/cd/0bcdef`
// - for block `abcdefg`, the path is `abc/de/abcdefg`
func blockToPath(blk uint64) string {
	s := blockToFileName(blk)
	return path.Join(
		s[0:(len(s)+2-minLenBlockStr)],
		s[(len(s)+2-minLenBlockStr):(len(s)+4-minLenBlockStr)],
		s,
	)
}

// stringBlockToPath is the same as blockToPath except it takes a (non-padded) base-36 block
func stringBlockToPath(s string) (string, error) {
	blk, err := stringToBlock(s)
	if err != nil {
		return "", err
	}
	return blockToPath(blk), nil
}

// blockDir returns the root folder where all the blocks are stored
func blockDir() string {
	return filepath.Join(*dirFlag, fmt.Sprintf("v1/%s/block", *genesisFlag))
}

// blockFullPath returns the full path to a block, including blockDir
func blockFullPath(blk uint64) string {
	return filepath.Join(blockDir(), blockToPath(blk))
}

func blockURL(server string, blk uint64) string {
	return fmt.Sprintf("http://%s/v1/%s/block/%s", server, *genesisFlag, blockToString(blk))
}

func fetchBlock(server string, blk uint64) error {
	log := logging.Base()

	fn := blockFullPath(blk)
	_, err := os.Stat(fn)
	if err == nil {
		log.Debugf("block %d already exists", blk)
		return nil
	}

	if !os.IsNotExist(err) {
		return err
	}

	log.Infof("fetching %d (%s) from %s..", blk, blockToFileName(blk), server)
	resp, err := http.Get(blockURL(server, blk))
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return fmt.Errorf("HTTP response: %s", resp.Status)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	// Create the folder if needed
	d := path.Dir(fn)
	_, err = os.Stat(d)
	if os.IsNotExist(err) {
		// Create the folder if it does not exist
		err = os.MkdirAll(d, 0777)
		if err != nil {
			panic(err)
		}
	} else if err != nil {
		panic(err)
	}

	return os.WriteFile(fn, body, 0666)
}

func fetcher(server string, wg *sync.WaitGroup) {
	log := logging.Base()

	for {
		myBlock := atomic.AddUint64(&nextBlk, 1) - 1

		err := fetchBlock(server, myBlock)
		if err != nil {
			log.Errorf("fetching %d (%s) from %s: %v", myBlock, blockToFileName(myBlock), server, err)
			break
		}
	}

	wg.Done()
}

// TODO: We may want to implement conditional fallback to backup bootstrap logic here
func download() {
	if *genesisFlag == "" {
		panic("Must specify -genesis")
	}

	if *serversFlag != "" {
		serverList = strings.Split(*serversFlag, ";")
	} else if *networkFlag != "" {
		cfg := config.GetDefaultLocal()
		// only using first dnsBootstrap entry (if more than one are configured) and just the primary SRV, not backup
		dnsBootstrap := cfg.DNSBootstrapArray(protocol.NetworkID(*networkFlag))[0]
		_, records, err := net.LookupSRV("algobootstrap", "tcp", dnsBootstrap.PrimarySRVBootstrap)
		if err != nil {
			dnsAddr, err2 := net.ResolveIPAddr("ip", cfg.FallbackDNSResolverAddress)
			if err2 != nil {
				// Report original LookupSRV error
				panic(err)
			}

			var resolver tools_network.Resolver
			resolver.SetFallbackResolverAddress(*dnsAddr)
			_, records, err = resolver.LookupSRV(context.Background(), "algobootstrap", "tcp", dnsBootstrap.PrimarySRVBootstrap)
			if err != nil {
				panic(err)
			}
		}

		for _, srv := range records {
			serverList = append(serverList, fmt.Sprintf("%s:%d", srv.Target, srv.Port))
		}
	} else {
		panic("Must specify -servers or -network")
	}

	http.DefaultTransport.(*http.Transport).MaxConnsPerHost = *connsFlag
	http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = *connsFlag

	err := os.MkdirAll(blockDir(), 0777)
	if err != nil {
		panic(err)
	}

	var wg sync.WaitGroup

	fetchPerServer := *connsFlag
	for _, srv := range serverList {
		wg.Add(fetchPerServer)
		for i := 0; i < fetchPerServer; i++ {
			go fetcher(srv, &wg)
		}
	}

	wg.Wait()
}