github.com/koko1123/flow-go-1@v0.29.6/engine/execution/ingestion/deltas.go (about)

     1  package ingestion
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/model/flow"
     5  	"github.com/koko1123/flow-go-1/model/messages"
     6  	"github.com/koko1123/flow-go-1/module/mempool/stdmap"
     7  )
     8  
     9  type Deltas struct {
    10  	*stdmap.Backend
    11  }
    12  
    13  // NewDeltas creates a new memory pool for state deltas
    14  func NewDeltas(limit uint, opts ...stdmap.OptionFunc) (*Deltas, error) {
    15  	s := &Deltas{
    16  		Backend: stdmap.NewBackend(append(opts, stdmap.WithLimit(limit))...),
    17  	}
    18  
    19  	return s, nil
    20  }
    21  
    22  // Add adds an state deltas to the mempool.
    23  func (s *Deltas) Add(delta *messages.ExecutionStateDelta) bool {
    24  	// delta's ID is block's ID
    25  	return s.Backend.Add(delta)
    26  }
    27  
    28  // Remove will remove a deltas by block ID.
    29  func (s *Deltas) Remove(blockID flow.Identifier) bool {
    30  	removed := s.Backend.Remove(blockID)
    31  	return removed
    32  }
    33  
    34  // ByBlockID returns the state deltas for a block from the mempool.
    35  func (s *Deltas) ByBlockID(blockID flow.Identifier) (*messages.ExecutionStateDelta, bool) {
    36  	entity, exists := s.Backend.ByID(blockID)
    37  	if !exists {
    38  		return nil, false
    39  	}
    40  	delta := entity.(*messages.ExecutionStateDelta)
    41  	return delta, true
    42  }
    43  
    44  // All returns all block Deltass from the pool.
    45  func (s *Deltas) All() []*messages.ExecutionStateDelta {
    46  	entities := s.Backend.All()
    47  	deltas := make([]*messages.ExecutionStateDelta, 0, len(entities))
    48  	for _, entity := range entities {
    49  		deltas = append(deltas, entity.(*messages.ExecutionStateDelta))
    50  	}
    51  	return deltas
    52  }