github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/models/block.go (about) 1 package models 2 3 import ( 4 "github.com/onflow/flow-go/engine/access/rest/util" 5 "github.com/onflow/flow-go/model/flow" 6 ) 7 8 func (b *Block) Build( 9 block *flow.Block, 10 execResult *flow.ExecutionResult, 11 link LinkGenerator, 12 blockStatus flow.BlockStatus, 13 expand map[string]bool, 14 ) error { 15 self, err := SelfLink(block.ID(), link.BlockLink) 16 if err != nil { 17 return err 18 } 19 20 var header BlockHeader 21 header.Build(block.Header) 22 b.Header = &header 23 24 // add the payload to the response if it is specified as an expandable field 25 b.Expandable = &BlockExpandable{} 26 const ExpandableFieldPayload = "payload" 27 if expand[ExpandableFieldPayload] { 28 var payload BlockPayload 29 err := payload.Build(block.Payload) 30 if err != nil { 31 return err 32 } 33 b.Payload = &payload 34 } else { 35 // else add the payload expandable link 36 payloadExpandable, err := link.PayloadLink(block.ID()) 37 if err != nil { 38 return err 39 } 40 b.Expandable.Payload = payloadExpandable 41 } 42 43 // execution result might not yet exist 44 if execResult != nil { 45 // add the execution result to the response if it is specified as an expandable field 46 const ExpandableExecutionResult = "execution_result" 47 if expand[ExpandableExecutionResult] { 48 var exeResult ExecutionResult 49 err := exeResult.Build(execResult, link) 50 if err != nil { 51 return err 52 } 53 b.ExecutionResult = &exeResult 54 } else { 55 // else add the execution result expandable link 56 executionResultExpandable, err := link.ExecutionResultLink(execResult.ID()) 57 if err != nil { 58 return err 59 } 60 b.Expandable.ExecutionResult = executionResultExpandable 61 } 62 } 63 64 b.Links = self 65 b.BlockStatus = blockStatus.String() 66 return nil 67 } 68 69 func (b *BlockPayload) Build(payload *flow.Payload) error { 70 var blockSeal BlockSeals 71 err := blockSeal.Build(payload.Seals) 72 if err != nil { 73 return err 74 } 75 b.BlockSeals = blockSeal 76 77 var guarantees CollectionGuarantees 78 guarantees.Build(payload.Guarantees) 79 b.CollectionGuarantees = guarantees 80 81 return nil 82 } 83 84 func (b *BlockHeader) Build(header *flow.Header) { 85 b.Id = header.ID().String() 86 b.ParentId = header.ParentID.String() 87 b.Height = util.FromUint64(header.Height) 88 b.Timestamp = header.Timestamp 89 b.ParentVoterSignature = util.ToBase64(header.ParentVoterSigData) 90 } 91 92 type BlockSeals []BlockSeal 93 94 func (b *BlockSeals) Build(seals []*flow.Seal) error { 95 blkSeals := make([]BlockSeal, len(seals)) 96 for i, s := range seals { 97 var seal BlockSeal 98 err := seal.Build(s) 99 if err != nil { 100 return err 101 } 102 blkSeals[i] = seal 103 } 104 105 *b = blkSeals 106 return nil 107 } 108 109 func (b *BlockSeal) Build(seal *flow.Seal) error { 110 finalState := "" 111 if len(seal.FinalState) > 0 { // todo(sideninja) this is always true? 112 finalStateBytes, err := seal.FinalState.MarshalJSON() 113 if err != nil { 114 return err 115 } 116 finalState = string(finalStateBytes) 117 } 118 119 var aggregatedSigs AggregatedSignatures 120 aggregatedSigs.Build(seal.AggregatedApprovalSigs) 121 122 b.BlockId = seal.BlockID.String() 123 b.ResultId = seal.ResultID.String() 124 b.FinalState = finalState 125 b.AggregatedApprovalSignatures = aggregatedSigs 126 return nil 127 } 128 129 type AggregatedSignatures []AggregatedSignature 130 131 func (a *AggregatedSignatures) Build(signatures []flow.AggregatedSignature) { 132 response := make([]AggregatedSignature, len(signatures)) 133 for i, signature := range signatures { 134 var sig AggregatedSignature 135 sig.Build(signature) 136 response[i] = sig 137 } 138 139 *a = response 140 } 141 142 func (a *AggregatedSignature) Build(signature flow.AggregatedSignature) { 143 verifierSignatures := make([]string, len(signature.VerifierSignatures)) 144 for y, verifierSignature := range signature.VerifierSignatures { 145 verifierSignatures[y] = util.ToBase64(verifierSignature.Bytes()) 146 } 147 148 signerIDs := make([]string, len(signature.SignerIDs)) 149 for j, signerID := range signature.SignerIDs { 150 signerIDs[j] = signerID.String() 151 } 152 153 a.VerifierSignatures = verifierSignatures 154 a.SignerIds = signerIDs 155 }