-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[KS-430] Provide an OracleFactory to StandardCapabilities #14305
Draft
DeividasK
wants to merge
14
commits into
develop
Choose a base branch
from
KS-430-build-and-inject-the-libocr-oracle-api
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8547ac4
--wip-- [skip CI]
DeividasK ae26b40
--wip-- [skip CI]
DeividasK fe77135
Merge branch 'develop' into KS-430-build-and-inject-the-libocr-oracle…
DeividasK c9e2864
Add bootstrap peers via capabilities spec
DeividasK 80c43de
Add peer wrapper to Oracle Factory
DeividasK 0b969fb
Add comment explaining delegate position
DeividasK c9a3fca
Cleanup
DeividasK fb1c2ed
Merge remote-tracking branch 'origin/develop' into KS-430-build-and-i…
DeividasK 8f09d65
Create a key bundle if none exists
DeividasK 1615836
Merge remote-tracking branch 'origin/develop' into KS-430-build-and-i…
DeividasK 1af1421
Use PR #738 hash
DeividasK 475e292
Fix bad merge
DeividasK 5f2fc40
Bump chainlink-common
DeividasK 0d24b8b
Use in-memory DB for OCR persistance
DeividasK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package generic | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/smartcontractkit/libocr/commontypes" | ||
ocr "github.com/smartcontractkit/libocr/offchainreporting2plus" | ||
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/job" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ocr2key" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/ocrcommon" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/telemetry" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/types/core" | ||
) | ||
|
||
type oracleFactoryConfig struct { | ||
Enabled bool | ||
TraceLogging bool | ||
BootstrapPeers []commontypes.BootstrapperLocator | ||
} | ||
|
||
func NewOracleFactoryConfig(config job.JSONConfig) (*oracleFactoryConfig, error) { | ||
var ofc struct { | ||
Enabled bool `json:"enabled"` | ||
TraceLogging bool `json:"traceLogging"` | ||
BootstrapPeers []string `json:"bootstrapPeers"` | ||
} | ||
err := json.Unmarshal(config.Bytes(), &ofc) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal oracle factory config") | ||
} | ||
|
||
if !ofc.Enabled { | ||
return &oracleFactoryConfig{}, nil | ||
} | ||
|
||
// If Oracle Factory is enabled, it must have at least one bootstrap peer | ||
if len(ofc.BootstrapPeers) == 0 { | ||
return nil, errors.New("no bootstrap peers found") | ||
} | ||
|
||
bootstrapPeers, err := ocrcommon.ParseBootstrapPeers(ofc.BootstrapPeers) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to parse bootstrap peers") | ||
} | ||
|
||
return &oracleFactoryConfig{ | ||
Enabled: true, | ||
TraceLogging: ofc.TraceLogging, | ||
BootstrapPeers: bootstrapPeers, | ||
}, nil | ||
} | ||
|
||
type oracleFactory struct { | ||
database ocr3types.Database | ||
jobID int32 | ||
jobName string | ||
jobORM job.ORM | ||
kb ocr2key.KeyBundle | ||
lggr logger.Logger | ||
config *oracleFactoryConfig | ||
peerWrapper *ocrcommon.SingletonPeerWrapper | ||
} | ||
|
||
type OracleFactoryParams struct { | ||
Database ocr3types.Database | ||
JobID int32 | ||
JobName string | ||
JobORM job.ORM | ||
Kb ocr2key.KeyBundle | ||
Logger logger.Logger | ||
Config *oracleFactoryConfig | ||
PeerWrapper *ocrcommon.SingletonPeerWrapper | ||
} | ||
|
||
func NewOracleFactory(params OracleFactoryParams) (core.OracleFactory, error) { | ||
return &oracleFactory{ | ||
database: params.Database, | ||
jobID: params.JobID, | ||
jobName: params.JobName, | ||
jobORM: params.JobORM, | ||
kb: params.Kb, | ||
lggr: params.Logger, | ||
config: params.Config, | ||
peerWrapper: params.PeerWrapper, | ||
}, nil | ||
} | ||
|
||
func (of *oracleFactory) NewOracle(ctx context.Context, args core.OracleArgs) (core.Oracle, error) { | ||
oracle, err := ocr.NewOracle(ocr.OCR3OracleArgs[[]byte]{ | ||
LocalConfig: args.LocalConfig, | ||
ContractConfigTracker: args.ContractConfigTracker, | ||
ContractTransmitter: args.ContractTransmitter, | ||
OffchainConfigDigester: args.OffchainConfigDigester, | ||
ReportingPluginFactory: args.ReportingPluginFactoryService, | ||
BinaryNetworkEndpointFactory: of.peerWrapper.Peer2, | ||
V2Bootstrappers: of.config.BootstrapPeers, | ||
Database: of.database, | ||
Logger: ocrcommon.NewOCRWrapper(of.lggr, of.config.TraceLogging, func(ctx context.Context, msg string) { | ||
logger.Sugared(of.lggr).ErrorIf(of.jobORM.RecordError(ctx, of.jobID, msg), "unable to record error") | ||
}), | ||
// TODO? | ||
MonitoringEndpoint: &telemetry.NoopAgent{}, | ||
OffchainKeyring: of.kb, | ||
OnchainKeyring: ocrcommon.NewOCR3OnchainKeyringAdapter(of.kb), | ||
MetricsRegisterer: prometheus.WrapRegistererWith(map[string]string{"job_name": of.jobName}, prometheus.DefaultRegisterer), | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("%w: failed to create new OCR oracle", err) | ||
} | ||
|
||
return &adaptedOracle{oracle: oracle}, nil | ||
} | ||
|
||
type adaptedOracle struct { | ||
oracle ocr.Oracle | ||
} | ||
|
||
func (a *adaptedOracle) Start(ctx context.Context) error { | ||
return a.oracle.Start() | ||
} | ||
|
||
func (a *adaptedOracle) Close(ctx context.Context) error { | ||
return a.oracle.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,10 @@ import ( | |
"github.com/smartcontractkit/chainlink-common/pkg/types/core" | ||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/job" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/keystore" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/generic" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/ocrcommon" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/telemetry" | ||
"github.com/smartcontractkit/chainlink/v2/plugins" | ||
|
@@ -34,15 +37,23 @@ type Delegate struct { | |
monitoringEndpointGen telemetry.MonitoringEndpointGenerator | ||
pipelineRunner pipeline.Runner | ||
relayers RelayGetter | ||
ocrKs keystore.OCR2 | ||
peerWrapper *ocrcommon.SingletonPeerWrapper | ||
|
||
isNewlyCreatedJob bool | ||
} | ||
|
||
func NewDelegate(logger logger.Logger, ds sqlutil.DataSource, jobORM job.ORM, registry core.CapabilitiesRegistry, | ||
cfg plugins.RegistrarConfig, monitoringEndpointGen telemetry.MonitoringEndpointGenerator, pipelineRunner pipeline.Runner, | ||
relayers RelayGetter) *Delegate { | ||
relayers RelayGetter, | ||
ocrKs keystore.OCR2, | ||
peerWrapper *ocrcommon.SingletonPeerWrapper, | ||
) *Delegate { | ||
return &Delegate{logger: logger, ds: ds, jobORM: jobORM, registry: registry, cfg: cfg, monitoringEndpointGen: monitoringEndpointGen, pipelineRunner: pipelineRunner, | ||
relayers: relayers, isNewlyCreatedJob: false} | ||
relayers: relayers, isNewlyCreatedJob: false, | ||
ocrKs: ocrKs, | ||
peerWrapper: peerWrapper, | ||
} | ||
} | ||
|
||
func (d *Delegate) JobType() job.Type { | ||
|
@@ -67,8 +78,38 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, spec job.Job) ([]job.Ser | |
return nil, fmt.Errorf("failed to create relayer set: %w", err) | ||
} | ||
|
||
keyBundles, err := d.ocrKs.GetAll() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% certain this will work, but we can try it |
||
if err != nil { | ||
return nil, err | ||
} | ||
if len(keyBundles) > 1 { | ||
return nil, fmt.Errorf("expected only one OCR key bundle, but found found: %d", len(keyBundles)) | ||
} | ||
|
||
oracleFactoryConfig, err := generic.NewOracleFactoryConfig(spec.StandardCapabilitiesSpec.OracleFactory) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal oracle factory config") | ||
} | ||
|
||
if oracleFactoryConfig.Enabled && d.peerWrapper == nil { | ||
return nil, errors.New("P2P stack required for Oracle Factory") | ||
} | ||
|
||
oracleFactory, err := generic.NewOracleFactory(generic.OracleFactoryParams{ | ||
Logger: log, | ||
JobORM: d.jobORM, | ||
JobID: spec.ID, | ||
JobName: spec.Name.ValueOrZero(), | ||
Database: ocr2.NewDB(d.ds, spec.ID, 0, log), | ||
Kb: keyBundles[0], | ||
Config: oracleFactoryConfig, | ||
PeerWrapper: d.peerWrapper, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create oracle factory: %w", err) | ||
} | ||
standardCapability := newStandardCapabilities(log, spec.StandardCapabilitiesSpec, d.cfg, telemetryService, kvStore, d.registry, errorLog, | ||
pr, relayerSet) | ||
pr, relayerSet, oracleFactory) | ||
|
||
return []job.ServiceCtx{standardCapability}, nil | ||
} | ||
|
@@ -107,6 +148,11 @@ func ValidatedStandardCapabilitiesSpec(tomlString string) (job.Job, error) { | |
return jb, errors.Errorf("standard capabilities command must be set") | ||
} | ||
|
||
_, err = generic.NewOracleFactoryConfig(jb.StandardCapabilitiesSpec.OracleFactory) | ||
if err != nil { | ||
return jb, errors.Wrap(err, "failed to unmarshal oracle factory config") | ||
} | ||
|
||
return jb, nil | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DeividasK OracleFactoryConfig?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we put a comment here to document where we define what's expected in OracleConfig?