github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/finalizer/consensus/cleanup.go (about)

     1  package consensus
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onflow/flow-go/model/flow"
     7  	"github.com/onflow/flow-go/module"
     8  	"github.com/onflow/flow-go/module/mempool"
     9  	"github.com/onflow/flow-go/module/metrics"
    10  	"github.com/onflow/flow-go/storage"
    11  )
    12  
    13  // CleanupFunc is called after a block was finalized to allow other components
    14  // to execute cleanup operations.
    15  type CleanupFunc func(blockID flow.Identifier) error
    16  
    17  func CleanupNothing() CleanupFunc {
    18  	return func(flow.Identifier) error {
    19  		return nil
    20  	}
    21  }
    22  
    23  func CleanupMempools(
    24  	collector module.MempoolMetrics,
    25  	spans module.ConsensusMetrics,
    26  	payloads storage.Payloads,
    27  	guarantees mempool.Guarantees,
    28  	seals mempool.IncorporatedResultSeals) CleanupFunc {
    29  
    30  	return func(blockID flow.Identifier) error {
    31  
    32  		payload, err := payloads.ByBlockID(blockID)
    33  		if err != nil {
    34  			return fmt.Errorf("could not retrieve  payload (%x): %w", blockID, err)
    35  		}
    36  
    37  		for _, guarantee := range payload.Guarantees {
    38  			_ = guarantees.Remove(guarantee.ID())
    39  		}
    40  
    41  		collector.MempoolEntries(metrics.ResourceGuarantee, guarantees.Size())
    42  
    43  		for _, seal := range payload.Seals {
    44  			_ = seals.Remove(seal.ID())
    45  		}
    46  
    47  		collector.MempoolEntries(metrics.ResourceSeal, seals.Size())
    48  
    49  		return nil
    50  	}
    51  }