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

[LIT-2333] Add e2e test for initializing multiple clients #331

Conversation

cairomassimo
Copy link
Contributor

@cairomassimo cairomassimo commented Jan 29, 2024

Description

This PR enables the use of multiple instances of LitNodeClient, in order to connect to different networks in the same application, at the same time.

This was achieved by removing the global variables litConfig, litNodeClient, logger and logManager and their references.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added an e2e test which calls .connect() on multiple clients.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

This commit removes the following global variables:
logManager, logger, litConfig, litNodeClient
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cairomassimo cairomassimo marked this pull request as ready for review January 29, 2024 11:16
Copy link

@joshLong145 joshLong145 left a comment

Choose a reason for hiding this comment

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

I see you removed methods which we where using in many packages. Can you outline the implications and behavior changes that are introduced with these utilities gone? #331 will also add many of these methods. back when merged. There does not seem to be a path forward for the above mentioned pr with the current implementation.
While removing the implementations which utilize the global state does make a path forward, I am unsure what this pr recommends as a migration from the previous implementations.

if (!client.ready) {
return fail('client not ready');
}
if (client.config.litNetwork !== network) {

Choose a reason for hiding this comment

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

Could we add a check on globalThis to assert on the new global namespacing?

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 didn't implement the global namespacing, I think it's a risky approach (see also #331 (comment)). There could be a use case for global namespaces I'm not aware of yet: in that case please let me know.

@@ -60,7 +60,6 @@ export class LitAuthClient {
*/
constructor(options?: LitAuthClientOptions) {
this.providers = new Map();
bootstrapLogManager('auth-client');

Choose a reason for hiding this comment

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

why did we remove this line?

import { version } from '@lit-protocol/constants';

const logBuffer: Array<Array<any>> = [];
const defaultLogger = LogManager.Instance.get('lit-sdk');
Copy link

@joshLong145 joshLong145 Jan 29, 2024

Choose a reason for hiding this comment

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

will this be a global logger? I would like to keep the package names as categories if possible. Open to discussion however.
Also why did you remove the buffer which allows us to collect logs when we are not in a connected state?

Comment on lines 324 to 350
if (!globalThis) {
// there is no globalThis, just print the log
console.log(...args);
return;
}

// check if config is loaded yet
if (!globalThis?.litConfig) {
// config isn't loaded yet, push into buffer
logBuffer.push(args);
return;
}

if (globalThis?.litConfig?.debug !== true) {
return;
}
// config is loaded, and debug is true

// if there are there are logs in buffer, print them first and empty the buffer.
while (logBuffer.length > 0) {
const log = logBuffer.shift() ?? '';
globalThis?.logger &&
globalThis.logManager.get(globalThis.logger.category).error(...log);
}

globalThis?.logger &&
globalThis.logManager.get(globalThis.logger.category).error(...args);

Choose a reason for hiding this comment

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

Why did we remove all of these utilities?

@cairomassimo cairomassimo marked this pull request as draft January 29, 2024 16:40
@cairomassimo cairomassimo changed the title [LIT-2333] Add support for initializing multiple clients [LIT-2333] Add e2e test for initializing multiple clients Jan 30, 2024
@cairomassimo
Copy link
Contributor Author

@joshLong145 I updated the PR so that it only adds the e2e test for connecting to multiple networks. The test passes with the code as is.
We discussed about the changes on the logger in a call, but overall I think it makes sense to not apply those changes in this PR anyway.

To be clear, with the code as it is, creating multiple clients does mess up the global state (globalThis.litConfig and globalThis.litNodeClient get replaced). However:

  1. this is the current behavior anyway, so the PR is not introducing a regression here,
  2. I think we should recommend the SDK consumers to not rely on the global state anyway, rather than introducing a namespacing convention, espcially since they have to instantiate the clients themselves anyway.

There is one more check to do: making sure that there are no conflicts with the usage of localStorage when instantiating more than one client. I couldn't find any, but it's better that someone with more experience with the codebase has a look at this specifically.

@joshLong145
Copy link

@cairomassimo can we close this pr since we no longer need to make code changes?

@cairomassimo
Copy link
Contributor Author

@cairomassimo can we close this pr since we no longer need to make code changes?

@joshLong145 It's not much, but we can merge it so we have the new e2e test case? I'm making it "ready for review" just in case

@cairomassimo cairomassimo marked this pull request as ready for review February 1, 2024 09:34
Copy link
Collaborator

@Ansonhkg Ansonhkg left a comment

Choose a reason for hiding this comment

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

I'm ok with having this for our e2e tests

@Ansonhkg Ansonhkg changed the base branch from master to staging/2024-02-06 February 2, 2024 12:41
@Ansonhkg Ansonhkg merged commit 7fbb90e into staging/2024-02-06 Feb 2, 2024
1 of 4 checks passed
@Ansonhkg Ansonhkg deleted the feature/lit-2333-add-support-for-initializing-global-lit-variables-under branch February 2, 2024 12:41
@Ansonhkg Ansonhkg mentioned this pull request Feb 2, 2024
5 tasks
Ansonhkg added a commit that referenced this pull request Feb 6, 2024
* make capacityDelegationAuthSig optional (#337)

* Ref/run sev att check on mainnets (#339)

* add sev snp att check by default on main nets

* fmt

* [LIT-2333] Add e2e test for initializing multiple clients (#331)

* Remove globals for loggers and client

This commit removes the following global variables:
logManager, logger, litConfig, litNodeClient

* Test e2e parallel connections to multiple networks

* Revert "Remove globals for loggers and client"

This reverts commit 14f35f0.

* Cleanup/logger (#332)

* aggregate logs under categories. allow for cross category tracing

* autogen

* fmt

* change log persistence to use request id's as keys for lookup

* fmt

* fmt

* fix logger creation

* fmt

* fmt

* Update/test token (#336)

* update token symbol

* fmt

* fmt

* fmt

* fix: rm console.log (#342)

* Fix logging tests in 3.1.2 staging (#343)

* fix logging tests

* fix category in test

* Published version: 3.1.2

---------

Signed-off-by: Anson <[email protected]>
Co-authored-by: Bean <[email protected]>
Co-authored-by: Massimo Cairo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants