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

Create RPC middleware using RPC services #5290

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 1 addition & 14 deletions eslint-warning-thresholds.json
Original file line number Diff line number Diff line change
Expand Up @@ -417,33 +417,20 @@
"jsdoc/tag-lines": 1,
"prettier/prettier": 1
},
"packages/network-controller/src/create-auto-managed-network-client.test.ts": {
"import-x/order": 1
},
"packages/network-controller/src/create-network-client.ts": {
"@typescript-eslint/no-unsafe-enum-comparison": 1
},
"packages/network-controller/tests/NetworkController.test.ts": {
"@typescript-eslint/no-unused-vars": 1,
"@typescript-eslint/prefer-promise-reject-errors": 1,
"import-x/order": 1,
"jest/no-conditional-in-test": 4
"import-x/order": 1
},
"packages/network-controller/tests/create-network-client.test.ts": {
"import-x/order": 1
},
"packages/network-controller/tests/provider-api-tests/block-param.ts": {
"jest/no-conditional-in-test": 1
},
"packages/network-controller/tests/provider-api-tests/helpers.ts": {
"@typescript-eslint/prefer-promise-reject-errors": 1,
"import-x/namespace": 1,
"import-x/no-named-as-default-member": 1,
"promise/catch-or-return": 1
},
"packages/network-controller/tests/provider-api-tests/no-block-param.ts": {
"jest/no-conditional-in-test": 2
},
"packages/permission-controller/src/Permission.ts": {
"prettier/prettier": 11
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ async function setupAssetContractControllers({
allowedActions: [],
allowedEvents: [],
}),
fetch,
btoa,
});
if (useNetworkControllerProvider) {
await networkController.initializeProvider();
Expand Down
2 changes: 2 additions & 0 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const setupNetworkController = async ({
messenger: restrictedMessenger,
state,
infuraProjectId: '123',
fetch,
btoa,
});

if (initializeProvider) {
Expand Down
32 changes: 31 additions & 1 deletion packages/network-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Implement circuit breaker pattern when retrying requests to Infura and custom RPC endpoints ([#5290](https://github.com/MetaMask/core/pull/5290))
- If the network is perceived to be down after 5 attempts, further retries will be paused for 30 seconds
- "Down" means the following:
- A failure to reach the network (exact error depending on platform / HTTP client)
- The request responds with a non-JSON-parseable or non-JSON-RPC-compatible body
- The request returns a non-200 response
- Use exponential backoff / jitter when retrying requests to Infura and custom RPC endpoints ([#5290](https://github.com/MetaMask/core/pull/5290))
- As requests are retried, the delay between retries will increase exponentially (using random variance to prevent bursts)

### Changed

- **BREAKING:** `NetworkController` constructor now takes two required options, `fetch` and `btoa` ([#5290](https://github.com/MetaMask/core/pull/5290))
- These are passed along to functions that create the JSON-RPC middleware
- Synchronize retry logic and error handling behavior between Infura and custom RPC endpoints ([#5290](https://github.com/MetaMask/core/pull/5290))
- A request to a custom endpoint that returns a 418 response will no longer return a JSON-RPC response with the error "Request is being rate limited"
- A request to a custom endpoint that returns a 429 response now returns a JSON-RPC response with the error "Request is being rate limited"
- A request to a custom endpoint that throws an "ECONNRESET" error will now be retried up to 5 times
- A request to a Infura endpoint that fails more than 5 times in a row will now respond with a JSON-RPC error that encompasses the failure instead of hiding it as "InfuraProvider - cannot complete request. All retries exhausted"
- A request to a Infura endpoint that returns a non-retriable, non-2xx response will now respond with a JSON-RPC error that has the underling message "Non-200 status code: '\<code\>'" rather than including the raw response from the endpoint
- A request to a custom endpoint that fails with a retriable error more than 5 times in a row will now respond with a JSON-RPC error that encompasses the failure instead of returning an empty response
- A "retriable error" is now regarded as the following:
- A failure to reach the network (exact error depending on platform / HTTP client)
- The request responds with a non-JSON-parseable or non-JSON-RPC-compatible body
- The request returns a 503 or 504 response
- Bump dependencies to support usage of RPC services internally for network requests ([#5290](https://github.com/MetaMask/core/pull/5290))
- Bump `@metamask/eth-json-rpc-infura` to `^10.1.0`
- Bump `@metamask/eth-json-rpc-middleware` to `^15.1.0`

## [22.2.1]

### Changed

- Bump `@metamask/base-controller` from `^7.1.1` to `^8.0.0` [#5305](https://github.com/MetaMask/core/pull/5305))
- Bump `@metamask/base-controller` from `^7.1.1` to `^8.0.0` ([#5305](https://github.com/MetaMask/core/pull/5305))

## [22.2.0]

Expand Down
4 changes: 2 additions & 2 deletions packages/network-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"@metamask/base-controller": "^8.0.0",
"@metamask/controller-utils": "^11.5.0",
"@metamask/eth-block-tracker": "^11.0.3",
"@metamask/eth-json-rpc-infura": "^10.0.0",
"@metamask/eth-json-rpc-middleware": "^15.0.1",
"@metamask/eth-json-rpc-infura": "^10.1.0",
"@metamask/eth-json-rpc-middleware": "^15.1.0",
"@metamask/eth-json-rpc-provider": "^4.1.8",
"@metamask/eth-query": "^4.0.0",
"@metamask/json-rpc-engine": "^10.0.3",
Expand Down
69 changes: 51 additions & 18 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,15 @@ export type NetworkControllerOptions = {
infuraProjectId: string;
state?: Partial<NetworkState>;
log?: Logger;
/**
* A function that can be used to make an HTTP request, compatible with the
* Fetch API.
*/
fetch: typeof fetch;
/**
* A function that can be used to convert a binary string into base-64.
*/
btoa: typeof btoa;
};

/**
Expand Down Expand Up @@ -909,6 +918,10 @@ export class NetworkController extends BaseController<

#log: Logger | undefined;

readonly #fetch: typeof fetch;

readonly #btoa: typeof btoa;

#networkConfigurationsByNetworkClientId: Map<
NetworkClientId,
NetworkConfiguration
Expand All @@ -919,6 +932,8 @@ export class NetworkController extends BaseController<
state,
infuraProjectId,
log,
fetch: givenFetch,
btoa: givenBtoa,
}: NetworkControllerOptions) {
const initialState = { ...getDefaultNetworkControllerState(), ...state };
validateNetworkControllerState(initialState);
Expand Down Expand Up @@ -948,6 +963,8 @@ export class NetworkController extends BaseController<

this.#infuraProjectId = infuraProjectId;
this.#log = log;
this.#fetch = givenFetch;
this.#btoa = givenBtoa;

this.#previouslySelectedNetworkClientId =
this.state.selectedNetworkClientId;
Expand Down Expand Up @@ -2425,20 +2442,28 @@ export class NetworkController extends BaseController<
autoManagedNetworkClientRegistry[NetworkClientType.Infura][
addedRpcEndpoint.networkClientId
] = createAutoManagedNetworkClient({
type: NetworkClientType.Infura,
chainId: networkFields.chainId,
network: addedRpcEndpoint.networkClientId,
infuraProjectId: this.#infuraProjectId,
ticker: networkFields.nativeCurrency,
networkClientConfiguration: {
type: NetworkClientType.Infura,
chainId: networkFields.chainId,
network: addedRpcEndpoint.networkClientId,
infuraProjectId: this.#infuraProjectId,
ticker: networkFields.nativeCurrency,
},
fetch: this.#fetch,
btoa: this.#btoa,
});
} else {
autoManagedNetworkClientRegistry[NetworkClientType.Custom][
addedRpcEndpoint.networkClientId
] = createAutoManagedNetworkClient({
type: NetworkClientType.Custom,
chainId: networkFields.chainId,
rpcUrl: addedRpcEndpoint.url,
ticker: networkFields.nativeCurrency,
networkClientConfiguration: {
type: NetworkClientType.Custom,
chainId: networkFields.chainId,
rpcUrl: addedRpcEndpoint.url,
ticker: networkFields.nativeCurrency,
},
fetch: this.#fetch,
btoa: this.#btoa,
});
}
}
Expand Down Expand Up @@ -2589,21 +2614,29 @@ export class NetworkController extends BaseController<
return [
rpcEndpoint.networkClientId,
createAutoManagedNetworkClient({
type: NetworkClientType.Infura,
network: infuraNetworkName,
infuraProjectId: this.#infuraProjectId,
chainId: networkConfiguration.chainId,
ticker: networkConfiguration.nativeCurrency,
networkClientConfiguration: {
type: NetworkClientType.Infura,
network: infuraNetworkName,
infuraProjectId: this.#infuraProjectId,
chainId: networkConfiguration.chainId,
ticker: networkConfiguration.nativeCurrency,
},
fetch: this.#fetch,
btoa: this.#btoa,
}),
] as const;
}
return [
rpcEndpoint.networkClientId,
createAutoManagedNetworkClient({
type: NetworkClientType.Custom,
chainId: networkConfiguration.chainId,
rpcUrl: rpcEndpoint.url,
ticker: networkConfiguration.nativeCurrency,
networkClientConfiguration: {
type: NetworkClientType.Custom,
chainId: networkConfiguration.chainId,
rpcUrl: rpcEndpoint.url,
ticker: networkConfiguration.nativeCurrency,
},
fetch: this.#fetch,
btoa: this.#btoa,
}),
] as const;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BUILT_IN_NETWORKS, NetworkType } from '@metamask/controller-utils';

import { mockNetwork } from '../../../tests/mock-network';
import { createAutoManagedNetworkClient } from './create-auto-managed-network-client';
import * as createNetworkClientModule from './create-network-client';
import type {
CustomNetworkClientConfiguration,
InfuraNetworkClientConfiguration,
} from './types';
import { NetworkClientType } from './types';
import { mockNetwork } from '../../../tests/mock-network';

describe('createAutoManagedNetworkClient', () => {
const networkClientConfigurations: [
Expand All @@ -31,24 +31,32 @@ describe('createAutoManagedNetworkClient', () => {
for (const networkClientConfiguration of networkClientConfigurations) {
describe(`given configuration for a ${networkClientConfiguration.type} network client`, () => {
it('allows the network client configuration to be accessed', () => {
const { configuration } = createAutoManagedNetworkClient(
const { configuration } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});

expect(configuration).toStrictEqual(networkClientConfiguration);
});

it('does not make any network requests initially', () => {
// If unexpected requests occurred, then Nock would throw
expect(() => {
createAutoManagedNetworkClient(networkClientConfiguration);
createAutoManagedNetworkClient({
networkClientConfiguration,
fetch,
btoa,
});
}).not.toThrow();
});

it('returns a provider proxy that has the same interface as a provider', () => {
const { provider } = createAutoManagedNetworkClient(
const { provider } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});

// This also tests the `has` trap in the proxy
expect('addListener' in provider).toBe(true);
Expand Down Expand Up @@ -87,9 +95,11 @@ describe('createAutoManagedNetworkClient', () => {
],
});

const { provider } = createAutoManagedNetworkClient(
const { provider } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});

const result = await provider.request({
id: 1,
Expand Down Expand Up @@ -121,9 +131,11 @@ describe('createAutoManagedNetworkClient', () => {
'createNetworkClient',
);

const { provider } = createAutoManagedNetworkClient(
const { provider } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});

await provider.request({
id: 1,
Expand All @@ -138,15 +150,19 @@ describe('createAutoManagedNetworkClient', () => {
params: [],
});
expect(createNetworkClientMock).toHaveBeenCalledTimes(1);
expect(createNetworkClientMock).toHaveBeenCalledWith(
networkClientConfiguration,
);
expect(createNetworkClientMock).toHaveBeenCalledWith({
configuration: networkClientConfiguration,
fetch,
btoa,
});
});

it('returns a block tracker proxy that has the same interface as a block tracker', () => {
const { blockTracker } = createAutoManagedNetworkClient(
const { blockTracker } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});

// This also tests the `has` trap in the proxy
expect('addListener' in blockTracker).toBe(true);
Expand Down Expand Up @@ -196,9 +212,11 @@ describe('createAutoManagedNetworkClient', () => {
],
});

const { blockTracker } = createAutoManagedNetworkClient(
const { blockTracker } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});

const blockNumberViaLatest = await new Promise((resolve) => {
blockTracker.once('latest', resolve);
Expand Down Expand Up @@ -251,9 +269,11 @@ describe('createAutoManagedNetworkClient', () => {
'createNetworkClient',
);

const { blockTracker } = createAutoManagedNetworkClient(
const { blockTracker } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});

await new Promise((resolve) => {
blockTracker.once('latest', resolve);
Expand All @@ -264,9 +284,11 @@ describe('createAutoManagedNetworkClient', () => {
await blockTracker.getLatestBlock();
await blockTracker.checkForLatestBlock();
expect(createNetworkClientMock).toHaveBeenCalledTimes(1);
expect(createNetworkClientMock).toHaveBeenCalledWith(
networkClientConfiguration,
);
expect(createNetworkClientMock).toHaveBeenCalledWith({
configuration: networkClientConfiguration,
fetch,
btoa,
});
});

it('allows the block tracker to be destroyed', () => {
Expand All @@ -284,9 +306,11 @@ describe('createAutoManagedNetworkClient', () => {
},
],
});
const { blockTracker, destroy } = createAutoManagedNetworkClient(
const { blockTracker, destroy } = createAutoManagedNetworkClient({
networkClientConfiguration,
);
fetch,
btoa,
});
// Start the block tracker
blockTracker.on('latest', () => {
// do nothing
Expand Down
Loading
Loading