forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
stringer.go
62 lines (50 loc) · 1.46 KB
/
stringer.go
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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowstorm
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/formatting"
)
type snowballNode struct {
txID ids.ID
numSuccessfulPolls int
confidence int
}
func (sb *snowballNode) String() string {
return fmt.Sprintf(
"SB(NumSuccessfulPolls = %d, Confidence = %d)",
sb.numSuccessfulPolls,
sb.confidence)
}
type sortSnowballNodeData []*snowballNode
func (sb sortSnowballNodeData) Less(i, j int) bool {
return bytes.Compare(sb[i].txID[:], sb[j].txID[:]) == -1
}
func (sb sortSnowballNodeData) Len() int { return len(sb) }
func (sb sortSnowballNodeData) Swap(i, j int) { sb[j], sb[i] = sb[i], sb[j] }
func sortSnowballNodes(nodes []*snowballNode) {
sort.Sort(sortSnowballNodeData(nodes))
}
// consensusString converts a list of snowball nodes into a human-readable
// string.
func consensusString(nodes []*snowballNode) string {
// Sort the nodes so that the string representation is canonical
sortSnowballNodes(nodes)
sb := strings.Builder{}
sb.WriteString("DG(")
format := fmt.Sprintf(
"\n Choice[%s] = ID: %%50s %%s",
formatting.IntFormat(len(nodes)-1))
for i, txNode := range nodes {
sb.WriteString(fmt.Sprintf(format, i, txNode.txID, txNode))
}
if len(nodes) > 0 {
sb.WriteString("\n")
}
sb.WriteString(")")
return sb.String()
}