Skip to content

Commit

Permalink
Merge pull request #6201 from NomicFoundation/network-manager-tests
Browse files Browse the repository at this point in the history
Network manager tests
  • Loading branch information
schaable authored Jan 30, 2025
2 parents 66fcd56 + df927dd commit a033550
Show file tree
Hide file tree
Showing 11 changed files with 1,192 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import { EDR_NETWORK_REVERT_SNAPSHOT_EVENT } from "../../../constants.js";
import { DEFAULT_HD_ACCOUNTS_CONFIG_PARAMS } from "../accounts/constants.js";
import { BaseProvider } from "../base-provider.js";
import { getJsonRpcRequest, isFailedJsonRpcResponse } from "../json-rpc.js";
import { InvalidArgumentsError, ProviderError } from "../provider-errors.js";

import { getGlobalEdrContext } from "./edr-context.js";
import { InvalidArgumentsError, ProviderError } from "./errors.js";
import { createSolidityErrorWithStackTrace } from "./stack-traces/stack-trace-solidity-errors.js";
import {
isDebugTraceResult,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ import {
L1_CHAIN_TYPE as EDR_L1_CHAIN_TYPE,
GENERIC_CHAIN_TYPE as EDR_GENERIC_CHAIN_TYPE,
} from "@ignored/edr-optimism";
import {
bytesToHexString,
getUnprefixedHexString,
} from "@ignored/hardhat-vnext-utils/hex";
import { getUnprefixedHexString } from "@ignored/hardhat-vnext-utils/hex";

import { L1_CHAIN_TYPE, OPTIMISM_CHAIN_TYPE } from "../../../../constants.js";
import { FixedValueConfigurationVariable } from "../../../../core/configuration-variables.js";
Expand Down Expand Up @@ -177,6 +174,7 @@ export function hardhatMempoolOrderToEdrMineOrdering(
}
}

// TODO: EDR should handle this conversion. This is a temporary solution.
export function edrRpcDebugTraceToHardhat(
debugTraceResult: DebugTraceResult,
): RpcDebugTraceOutput {
Expand Down Expand Up @@ -222,10 +220,9 @@ export function edrRpcDebugTraceToHardhat(
structLogs.shift();
}

let returnValue =
debugTraceResult.output !== undefined
? bytesToHexString(debugTraceResult.output)
: "";
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
debugTraceResult.output is a string, but it's typed as Buffer in Edr */
let returnValue = (debugTraceResult.output as unknown as string) ?? "0x";
if (returnValue === "0x") {
returnValue = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class HttpProvider extends BaseProvider {
}

// eslint-disable-next-line no-restricted-syntax -- allow throwing ProviderError
throw new LimitExceededError(e);
throw new LimitExceededError(undefined, e);
}

throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ import { isObject } from "@ignored/hardhat-vnext-utils/lang";

const IS_PROVIDER_ERROR_PROPERTY_NAME = "_isProviderError";

/**
* Codes taken from: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md#error-codes
*/
// Codes taken from: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md#error-codes
//
// Code Message Meaning Category
//
// -32600 Invalid request JSON is not a valid request object standard
// -32601 Method not found Method does not exist standard
// -32602 Invalid params Invalid method parameters standard
// -32603 Internal error Internal JSON-RPC error standard
// -32700 Parse error Invalid JSON standard
//
// -32003 Transaction rejected Transaction creation failed non-standard
// -32004 Method not supported Method is not implemented non-standard
// -32005 Limit exceeded Request exceeds defined limit non-standard
//
// -32999 Invalid response The server returned a JSON-RPC hardhat-sepecific
// response, but the result is not
// in the expected format
export class ProviderError extends CustomError implements ProviderRpcError {
public code: number;
public data?: unknown;
Expand Down Expand Up @@ -41,7 +55,75 @@ export class ProviderError extends CustomError implements ProviderRpcError {
export class LimitExceededError extends ProviderError {
public static readonly CODE = -32005;

constructor(parent?: Error) {
super("Request exceeds defined limit", LimitExceededError.CODE, parent);
constructor(message: string = "Limit exceeded", parent?: Error) {
super(message, LimitExceededError.CODE, parent);
}
}

export class InvalidJsonInputError extends ProviderError {
public static readonly CODE = -32700;

constructor(message: string = "Parse error", parent?: Error) {
super(message, InvalidJsonInputError.CODE, parent);
}
}

export class InvalidRequestError extends ProviderError {
public static readonly CODE = -32600;

constructor(message: string = "Invalid request", parent?: Error) {
super(message, InvalidRequestError.CODE, parent);
}
}

// TODO: not used, should we remove it?
export class MethodNotFoundError extends ProviderError {
public static readonly CODE = -32601;

constructor(message: string = "Method not found", parent?: Error) {
super(message, MethodNotFoundError.CODE, parent);
}
}

export class InvalidArgumentsError extends ProviderError {
public static readonly CODE = -32602;

constructor(message: string = "Invalid params", parent?: Error) {
super(message, InvalidArgumentsError.CODE, parent);
}
}

export class InternalError extends ProviderError {
public static readonly CODE = -32603;

constructor(message: string = "Internal error", parent?: Error) {
super(message, InternalError.CODE, parent);
}
}

// TODO: not used, should we remove it?
export class TransactionExecutionError extends ProviderError {
public static readonly CODE = -32003;

constructor(message: string = "Transaction rejected", parent?: Error) {
super(message, TransactionExecutionError.CODE, parent);
}
}

// TODO: not used, should we remove it?
export class MethodNotSupportedError extends ProviderError {
public static readonly CODE = -32004;

constructor(message: string = "Method not supported", parent?: Error) {
super(message, MethodNotSupportedError.CODE, parent);
}
}

// TODO: not used, should we remove it?
export class InvalidResponseError extends ProviderError {
public static readonly CODE = -32999;

constructor(message: string = "Invalid response", parent?: Error) {
super(message, InvalidResponseError.CODE, parent);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type {
EdrNetworkForkingConfig,
EdrNetworkHDAccountsConfig,
EdrNetworkMiningConfig,
HardhatUserConfig,
HttpNetworkHDAccountsConfig,
HttpNetworkHDAccountsUserConfig,
Expand Down Expand Up @@ -306,6 +309,8 @@ export async function validateNetworkConfigOverride(
);
}

// This type guard does not check beyond accounts being an object
// because the actual structure of the object is validated by Zod.
export function isHttpNetworkHdAccountsUserConfig(
accounts: unknown,
): accounts is HttpNetworkHDAccountsUserConfig {
Expand All @@ -315,5 +320,48 @@ export function isHttpNetworkHdAccountsUserConfig(
export function isHttpNetworkHdAccountsConfig(
accounts: unknown,
): accounts is HttpNetworkHDAccountsConfig {
return isObject(accounts);
return (
isObject(accounts) &&
"mnemonic" in accounts &&
"count" in accounts &&
"initialIndex" in accounts &&
"passphrase" in accounts &&
"path" in accounts
);
}

export function isEdrNetworkHdAccountsConfig(
accounts: unknown,
): accounts is EdrNetworkHDAccountsConfig {
return (
isObject(accounts) &&
"mnemonic" in accounts &&
"accountsBalance" in accounts &&
"count" in accounts &&
"initialIndex" in accounts &&
"passphrase" in accounts &&
"path" in accounts
);
}

export function isEdrNetworkForkingConfig(
forking: unknown,
): forking is EdrNetworkForkingConfig {
return (
isObject(forking) &&
"enabled" in forking &&
"url" in forking &&
"cacheDir" in forking
);
}

export function isEdrNetworkMiningConfig(
mining: unknown,
): mining is EdrNetworkMiningConfig {
return (
isObject(mining) &&
"auto" in mining &&
"interval" in mining &&
"mempool" in mining
);
}
Loading

0 comments on commit a033550

Please sign in to comment.