-
Notifications
You must be signed in to change notification settings - Fork 4
/
proto_vnvn_validation_vstatereport.go
198 lines (164 loc) · 6.2 KB
/
proto_vnvn_validation_vstatereport.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
package rms
import (
"github.com/insolar/assured-ledger/ledger-core/pulse"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
var _ Validatable = &VStateReport{}
type vStateReportValidateStatusFunc func(pulse.Number, pulse.Number) error
func (m *VStateReport) getValidateStatusFunc(s VStateReport_StateStatus) (vStateReportValidateStatusFunc, bool) {
switch s {
case StateStatusReady:
return m.validateStatusReady, true
case StateStatusEmpty:
return m.validateStatusEmpty, true
case StateStatusMissing, StateStatusInactive:
return m.validateStatusMissingOrInactive, true
}
return nil, false
}
func (m *VStateReport) Validate(currentPulse pulse.Number) error {
if err := m.validateUnimplemented(); err != nil {
return err
}
if !isTimePulseBefore(m.GetAsOf(), currentPulse) {
return throw.New("AsOf should be time pulse and less that current pulse")
}
objectPulse, err := validSelfScopedGlobalWithPulseBeforeOrEq(m.Object.GetValue(), currentPulse, "Object")
if err != nil {
return err
}
validateStatusFunc, ok := m.getValidateStatusFunc(m.GetStatus())
if !ok {
return throw.New("Unexpected state received")
}
return validateStatusFunc(objectPulse, currentPulse)
}
func (m *VStateReport) validateStatusReady(objectPulse pulse.Number, currentPulse pulse.Number) error {
switch pendingCount, earliestPendingPulse := m.GetUnorderedPendingCount(), m.GetUnorderedPendingEarliestPulse(); {
case pendingCount == 0:
if !earliestPendingPulse.IsUnknown() {
return throw.New("UnorderedPendingEarliestPulse should be Unknown")
}
case pendingCount > 0 && pendingCount < 127:
if !isTimePulseBeforeOrEq(earliestPendingPulse, currentPulse) || !earliestPendingPulse.IsEqOrAfter(objectPulse) {
return throw.New("UnorderedPendingEarliestPulse should be in range [objectPulse..currentPulse]")
}
default:
return throw.New("UnorderedPendingCount should be in range [0..127)")
}
switch pendingCount, earliestPendingPulse := m.GetOrderedPendingCount(), m.GetOrderedPendingEarliestPulse(); {
case pendingCount == 0:
if !earliestPendingPulse.IsUnknown() {
return throw.New("UnorderedPendingEarliestPulse should be Unknown")
}
case pendingCount > 0 && pendingCount < 127:
if !isTimePulseBeforeOrEq(earliestPendingPulse, currentPulse) || !earliestPendingPulse.IsEqOrAfter(objectPulse) {
return throw.New("OrderedPendingEarliestPulse should be in range [objectPulse..currentPulse]")
}
default:
return throw.New("UnorderedPendingCount should be in range [0..127)")
}
return nil
}
func (m *VStateReport) validateStatusEmpty(objectPulse pulse.Number, currentPulse pulse.Number) error {
if m.GetOrderedPendingCount() != 1 {
return throw.New("Should be one ordered pending")
}
if m.GetUnorderedPendingCount() != 0 {
return throw.New("Unordered pending count should be 0")
}
if !m.GetUnorderedPendingEarliestPulse().IsUnknown() {
return throw.New("Unordered pending earliest pulse should be Unknown")
}
var (
orderedPendingPulse = m.GetOrderedPendingEarliestPulse()
)
if !isTimePulseBefore(orderedPendingPulse, currentPulse) || !objectPulse.IsBeforeOrEq(orderedPendingPulse) {
return throw.New("Incorrect pending ordered pulse number")
}
if err := m.validateStateAndCodeAreEmpty(); err != nil {
return err
}
if err := m.ProvidedContent.validateIsEmpty(); err != nil {
return err
}
return nil
}
func (m *VStateReport) validateStatusMissingOrInactive(pulse.Number, pulse.Number) error {
// validate we've got zero pendings on object
switch {
case m.GetOrderedPendingCount() != 0:
return throw.New("OrderedPendingCount should be 0")
// TODO: PLAT-717: VStateReport can be StateStatusInactive and contain UnorderedPendingCount > 0 in R0
case m.GetStatus() == StateStatusMissing && m.GetUnorderedPendingCount() != 0:
return throw.New("UnorderedPendingCount should be 0")
case !m.GetOrderedPendingEarliestPulse().IsUnknown():
return throw.New("OrderedPendingEarliestPulse should be Unknown")
case !m.GetUnorderedPendingEarliestPulse().IsUnknown():
return throw.New("UnorderedPendingEarliestPulse should be Unknown")
}
// validate ProvidedContent is empty
if err := m.ProvidedContent.validateIsEmpty(); err != nil {
return err
}
// validate internal state description is empty
if err := m.validateStateAndCodeAreEmpty(); err != nil {
return err
}
return nil
}
func (m *VStateReport) validateStateAndCodeAreEmpty() error {
switch {
case !m.LatestValidatedState.IsEmpty():
return throw.New("LatestValidatedState should be empty")
case !m.LatestValidatedCode.IsEmpty():
return throw.New("LatestValidatedCode should be empty")
case !m.LatestDirtyState.IsEmpty():
return throw.New("LatestDirtyState should be empty")
case !m.LatestDirtyCode.IsEmpty():
return throw.New("LatestDirtyCode should be empty")
}
return nil
}
func (m *VStateReport) validateUnimplemented() error {
switch {
case !m.GetDelegationSpec().IsZero():
return throw.New("DelegationSpec should be empty")
case m.GetPreRegisteredQueueCount() != 0:
return throw.New("PriorityCallQueueCount should be 0")
case !m.GetPreRegisteredEarliestPulse().IsUnknown():
return throw.New("PreRegisteredEarliestPulse should be unknown")
case m.GetPriorityCallQueueCount() != 0:
return throw.New("PriorityCallQueueCount should be 0")
}
if err := m.ProvidedContent.validateUnimplemented(); err != nil {
return err
}
return nil
}
func (m *VStateReport_ProvidedContentBody) validateIsEmpty() error {
if m != nil {
switch {
case m.GetLatestValidatedState() != nil:
return throw.New("ProvidedContent.LatestValidatedState should be empty")
case m.GetLatestValidatedCode() != nil:
return throw.New("ProvidedContent.LatestValidatedCode should be empty")
case m.GetLatestDirtyState() != nil:
return throw.New("ProvidedContent.LatestDirtyState should be empty")
case m.GetLatestDirtyCode() != nil:
return throw.New("ProvidedContent.LatestDirtyCode should be empty")
}
}
return nil
}
func (m *VStateReport_ProvidedContentBody) validateUnimplemented() error {
if m != nil {
switch {
case len(m.GetOrderedQueue()) != 0:
return throw.New("ProvidedContent.OrderedQueue should be empty")
case len(m.GetUnorderedQueue()) != 0:
return throw.New("ProvidedContent.UnorderedQueue should be empty")
}
}
return nil
}