Skip to content

Commit 7c952ed

Browse files
Merge branch 'master' into illia-malachyn/784-add-more-examples
2 parents 526381d + 1ba8047 commit 7c952ed

File tree

3 files changed

+216
-16
lines changed

3 files changed

+216
-16
lines changed

access/grpc/convert/convert.go

+147-12
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,28 @@ func BlockToMessage(b flow.Block) (*entities.Block, error) {
172172
return nil, err
173173
}
174174

175+
execReceipts, err := ExecutionReceiptMetaListToMessage(b.ExecutionReceiptMetaList)
176+
if err != nil {
177+
return nil, fmt.Errorf("error converting execution receipts: %w", err)
178+
}
179+
180+
execResults, err := ExecutionResultsToMessage(b.ExecutionResultsList)
181+
if err != nil {
182+
return nil, fmt.Errorf("error converting execution results: %w", err)
183+
}
184+
175185
return &entities.Block{
176-
Id: b.BlockHeader.ID.Bytes(),
177-
ParentId: b.BlockHeader.ParentID.Bytes(),
178-
Height: b.BlockHeader.Height,
179-
Timestamp: t,
180-
CollectionGuarantees: CollectionGuaranteesToMessages(b.BlockPayload.CollectionGuarantees),
181-
BlockSeals: BlockSealsToMessages(b.BlockPayload.Seals),
182-
BlockHeader: header,
186+
Id: b.BlockHeader.ID.Bytes(),
187+
ParentId: b.BlockHeader.ParentID.Bytes(),
188+
Height: b.BlockHeader.Height,
189+
Timestamp: t,
190+
CollectionGuarantees: CollectionGuaranteesToMessages(b.BlockPayload.CollectionGuarantees),
191+
BlockSeals: BlockSealsToMessages(b.BlockPayload.Seals),
192+
Signatures: b.Signatures,
193+
ExecutionReceiptMetaList: execReceipts,
194+
ExecutionResultList: execResults,
195+
BlockHeader: header,
196+
ProtocolStateId: b.ProtocolStateID.Bytes(),
183197
}, nil
184198
}
185199

@@ -193,7 +207,7 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) {
193207

194208
tc, err := MessageToTimeoutCertificate(m.BlockHeader.GetLastViewTc())
195209
if err != nil {
196-
return flow.Block{}, err
210+
return flow.Block{}, fmt.Errorf("error converting timeout certificate: %w", err)
197211
}
198212

199213
header := &flow.BlockHeader{
@@ -214,17 +228,31 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) {
214228

215229
guarantees, err := MessagesToCollectionGuarantees(m.GetCollectionGuarantees())
216230
if err != nil {
217-
return flow.Block{}, err
231+
return flow.Block{}, fmt.Errorf("error converting collection guarantees: %w", err)
218232
}
219233

220234
seals, err := MessagesToBlockSeals(m.GetBlockSeals())
221235
if err != nil {
222-
return flow.Block{}, err
236+
return flow.Block{}, fmt.Errorf("error converting block seals: %w", err)
237+
}
238+
239+
executionReceiptsMeta, err := MessageToExecutionReceiptMetaList(m.GetExecutionReceiptMetaList())
240+
if err != nil {
241+
return flow.Block{}, fmt.Errorf("error converting execution receipt meta list: %w", err)
242+
}
243+
244+
executionResults, err := MessageToExecutionResults(m.GetExecutionResultList())
245+
if err != nil {
246+
return flow.Block{}, fmt.Errorf("error converting execution results: %w", err)
223247
}
224248

225249
payload := &flow.BlockPayload{
226-
CollectionGuarantees: guarantees,
227-
Seals: seals,
250+
CollectionGuarantees: guarantees,
251+
Seals: seals,
252+
Signatures: m.GetSignatures(),
253+
ExecutionReceiptMetaList: executionReceiptsMeta,
254+
ExecutionResultsList: executionResults,
255+
ProtocolStateID: flow.HashToID(m.GetProtocolStateId()),
228256
}
229257

230258
return flow.Block{
@@ -233,6 +261,54 @@ func MessageToBlock(m *entities.Block) (flow.Block, error) {
233261
}, nil
234262
}
235263

264+
func MessageToExecutionReceiptMeta(m *entities.ExecutionReceiptMeta) (*flow.ExecutionReceiptMeta, error) {
265+
if m == nil {
266+
return nil, ErrEmptyMessage
267+
}
268+
269+
return &flow.ExecutionReceiptMeta{
270+
ExecutorID: flow.HashToID(m.GetExecutorId()),
271+
ResultID: flow.HashToID(m.GetResultId()),
272+
Spocks: m.GetSpocks(),
273+
ExecutorSignature: m.GetExecutorSignature(),
274+
}, nil
275+
}
276+
277+
func ExecutionReceiptMetaToMessage(receipt flow.ExecutionReceiptMeta) (*entities.ExecutionReceiptMeta, error) {
278+
return &entities.ExecutionReceiptMeta{
279+
ExecutorId: receipt.ExecutorID.Bytes(),
280+
ResultId: receipt.ResultID.Bytes(),
281+
Spocks: receipt.Spocks,
282+
ExecutorSignature: receipt.ExecutorSignature,
283+
}, nil
284+
}
285+
286+
func MessageToExecutionReceiptMetaList(m []*entities.ExecutionReceiptMeta) ([]*flow.ExecutionReceiptMeta, error) {
287+
results := make([]*flow.ExecutionReceiptMeta, len(m))
288+
289+
for i, entity := range m {
290+
executionReceiptMeta, err := MessageToExecutionReceiptMeta(entity)
291+
if err != nil {
292+
return nil, err
293+
}
294+
results[i] = executionReceiptMeta
295+
}
296+
297+
return results, nil
298+
}
299+
300+
func ExecutionReceiptMetaListToMessage(receipts []*flow.ExecutionReceiptMeta) ([]*entities.ExecutionReceiptMeta, error) {
301+
results := make([]*entities.ExecutionReceiptMeta, len(receipts))
302+
for i, receipt := range receipts {
303+
executionReceiptMeta, err := ExecutionReceiptMetaToMessage(*receipt)
304+
if err != nil {
305+
return nil, err
306+
}
307+
results[i] = executionReceiptMeta
308+
}
309+
return results, nil
310+
}
311+
236312
func BlockHeaderToMessage(b flow.BlockHeader) (*entities.BlockHeader, error) {
237313
t := timestamppb.New(b.Timestamp)
238314
tc, err := TimeoutCertificateToMessage(b.LastViewTimeoutCertificate)
@@ -896,6 +972,65 @@ func MessageToExecutionResult(execResult *entities.ExecutionResult) (*flow.Execu
896972
}, nil
897973
}
898974

975+
func ExecutionResultToMessage(result flow.ExecutionResult) (*entities.ExecutionResult, error) {
976+
chunks := make([]*entities.Chunk, len(result.Chunks))
977+
for i, chunk := range result.Chunks {
978+
chunks[i] = &entities.Chunk{
979+
CollectionIndex: uint32(chunk.CollectionIndex),
980+
StartState: IdentifierToMessage(flow.Identifier(chunk.StartState)),
981+
EventCollection: chunk.EventCollection,
982+
BlockId: chunk.BlockID.Bytes(),
983+
TotalComputationUsed: chunk.TotalComputationUsed,
984+
NumberOfTransactions: uint32(chunk.NumberOfTransactions),
985+
Index: chunk.Index,
986+
EndState: IdentifierToMessage(flow.Identifier(chunk.EndState)),
987+
}
988+
}
989+
990+
serviceEvents := make([]*entities.ServiceEvent, len(result.ServiceEvents))
991+
for i, event := range result.ServiceEvents {
992+
serviceEvents[i] = &entities.ServiceEvent{
993+
Type: event.Type,
994+
Payload: event.Payload,
995+
}
996+
}
997+
998+
return &entities.ExecutionResult{
999+
PreviousResultId: result.PreviousResultID.Bytes(),
1000+
BlockId: result.BlockID.Bytes(),
1001+
Chunks: chunks,
1002+
ServiceEvents: serviceEvents,
1003+
}, nil
1004+
}
1005+
1006+
func MessageToExecutionResults(m []*entities.ExecutionResult) ([]*flow.ExecutionResult, error) {
1007+
results := make([]*flow.ExecutionResult, len(m))
1008+
1009+
for i, result := range m {
1010+
res, err := MessageToExecutionResult(result)
1011+
if err != nil {
1012+
return nil, err
1013+
}
1014+
results[i] = res
1015+
}
1016+
1017+
return results, nil
1018+
}
1019+
1020+
func ExecutionResultsToMessage(execResults []*flow.ExecutionResult) ([]*entities.ExecutionResult, error) {
1021+
results := make([]*entities.ExecutionResult, len(execResults))
1022+
1023+
for i, result := range execResults {
1024+
res, err := ExecutionResultToMessage(*result)
1025+
if err != nil {
1026+
return nil, err
1027+
}
1028+
results[i] = res
1029+
}
1030+
1031+
return results, nil
1032+
}
1033+
8991034
func BlockExecutionDataToMessage(
9001035
execData *flow.ExecutionData,
9011036
) (*entities.BlockExecutionData, error) {

block.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,19 @@ func BlockStatusFromString(s string) BlockStatus {
8888
//
8989
// A payload contains the collection guarantees and seals for a block.
9090
type BlockPayload struct {
91-
CollectionGuarantees []*CollectionGuarantee
92-
Seals []*BlockSeal
91+
CollectionGuarantees []*CollectionGuarantee
92+
Seals []*BlockSeal
93+
Signatures [][]byte
94+
ExecutionReceiptMetaList []*ExecutionReceiptMeta
95+
ExecutionResultsList []*ExecutionResult
96+
ProtocolStateID Identifier
97+
}
98+
99+
type ExecutionReceiptMeta struct {
100+
ExecutorID Identifier
101+
ResultID Identifier
102+
Spocks [][]byte
103+
ExecutorSignature []byte
93104
}
94105

95106
// BlockSeal is the attestation by verification nodes that the transactions in a previously

test/entities.go

+56-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ type Blocks struct {
127127
guarantees *CollectionGuarantees
128128
seals *BlockSeals
129129
signatures *Signatures
130+
ids *Identifiers
131+
bytes *Bytes
132+
chunks *ChuckExecutionResult
130133
}
131134

132135
func BlockGenerator() *Blocks {
@@ -135,6 +138,9 @@ func BlockGenerator() *Blocks {
135138
guarantees: CollectionGuaranteeGenerator(),
136139
seals: BlockSealGenerator(),
137140
signatures: SignaturesGenerator(),
141+
ids: IdentifierGenerator(),
142+
bytes: BytesGenerator(),
143+
chunks: ChuckExecutionResultGenerator(),
138144
}
139145
}
140146

@@ -151,9 +157,32 @@ func (g *Blocks) New() *flow.Block {
151157
g.seals.New(),
152158
}
153159

160+
executionReceiptMetaList := []*flow.ExecutionReceiptMeta{{
161+
ExecutorID: g.ids.New(),
162+
ResultID: g.ids.New(),
163+
Spocks: [][]byte{g.bytes.New()},
164+
ExecutorSignature: g.bytes.New(),
165+
}}
166+
167+
serviceEvents := []*flow.ServiceEvent{{
168+
Type: string(g.bytes.New()),
169+
Payload: g.bytes.New(),
170+
}}
171+
172+
executionResults := []*flow.ExecutionResult{{
173+
PreviousResultID: g.ids.New(),
174+
BlockID: g.ids.New(),
175+
Chunks: []*flow.Chunk{g.chunks.New()},
176+
ServiceEvents: serviceEvents,
177+
}}
178+
154179
payload := flow.BlockPayload{
155-
CollectionGuarantees: guarantees,
156-
Seals: seals,
180+
CollectionGuarantees: guarantees,
181+
Seals: seals,
182+
Signatures: g.signatures.New(),
183+
ExecutionReceiptMetaList: executionReceiptMetaList,
184+
ExecutionResultsList: executionResults,
185+
ProtocolStateID: g.ids.New(),
157186
}
158187

159188
return &flow.Block{
@@ -639,3 +668,28 @@ func (g *Bytes) New() []byte {
639668
}
640669
return randomBytes
641670
}
671+
672+
type ChuckExecutionResult struct {
673+
ids *Identifiers
674+
bytes *Bytes
675+
}
676+
677+
func ChuckExecutionResultGenerator() *ChuckExecutionResult {
678+
return &ChuckExecutionResult{
679+
ids: IdentifierGenerator(),
680+
bytes: BytesGenerator(),
681+
}
682+
}
683+
684+
func (g *ChuckExecutionResult) New() *flow.Chunk {
685+
return &flow.Chunk{
686+
CollectionIndex: 42,
687+
StartState: flow.StateCommitment(g.ids.New()),
688+
EventCollection: g.bytes.New(),
689+
BlockID: g.ids.New(),
690+
TotalComputationUsed: 42,
691+
NumberOfTransactions: 42,
692+
Index: 42,
693+
EndState: flow.StateCommitment(g.ids.New()),
694+
}
695+
}

0 commit comments

Comments
 (0)