Skip to content

Commit

Permalink
Merge pull request #2771 from JuanuMusic/remove-ganachegasmultiplierp…
Browse files Browse the repository at this point in the history
…rovider

Removed all GanacheGasMultiplierProvider refs
  • Loading branch information
alcuadrado committed Jun 15, 2022
2 parents 5e5af74 + 8c03393 commit ac0709d
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 139 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-balloons-bake.md
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Removed a workaround to past Ganache's gas estimation problems.
Expand Up @@ -171,11 +171,6 @@ export function applyProviderWrappers(
typeof import("./gas-providers"),
"FixedGasPriceProvider"
>("./gas-providers", "FixedGasPriceProvider");
const GanacheGasMultiplierProvider = importProvider<
typeof import("./gas-providers"),
"GanacheGasMultiplierProvider"
>("./gas-providers", "GanacheGasMultiplierProvider");

const ChainIdValidatorProvider = importProvider<
typeof import("./chainId"),
"ChainIdValidatorProvider"
Expand All @@ -198,10 +193,6 @@ export function applyProviderWrappers(
}

// TODO: Add some extension mechanism for account plugins here

if (typeof netConfig.gas !== "number") {
provider = new GanacheGasMultiplierProvider(provider);
}
}

if (netConfig.from !== undefined) {
Expand Down
39 changes: 0 additions & 39 deletions packages/hardhat-core/src/internal/core/providers/gas-providers.ts
Expand Up @@ -9,7 +9,6 @@ import {
import { ProviderWrapper } from "./wrapper";

const DEFAULT_GAS_MULTIPLIER = 1;
export const GANACHE_GAS_MULTIPLIER = 5;

export class FixedGasProvider extends ProviderWrapper {
constructor(provider: EIP1193Provider, private readonly _gasLimit: number) {
Expand Down Expand Up @@ -286,41 +285,3 @@ export class AutomaticGasPriceProvider extends ProviderWrapper {
}
}
}

/**
* This provider multiplies whatever gas estimation Ganache gives by [[GANACHE_GAS_MULTIPLIER]]
*
* NOTE: This bug was present at least in Ganache 6.4.x.
* One way to test if the bug is still present is to check if the estimation to
* run a deployment transaction with this data is high enough:
* * 0x608060405234801561001057600080fd5b5060405161043e38038061043e8339810180604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185600182028301116401000000008211171561007e57600080fd5b50509291905050506040516100929061010b565b604051809103906000f0801580156100ae573d6000803e3d6000fd5b506000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610104929190610117565b50506101bc565b6088806103b683390190565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061015857805160ff1916838001178555610186565b82800160010185558215610186579182015b8281111561018557825182559160200191906001019061016a565b5b5090506101939190610197565b5090565b6101b991905b808211156101b557600081600090555060010161019d565b5090565b90565b6101eb806101cb6000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063f86cc00914610030575b600080fd5b61003861003a565b005b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166319ff1d216040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156100a357600080fd5b505af11580156100b7573d6000803e3d6000fd5b505050506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166319ff1d216040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561012457600080fd5b505af1158015610138573d6000803e3d6000fd5b505050506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166319ff1d216040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156101a557600080fd5b505af11580156101b9573d6000803e3d6000fd5b5050505056fea165627a7a723058203691efa02f6279a7b7eea9265988d2deaf417c2590c3103779c96b68e78463b700296080604052348015600f57600080fd5b50606b80601d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806319ff1d2114602d575b600080fd5b60336035565b005b600560008190555056fea165627a7a72305820a00cf00e60c019ed83e0857faef9e9383880a5addd91429d30203771c82a4014002900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000
*/
export class GanacheGasMultiplierProvider extends MultipliedGasEstimationProvider {
private _cachedIsGanache: boolean | undefined;

constructor(provider: EIP1193Provider) {
super(provider, GANACHE_GAS_MULTIPLIER);
}

public async request(args: RequestArguments): Promise<unknown> {
const isGanache = await this._isGanache();
if (args.method === "eth_estimateGas" && isGanache) {
const params = this._getParams(args);
return this._getMultipliedGasEstimation(params);
}

return this._wrappedProvider.request(args);
}

private async _isGanache(): Promise<boolean> {
if (this._cachedIsGanache === undefined) {
const clientVersion = (await this._wrappedProvider.request({
method: "web3_clientVersion",
})) as string;

this._cachedIsGanache = clientVersion.includes("TestRPC");
}

return this._cachedIsGanache;
}
}
Expand Up @@ -5,17 +5,13 @@ import {
defaultHttpNetworkParams,
} from "../../../../src/internal/core/config/default-config";
import { ERRORS } from "../../../../src/internal/core/errors-list";
import {
numberToRpcQuantity,
rpcQuantityToNumber,
} from "../../../../src/internal/core/jsonrpc/types/base-types";
import { numberToRpcQuantity } from "../../../../src/internal/core/jsonrpc/types/base-types";
import { BackwardsCompatibilityProviderAdapter } from "../../../../src/internal/core/providers/backwards-compatibility";
import {
applyProviderWrappers,
createProvider,
isHDAccountsConfig,
} from "../../../../src/internal/core/providers/construction";
import { GANACHE_GAS_MULTIPLIER } from "../../../../src/internal/core/providers/gas-providers";
import { expectHardhatErrorAsync } from "../../../helpers/errors";

import { MockedProvider } from "./mocks";
Expand Down Expand Up @@ -267,31 +263,4 @@ describe("Base providers wrapping", () => {
);
});
});

describe("Ganache multiplier provider", () => {
it("Should wrap with a ganache multiplier provider", async () => {
mockedProvider.setReturnValue(
"eth_estimateGas",
numberToRpcQuantity(123)
);
mockedProvider.setReturnValue(
"web3_clientVersion",
"EthereumJS TestRPC/v2.5.5/ethereum-js"
);

const provider = applyProviderWrappers(mockedProvider, {
url: "",
});

const estimation = (await provider.request({
method: "eth_estimateGas",
params: [
{ to: "0xa2b6816c50d49101901d93f5302a3a57e0a1281b", value: 1 },
],
})) as string;

const gas = rpcQuantityToNumber(estimation);
assert.equal(gas, Math.floor(123 * GANACHE_GAS_MULTIPLIER));
});
});
});
Expand Up @@ -10,8 +10,6 @@ import {
AutomaticGasProvider,
FixedGasPriceProvider,
FixedGasProvider,
GANACHE_GAS_MULTIPLIER,
GanacheGasMultiplierProvider,
} from "../../../../src/internal/core/providers/gas-providers";
import { EIP1193Provider } from "../../../../src/types";

Expand Down Expand Up @@ -493,60 +491,3 @@ describe("FixedGasPriceProvider", () => {
assert.deepEqual(params, input);
});
});

describe("GanacheGasMultiplierProvider", () => {
it("Should multiply the gas if connected to Ganache", async () => {
const mockedProvider = new MockedProvider();
mockedProvider.setReturnValue("eth_estimateGas", numberToRpcQuantity(123));
mockedProvider.setReturnValue(
"web3_clientVersion",
"EthereumJS TestRPC/v2.5.5/ethereum-js"
);
mockedProvider.setReturnValue("eth_getBlockByNumber", {
gasLimit: numberToRpcQuantity(12300000),
});

const wrapped = new GanacheGasMultiplierProvider(mockedProvider);

const estimation = (await wrapped.request({
method: "eth_estimateGas",
params: [
{
from: "0x0000000000000000000000000000000000000011",
to: "0x0000000000000000000000000000000000000011",
value: 1,
},
],
})) as string;

const gas = rpcQuantityToNumber(estimation);
assert.equal(gas, Math.floor(123 * GANACHE_GAS_MULTIPLIER));
});

it("Should not multiply the gas if connected to other node", async () => {
const mockedProvider = new MockedProvider();
mockedProvider.setReturnValue("eth_estimateGas", numberToRpcQuantity(123));
mockedProvider.setReturnValue(
"web3_clientVersion",
"Parity-Ethereum//v2.5.1-beta-e0141f8-20190510/x86_64-linux-gnu/rustc1.34.1"
);
mockedProvider.setReturnValue("eth_getBlockByNumber", {
gasLimit: numberToRpcQuantity(12300000),
});
const wrapped = new GanacheGasMultiplierProvider(mockedProvider);

const estimation = (await wrapped.request({
method: "eth_estimateGas",
params: [
{
from: "0x0000000000000000000000000000000000000011",
to: "0x0000000000000000000000000000000000000011",
value: 1,
},
],
})) as string;

const gas = rpcQuantityToNumber(estimation);
assert.equal(gas, 123);
});
});

1 comment on commit ac0709d

@vercel
Copy link

@vercel vercel bot commented on ac0709d Jun 15, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.