-
Notifications
You must be signed in to change notification settings - Fork 179
/
register_engine.go
57 lines (49 loc) · 1.51 KB
/
register_engine.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
package storehouse
import (
"fmt"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/irrecoverable"
)
// RegisterEngine is a wrapper for RegisterStore in order to make Block Finalization process
// non-blocking.
type RegisterEngine struct {
*component.ComponentManager
store *RegisterStore
finalizationNotifier engine.Notifier
}
func NewRegisterEngine(store *RegisterStore) *RegisterEngine {
e := &RegisterEngine{
store: store,
finalizationNotifier: engine.NewNotifier(),
}
// Add workers
e.ComponentManager = component.NewComponentManagerBuilder().
AddWorker(e.finalizationProcessingLoop).
Build()
return e
}
// OnBlockFinalized will create a single goroutine to notify register store
// when a block is finalized.
// This call is non-blocking in order to avoid blocking the consensus
func (e *RegisterEngine) OnBlockFinalized(*model.Block) {
e.finalizationNotifier.Notify()
}
// finalizationProcessingLoop notify the register store when a block is finalized
// and handle the error if any
func (e *RegisterEngine) finalizationProcessingLoop(ctx irrecoverable.SignalerContext, ready component.ReadyFunc) {
ready()
notifier := e.finalizationNotifier.Channel()
for {
select {
case <-ctx.Done():
return
case <-notifier:
err := e.store.OnBlockFinalized()
if err != nil {
ctx.Throw(fmt.Errorf("could not process finalized block: %w", err))
}
}
}
}