-
Notifications
You must be signed in to change notification settings - Fork 175
/
block.go
153 lines (131 loc) · 3.72 KB
/
block.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
package models
import (
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)
func (b *Block) Build(
block *flow.Block,
execResult *flow.ExecutionResult,
link LinkGenerator,
expand map[string]bool,
) error {
self, err := SelfLink(block.ID(), link.BlockLink)
if err != nil {
return err
}
var header BlockHeader
header.Build(block.Header)
b.Header = &header
// add the payload to the response if it is specified as an expandable field
b.Expandable = &BlockExpandable{}
const ExpandableFieldPayload = "payload"
if expand[ExpandableFieldPayload] {
var payload BlockPayload
err := payload.Build(block.Payload)
if err != nil {
return err
}
b.Payload = &payload
} else {
// else add the payload expandable link
payloadExpandable, err := link.PayloadLink(block.ID())
if err != nil {
return err
}
b.Expandable.Payload = payloadExpandable
}
// execution result might not yet exist
if execResult != nil {
// add the execution result to the response if it is specified as an expandable field
const ExpandableExecutionResult = "execution_result"
if expand[ExpandableExecutionResult] {
var exeResult ExecutionResult
err := exeResult.Build(execResult, link)
if err != nil {
return err
}
b.ExecutionResult = &exeResult
} else {
// else add the execution result expandable link
executionResultExpandable, err := link.ExecutionResultLink(execResult.ID())
if err != nil {
return err
}
b.Expandable.ExecutionResult = executionResultExpandable
}
}
b.Links = self
return nil
}
func (b *BlockPayload) Build(payload *flow.Payload) error {
var blockSeal BlockSeals
err := blockSeal.Build(payload.Seals)
if err != nil {
return err
}
b.BlockSeals = blockSeal
var guarantees CollectionGuarantees
guarantees.Build(payload.Guarantees)
b.CollectionGuarantees = guarantees
return nil
}
func (b *BlockHeader) Build(header *flow.Header) {
b.Id = header.ID().String()
b.ParentId = header.ParentID.String()
b.Height = util.FromUint64(header.Height)
b.Timestamp = header.Timestamp
b.ParentVoterSignature = util.ToBase64(header.ParentVoterSigData)
}
type BlockSeals []BlockSeal
func (b *BlockSeals) Build(seals []*flow.Seal) error {
blkSeals := make([]BlockSeal, len(seals))
for i, s := range seals {
var seal BlockSeal
err := seal.Build(s)
if err != nil {
return err
}
blkSeals[i] = seal
}
*b = blkSeals
return nil
}
func (b *BlockSeal) Build(seal *flow.Seal) error {
finalState := ""
if len(seal.FinalState) > 0 { // todo(sideninja) this is always true?
finalStateBytes, err := seal.FinalState.MarshalJSON()
if err != nil {
return err
}
finalState = string(finalStateBytes)
}
var aggregatedSigs AggregatedSignatures
aggregatedSigs.Build(seal.AggregatedApprovalSigs)
b.BlockId = seal.BlockID.String()
b.ResultId = seal.ResultID.String()
b.FinalState = finalState
b.AggregatedApprovalSignatures = aggregatedSigs
return nil
}
type AggregatedSignatures []AggregatedSignature
func (a *AggregatedSignatures) Build(signatures []flow.AggregatedSignature) {
response := make([]AggregatedSignature, len(signatures))
for i, signature := range signatures {
var sig AggregatedSignature
sig.Build(signature)
response[i] = sig
}
*a = response
}
func (a *AggregatedSignature) Build(signature flow.AggregatedSignature) {
verifierSignatures := make([]string, len(signature.VerifierSignatures))
for y, verifierSignature := range signature.VerifierSignatures {
verifierSignatures[y] = util.ToBase64(verifierSignature.Bytes())
}
signerIDs := make([]string, len(signature.SignerIDs))
for j, signerID := range signature.SignerIDs {
signerIDs[j] = signerID.String()
}
a.VerifierSignatures = verifierSignatures
a.SignerIds = signerIDs
}