github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/models/block.go (about)

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