diff --git a/.changeset/healthy-lamps-argue.md b/.changeset/healthy-lamps-argue.md new file mode 100644 index 00000000000..33357ddd6ce --- /dev/null +++ b/.changeset/healthy-lamps-argue.md @@ -0,0 +1,5 @@ +--- +"chainlink": minor +--- + +Initialize registry syncer' contract reader lazily #keystone #internal diff --git a/core/services/registrysyncer/syncer.go b/core/services/registrysyncer/syncer.go index e6a54078738..5a4ce4b6ba5 100644 --- a/core/services/registrysyncer/syncer.go +++ b/core/services/registrysyncer/syncer.go @@ -35,9 +35,12 @@ type Syncer interface { } type registrySyncer struct { - stopCh services.StopChan - launchers []Launcher - reader types.ContractReader + stopCh services.StopChan + launchers []Launcher + reader types.ContractReader + initReader func(ctx context.Context, lggr logger.Logger, relayer contractReaderFactory, registryAddress string) (types.ContractReader, error) + relayer contractReaderFactory + registryAddress string wg sync.WaitGroup lggr logger.Logger @@ -57,17 +60,13 @@ func New( registryAddress string, ) (*registrySyncer, error) { stopCh := make(services.StopChan) - ctx, _ := stopCh.NewCtx() - reader, err := newReader(ctx, lggr, relayer, registryAddress) - if err != nil { - return nil, err - } - - return newSyncer( - stopCh, - lggr.Named("RegistrySyncer"), - reader, - ), nil + return ®istrySyncer{ + stopCh: stopCh, + lggr: lggr, + relayer: relayer, + registryAddress: registryAddress, + initReader: newReader, + }, nil } type contractReaderFactory interface { @@ -114,18 +113,6 @@ func newReader(ctx context.Context, lggr logger.Logger, relayer contractReaderFa return cr, err } -func newSyncer( - stopCh services.StopChan, - lggr logger.Logger, - reader types.ContractReader, -) *registrySyncer { - return ®istrySyncer{ - stopCh: stopCh, - lggr: lggr, - reader: reader, - } -} - func (s *registrySyncer) Start(ctx context.Context) error { s.wg.Add(1) go func() { @@ -211,6 +198,15 @@ func (s *registrySyncer) sync(ctx context.Context) error { return nil } + if s.reader == nil { + reader, err := s.initReader(ctx, s.lggr, s.relayer, s.registryAddress) + if err != nil { + return err + } + + s.reader = reader + } + state, err := s.state(ctx) if err != nil { return fmt.Errorf("failed to sync with remote registry: %w", err) diff --git a/core/services/registrysyncer/syncer_test.go b/core/services/registrysyncer/syncer_test.go index 56818c81dc3..f36aa806067 100644 --- a/core/services/registrysyncer/syncer_test.go +++ b/core/services/registrysyncer/syncer_test.go @@ -116,6 +116,15 @@ func randomWord() [32]byte { return [32]byte(word) } +type launcher struct { + localRegistry State +} + +func (l *launcher) Launch(ctx context.Context, localRegistry State) error { + l.localRegistry = localRegistry + return nil +} + func TestReader_Integration(t *testing.T) { ctx := testutils.Context(t) reg, regAddress, owner, sim := startNewChainWithRegistry(t) @@ -192,10 +201,14 @@ func TestReader_Integration(t *testing.T) { require.NoError(t, err) factory := newContractReaderFactory(t, sim) - reader, err := New(logger.TestLogger(t), factory, regAddress.Hex()) + syncer, err := New(logger.TestLogger(t), factory, regAddress.Hex()) require.NoError(t, err) - s, err := reader.state(ctx) + l := &launcher{} + syncer.AddLauncher(l) + + err = syncer.sync(ctx) + s := l.localRegistry require.NoError(t, err) assert.Len(t, s.IDsToCapabilities, 1)