-
Notifications
You must be signed in to change notification settings - Fork 175
/
blockbycollections.go
62 lines (51 loc) · 1.74 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
56
57
58
59
60
61
62
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool"
"github.com/onflow/flow-go/module/mempool/entity"
_ "github.com/onflow/flow-go/utils/binstat"
)
// 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 {
mempool.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 {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(BlockByCollections).Run")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(BlockByCollections).Run)")
defer b.Unlock()
err := f(&BlockByCollectionBackdata{b.backData})
//binstat.Leave(bs2)
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
}