github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/incorporated_result.go (about)

     1  package flow
     2  
     3  // IncorporatedResult is a wrapper around an ExecutionResult which contains the
     4  // ID of the first block on its fork in which it was incorporated.
     5  type IncorporatedResult struct {
     6  	// IncorporatedBlockID is the ID of the first block on its fork where a
     7  	// receipt for this result was incorporated. Within a fork, multiple blocks
     8  	// may contain receipts for the same result; only the first one is used to
     9  	// compute the random beacon of the result's chunk assignment.
    10  	IncorporatedBlockID Identifier
    11  
    12  	// Result is the ExecutionResult contained in the ExecutionReceipt that was
    13  	// incorporated in the payload of IncorporatedBlockID.
    14  	Result *ExecutionResult
    15  }
    16  
    17  func NewIncorporatedResult(incorporatedBlockID Identifier, result *ExecutionResult) *IncorporatedResult {
    18  	return &IncorporatedResult{
    19  		IncorporatedBlockID: incorporatedBlockID,
    20  		Result:              result,
    21  	}
    22  }
    23  
    24  // ID implements flow.Entity.ID for IncorporatedResult to make it capable of
    25  // being stored directly in mempools and storage.
    26  func (ir *IncorporatedResult) ID() Identifier {
    27  	return MakeID([2]Identifier{ir.IncorporatedBlockID, ir.Result.ID()})
    28  }
    29  
    30  // CheckSum implements flow.Entity.CheckSum for IncorporatedResult to make it
    31  // capable of being stored directly in mempools and storage.
    32  func (ir *IncorporatedResult) Checksum() Identifier {
    33  	return MakeID(ir)
    34  }
    35  
    36  /*******************************************************************************
    37  GROUPING allows to split a list incorporated results by some property
    38  *******************************************************************************/
    39  
    40  // IncorporatedResultList is a slice of IncorporatedResults with the additional
    41  // functionality to group them by various properties
    42  type IncorporatedResultList []*IncorporatedResult
    43  
    44  // IncorporatedResultGroupedList is a partition of an IncorporatedResultList
    45  type IncorporatedResultGroupedList map[Identifier]IncorporatedResultList
    46  
    47  // IncorporatedResultGroupingFunction is a function that assigns an identifier to each IncorporatedResult
    48  type IncorporatedResultGroupingFunction func(*IncorporatedResult) Identifier
    49  
    50  // GroupBy partitions the IncorporatedResultList. All IncorporatedResults that are
    51  // mapped by the grouping function to the same identifier are placed in the same group.
    52  // Within each group, the order and multiplicity of the IncorporatedResults is preserved.
    53  func (l IncorporatedResultList) GroupBy(grouper IncorporatedResultGroupingFunction) IncorporatedResultGroupedList {
    54  	groups := make(map[Identifier]IncorporatedResultList)
    55  	for _, ir := range l {
    56  		groupID := grouper(ir)
    57  		groups[groupID] = append(groups[groupID], ir)
    58  	}
    59  	return groups
    60  }
    61  
    62  // GroupByIncorporatedBlockID partitions the IncorporatedResultList by the ID of the block that
    63  // incorporates the result. Within each group, the order and multiplicity of the
    64  // IncorporatedResults is preserved.
    65  func (l IncorporatedResultList) GroupByIncorporatedBlockID() IncorporatedResultGroupedList {
    66  	grouper := func(ir *IncorporatedResult) Identifier { return ir.IncorporatedBlockID }
    67  	return l.GroupBy(grouper)
    68  }
    69  
    70  // GroupByResultID partitions the IncorporatedResultList by the Results' IDs.
    71  // Within each group, the order and multiplicity of the IncorporatedResults is preserved.
    72  func (l IncorporatedResultList) GroupByResultID() IncorporatedResultGroupedList {
    73  	grouper := func(ir *IncorporatedResult) Identifier { return ir.Result.ID() }
    74  	return l.GroupBy(grouper)
    75  }
    76  
    77  // GroupByExecutedBlockID partitions the IncorporatedResultList by the IDs of the executed blocks.
    78  // Within each group, the order and multiplicity of the IncorporatedResults is preserved.
    79  func (l IncorporatedResultList) GroupByExecutedBlockID() IncorporatedResultGroupedList {
    80  	grouper := func(ir *IncorporatedResult) Identifier { return ir.Result.BlockID }
    81  	return l.GroupBy(grouper)
    82  }
    83  
    84  // Size returns the number of IncorporatedResults in the list
    85  func (l IncorporatedResultList) Size() int {
    86  	return len(l)
    87  }
    88  
    89  // GetGroup returns the IncorporatedResults that were mapped to the same identifier by the
    90  // grouping function. Returns an empty (nil) IncorporatedResultList if groupID does not exist.
    91  func (g IncorporatedResultGroupedList) GetGroup(groupID Identifier) IncorporatedResultList {
    92  	return g[groupID]
    93  }
    94  
    95  // NumberGroups returns the number of groups
    96  func (g IncorporatedResultGroupedList) NumberGroups() int {
    97  	return len(g)
    98  }