summaryrefslogtreecommitdiff
path: root/cmd/catchupsrv/main.go
blob: 91fa4b0702ce56f76ba1462c47d52518867cbc7c (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
// 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 (
	"encoding/base64"
	"flag"
	"fmt"
	"math/rand"
	"net/http"
	"os"
	"path"
	"strconv"

	"github.com/algorand/websocket"
	"github.com/gorilla/mux"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/network"
	"github.com/algorand/go-algorand/rpcs"
)

var addrFlag = flag.String("addr", "127.0.0.1:4160", "Address to listen on")
var dirFlag = flag.String("dir", "", "Directory containing catchup blocks")
var tarDirFlag = flag.String("tardir", "", "Directory containing catchup blocks in M_N.tar.bz2")

func main() {
	flag.Parse()

	log := logging.Base()
	log.SetLevel(logging.Info)

	if *dirFlag == "" && *tarDirFlag == "" {
		panic("Must specify -dir or -tardir")
	}

	var blocktars *tarBlockSet
	if *tarDirFlag != "" {
		var err error
		blocktars, err = openTarBlockDir(*tarDirFlag)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: error opening block tar dir, %v\n", *tarDirFlag, err)
			os.Exit(1)
		}
	}

	if *downloadFlag {
		download()
		return
	}

	var srv http.Server
	r := mux.NewRouter()
	srv.Handler = r
	srv.Addr = *addrFlag

	var upgrader = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}

	r.HandleFunc(network.GossipNetworkPath, func(w http.ResponseWriter, r *http.Request) {
		pathVars := mux.Vars(r)
		genesisID := pathVars["genesisID"]

		var rnd [10]byte
		crypto.RandBytes(rnd[:])

		requestHeader := make(http.Header)
		requestHeader.Set(network.GenesisHeader, genesisID)
		requestHeader.Set(network.NodeRandomHeader, base64.StdEncoding.EncodeToString(rnd[:]))
		requestHeader.Set(network.ProtocolVersionHeader, "2.1")

		conn, err := upgrader.Upgrade(w, r, requestHeader)
		if err != nil {
			return
		}

		go func() {
			for {
				_, _, err := conn.ReadMessage()
				if err != nil {
					break
				}
			}
		}()
	})

	r.HandleFunc(rpcs.BlockServiceBlockPath, func(w http.ResponseWriter, r *http.Request) {
		pathVars := mux.Vars(r)
		versionStr := pathVars["version"]
		roundStr := pathVars["round"]
		genesisID := pathVars["genesisID"]

		roundNumber, err := stringToBlock(roundStr)
		if err != nil {
			log.Infof("%s %s: %v", r.Method, r.URL, err)
			http.NotFound(w, r)
			return
		}

		var data []byte
		if *dirFlag != "" {
			blkPath := blockToPath(roundNumber)
			data, err = os.ReadFile(
				path.Join(
					*dirFlag,
					"v"+versionStr,
					genesisID,
					"block",
					blkPath,
				),
			)
		} else if blocktars != nil {
			data, err = blocktars.getBlock(roundNumber)
		} else {
			fmt.Fprintf(os.Stderr, "config err, no block dir and no block tar dir\n")
			defer os.Exit(1)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		if err != nil {
			log.Infof("%s %s: %v", r.Method, r.URL, err)
			http.NotFound(w, r)
			return
		}

		w.Header().Set("Content-Type", "application/x-algorand-block-v1")
		w.Header().Set("Content-Length", strconv.Itoa(len(data)))
		w.WriteHeader(http.StatusOK)
		w.Write(data)
		if rand.Intn(20) == 0 {
			log.Infof("OK %d", roundNumber)
		}
	})

	log.Infof("serving %s", srv.Addr)
	err := srv.ListenAndServe()
	if err != nil {
		panic(err)
	}
}