-
Notifications
You must be signed in to change notification settings - Fork 178
/
execution_results.go
174 lines (159 loc) · 5.84 KB
/
execution_results.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
package convert
import (
"fmt"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow-go/model/flow"
)
// ExecutionResultToMessage converts an execution result to a protobuf message
func ExecutionResultToMessage(er *flow.ExecutionResult) (
*entities.ExecutionResult,
error,
) {
chunks := make([]*entities.Chunk, len(er.Chunks))
for i, chunk := range er.Chunks {
chunks[i] = ChunkToMessage(chunk)
}
serviceEvents := make([]*entities.ServiceEvent, len(er.ServiceEvents))
var err error
for i, serviceEvent := range er.ServiceEvents {
serviceEvents[i], err = ServiceEventToMessage(serviceEvent)
if err != nil {
return nil, fmt.Errorf("error while convering service event %d: %w", i, err)
}
}
return &entities.ExecutionResult{
PreviousResultId: IdentifierToMessage(er.PreviousResultID),
BlockId: IdentifierToMessage(er.BlockID),
Chunks: chunks,
ServiceEvents: serviceEvents,
ExecutionDataId: IdentifierToMessage(er.ExecutionDataID),
}, nil
}
// MessageToExecutionResult converts a protobuf message to an execution result
func MessageToExecutionResult(m *entities.ExecutionResult) (
*flow.ExecutionResult,
error,
) {
// convert Chunks
parsedChunks, err := MessagesToChunkList(m.Chunks)
if err != nil {
return nil, fmt.Errorf("failed to parse messages to ChunkList: %w", err)
}
// convert ServiceEvents
parsedServiceEvents, err := MessagesToServiceEventList(m.ServiceEvents)
if err != nil {
return nil, err
}
return &flow.ExecutionResult{
PreviousResultID: MessageToIdentifier(m.PreviousResultId),
BlockID: MessageToIdentifier(m.BlockId),
Chunks: parsedChunks,
ServiceEvents: parsedServiceEvents,
ExecutionDataID: MessageToIdentifier(m.ExecutionDataId),
}, nil
}
// ExecutionResultsToMessages converts a slice of execution results to a slice of protobuf messages
func ExecutionResultsToMessages(e []*flow.ExecutionResult) (
[]*entities.ExecutionResult,
error,
) {
execResults := make([]*entities.ExecutionResult, len(e))
for i, execRes := range e {
parsedExecResult, err := ExecutionResultToMessage(execRes)
if err != nil {
return nil, err
}
execResults[i] = parsedExecResult
}
return execResults, nil
}
// MessagesToExecutionResults converts a slice of protobuf messages to a slice of execution results
func MessagesToExecutionResults(m []*entities.ExecutionResult) (
[]*flow.ExecutionResult,
error,
) {
execResults := make([]*flow.ExecutionResult, len(m))
for i, e := range m {
parsedExecResult, err := MessageToExecutionResult(e)
if err != nil {
return nil, fmt.Errorf("failed to convert message at index %d to execution result: %w", i, err)
}
execResults[i] = parsedExecResult
}
return execResults, nil
}
// ExecutionResultMetaListToMessages converts an execution result meta list to a slice of protobuf messages
func ExecutionResultMetaListToMessages(e flow.ExecutionReceiptMetaList) []*entities.ExecutionReceiptMeta {
messageList := make([]*entities.ExecutionReceiptMeta, len(e))
for i, execMeta := range e {
messageList[i] = &entities.ExecutionReceiptMeta{
ExecutorId: IdentifierToMessage(execMeta.ExecutorID),
ResultId: IdentifierToMessage(execMeta.ResultID),
Spocks: SignaturesToMessages(execMeta.Spocks),
ExecutorSignature: MessageToSignature(execMeta.ExecutorSignature),
}
}
return messageList
}
// MessagesToExecutionResultMetaList converts a slice of protobuf messages to an execution result meta list
func MessagesToExecutionResultMetaList(m []*entities.ExecutionReceiptMeta) flow.ExecutionReceiptMetaList {
execMetaList := make([]*flow.ExecutionReceiptMeta, len(m))
for i, message := range m {
execMetaList[i] = &flow.ExecutionReceiptMeta{
ExecutorID: MessageToIdentifier(message.ExecutorId),
ResultID: MessageToIdentifier(message.ResultId),
Spocks: MessagesToSignatures(message.Spocks),
ExecutorSignature: MessageToSignature(message.ExecutorSignature),
}
}
return execMetaList[:]
}
// ChunkToMessage converts a chunk to a protobuf message
func ChunkToMessage(chunk *flow.Chunk) *entities.Chunk {
return &entities.Chunk{
CollectionIndex: uint32(chunk.CollectionIndex),
StartState: StateCommitmentToMessage(chunk.StartState),
EventCollection: IdentifierToMessage(chunk.EventCollection),
BlockId: IdentifierToMessage(chunk.BlockID),
TotalComputationUsed: chunk.TotalComputationUsed,
NumberOfTransactions: uint32(chunk.NumberOfTransactions),
Index: chunk.Index,
EndState: StateCommitmentToMessage(chunk.EndState),
}
}
// MessageToChunk converts a protobuf message to a chunk
func MessageToChunk(m *entities.Chunk) (*flow.Chunk, error) {
startState, err := flow.ToStateCommitment(m.StartState)
if err != nil {
return nil, fmt.Errorf("failed to parse Message start state to Chunk: %w", err)
}
endState, err := flow.ToStateCommitment(m.EndState)
if err != nil {
return nil, fmt.Errorf("failed to parse Message end state to Chunk: %w", err)
}
chunkBody := flow.ChunkBody{
CollectionIndex: uint(m.CollectionIndex),
StartState: startState,
EventCollection: MessageToIdentifier(m.EventCollection),
BlockID: MessageToIdentifier(m.BlockId),
TotalComputationUsed: m.TotalComputationUsed,
NumberOfTransactions: uint64(m.NumberOfTransactions),
}
return &flow.Chunk{
ChunkBody: chunkBody,
Index: m.Index,
EndState: endState,
}, nil
}
// MessagesToChunkList converts a slice of protobuf messages to a chunk list
func MessagesToChunkList(m []*entities.Chunk) (flow.ChunkList, error) {
parsedChunks := make(flow.ChunkList, len(m))
for i, chunk := range m {
parsedChunk, err := MessageToChunk(chunk)
if err != nil {
return nil, fmt.Errorf("failed to parse message at index %d to chunk: %w", i, err)
}
parsedChunks[i] = parsedChunk
}
return parsedChunks, nil
}