Skip to content

Commit

Permalink
[KS-55] Engine and Registry improvements (#12115)
Browse files Browse the repository at this point in the history
1. Move registry operations into a separate goroutine launched at Start() and supporting re-tries, to avoid race conditions on node init.
2. Move most of the hardcoded workflow into Engine constructor, for clarity.
3. Add missing call to consensus.RegisterToWorkflow().
4. Add logger to Registry.
  • Loading branch information
bolekk authored Feb 21, 2024
1 parent b84bdb5 commit 216efed
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 110 deletions.
13 changes: 9 additions & 4 deletions core/capabilities/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import (
"sync"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities"
"github.com/smartcontractkit/chainlink/v2/core/logger"
)

// Registry is a struct for the registry of capabilities.
// Registry is safe for concurrent use.
type Registry struct {
m map[string]capabilities.BaseCapability
mu sync.RWMutex
m map[string]capabilities.BaseCapability
mu sync.RWMutex
lggr logger.Logger
}

// Get gets a capability from the registry.
func (r *Registry) Get(_ context.Context, id string) (capabilities.BaseCapability, error) {
r.mu.RLock()
defer r.mu.RUnlock()

r.lggr.Debugw("get capability", "id", id)
c, ok := r.m[id]
if !ok {
return nil, fmt.Errorf("capability not found with id %s", id)
Expand Down Expand Up @@ -142,13 +145,15 @@ func (r *Registry) Add(ctx context.Context, c capabilities.BaseCapability) error
}

r.m[id] = c
r.lggr.Infow("capability added", "id", id, "type", info.CapabilityType, "description", info.Description, "version", info.Version)
return nil

}

// NewRegistry returns a new Registry.
func NewRegistry() *Registry {
func NewRegistry(lggr logger.Logger) *Registry {
return &Registry{
m: map[string]capabilities.BaseCapability{},
m: map[string]capabilities.BaseCapability{},
lggr: lggr.Named("CapabilityRegistry"),
}
}
7 changes: 4 additions & 3 deletions core/capabilities/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/triggers"
coreCapabilities "github.com/smartcontractkit/chainlink/v2/core/capabilities"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/logger"
)

type mockCapability struct {
Expand All @@ -33,7 +34,7 @@ func (m *mockCapability) UnregisterFromWorkflow(ctx context.Context, request cap
func TestRegistry(t *testing.T) {
ctx := testutils.Context(t)

r := coreCapabilities.NewRegistry()
r := coreCapabilities.NewRegistry(logger.TestLogger(t))

id := "capability-1"
ci, err := capabilities.NewCapabilityInfo(
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestRegistry(t *testing.T) {

func TestRegistry_NoDuplicateIDs(t *testing.T) {
ctx := testutils.Context(t)
r := coreCapabilities.NewRegistry()
r := coreCapabilities.NewRegistry(logger.TestLogger(t))

id := "capability-1"
ci, err := capabilities.NewCapabilityInfo(
Expand Down Expand Up @@ -172,7 +173,7 @@ func TestRegistry_ChecksExecutionAPIByType(t *testing.T) {
}

ctx := testutils.Context(t)
reg := coreCapabilities.NewRegistry()
reg := coreCapabilities.NewRegistry(logger.TestLogger(t))
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
id, err := tc.newCapability(ctx, reg)
Expand Down
2 changes: 1 addition & 1 deletion core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.1
github.com/shopspring/decimal v1.3.1
github.com/smartcontractkit/chainlink-automation v1.0.2-0.20240118014648-1ab6a88c9429
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240219152510-85226a0fbdc1
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240221153538-1ea85cf3dc6c
github.com/smartcontractkit/chainlink-vrf v0.0.0-20231120191722-fef03814f868
github.com/smartcontractkit/chainlink/v2 v2.0.0-00010101000000-000000000000
github.com/smartcontractkit/libocr v0.0.0-20240215150045-fe2ba71b2f0a
Expand Down
4 changes: 2 additions & 2 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,8 @@ github.com/smartcontractkit/chain-selectors v1.0.10 h1:t9kJeE6B6G+hKD0GYR4kGJSCq
github.com/smartcontractkit/chain-selectors v1.0.10/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE=
github.com/smartcontractkit/chainlink-automation v1.0.2-0.20240118014648-1ab6a88c9429 h1:xkejUBZhcBpBrTSfxc91Iwzadrb6SXw8ks69bHIQ9Ww=
github.com/smartcontractkit/chainlink-automation v1.0.2-0.20240118014648-1ab6a88c9429/go.mod h1:wJmVvDf4XSjsahWtfUq3wvIAYEAuhr7oxmxYnEL/LGQ=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240219152510-85226a0fbdc1 h1:MNYkjakmoKxg7L1nmfAVeFOdONaLT7E62URBpmcTh84=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240219152510-85226a0fbdc1/go.mod h1:6aXWSEQawX2oZXcPPOdxnEGufAhj7PqPKolXf6ijRGA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240221153538-1ea85cf3dc6c h1:ielGD+tVCB+irZ+nDt5VDTYJauJI88tirkLLaHWLaTs=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240221153538-1ea85cf3dc6c/go.mod h1:6aXWSEQawX2oZXcPPOdxnEGufAhj7PqPKolXf6ijRGA=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240213120401-01a23955f9f8 h1:I326nw5GwHQHsLKHwtu5Sb9EBLylC8CfUd7BFAS0jtg=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240213120401-01a23955f9f8/go.mod h1:a65NtrK4xZb01mf0dDNghPkN2wXgcqFQ55ADthVBgMc=
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240214203158-47dae5de1336 h1:j00D0/EqE9HRu+63v7KwUOe4ZxLc4AN5SOJFiinkkH0=
Expand Down
2 changes: 1 addition & 1 deletion core/services/chainlink/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func NewApplication(opts ApplicationOpts) (Application, error) {
keyStore := opts.KeyStore
restrictedHTTPClient := opts.RestrictedHTTPClient
unrestrictedHTTPClient := opts.UnrestrictedHTTPClient
registry := capabilities.NewRegistry()
registry := capabilities.NewRegistry(globalLogger)

// LOOPs can be created as options, in the case of LOOP relayers, or
// as OCR2 job implementations, in the case of Median today.
Expand Down
2 changes: 1 addition & 1 deletion core/services/job/spawner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) {
ocr2DelegateConfig := ocr2.NewDelegateConfig(config.OCR2(), config.Mercury(), config.Threshold(), config.Insecure(), config.JobPipeline(), config.Database(), processConfig)

d := ocr2.NewDelegate(nil, orm, nil, nil, nil, nil, monitoringEndpoint, legacyChains, lggr, ocr2DelegateConfig,
keyStore.OCR2(), keyStore.DKGSign(), keyStore.DKGEncrypt(), ethKeyStore, testRelayGetter, mailMon, capabilities.NewRegistry())
keyStore.OCR2(), keyStore.DKGSign(), keyStore.DKGEncrypt(), ethKeyStore, testRelayGetter, mailMon, capabilities.NewRegistry(lggr))
delegateOCR2 := &delegate{jobOCR2VRF.Type, []job.ServiceCtx{}, 0, nil, d}

spawner := job.NewSpawner(orm, config.Database(), noopChecker{}, map[job.Type]job.Delegate{
Expand Down
Loading

0 comments on commit 216efed

Please sign in to comment.