Skip to content

Commit

Permalink
[KS-372] Lazy initialize the registrysyncer (#13787)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier authored Jul 9, 2024
1 parent 4e3f5e8 commit e065b82
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-lamps-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

Initialize registry syncer' contract reader lazily #keystone #internal
48 changes: 22 additions & 26 deletions core/services/registrysyncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 &registrySyncer{
stopCh: stopCh,
lggr: lggr,
relayer: relayer,
registryAddress: registryAddress,
initReader: newReader,
}, nil
}

type contractReaderFactory interface {
Expand Down Expand Up @@ -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 &registrySyncer{
stopCh: stopCh,
lggr: lggr,
reader: reader,
}
}

func (s *registrySyncer) Start(ctx context.Context) error {
s.wg.Add(1)
go func() {
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions core/services/registrysyncer/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit e065b82

Please sign in to comment.