Skip to content

Commit

Permalink
Use the AutomationProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ferglor committed Dec 13, 2023
1 parent f3d5417 commit c3c8f67
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 34 deletions.
83 changes: 70 additions & 13 deletions core/services/ocr2/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (d *Delegate) ServicesForSpec(jb job.Job) ([]job.ServiceCtx, error) {
return d.newServicesOCR2VRF(lggr, jb, bootstrapPeers, kb, ocrDB, lc)

case types.OCR2Keeper:
return d.newServicesOCR2Keepers(lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger)
return d.newServicesOCR2Keepers(ctx, lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger)

case types.Functions:
const (
Expand Down Expand Up @@ -1026,6 +1026,7 @@ func (d *Delegate) newServicesOCR2VRF(
}

func (d *Delegate) newServicesOCR2Keepers(
ctx context.Context,
lggr logger.SugaredLogger,
jb job.Job,
bootstrapPeers []commontypes.BootstrapperLocator,
Expand All @@ -1046,15 +1047,16 @@ func (d *Delegate) newServicesOCR2Keepers(

switch cfg.ContractVersion {
case "v2.1":
return d.newServicesOCR2Keepers21(lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger, cfg, spec)
return d.newServicesOCR2Keepers21(ctx, lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger, cfg, spec)
case "v2.0":
return d.newServicesOCR2Keepers20(lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger, cfg, spec)
return d.newServicesOCR2Keepers20(ctx, lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger, cfg, spec)
default:
return d.newServicesOCR2Keepers20(lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger, cfg, spec)
return d.newServicesOCR2Keepers20(ctx, lggr, jb, bootstrapPeers, kb, ocrDB, lc, ocrLogger, cfg, spec)
}
}

func (d *Delegate) newServicesOCR2Keepers21(
ctx context.Context,
lggr logger.SugaredLogger,
jb job.Job,
bootstrapPeers []commontypes.BootstrapperLocator,
Expand All @@ -1079,14 +1081,41 @@ func (d *Delegate) newServicesOCR2Keepers21(
return nil, fmt.Errorf("keeper2 services: expected EVM relayer got %s", rid.Network)
}

chain, err2 := d.legacyChains.Get(rid.ChainID)
if err2 != nil {
return nil, fmt.Errorf("keeper2 services: failed to get chain %s: %w", rid.ChainID, err2)
transmitterID := spec.TransmitterID.String
relayer, err := d.RelayGetter.Get(rid)
if err != nil {
return nil, ErrRelayNotEnabled{Err: err, Relay: spec.Relay, PluginName: "ocr2keepers"}
}

keeperProvider, services, err2 := ocr2keeper.EVMDependencies21(jb, d.db, lggr, chain, mc, kb, d.cfg.Database(), d.ethKs)
if err2 != nil {
return nil, errors.Wrap(err2, "could not build dependencies for ocr2 keepers")
provider, err := relayer.NewPluginProvider(ctx,
types.RelayArgs{
ExternalJobID: jb.ExternalJobID,
JobID: jb.ID,
ContractID: spec.ContractID,
New: d.isNewlyCreatedJob,
RelayConfig: spec.RelayConfig.Bytes(),
ProviderType: string(spec.PluginType),
}, types.PluginArgs{
TransmitterID: transmitterID,
PluginConfig: spec.PluginConfig.Bytes(),
})
if err != nil {
return nil, err
}

keeperProvider, ok := provider.(types.AutomationProvider)
if !ok {
return nil, errors.New("could not coerce PluginProvider to AutomationProvider")
}

chain, err := d.legacyChains.Get(rid.ChainID)
if err != nil {
return nil, fmt.Errorf("keeper2 services: failed to get chain %s: %w", rid.ChainID, err)
}

services, err := ocr2keeper.EVMDependencies21(jb, d.db, lggr, chain, mc, kb, d.cfg.Database())
if err != nil {
return nil, errors.Wrap(err, "could not build dependencies for ocr2 keepers")
}
// set some defaults
conf := ocr2keepers21config.ReportingFactoryConfig{
Expand Down Expand Up @@ -1178,6 +1207,7 @@ func (d *Delegate) newServicesOCR2Keepers21(
}

func (d *Delegate) newServicesOCR2Keepers20(
ctx context.Context,
lggr logger.SugaredLogger,
jb job.Job,
bootstrapPeers []commontypes.BootstrapperLocator,
Expand All @@ -1201,9 +1231,36 @@ func (d *Delegate) newServicesOCR2Keepers20(
return nil, fmt.Errorf("keepers2.0 services: failed to get chain (%s): %w", rid.ChainID, err2)
}

keeperProvider, rgstry, encoder, logProvider, err2 := ocr2keeper.EVMDependencies20(jb, d.db, lggr, chain, d.ethKs)
if err2 != nil {
return nil, errors.Wrap(err2, "could not build dependencies for ocr2 keepers")
transmitterID := spec.TransmitterID.String
relayer, err := d.RelayGetter.Get(rid)
if err != nil {
return nil, ErrRelayNotEnabled{Err: err, Relay: spec.Relay, PluginName: "mercury"}
}

provider, err := relayer.NewPluginProvider(ctx,
types.RelayArgs{
ExternalJobID: jb.ExternalJobID,
JobID: jb.ID,
ContractID: spec.ContractID,
New: d.isNewlyCreatedJob,
RelayConfig: spec.RelayConfig.Bytes(),
ProviderType: string(spec.PluginType),
}, types.PluginArgs{
TransmitterID: transmitterID,
PluginConfig: spec.PluginConfig.Bytes(),
})
if err != nil {
return nil, err
}

keeperProvider, ok := provider.(types.AutomationProvider)
if !ok {
return nil, errors.New("could not coerce PluginProvider to AutomationProvider")
}

rgstry, encoder, logProvider, err := ocr2keeper.EVMDependencies20(jb, lggr, chain)
if err != nil {
return nil, errors.Wrap(err, "could not build dependencies for ocr2 keepers")
}

w := &logWriter{log: lggr.Named("Automation Dependencies")}
Expand Down
26 changes: 6 additions & 20 deletions core/services/ocr2/plugins/ocr2keeper/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,16 @@ func EVMProvider(db *sqlx.DB, chain legacyevm.Chain, lggr logger.Logger, spec jo

func EVMDependencies20(
spec job.Job,
db *sqlx.DB,
lggr logger.Logger,
chain legacyevm.Chain,
ethKeystore keystore.Eth,
) (evmrelay.OCR2KeeperProvider, *evmregistry20.EvmRegistry, Encoder20, *evmregistry20.LogProvider, error) {
) (*evmregistry20.EvmRegistry, Encoder20, *evmregistry20.LogProvider, error) {
var err error

var keeperProvider evmrelay.OCR2KeeperProvider
var registry *evmregistry20.EvmRegistry

// the provider will be returned as a dependency
if keeperProvider, err = EVMProvider(db, chain, lggr, spec, ethKeystore); err != nil {
return nil, nil, nil, nil, err
}

rAddr := ethkey.MustEIP55Address(spec.OCR2OracleSpec.ContractID).Address()
if registry, err = evmregistry20.NewEVMRegistryService(rAddr, chain, lggr); err != nil {
return nil, nil, nil, nil, err
return nil, nil, nil, err
}

encoder := evmregistry20.EVMAutomationEncoder20{}
Expand All @@ -96,7 +88,7 @@ func EVMDependencies20(
// TODO: accept a version of the registry contract and use the correct interfaces
logProvider, err := evmregistry20.NewLogProvider(lggr, chain.LogPoller(), rAddr, chain.Client(), lookbackBlocks)

return keeperProvider, registry, encoder, logProvider, err
return registry, encoder, logProvider, err
}

func FilterNamesFromSpec20(spec *job.OCR2OracleSpec) (names []string, err error) {
Expand All @@ -115,24 +107,18 @@ func EVMDependencies21(
mc *models.MercuryCredentials,
keyring ocrtypes.OnchainKeyring,
dbCfg pg.QConfig,
ethKeystore keystore.Eth,
) (evmrelay.OCR2KeeperProvider, evmregistry21.AutomationServices, error) {
) (evmregistry21.AutomationServices, error) {
var err error
var keeperProvider evmrelay.OCR2KeeperProvider

oSpec := spec.OCR2OracleSpec
// the provider will be returned as a dependency
if keeperProvider, err = EVMProvider(db, chain, lggr, spec, ethKeystore); err != nil {
return nil, nil, err
}

rAddr := ethkey.MustEIP55Address(oSpec.ContractID).Address()
services, err := evmregistry21.New(rAddr, chain, mc, keyring, lggr, db, dbCfg)
if err != nil {
return nil, nil, err
return nil, err
}

return keeperProvider, services, err
return services, err
}

func FilterNamesFromSpec21(spec *job.OCR2OracleSpec) (names []string, err error) {
Expand Down
7 changes: 7 additions & 0 deletions core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@ func (r *Relayer) NewMedianProvider(rargs commontypes.RelayArgs, pargs commontyp
return &medianProvider, nil
}

func (r *Relayer) NewAutomationProvider(rargs commontypes.RelayArgs, pargs commontypes.PluginArgs) (commontypes.AutomationProvider, error) {
lggr := r.lggr.Named("AutomationProvider").Named(rargs.ExternalJobID.String())
ocr2keeperRelayer := NewOCR2KeeperRelayer(r.db, r.chain, lggr.Named("OCR2KeeperRelayer"), r.ks.Eth())

return ocr2keeperRelayer.NewOCR2KeeperProvider(rargs, pargs)
}

var _ commontypes.MedianProvider = (*medianProvider)(nil)

type medianProvider struct {
Expand Down
4 changes: 3 additions & 1 deletion core/services/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func (r *ServerAdapter) NewPluginProvider(ctx context.Context, rargs types.Relay
return r.NewFunctionsProvider(ctx, rargs, pargs)
case types.Mercury:
return r.NewMercuryProvider(ctx, rargs, pargs)
case types.DKG, types.OCR2VRF, types.OCR2Keeper, types.GenericPlugin:
case types.OCR2Keeper:
return r.NewAutomationProvider(ctx, rargs, pargs)
case types.DKG, types.OCR2VRF, types.GenericPlugin:
return r.RelayerAdapter.NewPluginProvider(ctx, rargs, pargs)
case types.CCIPCommit, types.CCIPExecution:
return nil, fmt.Errorf("provider type not supported: %s", rargs.ProviderType)
Expand Down
8 changes: 8 additions & 0 deletions core/services/relay/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type staticMercuryProvider struct {
types.MercuryProvider
}

type staticAutomationProvider struct {
types.AutomationProvider
}

type mockRelayer struct {
types.Relayer
}
Expand All @@ -113,6 +117,10 @@ func (m *mockRelayer) NewMercuryProvider(rargs types.RelayArgs, pargs types.Plug
return staticMercuryProvider{}, nil
}

func (m *mockRelayer) NewAutomationProvider(rargs types.RelayArgs, pargs types.PluginArgs) (types.AutomationProvider, error) {
return staticAutomationProvider{}, nil
}

type mockRelayerExt struct {
loop.RelayerExt
}
Expand Down

0 comments on commit c3c8f67

Please sign in to comment.