/
ballot_isvalid.go
213 lines (166 loc) · 5.04 KB
/
ballot_isvalid.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
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
package base
import (
"github.com/spikeekips/mitum/util"
)
func IsValidBallot(bl Ballot, networkID []byte) error {
e := util.ErrInvalid.Errorf("ballot")
if err := util.CheckIsValiders(networkID, false,
bl.Voteproof(),
bl.SignFact(),
); err != nil {
return e.Wrap(err)
}
return nil
}
func IsValidINITBallot(bl INITBallot, _ []byte) error {
e := util.ErrInvalid.Errorf("init ballot")
if _, err := util.AssertInterfaceValue[INITBallotSignFact](bl.SignFact()); err != nil {
return e.Wrap(err)
}
switch bl.Voteproof().Point().Stage() { //nolint:exhaustive // state already checked
case StageINIT:
ivp, err := EnsureINITVoteproof(bl.Voteproof())
if err != nil {
return e.Wrap(err)
}
if err := isValidINITVoteproofInINITBallot(bl, ivp); err != nil {
return e.Wrap(err)
}
case StageACCEPT:
avp, err := EnsureACCEPTVoteproof(bl.Voteproof())
if err != nil {
return e.Wrap(err)
}
if err := isValidACCEPTVoteproofInINITBallot(bl, avp); err != nil {
return e.Wrap(err)
}
}
return nil
}
func isValidINITVoteproofInINITBallot(bl INITBallot, vp INITVoteproof) error {
e := util.ErrInvalid.Errorf("init voteproof in init ballot")
switch {
case bl.Point().Round() == 0:
return e.Errorf("init voteproof should not be in 0 round init ballot")
case !vp.Point().Point.NextRound().Equal(bl.Point().Point):
return e.Errorf(
"next round not match; ballot(%q) == voteproof(%q)", bl.Point(), vp.Point())
}
return nil
}
func isValidACCEPTVoteproofInINITBallot(bl INITBallot, vp ACCEPTVoteproof) error {
e := util.ErrInvalid.Errorf("accept voteproof in init ballot")
switch {
case vp.Result() == VoteResultMajority:
fact := bl.BallotSignFact().BallotFact()
if !fact.PreviousBlock().Equal(vp.BallotMajority().NewBlock()) {
return e.Errorf("block does not match, ballot(%q) != voteproof(%q)",
fact.PreviousBlock(),
vp.BallotMajority().NewBlock(),
)
}
default:
if !vp.Point().Point.NextRound().Equal(bl.Point().Point) {
return e.Errorf(
"not majority, but next round not match; ballot(%q) == voteproof(%q)", bl.Point(), vp.Point())
}
}
return nil
}
func isValidVoteproofInACCEPTBallot(bl Ballot, vp INITVoteproof) error {
e := util.ErrInvalid.Errorf("init voteproof in accept ballot")
if vp == nil {
return e.Errorf("empty voteproof")
}
switch {
case !bl.Point().Point.Equal(vp.Point().Point):
return e.Errorf(
"not same point; ballot(%q) == voteproof(%q)", bl.Point(), vp.Point())
case vp.Result() != VoteResultMajority:
return e.Errorf("init voteproof not majority result, %q", vp.Result())
}
return nil
}
func IsValidACCEPTBallot(bl ACCEPTBallot, _ []byte) error {
e := util.ErrInvalid.Errorf("accept ballot")
if _, err := util.AssertInterfaceValue[ACCEPTBallotSignFact](bl.SignFact()); err != nil {
return e.Wrap(err)
}
switch ivp, err := util.AssertInterfaceValue[INITVoteproof](bl.Voteproof()); {
case err != nil:
return e.Errorf("not init voteproof in accept ballot, %T", bl.Voteproof())
default:
if err := isValidVoteproofInACCEPTBallot(bl, ivp); err != nil {
return e.Wrap(err)
}
}
return nil
}
func IsValidBallotFact(fact BallotFact) error {
e := util.ErrInvalid.Errorf("ballot fact")
if err := fact.Point().IsValid(nil); err != nil {
return e.Wrap(err)
}
return nil
}
func IsValidINITBallotFact(fact INITBallotFact) error {
e := util.ErrInvalid.Errorf("init ballot fact")
if fact.Point().Stage() != StageINIT {
return e.Errorf("invalid stage, %q", fact.Point().Stage())
}
if err := util.CheckIsValiders(nil, false,
util.DummyIsValider(func(b []byte) error {
if fact.Point().Point.Equal(GenesisPoint) {
return nil
}
return fact.PreviousBlock().IsValid(b)
}),
fact.Proposal(),
); err != nil {
return e.Wrap(err)
}
return nil
}
func IsValidACCEPTBallotFact(fact ACCEPTBallotFact) error {
e := util.ErrInvalid.Errorf("accept ballot fact")
if fact.Point().Stage() != StageACCEPT {
return e.Errorf("invalid stage, %q", fact.Point().Stage())
}
if err := util.CheckIsValiders(nil, false,
fact.Proposal(),
fact.NewBlock(),
); err != nil {
return e.Wrap(err)
}
return nil
}
func IsValidBallotSignFact(sf BallotSignFact, networkID []byte) error {
e := util.ErrInvalid.Errorf("ballot signfact")
if err := IsValidSignFact(sf, networkID); err != nil {
return e.Wrap(err)
}
if err := util.CheckIsValiders(nil, false, sf.Node()); err != nil {
return e.Wrap(err)
}
return nil
}
func IsValidINITBallotSignFact(sf BallotSignFact, networkID []byte) error {
e := util.ErrInvalid.Errorf("init ballot signfact")
if err := IsValidBallotSignFact(sf, networkID); err != nil {
return e.Wrap(err)
}
if _, err := util.AssertInterfaceValue[INITBallotFact](sf.Fact()); err != nil {
return e.Wrap(err)
}
return nil
}
func IsValidACCEPTBallotSignFact(sf BallotSignFact, networkID []byte) error {
e := util.ErrInvalid.Errorf("accept ballot signfact")
if err := IsValidBallotSignFact(sf, networkID); err != nil {
return e.Wrap(err)
}
if _, err := util.AssertInterfaceValue[ACCEPTBallotFact](sf.Fact()); err != nil {
return e.Wrap(err)
}
return nil
}