summaryrefslogtreecommitdiff
path: root/tools/debug/coroner/main.go
blob: c8670b7b14716e98ef297a6d034a775fd3c2afc7 (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
// 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/>.

// coroner performs post-mortem autospies on Algorand nodes
package main

import (
	"flag"
	"log"
	"os"
	"regexp"
	"strconv"

	"github.com/algorand/go-algorand/agreement"
	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/data/basics"
)

var numRegex = regexp.MustCompile(`^\d+$`)

var filename = flag.String("file", "", "Name of the input cadaver file (otherwise, use stdin)")
var versionCheck = flag.Bool("version", false, "Display current coroner build version and exit")
var printmsgpack = flag.Bool("msgpack", false, "If provided, emit msgpack instead of a string")

var skipHead = flag.String("skip-head", "", "The first round to trim before")
var skipTail = flag.String("skip-tail", "", "The last round to trim after")

func mustParse(data []byte) uint64 {
	x, err := strconv.ParseUint(string(data), 10, 64)
	if err != nil {
		log.Fatalf(`failed to parse round bound in "%s": %s`, string(data), err)
	}
	return x
}

func parseRoundBound(s string) uint64 {
	if !numRegex.Match([]byte(s)) {
		log.Fatalf(`failed to parse round bound in "%s": string does not match regex "^\d+$"`, s)
	}
	return mustParse(numRegex.Find([]byte(s)))
}

func done(n int, err error) {
	if n == 0 {
		log.Println("coroner: no cadavers autopsied")
	}

	if err != nil {
		log.Println("coroner: failed to extract full autopsy trace:", err)
	}
}

func nextBounds(i int, bounds agreement.AutopsyBounds) {
	log.Printf("cadaver seq: %d\tstart(r,p): (%d,%d)\tend(r,p): (%d,%d)\n", i, bounds.StartRound, bounds.StartPeriod, bounds.EndRound, bounds.EndPeriod)
}

func main() {
	flag.Parse()
	var autopsy *agreement.Autopsy
	var err error
	version := config.GetCurrentVersion()

	if *versionCheck {
		log.Printf("uint64 version: %d\n%s.%s [%s] (commit #%s)\n", version.AsUInt64(), version.String(),
			version.Channel, version.Branch, version.GetCommitHash())
		return
	}

	if *filename == "" {
		log.Println("coroner: no filename provided; reading from stdin...")
		autopsy, err = agreement.PrepareAutopsyFromStream(os.Stdin, nextBounds, done)
	} else {
		autopsy, err = agreement.PrepareAutopsy(*filename, nextBounds, done)
	}
	if err != nil {
		log.Fatalln("coroner: failed to prepare autopsy:", err)
	}
	defer autopsy.Close()

	var filter agreement.AutopsyFilter
	if *skipHead != "" {
		filter.Enabled = true
		filter.First = basics.Round(parseRoundBound(*skipHead))
	}
	if *skipTail != "" {
		filter.Enabled = true
		filter.Last = basics.Round(parseRoundBound(*skipTail))
	}

	var commitHash string
	if *printmsgpack {
		commitHash = autopsy.DumpMessagePack(filter, os.Stdout)
	} else {
		commitHash = autopsy.DumpString(filter, os.Stdout)
	}
	if commitHash != version.GetCommitHash() {
		log.Printf("coroner: cadaver version mismatches coroner version:\n(%s (cadaver) != %s (coroner))\n", commitHash, version.GetCommitHash())
	}

	return
}