/
proto_vnvn_validation_vobjecttranscriptreport.go
67 lines (58 loc) · 1.75 KB
/
proto_vnvn_validation_vobjecttranscriptreport.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
package rms
import (
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
func (m *VObjectTranscriptReport) Validate(currentPulse PulseNumber) error {
object := m.Object.GetValue()
_, err := validSelfScopedGlobalWithPulseBeforeOrEq(object, currentPulse, "Object")
if err != nil {
return err
}
if !isTimePulseBefore(m.GetAsOf(), currentPulse) {
return throw.New("AsOf should be time pulse and less than current pulse")
}
transcript := m.GetObjectTranscript()
if len(transcript.GetEntries()) == 0 {
return throw.New("Transcript mustn't be empty")
}
transcriptErr := validateEntries(transcript.GetEntries())
if transcriptErr != nil {
return throw.W(transcriptErr, "ObjectTranscript validation failed")
}
pendings := m.GetPendingTranscripts()
if len(pendings) != 0 {
pendingTranscriptsErr := validatePendingTranscripts(pendings)
if pendingTranscriptsErr != nil {
return pendingTranscriptsErr
}
}
return nil
}
func validateEntries(entries []Any) error {
// todo: check properly
for _, entry := range entries {
entryValue := entry.Get()
switch entryValue.(type) {
case *Transcript_TranscriptEntryIncomingRequest:
case *Transcript_TranscriptEntryIncomingResult:
case *Transcript_TranscriptEntryOutgoingRequest:
case *Transcript_TranscriptEntryOutgoingResult:
default:
return throw.New("unexpected type: %T", entryValue)
}
}
return nil
}
func validatePendingTranscripts(transcripts []Transcript) error {
for _, tr := range transcripts {
entries := tr.GetEntries()
if len(entries) == 0 {
return throw.New("PendingTranscript entries mustn't be empty")
}
transcriptErr := validateEntries(entries)
if transcriptErr != nil {
return throw.W(transcriptErr, "PendingTranscript validation failed")
}
}
return nil
}