-
Notifications
You must be signed in to change notification settings - Fork 28
Deprecate use of polling provider #105
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
base: main
Are you sure you want to change the base?
Changes from all commits
d43f658
4ff6e52
7c05beb
0ac5673
ac818ed
c5ff86f
c23d095
9907a34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,82 +4,63 @@ import { | |
| LoadProviderResponse, | ||
| NETWORK_CONFIG, | ||
| NetworkName, | ||
| ProviderJson, | ||
| TXIDVersion, | ||
| createFallbackProviderFromJsonConfig, | ||
| createProviderFromJsonConfig, | ||
| isDefined, | ||
| } from '@railgun-community/shared-models'; | ||
| import { sendMessage } from '../../../utils'; | ||
| import { reportAndSanitizeError } from '../../../utils/error'; | ||
| import { WalletPOI } from '../../poi/wallet-poi'; | ||
| import { getEngine } from './engine'; | ||
| import { | ||
| PollingJsonRpcProvider, | ||
| RailgunVersionedSmartContracts, | ||
| createPollingJsonRpcProviderForListeners, | ||
| } from '@railgun-community/engine'; | ||
| import { FallbackProvider } from 'ethers' | ||
| import { type Provider} from 'ethers' | ||
| import { | ||
| fallbackProviderMap, | ||
| pollingProviderMap, | ||
| setFallbackProviderForNetwork, | ||
| setPollingProviderForNetwork, | ||
| providerMap, | ||
| setProviderForNetwork, | ||
| } from './providers'; | ||
| import { WalletPOINodeInterface } from '../../poi/wallet-poi-node-interface'; | ||
|
|
||
| const createFallbackProviderForNetwork = async ( | ||
| const createProviderForNetwork = async ( | ||
| networkName: NetworkName, | ||
| fallbackProviderJsonConfig: FallbackProviderJsonConfig, | ||
| ): Promise<FallbackProvider> => { | ||
| const existingProvider = fallbackProviderMap[networkName]; | ||
| providerJsonConfig: FallbackProviderJsonConfig | ProviderJson, | ||
| pollingInterval?: number, | ||
| ): Promise<Provider> => { | ||
| const existingProvider = providerMap[networkName]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's throwing a lint error for me here |
||
| if (existingProvider) { | ||
| return existingProvider; | ||
| } | ||
| const fallbackProvider = createFallbackProviderFromJsonConfig( | ||
| fallbackProviderJsonConfig, | ||
| ); | ||
| setFallbackProviderForNetwork(networkName, fallbackProvider); | ||
| return fallbackProvider; | ||
| }; | ||
|
|
||
| const createPollingProviderForNetwork = async ( | ||
| networkName: NetworkName, | ||
| fallbackProvider: FallbackProvider, | ||
| pollingInterval: number, | ||
| ): Promise<PollingJsonRpcProvider> => { | ||
| const existingProvider = pollingProviderMap[networkName]; | ||
| if (existingProvider) { | ||
| return existingProvider; | ||
| } | ||
| const network = NETWORK_CONFIG[networkName]; | ||
| if (!isDefined(network)) { | ||
| throw new Error('No network found'); | ||
| } | ||
| const pollingProvider = await createPollingJsonRpcProviderForListeners( | ||
| fallbackProvider, | ||
| network.chain.id, | ||
| pollingInterval, | ||
| const provider = createProviderFromJsonConfig( | ||
| providerJsonConfig, | ||
| pollingInterval | ||
| ); | ||
| setPollingProviderForNetwork(networkName, pollingProvider); | ||
| return pollingProvider; | ||
| setProviderForNetwork(networkName, provider); | ||
| return provider; | ||
| }; | ||
|
|
||
| /** | ||
| * | ||
| * @param chain | ||
| * @param networkName | ||
| * @param fallbackProviderJsonConfig | ||
| * @param pollingInterval | ||
| */ | ||
| const loadProviderForNetwork = async ( | ||
| chain: Chain, | ||
| networkName: NetworkName, | ||
| fallbackProviderJsonConfig: FallbackProviderJsonConfig, | ||
| providerJsonConfig: FallbackProviderJsonConfig | ProviderJson, | ||
| pollingInterval: number, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add deprecated to this too?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's already there if you are asking to ensure it has deprecated on it |
||
| ) => { | ||
| sendMessage(`Load provider for network: ${networkName}`); | ||
|
|
||
| const fallbackProvider = await createFallbackProviderForNetwork( | ||
| networkName, | ||
| fallbackProviderJsonConfig, | ||
| ); | ||
| const pollingProvider = await createPollingProviderForNetwork( | ||
| networkName, | ||
| fallbackProvider, | ||
| pollingInterval | ||
| ); | ||
| // Create the provider from the JSON config | ||
| const provider = await createProviderForNetwork( | ||
| networkName, | ||
| providerJsonConfig, | ||
| pollingInterval, | ||
| ); | ||
|
|
||
| const network = NETWORK_CONFIG[networkName]; | ||
| const { | ||
|
|
@@ -125,8 +106,8 @@ const loadProviderForNetwork = async ( | |
| poseidonMerkleAccumulatorV3Contract, | ||
| poseidonMerkleVerifierV3Contract, | ||
| tokenVaultV3Contract, | ||
| fallbackProvider, | ||
| pollingProvider, | ||
| provider, // Can be of type FallbackProvider, WebSocketProvider, or JsonRpcProvider | ||
| undefined, // pollingProvider is being deprecated | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type error here. I do not think it's a breaking change to change the type to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you saying to remove the optional param and not pass undefined? Or just stating that it is okay It is part of the last commit to the relating engine PR Railgun-Community/engine@3e3f607 |
||
| deploymentBlocks, | ||
| poi?.launchBlock, | ||
| supportsV3, | ||
|
|
@@ -138,22 +119,25 @@ const loadProviderForNetwork = async ( | |
| * for new RAILGUN events (balance updates). | ||
| */ | ||
| export const loadProvider = async ( | ||
| fallbackProviderJsonConfig: FallbackProviderJsonConfig, | ||
| providerJsonConfig: FallbackProviderJsonConfig | ProviderJson, | ||
| networkName: NetworkName, | ||
| /** | ||
| * @deprecated pollingInterval - Default ethers polling interval is used | ||
| */ | ||
| pollingInterval = 15000, | ||
| ): Promise<LoadProviderResponse> => { | ||
| try { | ||
| delete fallbackProviderMap[networkName]; | ||
| delete providerMap[networkName]; | ||
|
|
||
| const { chain, supportsV3 } = NETWORK_CONFIG[networkName]; | ||
| if (fallbackProviderJsonConfig.chainId !== chain.id) { | ||
| if ('chainId' in providerJsonConfig && providerJsonConfig.chainId !== chain.id) { | ||
| throw new Error('Invalid chain ID'); | ||
| } | ||
|
|
||
| await loadProviderForNetwork( | ||
| chain, | ||
| networkName, | ||
| fallbackProviderJsonConfig, | ||
| providerJsonConfig, | ||
| pollingInterval, | ||
| ); | ||
| WalletPOINodeInterface.unpause(chain); | ||
|
|
@@ -197,23 +181,33 @@ export const unloadProvider = async ( | |
| networkName: NetworkName, | ||
| ): Promise<void> => { | ||
| WalletPOINodeInterface.pause(NETWORK_CONFIG[networkName].chain); | ||
| await fallbackProviderMap[networkName]?.destroy(); | ||
| pollingProviderMap[networkName]?.destroy(); | ||
| delete fallbackProviderMap[networkName]; | ||
| delete pollingProviderMap[networkName]; | ||
| providerMap[networkName]?.destroy(); | ||
| delete providerMap[networkName]; | ||
| }; | ||
|
|
||
| export const pauseAllPollingProviders = ( | ||
| excludeNetworkName?: NetworkName, | ||
| ): void => { | ||
| Object.keys(pollingProviderMap).forEach(networkName => { | ||
| Object.keys(providerMap).forEach(networkName => { | ||
| if (networkName === excludeNetworkName) { | ||
| return; | ||
| } | ||
| const pollingProvider = pollingProviderMap[networkName]; | ||
| if (isDefined(pollingProvider) && !pollingProvider.paused) { | ||
| pollingProvider.pause(); | ||
| const provider = providerMap[networkName]; | ||
|
|
||
| // Check if provider exists | ||
| if (!isDefined(provider)) { | ||
| throw new Error(`No provider found for network: ${networkName}`); | ||
| } | ||
|
|
||
| // Ensure provider is a pausable provider | ||
| if (!('paused' in provider && 'pause' in provider)) { | ||
| throw new Error( | ||
| `Provider for network ${networkName} is not a pausable provider`, | ||
| ); | ||
| } | ||
|
|
||
| // Safe to call pause() after type check | ||
| (provider as { pause(): void }).pause(); | ||
| }); | ||
| }; | ||
|
|
||
|
|
@@ -223,11 +217,20 @@ export const resumeIsolatedPollingProviderForNetwork = ( | |
| pauseAllPollingProviders( | ||
| networkName, // excludeNetworkName | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we rename this to excludedNetworkName instead of just having it as a comment ? |
||
| ); | ||
| const pollingProviderForNetwork = pollingProviderMap[networkName]; | ||
| if ( | ||
| isDefined(pollingProviderForNetwork) && | ||
| pollingProviderForNetwork.paused | ||
| ) { | ||
| pollingProviderForNetwork.resume(); | ||
| const provider = providerMap[networkName]; | ||
|
|
||
| // Check if provider exists | ||
| if (!isDefined(provider)) { | ||
| throw new Error(`No provider found for network: ${networkName}`); | ||
| } | ||
|
|
||
| // Ensure provider has resume functionality | ||
| if (!('paused' in provider && 'resume' in provider)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do a validateProvider function rather than having this statement twice, since the flow itself its pretty similar something like |
||
| throw new Error( | ||
| `Provider for network ${networkName} is not a pausable provider`, | ||
| ); | ||
| } | ||
|
|
||
| // Safe to call resume() after type check | ||
| (provider as { resume(): void }).resume(); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,21 @@ | ||
| import { NetworkName, isDefined } from '@railgun-community/shared-models'; | ||
| import { PollingJsonRpcProvider } from '@railgun-community/engine'; | ||
| import { FallbackProvider } from 'ethers'; | ||
| import { Provider } from 'ethers'; | ||
|
|
||
| export const fallbackProviderMap: MapType<FallbackProvider> = {}; | ||
| export const pollingProviderMap: MapType<PollingJsonRpcProvider> = {}; | ||
| export const providerMap: MapType<Provider> = {}; | ||
|
|
||
| export const getFallbackProviderForNetwork = ( | ||
| export const getProviderForNetwork = ( | ||
| networkName: NetworkName, | ||
| ): FallbackProvider => { | ||
| const provider = fallbackProviderMap[networkName]; | ||
| ): Provider => { | ||
| const provider = providerMap[networkName]; | ||
| if (!isDefined(provider)) { | ||
| throw new Error(`Provider not yet loaded for network ${networkName}`); | ||
| } | ||
| return provider; | ||
| }; | ||
|
|
||
| export const getPollingProviderForNetwork = ( | ||
| export const setProviderForNetwork = ( | ||
| networkName: NetworkName, | ||
| ): PollingJsonRpcProvider => { | ||
| const provider = pollingProviderMap[networkName]; | ||
| if (!isDefined(provider)) { | ||
| throw new Error( | ||
| `Polling provider not yet loaded for network ${networkName}`, | ||
| ); | ||
| } | ||
| return provider; | ||
| }; | ||
|
|
||
| export const setFallbackProviderForNetwork = ( | ||
| networkName: NetworkName, | ||
| provider: FallbackProvider, | ||
| ): void => { | ||
| fallbackProviderMap[networkName] = provider; | ||
| }; | ||
|
|
||
| export const setPollingProviderForNetwork = ( | ||
| networkName: NetworkName, | ||
| provider: PollingJsonRpcProvider, | ||
| provider: Provider, | ||
| ): void => { | ||
| pollingProviderMap[networkName] = provider; | ||
| providerMap[networkName] = provider; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we plan to remove this once the pr is approved right ?