Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

feat: Light clients #76

Closed
wants to merge 9 commits into from
Closed

feat: Light clients #76

wants to merge 9 commits into from

Conversation

nicolad
Copy link

@nicolad nicolad commented May 29, 2023

Resolves #40

  • There is an associated issue (required)
  • The change is described in detail
  • There are new or updated tests validating the change (if applicable)

Description

@nicolad nicolad requested a review from DoubleOTheven as a code owner May 29, 2023 14:02
@nicolad
Copy link
Author

nicolad commented May 29, 2023

image

@nicolad
Copy link
Author

nicolad commented May 29, 2023

need to fix this

image

});
};

const addSmoldotProvider = async (chain: Chain) => {
Copy link
Contributor

@DoubleOTheven DoubleOTheven May 31, 2023

Choose a reason for hiding this comment

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

We should only set one provider at a time, not both the light client and a WsProvider instance.

I think it makes sense to add a config in the ConfigProvider so a user or the developer can choose if they want one or the other globally or per chain. The default should be WsProvider.

  1. Maybe something like this?

// in config/model.ts

export type ConnectionType = 'trusted' | 'light-client';

export type ConfigProps = {
  // ... omitted
  api?: {
    default?: ConnectionType;
  } & Partial<Record<ChainId, ConnectionType>>;
};

Then inside of ApiProvider you use useConfig() to get the values and create the instances accordingly. This will update automatically when the config is changed

Copy link
Contributor

@DoubleOTheven DoubleOTheven May 31, 2023

Choose a reason for hiding this comment

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

We can also add a setter function to ConfigProvider so a user can change the connection type for all chains or for a specific one.

e.g.

const { setConnectionType } = useConfig();

// set for default chain
setConnectionType('light-client');

// set for a specific chain
setConnectionType('light-client', 'aleph');

// set for all chains
setConnectionType('light-client', 'all');

Maybe setConnectionType is defined like this:

type Chain = ChainId | 'all';
const setConnectionType = (type: ConnectionType, chain?: Chain) => void;
  1. If chain is omitted as an argument, it uses the default chain.
  2. If chain has the value all, set config.api to { default: '<type>' }
  3. If chain is a ChainId then set config.api[chainId] = '<type>'

Copy link
Author

Choose a reason for hiding this comment

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

thx, made some changes, will continue with ApiProvider in the upcoming commits

Comment on lines +31 to +39
if (!c) {
config.api = defaultChainId;
return;
}
if (c === "all") {
config.api = { default: type };
return;
}
config.api[c.id] = type;
Copy link
Author

Choose a reason for hiding this comment

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

used early returns to keep it flat and avoid if/ else

@nicolad nicolad changed the title Add smoldot provider feat: Light clients Jun 1, 2023
Comment on lines +30 to +36
useEffect(() => {
if (api?.default === "light-client") {
const provider = new ScProvider(Sc, "all");

provider.connect();
}
}, [api]);
Copy link
Author

Choose a reason for hiding this comment

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

@DoubleOTheven in this comment you mentioned

Then inside of ApiProvider you use useConfig() to get the values and create the instances accordingly. This will update automatically when the config is changed

I tried to use useEffect to update automatically, but not entirely sure if this is the right approach and/ or if the other useEffect becomes obsolete.

Let me know how do you envision APIProvider to behave.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you think there is a better approach you can suggest it. I was only giving some ideas.

useEffect can be used to update the state of ApiProvider if some other state changes.

import { APIContext } from './context.ts'
import { apiProvidersReducer } from './reducer.ts'
import { useConfig } from '../../mod.ts'
import React, { useEffect, useReducer } from "react";
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 looks like your IDE formatter is set to something other than Deno. Deno should be the formatter. I think this will fix the issue where ' is converted to "

@nicolad nicolad closed this by deleting the head repository Jul 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add light clients as an alternative to ApiPromise
2 participants