-
Notifications
You must be signed in to change notification settings - Fork 151
/
plugin.go
126 lines (102 loc) · 3.48 KB
/
plugin.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package migrator
import (
"context"
"fmt"
flag "github.com/spf13/pflag"
"go.uber.org/dig"
iotago "github.com/iotaledger/iota.go/v2"
"github.com/iotaledger/hive.go/timeutil"
"github.com/iotaledger/hive.go/configuration"
"github.com/iotaledger/hornet/pkg/common"
"github.com/iotaledger/hornet/pkg/model/migrator"
"github.com/iotaledger/hornet/pkg/model/utxo"
"github.com/iotaledger/hornet/pkg/node"
"github.com/iotaledger/hornet/pkg/shutdown"
)
const (
// CfgMigratorBootstrap configures whether the migration process is bootstrapped.
CfgMigratorBootstrap = "migratorBootstrap"
// CfgMigratorStartIndex configures the index of the first milestone to migrate.
CfgMigratorStartIndex = "migratorStartIndex"
)
func init() {
_ = flag.CommandLine.MarkHidden(CfgMigratorBootstrap)
_ = flag.CommandLine.MarkHidden(CfgMigratorStartIndex)
Plugin = &node.Plugin{
Status: node.StatusDisabled,
Pluggable: node.Pluggable{
Name: "Migrator",
DepsFunc: func(cDeps dependencies) { deps = cDeps },
Params: params,
Provide: provide,
Configure: configure,
Run: run,
},
}
}
var (
Plugin *node.Plugin
deps dependencies
bootstrap = flag.Bool(CfgMigratorBootstrap, false, "bootstrap the migration process")
startIndex = flag.Uint32(CfgMigratorStartIndex, 1, "index of the first milestone to migrate")
)
type dependencies struct {
dig.In
UTXOManager *utxo.Manager
NodeConfig *configuration.Configuration `name:"nodeConfig"`
MigratorService *migrator.MigratorService
ShutdownHandler *shutdown.ShutdownHandler
}
// provide provides the MigratorService as a singleton.
func provide(c *dig.Container) {
type serviceDeps struct {
dig.In
NodeConfig *configuration.Configuration `name:"nodeConfig"`
Validator *migrator.Validator
}
if err := c.Provide(func(deps serviceDeps) *migrator.MigratorService {
maxReceiptEntries := deps.NodeConfig.Int(CfgMigratorReceiptMaxEntries)
switch {
case maxReceiptEntries > iotago.MaxMigratedFundsEntryCount:
Plugin.LogPanicf("%s (set to %d) can be max %d", CfgMigratorReceiptMaxEntries, maxReceiptEntries, iotago.MaxMigratedFundsEntryCount)
case maxReceiptEntries <= 0:
Plugin.LogPanicf("%s must be greather than 0", CfgMigratorReceiptMaxEntries)
}
return migrator.NewService(
deps.Validator,
deps.NodeConfig.String(CfgMigratorStateFilePath),
deps.NodeConfig.Int(CfgMigratorReceiptMaxEntries),
)
}); err != nil {
Plugin.LogPanic(err)
}
}
func configure() {
var msIndex *uint32
if *bootstrap {
msIndex = startIndex
}
if err := deps.MigratorService.InitState(msIndex, deps.UTXOManager); err != nil {
Plugin.LogFatalf("failed to initialize migrator: %s", err)
}
}
func run() {
if err := Plugin.Node.Daemon().BackgroundWorker(Plugin.Name, func(ctx context.Context) {
Plugin.LogInfof("Starting %s ... done", Plugin.Name)
deps.MigratorService.Start(ctx, func(err error) bool {
if err := common.IsCriticalError(err); err != nil {
deps.ShutdownHandler.SelfShutdown(fmt.Sprintf("migrator plugin hit a critical error: %s", err))
return false
}
if err := common.IsSoftError(err); err != nil {
deps.MigratorService.Events.SoftError.Trigger(err)
}
// lets just log the err and halt querying for a configured period
Plugin.LogWarn(err)
return timeutil.Sleep(ctx, deps.NodeConfig.Duration(CfgMigratorQueryCooldownPeriod))
})
Plugin.LogInfof("Stopping %s ... done", Plugin.Name)
}, shutdown.PriorityMigrator); err != nil {
Plugin.LogPanicf("failed to start worker: %s", err)
}
}