-
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.
+514
−36
Draft
Changes from all 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
123 changes: 123 additions & 0 deletions
123
core/services/ocr2/plugins/generic/oracle_factory_db.go
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,123 @@ | ||
package generic | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
) | ||
|
||
type memoryDb struct { | ||
// The ID is used for logging and error messages | ||
// A single standard capabilities spec can instantiate multiple oracles | ||
// TODO: NewOracle should take a unique identifier for the oracle | ||
specID int32 | ||
lggr logger.SugaredLogger | ||
config *ocrtypes.ContractConfig | ||
states map[ocrtypes.ConfigDigest]*ocrtypes.PersistentState | ||
pendingTransmissions map[ocrtypes.ReportTimestamp]ocrtypes.PendingTransmission | ||
protocolStates map[ocrtypes.ConfigDigest]map[string][]byte | ||
} | ||
|
||
var ( | ||
_ ocrtypes.Database = &memoryDb{} | ||
) | ||
|
||
// NewDB returns a new DB scoped to this instanceID | ||
func NewMemoryDB(specID int32, lggr logger.Logger) *memoryDb { | ||
return &memoryDb{ | ||
specID: specID, | ||
lggr: logger.Sugared(lggr.Named("OracleFactoryMemoryDb")), | ||
} | ||
} | ||
|
||
func (md *memoryDb) ReadState(ctx context.Context, cd ocrtypes.ConfigDigest) (ps *ocrtypes.PersistentState, err error) { | ||
ps, ok := md.states[cd] | ||
if !ok { | ||
return nil, errors.Errorf("state not found for standard capabilities spec ID %d, config digest %s", md.specID, cd) | ||
} | ||
|
||
return ps, nil | ||
} | ||
|
||
func (md *memoryDb) WriteState(ctx context.Context, cd ocrtypes.ConfigDigest, state ocrtypes.PersistentState) error { | ||
md.states[cd] = &state | ||
return nil | ||
} | ||
|
||
func (md *memoryDb) ReadConfig(ctx context.Context) (c *ocrtypes.ContractConfig, err error) { | ||
if md.config == nil { | ||
return nil, errors.Errorf("config not found for standard capabilities spec ID %d", md.specID) | ||
} | ||
return md.config, nil | ||
} | ||
|
||
func (md *memoryDb) WriteConfig(ctx context.Context, c ocrtypes.ContractConfig) error { | ||
md.config = &c | ||
return nil | ||
} | ||
|
||
func (md *memoryDb) StorePendingTransmission(ctx context.Context, t ocrtypes.ReportTimestamp, tx ocrtypes.PendingTransmission) error { | ||
md.pendingTransmissions[t] = tx | ||
return nil | ||
} | ||
|
||
func (md *memoryDb) PendingTransmissionsWithConfigDigest(ctx context.Context, cd ocrtypes.ConfigDigest) (map[ocrtypes.ReportTimestamp]ocrtypes.PendingTransmission, error) { | ||
m := make(map[ocrtypes.ReportTimestamp]ocrtypes.PendingTransmission) | ||
for k, v := range md.pendingTransmissions { | ||
if k.ConfigDigest == cd { | ||
m[k] = v | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (md *memoryDb) DeletePendingTransmission(ctx context.Context, t ocrtypes.ReportTimestamp) error { | ||
delete(md.pendingTransmissions, t) | ||
return nil | ||
} | ||
|
||
func (md *memoryDb) DeletePendingTransmissionsOlderThan(ctx context.Context, t time.Time) error { | ||
for k, v := range md.pendingTransmissions { | ||
if v.Time.Before(t) { | ||
delete(md.pendingTransmissions, k) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (md *memoryDb) ReadProtocolState( | ||
ctx context.Context, | ||
configDigest ocrtypes.ConfigDigest, | ||
key string, | ||
) ([]byte, error) { | ||
value, ok := md.protocolStates[configDigest][key] | ||
if !ok { | ||
// Previously implementation returned nil if the state is not found | ||
// TODO: Should this return nil, nil? | ||
return nil, nil | ||
} | ||
return value, nil | ||
} | ||
|
||
func (md *memoryDb) WriteProtocolState( | ||
ctx context.Context, | ||
configDigest ocrtypes.ConfigDigest, | ||
key string, | ||
value []byte, | ||
) error { | ||
if value == nil { | ||
delete(md.protocolStates[configDigest], key) | ||
} else { | ||
if md.protocolStates[configDigest] == nil { | ||
md.protocolStates[configDigest] = make(map[string][]byte) | ||
} | ||
md.protocolStates[configDigest][key] = value | ||
} | ||
return nil | ||
} |
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,135 @@ | ||
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 { | ||
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: NewMemoryDB(params.JobID, params.Logger), | ||
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) { | ||
if !of.peerWrapper.IsStarted() { | ||
return nil, errors.New("peer wrapper not started") | ||
} | ||
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() | ||
} |
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?