Skip to content
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-372] Lazy initialize the registrysyncer #13787

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nit: it doesn't seem like this can be set by any caller. iiuc, then there would be less indirection to simply make it a static method on the syncer, or omit it entirely and call newReader in it's place

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this mainly to make testing easier, such that a test could set initReader and set up a mock rather than setting up an actual chain reader.

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
Loading