summaryrefslogtreecommitdiff
path: root/nodecontrol/algodControl.go
blob: 74137a72eb7a209197f4c6f285215970eb3f1735 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// 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 nodecontrol

import (
	"fmt"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"time"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/daemon/algod/api/client"
	"github.com/algorand/go-algorand/data/bookkeeping"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/util"
	"github.com/algorand/go-algorand/util/tokens"
)

// StdErrFilename is the name of the file in <datadir> where stderr will be captured if not redirected to host
const StdErrFilename = "algod-err.log"

// StdOutFilename is the name of the file in <datadir> where stdout will be captured if not redirected to host
const StdOutFilename = "algod-out.log"

// NodeNotRunningError thrown when StopAlgod is called but there is no running algod in requested directory
type NodeNotRunningError struct {
	algodDataDir string
}

func (e *NodeNotRunningError) Error() string {
	return fmt.Sprintf("no running node in directory '%s'", e.algodDataDir)
}

// MissingDataDirError thrown when StopAlgod is called but requested directory does not exist
type MissingDataDirError struct {
	algodDataDir string
}

func (e *MissingDataDirError) Error() string {
	return fmt.Sprintf("the provided directory '%s' does not exist", e.algodDataDir)
}

// AlgodClient attempts to build a client.RestClient for communication with
// the algod REST API, but fails if we can't find the net file
func (nc NodeController) AlgodClient() (algodClient client.RestClient, err error) {
	algodAPIToken, err := tokens.GetAndValidateAPIToken(nc.algodDataDir, tokens.AlgodAdminTokenFilename)
	if err != nil {
		algodAPIToken, err = tokens.GetAndValidateAPIToken(nc.algodDataDir, tokens.AlgodTokenFilename)
		if err != nil {
			return
		}
	}

	// Fetch the server URL from the net file, if it exists
	algodURL, err := nc.ServerURL()
	if err != nil {
		return
	}

	// Build the client from the URL and API token
	algodClient = client.MakeRestClient(algodURL, algodAPIToken)
	return
}

// ServerURL returns the appropriate URL for the node under control
func (nc NodeController) ServerURL() (url.URL, error) {
	addr, err := nc.GetHostAddress()
	if err != nil {
		return url.URL{}, err
	}
	if strings.HasPrefix(addr, "http:") || strings.HasPrefix(addr, "https:") {
		u, err := url.Parse(addr)
		if err != nil {
			return url.URL{}, err
		}
		return *u, nil
	}
	return url.URL{Scheme: "http", Host: addr}, nil
}

// GetHostAddress retrieves the REST address for the node from its algod.net file.
func (nc NodeController) GetHostAddress() (string, error) {
	// For now, we want the old behavior to 'just work';
	// so if data directory is not specified, we assume the default address of 127.0.0.1:8080
	if len(nc.algodDataDir) == 0 {
		return "127.0.0.1:8080", nil
	}
	return util.GetFirstLineFromFile(nc.algodNetFile)
}

// buildAlgodCommand
func (nc NodeController) buildAlgodCommand(args AlgodStartArgs) *exec.Cmd {
	startArgs := make([]string, 0)
	startArgs = append(startArgs, "-d")
	startArgs = append(startArgs, nc.algodDataDir)
	if len(args.TelemetryOverride) > 0 {
		startArgs = append(startArgs, "-t")
		startArgs = append(startArgs, args.TelemetryOverride)
	}

	// Parse peerDial and listenIP cmdline flags
	peerDial := args.PeerAddress
	if len(peerDial) > 0 {
		startArgs = append(startArgs, "-p")
		startArgs = append(startArgs, peerDial)
	}
	listenIP := args.ListenIP
	if len(listenIP) > 0 {
		startArgs = append(startArgs, "-l")
		startArgs = append(startArgs, listenIP)
	}

	// Check if we should be using algoh
	var cmd string
	if args.RunUnderHost {
		cmd = nc.algoh
	} else {
		cmd = nc.algod
	}

	return exec.Command(cmd, startArgs...)
}

// algodRunning returns a boolean indicating if algod is running
func (nc NodeController) algodRunning() (isRunning bool) {
	_, err := nc.GetAlgodPID()
	if err == nil {
		// no error means file already exists, and we just loaded its content.
		// check if we can communicate with it.
		algodClient, err := nc.AlgodClient()
		if err == nil {
			err = algodClient.HealthCheck()
			if err == nil {
				// yes, we can communicate with it.
				return true
			}
		}
	}
	return false
}

// StopAlgod reads the net file and kills the algod process
func (nc *NodeController) StopAlgod() (err error) {
	// Check for valid data directory
	if !util.IsDir(nc.algodDataDir) {
		return &MissingDataDirError{algodDataDir: nc.algodDataDir}
	}
	// Find algod PID
	algodPID, err := nc.GetAlgodPID()
	if err == nil {
		// Kill algod by PID
		killed, killErr := killPID(int(algodPID))
		if killErr != nil {
			return killErr
		}
		// if we ended up killing the process, make sure to delete the pid file to avoid
		// potential downstream issues.
		if killed {
			// delete the pid file.
			os.Remove(nc.algodPidFile)
		}
	} else {
		return &NodeNotRunningError{algodDataDir: nc.algodDataDir}
	}
	return
}

// StartAlgod spins up an algod process and waits for it to begin
func (nc *NodeController) StartAlgod(args AlgodStartArgs) (alreadyRunning bool, err error) {
	// If algod is already running, we can't start again
	alreadyRunning = nc.algodRunning()
	if alreadyRunning {
		return alreadyRunning, nil
	}

	algodCmd := nc.buildAlgodCommand(args)

	var errLogger, outLogger *LaggedStdIo
	if args.RedirectOutput {
		errLogger = NewLaggedStdIo(os.Stderr, "algod")
		outLogger = NewLaggedStdIo(os.Stdout, "algod")
		algodCmd.Stderr = errLogger
		algodCmd.Stdout = outLogger
	} else if !args.RunUnderHost {
		// If not redirecting output to the host, we want to capture stderr and stdout to files
		files := nc.setAlgodCmdLogFiles(algodCmd)
		// Descriptors will get dup'd after exec, so OK to close when we return
		for _, file := range files {
			defer func(file *os.File) {
				localError := file.Close()
				if localError != nil && err == nil {
					err = localError
				}
			}(file)
		}
	}

	err = algodCmd.Start()
	if err != nil {
		return
	}

	if args.RedirectOutput {
		// update the logger output prefix with the process id.
		linePrefix := fmt.Sprintf("algod(%d)", algodCmd.Process.Pid)
		errLogger.SetLinePrefix(linePrefix)
		outLogger.SetLinePrefix(linePrefix)
	}
	// Wait on the algod process and check if exits
	algodExitChan := make(chan error, 1)
	startAlgodCompletedChan := make(chan struct{})
	defer close(startAlgodCompletedChan)
	go func() {
		// this Wait call is important even beyond the scope of this function; it allows the system to
		// move the process from a "zombie" state into "done" state, and is required for the Signal(0) test.
		err := algodCmd.Wait()
		select {
		case <-startAlgodCompletedChan:
			// we've already exited this function, so we want to report to the error to the callback.
			if args.ExitErrorCallback != nil {
				args.ExitErrorCallback(nc, err)
			}
		default:
		}
		algodExitChan <- err
	}()
	success := false
	for !success {
		select {
		case err := <-algodExitChan:
			err = &errAlgodExitedEarly{err}
			return false, err
		case <-time.After(time.Millisecond * 100):
			// If we can't talk to the API yet, spin
			algodClient, err := nc.AlgodClient()
			if err != nil {
				continue
			}

			// See if the server is up
			err = algodClient.HealthCheck()
			if err == nil {
				success = true
				continue
			}

			// Perhaps we're running an old version with no HealthCheck endpoint?
			_, err = algodClient.Status()
			if err == nil {
				success = true
			}
		}
	}
	return
}

// GetListeningAddress retrieves the listening address from the algod-listen.net file for the node
func (nc NodeController) GetListeningAddress() (string, error) {
	return util.GetFirstLineFromFile(nc.algodNetListenFile)
}

// GetAlgodPID returns the PID from the algod.pid file in the node's data directory, or an error
func (nc NodeController) GetAlgodPID() (pid int64, err error) {
	// Pull out the PID, ignoring newlines
	pidStr, err := util.GetFirstLineFromFile(nc.algodPidFile)
	if err != nil {
		return -1, err
	}
	// Parse as an integer
	pid, err = strconv.ParseInt(pidStr, 10, 32)
	return
}

// GetDataDir provides read-only access to the controller's data directory
func (nc NodeController) GetDataDir() string {
	return nc.algodDataDir
}

// GetAlgodPath provides read-only access to the controller's algod instance
func (nc NodeController) GetAlgodPath() string {
	return nc.algod
}

// Clone creates a new DataDir based on the controller's DataDir; if copyLedger is true, we'll clone the ledger.sqlite file
func (nc NodeController) Clone(targetDir string, copyLedger bool) (err error) {
	err = os.RemoveAll(targetDir)
	if err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("unable to delete directory '%s' : %v", targetDir, err)
	}

	var sourceFolderStat os.FileInfo
	sourceFolderStat, err = os.Stat(nc.algodDataDir)
	if err != nil {
		return
	}

	mkDirErr := os.Mkdir(targetDir, sourceFolderStat.Mode())
	if mkDirErr != nil && !os.IsExist(mkDirErr) {
		return mkDirErr
	}

	// Copy Core Files, silently failing to copy any that don't exist
	files := []string{config.GenesisJSONFile, config.ConfigFilename, config.PhonebookFilename}
	for _, file := range files {
		src := filepath.Join(nc.algodDataDir, file)
		if util.FileExists(src) {
			dest := filepath.Join(targetDir, file)
			_, err = util.CopyFile(src, dest)
			if err != nil {
				switch err.(type) {
				case *os.PathError:
					continue
				default:
					return
				}
			}
		}
	}

	// Copy Ledger Files if requested
	if copyLedger {
		var genesis bookkeeping.Genesis
		genesis, err = nc.readGenesisJSON(filepath.Join(nc.algodDataDir, config.GenesisJSONFile))
		if err != nil {
			return
		}

		genesisFolder := filepath.Join(nc.algodDataDir, genesis.ID())
		var genesisFolderStat os.FileInfo
		genesisFolderStat, err = os.Stat(genesisFolder)
		if err != nil {
			return
		}

		targetGenesisFolder := filepath.Join(targetDir, genesis.ID())
		mkDirErr = os.Mkdir(targetGenesisFolder, genesisFolderStat.Mode())
		if mkDirErr != nil && !os.IsExist(mkDirErr) {
			return mkDirErr
		}

		files := []string{"ledger.block.sqlite", "ledger.block.sqlite-shm", "ledger.block.sqlite-wal", "ledger.tracker.sqlite", "ledger.tracker.sqlite-shm", "ledger.tracker.sqlite-wal"}
		for _, file := range files {
			src := filepath.Join(genesisFolder, file)
			dest := filepath.Join(targetGenesisFolder, file)
			_, err = util.CopyFile(src, dest)
			if err != nil {
				return fmt.Errorf("unable to copy '%s' to '%s' : %v", src, dest, err)
			}
		}
	}

	return
}

// GetGenesis returns the current genesis for our instance
func (nc NodeController) GetGenesis() (bookkeeping.Genesis, error) {
	var genesis bookkeeping.Genesis

	genesisFile := filepath.Join(nc.GetDataDir(), config.GenesisJSONFile)
	genesisText, err := os.ReadFile(genesisFile)
	if err != nil {
		return genesis, err
	}

	err = protocol.DecodeJSON(genesisText, &genesis)
	if err != nil {
		return genesis, err
	}

	return genesis, nil
}

// GetGenesisDir returns the current genesis directory for our instance
func (nc NodeController) GetGenesisDir() (string, error) {
	genesis, err := nc.GetGenesis()
	if err != nil {
		return "", err
	}
	genesisDir := filepath.Join(nc.GetDataDir(), genesis.ID())
	return genesisDir, nil
}

func (nc NodeController) setAlgodCmdLogFiles(cmd *exec.Cmd) (files []*os.File) {
	{ // Scoped to ensure err and out variables aren't mixed up
		errFileName := filepath.Join(nc.algodDataDir, StdErrFilename)
		errFile, err := os.OpenFile(errFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
		if err == nil {
			cmd.Stderr = errFile
			files = append(files, errFile)
		} else {
			fmt.Fprintf(os.Stderr, "error creating file for capturing stderr: %v\n", err)
		}
	}
	{
		outFileName := filepath.Join(nc.algodDataDir, StdOutFilename)
		outFile, err := os.OpenFile(outFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
		if err == nil {
			cmd.Stdout = outFile
			files = append(files, outFile)
		} else {
			fmt.Fprintf(os.Stderr, "error creating file for capturing stdout: %v\n", err)
		}
	}
	return
}

func (nc NodeController) readGenesisJSON(genesisFile string) (genesisLedger bookkeeping.Genesis, err error) {
	// Load genesis
	genesisText, err := os.ReadFile(genesisFile)
	if err != nil {
		return
	}

	err = protocol.DecodeJSON(genesisText, &genesisLedger)
	return
}

// SetConsensus applies a new consensus settings which would get deployed before
// any of the nodes starts
func (nc NodeController) SetConsensus(consensus config.ConsensusProtocols) error {
	return config.SaveConfigurableConsensus(nc.algodDataDir, consensus)
}

// GetConsensus rebuild the consensus version from the data directory
func (nc NodeController) GetConsensus() (config.ConsensusProtocols, error) {
	return config.PreloadConfigurableConsensusProtocols(nc.algodDataDir)
}

// Shutdown requests the node to shut itself down
func (nc NodeController) Shutdown() error {
	algodClient, err := nc.AlgodClient()
	if err == nil {
		err = algodClient.Shutdown()
	}
	return err
}