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