-
Notifications
You must be signed in to change notification settings - Fork 12
/
provider.go
40 lines (31 loc) · 1.5 KB
/
provider.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
package tipmanagerv1
import (
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/workerpool"
"github.com/iotaledger/iota-core/pkg/core/account"
"github.com/iotaledger/iota-core/pkg/protocol/engine"
"github.com/iotaledger/iota-core/pkg/protocol/engine/tipmanager"
iotago "github.com/iotaledger/iota.go/v4"
)
// NewProvider creates a new TipManager provider, that can be used to inject the component into an engine.
func NewProvider() module.Provider[*engine.Engine, tipmanager.TipManager] {
return module.Provide(func(e *engine.Engine) tipmanager.TipManager {
t := New(e.NewSubModule("TipManager"), e.BlockCache.Block, e.SybilProtection.SeatManager().CommitteeInSlot)
e.ConstructedEvent().OnTrigger(func() {
tipWorker := e.Workers.CreatePool("AddTip", workerpool.WithWorkerCount(2))
e.Events.Scheduler.BlockScheduled.Hook(lo.Void(t.AddBlock), event.WithWorkerPool(tipWorker))
// the tipmanager needs to know about all the blocks that passed the scheduler
e.Events.Scheduler.BlockSkipped.Hook(lo.Void(t.AddBlock), event.WithWorkerPool(tipWorker))
e.Events.Evict.Hook(t.Evict)
e.Events.SeatManager.OnlineCommitteeSeatAdded.Hook(func(index account.SeatIndex, _ iotago.AccountID) {
t.AddSeat(index)
})
e.Events.SeatManager.OnlineCommitteeSeatRemoved.Hook(t.RemoveSeat)
e.Events.TipManager.BlockAdded.LinkTo(t.blockAdded)
t.InitializedEvent().Trigger()
})
return t
})
}