-
Notifications
You must be signed in to change notification settings - Fork 175
/
execution_data.go
370 lines (323 loc) · 10.6 KB
/
execution_data.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package convert
import (
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/executiondatasync/execution_data"
)
// BlockExecutionDataEventPayloadsToVersion converts all event payloads to version
func BlockExecutionDataEventPayloadsToVersion(
m *entities.BlockExecutionData,
to entities.EventEncodingVersion,
) error {
if to == entities.EventEncodingVersion_CCF_V0 {
return nil
}
for i, chunk := range m.ChunkExecutionData {
for j, e := range chunk.Events {
converted, err := CcfPayloadToJsonPayload(e.Payload)
if err != nil {
return fmt.Errorf("failed to convert payload for event %d to json: %w", j, err)
}
m.ChunkExecutionData[i].Events[j].Payload = converted
}
}
return nil
}
// BlockExecutionDataToMessage converts a BlockExecutionData to a protobuf message
func BlockExecutionDataToMessage(data *execution_data.BlockExecutionData) (
*entities.BlockExecutionData,
error,
) {
chunkExecutionDatas := make([]*entities.ChunkExecutionData, len(data.ChunkExecutionDatas))
for i, chunk := range data.ChunkExecutionDatas {
chunkMessage, err := ChunkExecutionDataToMessage(chunk)
if err != nil {
return nil, err
}
chunkExecutionDatas[i] = chunkMessage
}
return &entities.BlockExecutionData{
BlockId: IdentifierToMessage(data.BlockID),
ChunkExecutionData: chunkExecutionDatas,
}, nil
}
// MessageToBlockExecutionData converts a protobuf message to a BlockExecutionData
func MessageToBlockExecutionData(
m *entities.BlockExecutionData,
chain flow.Chain,
) (*execution_data.BlockExecutionData, error) {
if m == nil {
return nil, ErrEmptyMessage
}
chunks := make([]*execution_data.ChunkExecutionData, len(m.ChunkExecutionData))
for i, chunk := range m.GetChunkExecutionData() {
convertedChunk, err := MessageToChunkExecutionData(chunk, chain)
if err != nil {
return nil, err
}
chunks[i] = convertedChunk
}
return &execution_data.BlockExecutionData{
BlockID: MessageToIdentifier(m.GetBlockId()),
ChunkExecutionDatas: chunks,
}, nil
}
// ChunkExecutionDataToMessage converts a ChunkExecutionData to a protobuf message
func ChunkExecutionDataToMessage(data *execution_data.ChunkExecutionData) (
*entities.ChunkExecutionData,
error,
) {
collection := &entities.ExecutionDataCollection{}
if data.Collection != nil {
collection = &entities.ExecutionDataCollection{
Transactions: TransactionsToMessages(data.Collection.Transactions),
}
}
events := EventsToMessages(data.Events)
if len(events) == 0 {
events = nil
}
trieUpdate, err := TrieUpdateToMessage(data.TrieUpdate)
if err != nil {
return nil, err
}
var results []*entities.ExecutionDataTransactionResult
if len(data.TransactionResults) > 0 {
results = make([]*entities.ExecutionDataTransactionResult, len(data.TransactionResults))
for i, result := range data.TransactionResults {
results[i] = &entities.ExecutionDataTransactionResult{
TransactionId: IdentifierToMessage(result.TransactionID),
Failed: result.Failed,
ComputationUsed: result.ComputationUsed,
}
}
}
return &entities.ChunkExecutionData{
Collection: collection,
Events: events,
TrieUpdate: trieUpdate,
TransactionResults: results,
}, nil
}
// MessageToChunkExecutionData converts a protobuf message to a ChunkExecutionData
func MessageToChunkExecutionData(
m *entities.ChunkExecutionData,
chain flow.Chain,
) (*execution_data.ChunkExecutionData, error) {
collection, err := messageToTrustedCollection(m.GetCollection(), chain)
if err != nil {
return nil, err
}
var trieUpdate *ledger.TrieUpdate
if m.GetTrieUpdate() != nil {
trieUpdate, err = MessageToTrieUpdate(m.GetTrieUpdate())
if err != nil {
return nil, err
}
}
events := MessagesToEvents(m.GetEvents())
if len(events) == 0 {
events = nil
}
var results []flow.LightTransactionResult
if len(m.GetTransactionResults()) > 0 {
results = make([]flow.LightTransactionResult, len(m.GetTransactionResults()))
for i, result := range m.GetTransactionResults() {
results[i] = flow.LightTransactionResult{
TransactionID: MessageToIdentifier(result.GetTransactionId()),
Failed: result.GetFailed(),
ComputationUsed: result.GetComputationUsed(),
}
}
}
return &execution_data.ChunkExecutionData{
Collection: collection,
Events: events,
TrieUpdate: trieUpdate,
TransactionResults: results,
}, nil
}
// MessageToTrieUpdate converts a protobuf message to a TrieUpdate
func MessageToTrieUpdate(m *entities.TrieUpdate) (*ledger.TrieUpdate, error) {
rootHash, err := ledger.ToRootHash(m.GetRootHash())
if err != nil {
return nil, fmt.Errorf("could not convert root hash: %w", err)
}
paths := make([]ledger.Path, len(m.GetPaths()))
for i, path := range m.GetPaths() {
convertedPath, err := ledger.ToPath(path)
if err != nil {
return nil, fmt.Errorf("could not convert path %d: %w", i, err)
}
paths[i] = convertedPath
}
payloads := make([]*ledger.Payload, len(m.Payloads))
for i, payload := range m.GetPayloads() {
keyParts := make([]ledger.KeyPart, len(payload.GetKeyPart()))
for j, keypart := range payload.GetKeyPart() {
keyParts[j] = ledger.NewKeyPart(uint16(keypart.GetType()), keypart.GetValue())
}
payloads[i] = ledger.NewPayload(ledger.NewKey(keyParts), payload.GetValue())
}
return &ledger.TrieUpdate{
RootHash: rootHash,
Paths: paths,
Payloads: payloads,
}, nil
}
// TrieUpdateToMessage converts a TrieUpdate to a protobuf message
func TrieUpdateToMessage(t *ledger.TrieUpdate) (*entities.TrieUpdate, error) {
if t == nil {
return nil, nil
}
paths := make([][]byte, len(t.Paths))
for i := range t.Paths {
paths[i] = t.Paths[i][:]
}
payloads := make([]*entities.Payload, len(t.Payloads))
for i, payload := range t.Payloads {
key, err := payload.Key()
if err != nil {
return nil, fmt.Errorf("could not convert payload %d: %w", i, err)
}
keyParts := make([]*entities.KeyPart, len(key.KeyParts))
for j, keyPart := range key.KeyParts {
keyParts[j] = &entities.KeyPart{
Type: uint32(keyPart.Type),
Value: keyPart.Value,
}
}
payloads[i] = &entities.Payload{
KeyPart: keyParts,
Value: payload.Value(),
}
}
return &entities.TrieUpdate{
RootHash: t.RootHash[:],
Paths: paths,
Payloads: payloads,
}, nil
}
// messageToTrustedCollection converts a protobuf message to a collection using the
// messageToTrustedTransaction converter to support service transactions.
func messageToTrustedCollection(
m *entities.ExecutionDataCollection,
chain flow.Chain,
) (*flow.Collection, error) {
messages := m.GetTransactions()
if len(messages) == 0 {
return &flow.Collection{}, nil
}
transactions := make([]*flow.TransactionBody, len(messages))
for i, message := range messages {
transaction, err := messageToTrustedTransaction(message, chain)
if err != nil {
return nil, fmt.Errorf("could not convert transaction %d: %w", i, err)
}
transactions[i] = &transaction
}
return &flow.Collection{Transactions: transactions}, nil
}
// messageToTrustedTransaction converts a transaction message to a transaction body.
// This is useful when converting transactions from trusted state like BlockExecutionData which
// contain service transactions that do not conform to external transaction format.
func messageToTrustedTransaction(
m *entities.Transaction,
chain flow.Chain,
) (flow.TransactionBody, error) {
if m == nil {
return flow.TransactionBody{}, ErrEmptyMessage
}
t := flow.NewTransactionBody()
proposalKey := m.GetProposalKey()
if proposalKey != nil {
proposalAddress, err := insecureAddress(proposalKey.GetAddress())
if err != nil {
return *t, fmt.Errorf("could not convert proposer address: %w", err)
}
t.SetProposalKey(proposalAddress, uint64(proposalKey.GetKeyId()), proposalKey.GetSequenceNumber())
}
payer := m.GetPayer()
if payer != nil {
payerAddress, err := insecureAddress(payer)
if err != nil {
return *t, fmt.Errorf("could not convert payer address: %w", err)
}
t.SetPayer(payerAddress)
}
for _, authorizer := range m.GetAuthorizers() {
authorizerAddress, err := Address(authorizer, chain)
if err != nil {
return *t, fmt.Errorf("could not convert authorizer address: %w", err)
}
t.AddAuthorizer(authorizerAddress)
}
for _, sig := range m.GetPayloadSignatures() {
addr, err := Address(sig.GetAddress(), chain)
if err != nil {
return *t, fmt.Errorf("could not convert payload signature address: %w", err)
}
t.AddPayloadSignature(addr, uint64(sig.GetKeyId()), sig.GetSignature())
}
for _, sig := range m.GetEnvelopeSignatures() {
addr, err := Address(sig.GetAddress(), chain)
if err != nil {
return *t, fmt.Errorf("could not convert envelope signature address: %w", err)
}
t.AddEnvelopeSignature(addr, uint64(sig.GetKeyId()), sig.GetSignature())
}
t.SetScript(m.GetScript())
t.SetArguments(m.GetArguments())
t.SetReferenceBlockID(flow.HashToID(m.GetReferenceBlockId()))
t.SetGasLimit(m.GetGasLimit())
return *t, nil
}
func MessageToRegisterID(m *entities.RegisterID, chain flow.Chain) (flow.RegisterID, error) {
if m == nil {
return flow.RegisterID{}, ErrEmptyMessage
}
owner := flow.EmptyAddress
if len(m.GetOwner()) > 0 {
var err error
owner, err = Address(m.GetOwner(), chain)
if err != nil {
return flow.RegisterID{}, fmt.Errorf("could not convert owner address: %w", err)
}
}
key := string(m.GetKey())
return flow.NewRegisterID(owner, key), nil
}
// MessagesToRegisterIDs converts a protobuf message to RegisterIDs
func MessagesToRegisterIDs(m []*entities.RegisterID, chain flow.Chain) (flow.RegisterIDs, error) {
if m == nil {
return nil, ErrEmptyMessage
}
result := make(flow.RegisterIDs, len(m))
for i, entry := range m {
regID, err := MessageToRegisterID(entry, chain)
if err != nil {
return nil, fmt.Errorf("failed to convert register id %d: %w", i, err)
}
result[i] = regID
}
return result, nil
}
func RegisterIDToMessage(id flow.RegisterID) *entities.RegisterID {
return &entities.RegisterID{
Owner: []byte(id.Owner),
Key: []byte(id.Key),
}
}
// insecureAddress converts a raw address to a flow.Address, skipping validation
// This is useful when converting transactions from trusted state like BlockExecutionData.
// This should only be used for trusted inputs
func insecureAddress(rawAddress []byte) (flow.Address, error) {
if len(rawAddress) == 0 {
return flow.EmptyAddress, status.Error(codes.InvalidArgument, "address cannot be empty")
}
return flow.BytesToAddress(rawAddress), nil
}