-
Notifications
You must be signed in to change notification settings - Fork 178
/
blockbycollections.go
55 lines (45 loc) · 1.44 KB
/
blockbycollections.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
53
54
55
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool/entity"
)
// Hold all the missing collections.
// Each entry is a missing collection, and all the blocks that contain
// this collection
type BlockByCollections struct {
*Backend
}
// BlockByCollectionBackdata contains all the collections is being requested,
// for each collection it stores the blocks that contains the collection.
// the Backdata is essentially map<collectionID>map<blockID>*ExecutableBlock
type BlockByCollectionBackdata struct {
*Backdata
}
func NewBlockByCollections() *BlockByCollections {
return &BlockByCollections{NewBackend(WithEject(EjectPanic))}
}
func (b *BlockByCollections) Add(block *entity.BlocksByCollection) bool {
return b.Backend.Add(block)
}
func (b *BlockByCollections) Get(collID flow.Identifier) (*entity.BlocksByCollection, bool) {
backdata := &BlockByCollectionBackdata{&b.Backdata}
return backdata.ByID(collID)
}
func (b *BlockByCollections) Run(f func(backdata *BlockByCollectionBackdata) error) error {
b.Lock()
defer b.Unlock()
err := f(&BlockByCollectionBackdata{&b.Backdata})
if err != nil {
return err
}
return nil
}
func (b *BlockByCollectionBackdata) ByID(id flow.Identifier) (*entity.BlocksByCollection, bool) {
e, exists := b.Backdata.ByID(id)
if !exists {
return nil, false
}
block := e.(*entity.BlocksByCollection)
return block, true
}