-
Notifications
You must be signed in to change notification settings - Fork 4
/
topic.go
159 lines (140 loc) · 3.1 KB
/
topic.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
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
package scp
import "fmt"
// Topic is the abstract type of the payload of an SCP message
// (conveyed in an envelope, see type Msg). The concrete type is one
// of NomTopic, NomPrepTopic, PrepTopic, CommitTopic, and ExtTopic.
type Topic interface {
Less(Topic) bool
String() string
}
// NomTopic is the payload of a nomination protocol message.
type NomTopic struct {
X, Y ValueSet
}
func (nt *NomTopic) Less(other Topic) bool {
o, ok := other.(*NomTopic)
if !ok {
return true // NOMINATE messages are less than all other messages
}
if len(nt.Y) < len(o.Y) {
return true
}
if len(nt.Y) > len(o.Y) {
return false
}
return len(nt.X) < len(o.X)
}
func (nt *NomTopic) String() string {
return fmt.Sprintf("NOM X=%s, Y=%s", nt.X, nt.Y)
}
// NomPrepTopic is the combined payload of a NOMINATE and a PREPARE
// message.
type NomPrepTopic struct {
NomTopic
PrepTopic
}
func (npt *NomPrepTopic) Less(other Topic) bool {
switch other := other.(type) {
case *NomTopic:
return false
case *NomPrepTopic:
if npt.NomTopic.Less(&other.NomTopic) {
return true
}
if other.NomTopic.Less(&npt.NomTopic) {
return false
}
return npt.PrepTopic.Less(&other.PrepTopic)
default:
return true
}
}
func (npt *NomPrepTopic) String() string {
return fmt.Sprintf("NOM/PREP X=%s, Y=%s B=%s P=%s PP=%s CN=%d HN=%d", npt.X, npt.Y, npt.B, npt.P, npt.PP, npt.CN, npt.HN)
}
// PrepTopic is the payload of a PREPARE message in the ballot protocol.
type PrepTopic struct {
B, P, PP Ballot
HN, CN int
}
func (pt *PrepTopic) Less(other Topic) bool {
switch other := other.(type) {
case *NomTopic:
return false
case *NomPrepTopic:
return false
case *PrepTopic:
if pt.B.Less(other.B) {
return true
}
if other.B.Less(pt.B) {
return false
}
if pt.P.Less(other.P) {
return true
}
if other.P.Less(pt.P) {
return false
}
if pt.PP.Less(other.PP) {
return true
}
if other.PP.Less(pt.PP) {
return false
}
return pt.HN < other.HN
}
return true
}
func (pt *PrepTopic) String() string {
return fmt.Sprintf("PREP B=%s P=%s PP=%s CN=%d HN=%d", pt.B, pt.P, pt.PP, pt.CN, pt.HN)
}
// CommitTopic is the payload of a COMMIT message in the ballot
// protocol.
type CommitTopic struct {
B Ballot
PN, HN, CN int
}
func (ct *CommitTopic) Less(other Topic) bool {
switch other := other.(type) {
case *NomTopic:
return false
case *NomPrepTopic:
return false
case *PrepTopic:
return false
case *CommitTopic:
if ct.B.Less(other.B) {
return true
}
if other.B.Less(ct.B) {
return false
}
if ct.PN < other.PN {
return true
}
if other.PN < ct.PN {
return false
}
return ct.HN < other.HN
}
return true
}
func (ct *CommitTopic) String() string {
return fmt.Sprintf("COMMIT B=%s PN=%d CN=%d HN=%d", ct.B, ct.PN, ct.CN, ct.HN)
}
// ExtTopic is the payload of an EXTERNALIZE message in the ballot
// protocol.
type ExtTopic struct {
C Ballot
HN int
}
func (et *ExtTopic) Less(other Topic) bool {
if other, ok := other.(*ExtTopic); ok {
return et.HN < other.HN
}
return false
}
func (et *ExtTopic) String() string {
return fmt.Sprintf("EXT C=%s HN=%d", et.C, et.HN)
}