-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
plugin.go
131 lines (112 loc) · 3.53 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
127
128
129
130
131
package medianpoc
import (
"context"
"encoding/json"
"fmt"
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/loop"
"github.com/smartcontractkit/chainlink-common/pkg/loop/reportingplugins"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/types"
)
func NewPlugin(lggr logger.Logger) *Plugin {
return &Plugin{
Plugin: loop.Plugin{Logger: lggr},
MedianProviderServer: reportingplugins.MedianProviderServer{},
stop: make(services.StopChan),
}
}
type Plugin struct {
loop.Plugin
stop services.StopChan
reportingplugins.MedianProviderServer
}
type pipelineSpec struct {
Name string `json:"name"`
Spec string `json:"spec"`
}
type jsonConfig struct {
Pipelines []pipelineSpec `json:"pipelines"`
}
func (j jsonConfig) defaultPipeline() (string, error) {
return j.getPipeline("__DEFAULT_PIPELINE__")
}
func (j jsonConfig) getPipeline(key string) (string, error) {
for _, v := range j.Pipelines {
if v.Name == key {
return v.Spec, nil
}
}
return "", fmt.Errorf("no pipeline found for %s", key)
}
func (p *Plugin) NewReportingPluginFactory(
ctx context.Context,
config types.ReportingPluginServiceConfig,
provider types.MedianProvider,
pipelineRunner types.PipelineRunnerService,
telemetry types.TelemetryClient,
errorLog types.ErrorLog,
) (types.ReportingPluginFactory, error) {
f, err := p.newFactory(ctx, config, provider, pipelineRunner, telemetry, errorLog)
if err != nil {
return nil, err
}
s := &reportingPluginFactoryService{lggr: p.Logger, ReportingPluginFactory: f}
p.SubService(s)
return s, nil
}
func (p *Plugin) newFactory(ctx context.Context, config types.ReportingPluginServiceConfig, provider types.MedianProvider, pipelineRunner types.PipelineRunnerService, telemetry types.TelemetryClient, errorLog types.ErrorLog) (*median.NumericalMedianFactory, error) {
jc := &jsonConfig{}
err := json.Unmarshal([]byte(config.PluginConfig), jc)
if err != nil {
return nil, err
}
dp, err := jc.defaultPipeline()
if err != nil {
return nil, err
}
ds := &DataSource{
pipelineRunner: pipelineRunner,
spec: dp,
lggr: p.Logger,
}
jfp, err := jc.getPipeline("juelsPerFeeCoinPipeline")
if err != nil {
return nil, err
}
jds := &DataSource{
pipelineRunner: pipelineRunner,
spec: jfp,
lggr: p.Logger,
}
factory := &median.NumericalMedianFactory{
ContractTransmitter: provider.MedianContract(),
DataSource: ds,
JuelsPerFeeCoinDataSource: jds,
Logger: logger.NewOCRWrapper(
p.Logger,
true,
func(msg string) {},
),
OnchainConfigCodec: provider.OnchainConfigCodec(),
ReportCodec: provider.ReportCodec(),
}
return factory, nil
}
type reportingPluginFactoryService struct {
services.StateMachine
lggr logger.Logger
ocrtypes.ReportingPluginFactory
}
func (r *reportingPluginFactoryService) Name() string { return r.lggr.Name() }
func (r *reportingPluginFactoryService) Start(ctx context.Context) error {
return r.StartOnce("ReportingPluginFactory", func() error { return nil })
}
func (r *reportingPluginFactoryService) Close() error {
return r.StopOnce("ReportingPluginFactory", func() error { return nil })
}
func (r *reportingPluginFactoryService) HealthReport() map[string]error {
return map[string]error{r.Name(): r.Healthy()}
}