-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ocr2vrf.go
180 lines (150 loc) · 5.01 KB
/
ocr2vrf.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
package evm
import (
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/libocr/offchainreporting2plus/chains/evmutil"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/dkg/config"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
)
// DKGProvider provides all components needed for a DKG plugin.
type DKGProvider interface {
commontypes.Plugin
}
// OCR2VRFProvider provides all components needed for a OCR2VRF plugin.
type OCR2VRFProvider interface {
commontypes.Plugin
}
// OCR2VRFRelayer contains the relayer and instantiating functions for OCR2VRF providers.
type OCR2VRFRelayer interface {
NewDKGProvider(rargs commontypes.RelayArgs, pargs commontypes.PluginArgs) (DKGProvider, error)
NewOCR2VRFProvider(rargs commontypes.RelayArgs, pargs commontypes.PluginArgs) (OCR2VRFProvider, error)
}
var (
_ OCR2VRFRelayer = (*ocr2vrfRelayer)(nil)
_ DKGProvider = (*dkgProvider)(nil)
_ OCR2VRFProvider = (*ocr2vrfProvider)(nil)
)
// Relayer with added DKG and OCR2VRF provider functions.
type ocr2vrfRelayer struct {
chain legacyevm.Chain
lggr logger.Logger
ethKeystore keystore.Eth
}
func NewOCR2VRFRelayer(chain legacyevm.Chain, lggr logger.Logger, ethKeystore keystore.Eth) OCR2VRFRelayer {
return &ocr2vrfRelayer{
chain: chain,
lggr: lggr,
ethKeystore: ethKeystore,
}
}
func (r *ocr2vrfRelayer) NewDKGProvider(rargs commontypes.RelayArgs, pargs commontypes.PluginArgs) (DKGProvider, error) {
// TODO https://smartcontract-it.atlassian.net/browse/BCF-2887
ctx := context.Background()
configWatcher, err := newOCR2VRFConfigProvider(ctx, r.lggr, r.chain, rargs)
if err != nil {
return nil, err
}
contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, pargs.TransmitterID, r.ethKeystore, configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI, 0)
if err != nil {
return nil, err
}
var pluginConfig config.PluginConfig
err = json.Unmarshal(pargs.PluginConfig, &pluginConfig)
if err != nil {
return nil, err
}
return &dkgProvider{
configWatcher: configWatcher,
contractTransmitter: contractTransmitter,
pluginConfig: pluginConfig,
}, nil
}
func (r *ocr2vrfRelayer) NewOCR2VRFProvider(rargs commontypes.RelayArgs, pargs commontypes.PluginArgs) (OCR2VRFProvider, error) {
// TODO https://smartcontract-it.atlassian.net/browse/BCF-2887
ctx := context.Background()
configWatcher, err := newOCR2VRFConfigProvider(ctx, r.lggr, r.chain, rargs)
if err != nil {
return nil, err
}
contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, pargs.TransmitterID, r.ethKeystore, configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI, 0)
if err != nil {
return nil, err
}
return &ocr2vrfProvider{
configWatcher: configWatcher,
contractTransmitter: contractTransmitter,
}, nil
}
type dkgProvider struct {
*configWatcher
contractTransmitter ContractTransmitter
pluginConfig config.PluginConfig
}
func (c *dkgProvider) ContractTransmitter() ocrtypes.ContractTransmitter {
return c.contractTransmitter
}
func (c *dkgProvider) ChainReader() commontypes.ChainReader {
return nil
}
func (c *dkgProvider) Codec() commontypes.Codec {
return nil
}
type ocr2vrfProvider struct {
*configWatcher
contractTransmitter ContractTransmitter
}
func (c *ocr2vrfProvider) ContractTransmitter() ocrtypes.ContractTransmitter {
return c.contractTransmitter
}
func (c *ocr2vrfProvider) ChainReader() commontypes.ChainReader {
return nil
}
func (c *ocr2vrfProvider) Codec() commontypes.Codec {
return nil
}
func newOCR2VRFConfigProvider(ctx context.Context, lggr logger.Logger, chain legacyevm.Chain, rargs commontypes.RelayArgs) (*configWatcher, error) {
var relayConfig types.RelayConfig
err := json.Unmarshal(rargs.RelayConfig, &relayConfig)
if err != nil {
return nil, err
}
if !common.IsHexAddress(rargs.ContractID) {
return nil, fmt.Errorf("invalid contract address '%s'", rargs.ContractID)
}
contractAddress := common.HexToAddress(rargs.ContractID)
configPoller, err := NewConfigPoller(
ctx,
lggr.With("contractID", rargs.ContractID),
CPConfig{
chain.Client(),
chain.LogPoller(),
contractAddress,
// TODO: Does ocr2vrf need to support config contract? DF-19182
nil,
OCR2AggregatorLogDecoder,
},
)
if err != nil {
return nil, err
}
offchainConfigDigester := evmutil.EVMOffchainConfigDigester{
ChainID: chain.Config().EVM().ChainID().Uint64(),
ContractAddress: contractAddress,
}
return newConfigWatcher(
lggr,
contractAddress,
offchainConfigDigester,
configPoller,
chain,
relayConfig.FromBlock,
rargs.New,
), nil
}