github.com/koko1123/flow-go-1@v0.29.6/model/messages/consensus.go (about)

     1  package messages
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/model/flow"
     5  )
     6  
     7  // UntrustedExecutionResult is a duplicate of flow.ExecutionResult used within
     8  // untrusted messages. It exists only to provide a memory-safe structure for
     9  // decoding messages and should be replaced in the future by updating the core
    10  // flow.ExecutionResult type.
    11  // Deprecated: Please update flow.ExecutionResult to use []flow.Chunk, then
    12  // replace instances of this type with flow.ExecutionResult
    13  type UntrustedExecutionResult struct {
    14  	PreviousResultID flow.Identifier
    15  	BlockID          flow.Identifier
    16  	Chunks           []flow.Chunk
    17  	ServiceEvents    flow.ServiceEventList
    18  	ExecutionDataID  flow.Identifier
    19  }
    20  
    21  // ToInternal returns the internal representation of the type.
    22  func (ur *UntrustedExecutionResult) ToInternal() *flow.ExecutionResult {
    23  	result := flow.ExecutionResult{
    24  		PreviousResultID: ur.PreviousResultID,
    25  		BlockID:          ur.BlockID,
    26  		Chunks:           make(flow.ChunkList, 0, len(ur.Chunks)),
    27  		ServiceEvents:    ur.ServiceEvents,
    28  		ExecutionDataID:  ur.ExecutionDataID,
    29  	}
    30  	for _, chunk := range ur.Chunks {
    31  		chunk := chunk
    32  		result.Chunks = append(result.Chunks, &chunk)
    33  	}
    34  	return &result
    35  }
    36  
    37  // UntrustedExecutionResultFromInternal converts the internal flow.ExecutionResult representation
    38  // to the representation used in untrusted messages.
    39  func UntrustedExecutionResultFromInternal(internal *flow.ExecutionResult) UntrustedExecutionResult {
    40  	result := UntrustedExecutionResult{
    41  		PreviousResultID: internal.PreviousResultID,
    42  		BlockID:          internal.BlockID,
    43  		ServiceEvents:    internal.ServiceEvents,
    44  		ExecutionDataID:  internal.ExecutionDataID,
    45  	}
    46  	for _, chunk := range internal.Chunks {
    47  		result.Chunks = append(result.Chunks, *chunk)
    48  	}
    49  	return result
    50  }
    51  
    52  // UntrustedBlockPayload is a duplicate of flow.Payload used within
    53  // untrusted messages. It exists only to provide a memory-safe structure for
    54  // decoding messages and should be replaced in the future by updating the core
    55  // flow.Payload type.
    56  // Deprecated: Please update flow.Payload to use []flow.Guarantee etc., then
    57  // replace instances of this type with flow.Payload
    58  type UntrustedBlockPayload struct {
    59  	Guarantees []flow.CollectionGuarantee
    60  	Seals      []flow.Seal
    61  	Receipts   []flow.ExecutionReceiptMeta
    62  	Results    []UntrustedExecutionResult
    63  }
    64  
    65  // UntrustedBlock is a duplicate of flow.Block used within
    66  // untrusted messages. It exists only to provide a memory-safe structure for
    67  // decoding messages and should be replaced in the future by updating the core
    68  // flow.Block type.
    69  // Deprecated: Please update flow.Payload to use []flow.Guarantee etc., then
    70  // replace instances of this type with flow.Block
    71  type UntrustedBlock struct {
    72  	Header  flow.Header
    73  	Payload UntrustedBlockPayload
    74  }
    75  
    76  // ToInternal returns the internal representation of the type.
    77  func (ub *UntrustedBlock) ToInternal() *flow.Block {
    78  	block := flow.Block{
    79  		Header:  &ub.Header,
    80  		Payload: &flow.Payload{},
    81  	}
    82  	for _, guarantee := range ub.Payload.Guarantees {
    83  		guarantee := guarantee
    84  		block.Payload.Guarantees = append(block.Payload.Guarantees, &guarantee)
    85  	}
    86  	for _, seal := range ub.Payload.Seals {
    87  		seal := seal
    88  		block.Payload.Seals = append(block.Payload.Seals, &seal)
    89  	}
    90  	for _, receipt := range ub.Payload.Receipts {
    91  		receipt := receipt
    92  		block.Payload.Receipts = append(block.Payload.Receipts, &receipt)
    93  	}
    94  	for _, result := range ub.Payload.Results {
    95  		result := result
    96  		block.Payload.Results = append(block.Payload.Results, result.ToInternal())
    97  	}
    98  
    99  	return &block
   100  }
   101  
   102  // UntrustedBlockFromInternal converts the internal flow.Block representation
   103  // to the representation used in untrusted messages.
   104  func UntrustedBlockFromInternal(flowBlock *flow.Block) UntrustedBlock {
   105  	block := UntrustedBlock{
   106  		Header: *flowBlock.Header,
   107  	}
   108  	for _, guarantee := range flowBlock.Payload.Guarantees {
   109  		block.Payload.Guarantees = append(block.Payload.Guarantees, *guarantee)
   110  	}
   111  	for _, seal := range flowBlock.Payload.Seals {
   112  		block.Payload.Seals = append(block.Payload.Seals, *seal)
   113  	}
   114  	for _, receipt := range flowBlock.Payload.Receipts {
   115  		block.Payload.Receipts = append(block.Payload.Receipts, *receipt)
   116  	}
   117  	for _, result := range flowBlock.Payload.Results {
   118  		block.Payload.Results = append(block.Payload.Results, UntrustedExecutionResultFromInternal(result))
   119  	}
   120  	return block
   121  }
   122  
   123  // BlockProposal is part of the consensus protocol and represents the leader
   124  // of a consensus round pushing a new proposal to the network.
   125  type BlockProposal struct {
   126  	Block UntrustedBlock
   127  }
   128  
   129  func NewBlockProposal(internal *flow.Block) *BlockProposal {
   130  	return &BlockProposal{
   131  		Block: UntrustedBlockFromInternal(internal),
   132  	}
   133  }
   134  
   135  // BlockVote is part of the consensus protocol and represents a consensus node
   136  // voting on the proposal of the leader of a given round.
   137  type BlockVote struct {
   138  	BlockID flow.Identifier
   139  	View    uint64
   140  	SigData []byte
   141  }