-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
plugin.go
175 lines (150 loc) · 5 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
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
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"
"github.com/smartcontractkit/chainlink-common/pkg/types/core"
)
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
}
func (p *Plugin) NewValidationService(ctx context.Context) (core.ValidationService, error) {
s := &reportingPluginValidationService{lggr: p.Logger}
p.SubService(s)
return s, nil
}
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 core.ReportingPluginServiceConfig,
provider types.MedianProvider,
pipelineRunner core.PipelineRunnerService,
telemetry core.TelemetryClient,
errorLog core.ErrorLog,
keyValueStore core.KeyValueStore,
relayerSet core.RelayerSet,
) (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 core.ReportingPluginServiceConfig, provider types.MedianProvider, pipelineRunner core.PipelineRunnerService, telemetry core.TelemetryClient, errorLog core.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,
GasPriceSubunitsDataSource: &ZeroDataSource{},
IncludeGasPriceSubunitsInObservation: false,
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()}
}
type reportingPluginValidationService struct {
services.StateMachine
lggr logger.Logger
}
func (r *reportingPluginValidationService) ValidateConfig(ctx context.Context, config map[string]interface{}) error {
tt, ok := config["telemetryType"]
if !ok {
return fmt.Errorf("expected telemtry type")
}
telemetryType, ok := tt.(string)
if !ok {
return fmt.Errorf("expected telemtry type to be of type string but got %T", tt)
}
if telemetryType != "median" {
return fmt.Errorf("expected telemtry type to be median but got %q", telemetryType)
}
return nil
}
func (r *reportingPluginValidationService) Name() string { return r.lggr.Name() }
func (r *reportingPluginValidationService) Start(ctx context.Context) error {
return r.StartOnce("ValidationService", func() error { return nil })
}
func (r *reportingPluginValidationService) Close() error {
return r.StopOnce("ValidationService", func() error { return nil })
}
func (r *reportingPluginValidationService) HealthReport() map[string]error {
return map[string]error{r.Name(): r.Healthy()}
}