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

Rework providers #5994

Merged
merged 19 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3ccca1a
remove reworked-files
ChristopherDedominici Nov 27, 2024
05ce4ca
new handlers logic + chain-id handler
ChristopherDedominici Nov 27, 2024
e27875f
rename chain-id files
ChristopherDedominici Nov 27, 2024
ffb4230
add automatic and fixed gas handlers
ChristopherDedominici Nov 27, 2024
31720c3
add automatic and fixed gas price handlers
ChristopherDedominici Nov 27, 2024
ef072a8
add automatic and fixed sender
ChristopherDedominici Nov 27, 2024
f8089cd
add LocalAccountsHandler and HDAccountsHandler
ChristopherDedominici Nov 28, 2024
7d5f6eb
add logic to create handler-array
ChristopherDedominici Nov 28, 2024
ccdff2a
revert change in local-account-handler
ChristopherDedominici Nov 28, 2024
64634a3
modify chain-id handlers
ChristopherDedominici Nov 28, 2024
ce212d4
update docs for the function createHandlersArray
ChristopherDedominici Nov 28, 2024
df725ab
improve if condition in createHandlersArray
ChristopherDedominici Nov 28, 2024
647044d
remove "assert.rejects"
ChristopherDedominici Nov 28, 2024
2fa55f6
removed "if http condition" for the AutomaticGasPriceHandler
ChristopherDedominici Dec 6, 2024
71d8935
remove unused "maxFeePerGas" in e2e test
ChristopherDedominici Dec 6, 2024
406593b
Update v-next/hardhat/src/internal/builtin-plugins/network-manager/ho…
ChristopherDedominici Dec 6, 2024
345ad12
Merge branch 'v-next' of github.com:NomicFoundation/hardhat into rewo…
ChristopherDedominici Dec 6, 2024
f74aae2
Merge branch 'rework-providers' of github.com:NomicFoundation/hardhat…
ChristopherDedominici Dec 6, 2024
64ed264
fix WeakMap errors
ChristopherDedominici Dec 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
JsonRpcRequest,
JsonRpcResponse,
} from "../../../../types/providers.js";
import type { RequestHandler } from "../request-handlers/types.js";
import type {
HookContext,
NetworkHooks,
Expand All @@ -11,14 +12,21 @@ import type {
NetworkConnection,
} from "@ignored/hardhat-vnext/types/network";

import { JsonRpcRequestModifier } from "../json-rpc-request-modifiers/json-rpc-request-modifier.js";
import { deepClone } from "@ignored/hardhat-vnext-utils/lang";

import { isJsonRpcResponse } from "../json-rpc.js";
import { createHandlersArray } from "../request-handlers/hanlders-array.js";

export default async (): Promise<Partial<NetworkHooks>> => {
// This map is necessary because Hardhat V3 supports multiple network connections, requiring us to track them
// to apply the appropriate modifiers to each request.
// When a connection is closed, it is removed from the map. Refer to "closeConnection" at the end of the file.
const jsonRpcRequestModifiers: Map<number, JsonRpcRequestModifier> =
new Map();
// This map is essential for managing multiple network connections in Hardhat V3.
// Since Hardhat V3 supports multiple connections, we use this map to track each one
// and associate it with the corresponding handlers array.
// When a connection is closed, its associated handlers array is removed from the map.
// See the "closeConnection" function at the end of the file for more details.
const requestHandlersPerConnection: WeakMap<
NetworkConnection<ChainType | string>,
RequestHandler[]
> = new Map();

const handlers: Partial<NetworkHooks> = {
async onRequest<ChainTypeT extends ChainType | string>(
Expand All @@ -31,31 +39,28 @@ export default async (): Promise<Partial<NetworkHooks>> => {
nextJsonRpcRequest: JsonRpcRequest,
) => Promise<JsonRpcResponse>,
) {
let jsonRpcRequestModifier = jsonRpcRequestModifiers.get(
networkConnection.id,
);

if (jsonRpcRequestModifier === undefined) {
jsonRpcRequestModifier = new JsonRpcRequestModifier(networkConnection);
let requestHandlers = requestHandlersPerConnection.get(networkConnection);

jsonRpcRequestModifiers.set(
networkConnection.id,
jsonRpcRequestModifier,
);
if (requestHandlers === undefined) {
requestHandlers = await createHandlersArray(networkConnection);
requestHandlersPerConnection.set(networkConnection, requestHandlers);
}

const newJsonRpcRequest =
await jsonRpcRequestModifier.createModifiedJsonRpcRequest(
jsonRpcRequest,
);
// We clone the request to avoid interfering with other hook handlers that
// might be using the original request.
let request = await deepClone(jsonRpcRequest);

for (const handler of requestHandlers) {
const newRequestOrResponse = await handler.handle(request);

const res = await jsonRpcRequestModifier.getResponse(jsonRpcRequest);
if (isJsonRpcResponse(newRequestOrResponse)) {
return newRequestOrResponse;
}

if (res !== null) {
return res;
request = newRequestOrResponse;
}

return next(context, networkConnection, newJsonRpcRequest);
return next(context, networkConnection, request);
},

async closeConnection<ChainTypeT extends ChainType | string>(
Expand All @@ -66,8 +71,8 @@ export default async (): Promise<Partial<NetworkHooks>> => {
nextNetworkConnection: NetworkConnection<ChainTypeT>,
) => Promise<void>,
): Promise<void> {
if (jsonRpcRequestModifiers.has(networkConnection.id) === true) {
jsonRpcRequestModifiers.delete(networkConnection.id);
if (requestHandlersPerConnection.has(networkConnection) === true) {
requestHandlersPerConnection.delete(networkConnection);
}

return next(context, networkConnection);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading