-
Notifications
You must be signed in to change notification settings - Fork 18
/
builder.go
245 lines (206 loc) · 6.94 KB
/
builder.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package node
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs-force-community/sophon-auth/jwtclient"
"github.com/multiformats/go-multiaddr"
logging "github.com/ipfs/go-log/v2"
metricsi "github.com/ipfs/go-metrics-interface"
"github.com/urfave/cli/v2"
"go.uber.org/fx"
"github.com/ipfs-force-community/sophon-miner/api"
"github.com/ipfs-force-community/sophon-miner/lib/journal"
"github.com/ipfs-force-community/sophon-miner/lib/metrics"
"github.com/ipfs-force-community/sophon-miner/miner"
"github.com/ipfs-force-community/sophon-miner/node/config"
"github.com/ipfs-force-community/sophon-miner/node/impl"
"github.com/ipfs-force-community/sophon-miner/node/impl/common"
"github.com/ipfs-force-community/sophon-miner/node/modules"
"github.com/ipfs-force-community/sophon-miner/node/modules/helpers"
minerecorder "github.com/ipfs-force-community/sophon-miner/node/modules/mine-recorder"
minermanager "github.com/ipfs-force-community/sophon-miner/node/modules/miner-manager"
"github.com/ipfs-force-community/sophon-miner/node/modules/slashfilter"
"github.com/ipfs-force-community/sophon-miner/node/repo"
"github.com/ipfs-force-community/sophon-miner/types"
)
//nolint:deadcode,varcheck
var log = logging.Logger("builder")
// special is a type used to give keys to modules which
//
// can't really be identified by the returned type
type special struct{ id int } //nolint
type invoke int
// Invokes are called in the order they are defined.
//
//nolint:golint
const (
// InitJournal at position 0 initializes the journal global var as soon as
// the system starts, so that it's available for all other components.
InitJournalKey = invoke(iota)
// daemon
ExtractApiKey
SetApiEndpointKey
SetRecorderDatastoreKey // set recorder datastore
LaunchMetricsSampleThread
_nInvokes // keep this last
)
type Settings struct {
// modules is a map of constructors for DI
//
// In most cases the index will be a reflect. Type of element returned by
// the constructor, but for some 'constructors' it's hard to specify what's
// the return type should be (or the constructor returns fx group)
modules map[interface{}]fx.Option
// invokes are separate from modules as they can't be referenced by return
// type, and must be applied in correct order
invokes []fx.Option
}
func defaults() []Option {
return []Option{
// global system journal.
Override(new(journal.DisabledEvents), journal.EnvDisabledEvents),
Override(new(journal.Journal), modules.OpenFilesystemJournal),
Override(new(helpers.MetricsCtx), func() context.Context {
return metricsi.CtxScope(context.Background(), "sophon-miner")
}),
Override(new(types.ShutdownChan), make(chan struct{})),
}
}
func Repo(cctx *cli.Context, r repo.Repo) Option {
return func(settings *Settings) error {
lr, err := r.Lock()
if err != nil {
return err
}
c, err := lr.Config()
if err != nil {
return err
}
return Options(
Override(new(repo.LockedRepo), modules.LockedRepo(lr)),
Override(new(types.MetadataDS), modules.Datastore),
ConfigMinerOptions(c),
)(settings)
}
}
func ConfigMinerOptions(c interface{}) Option {
cfg, ok := c.(*config.MinerConfig)
if !ok {
return Error(fmt.Errorf("invalid config from repo, got: %T", c))
}
configStr, _ := json.MarshalIndent(cfg, "", "\t")
log.Infof("final config: \n%v", string(configStr))
shareOps := Options(
Override(new(*config.MinerConfig), cfg),
Override(new(*config.MySQLConfig), &cfg.SlashFilter.MySQL),
Override(new(types.APIEndpoint), func() (types.APIEndpoint, error) {
return multiaddr.NewMultiaddr(cfg.API.ListenAddress)
}),
Override(new(api.Common), From(new(common.CommonAPI))),
)
minerOps := Options(
If(cfg.SlashFilter.Type == string(slashfilter.Local), Override(new(slashfilter.SlashFilterAPI), slashfilter.NewLocal)),
If(cfg.SlashFilter.Type == string(slashfilter.MySQL), Override(new(slashfilter.SlashFilterAPI), slashfilter.NewMysql)),
Override(new(jwtclient.IAuthClient), minermanager.NewVenusAuth(cfg.Auth.Addr, cfg.Auth.Token)),
Override(new(minermanager.MinerManageAPI), minermanager.NewMinerManager),
Override(SetRecorderDatastoreKey, func(ds types.MetadataDS) {
if cfg.Recorder != nil && cfg.Recorder.Enable {
if cfg.Recorder.MaxRecordPerQuery > 0 {
minerecorder.MaxRecordPerQuery = cfg.Recorder.MaxRecordPerQuery
}
if cfg.Recorder.ExpireEpoch > 0 {
minerecorder.ExpireEpoch = abi.ChainEpoch(cfg.Recorder.ExpireEpoch)
}
minerecorder.SetDatastore(ds)
}
}),
Override(new(miner.MiningAPI), modules.NewMinerProcessor),
Override(LaunchMetricsSampleThread, func(ctx helpers.MetricsCtx, api minermanager.MinerManageAPI) {
go func() {
// Record metrics
tm := time.NewTicker(time.Second * 60)
defer tm.Stop()
for {
select {
case <-ctx.Done():
log.Warnf("stop record metrics: %v", ctx.Err())
return
case <-tm.C:
miners, err := api.List(ctx)
if err != nil {
log.Warnf("record metrics: list miner: %s", err)
}
minerInState := map[bool]int64{true: 0, false: 0}
for _, miner := range miners {
minerInState[miner.OpenMining] += 1
}
for state, num := range minerInState {
stateStr := "open_for_mining"
if !state {
stateStr = "close_for_mining"
}
metrics.MinerNumInState.Set(ctx, stateStr, num)
}
}
}
}()
}),
)
return Options(
shareOps,
minerOps,
Override(SetApiEndpointKey, func(lr repo.LockedRepo, e types.APIEndpoint) error {
return lr.SetAPIEndpoint(e)
}),
)
}
func MinerAPI(out *api.MinerAPI) Option {
return Options(
func(s *Settings) error {
resAPI := &impl.MinerAPI{}
s.invokes[ExtractApiKey] = fx.Populate(resAPI)
*out = resAPI
return nil
},
)
}
type FullOption = Option
type StopFunc func(context.Context) error
// New builds and starts new Filecoin node
func New(ctx context.Context, opts ...Option) (StopFunc, error) {
settings := Settings{
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, _nInvokes),
}
// apply module options in the right order
if err := Options(Options(defaults()...), Options(opts...))(&settings); err != nil {
return nil, fmt.Errorf("applying node options failed: %w", err)
}
// gather constructors for fx.Options
ctors := make([]fx.Option, 0, len(settings.modules))
for _, opt := range settings.modules {
ctors = append(ctors, opt)
}
// fill holes in invokes for use in fx.Options
for i, opt := range settings.invokes {
if opt == nil {
settings.invokes[i] = fx.Options()
}
}
app := fx.New(
fx.Options(ctors...),
fx.Options(settings.invokes...),
fx.NopLogger,
)
// TODO: we probably should have a 'firewall' for Closing signal
// on this context, and implement closing logic through lifecycles
// correctly
if err := app.Start(ctx); err != nil {
// comment fx.NopLogger few lines above for easier debugging
return nil, fmt.Errorf("starting node: %w", err)
}
return app.Stop, nil
}