Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: eth_call should use the same baseFee as given block num (#1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Jan 5, 2022
1 parent f76e431 commit eba1077
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 63 deletions.
20 changes: 10 additions & 10 deletions src/chains/ethereum/ethereum/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/chains/ethereum/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"local-web-server": "4.2.1",
"mocha": "9.1.3",
"nyc": "15.1.0",
"solc": "0.7.4",
"solc": "0.8.11",
"superagent": "6.1.0",
"ts-node": "10.4.0",
"typedoc": "0.22.10",
Expand Down
9 changes: 7 additions & 2 deletions src/chains/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2551,11 +2551,16 @@ export default class EthereumApi implements Api {
// "effectiveGasPrice". however, if `maxPriorityFeePerGas` or
// `maxFeePerGas` values are set, the baseFeePerGas is used to calculate
// the effectiveGasPrice, which is used to calculate tx costs/refunds.
const baseFeePerGasBigInt = Block.calcNextBaseFee(parentBlock);
const baseFeePerGasBigInt = parentBlock.header.baseFeePerGas
? parentBlock.header.baseFeePerGas.toBigInt()
: undefined;

let gasPrice: Quantity;
const hasGasPrice = typeof transaction.gasPrice !== "undefined";
if (!common.isActivatedEIP(1559)) {
// if the original block didn't have a `baseFeePerGas` (baseFeePerGasBigInt
// is undefined) then EIP-1559 was not active on that block and we can't use
// type 2 fee values (as they rely on the baseFee)
if (!common.isActivatedEIP(1559) || baseFeePerGasBigInt === undefined) {
gasPrice = Quantity.from(hasGasPrice ? 0 : transaction.gasPrice);
} else {
const hasMaxFeePerGas = typeof transaction.maxFeePerGas !== "undefined";
Expand Down
4 changes: 2 additions & 2 deletions src/chains/ethereum/ethereum/tests/api/debug/debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe("api", () => {

assert.strictEqual(lastop.op, "STOP");
assert.strictEqual(lastop.gasCost, 0);
assert.strictEqual(lastop.pc, 202); // This will change if you edit Debug.sol
assert.strictEqual(lastop.pc, 166); // This will change if you edit Debug.sol

// This makes sure we get the initial value back (the first transaction to setValue())
// and not the value of the second setValue() transaction
Expand Down Expand Up @@ -176,7 +176,7 @@ describe("api", () => {
// in the final trace is found through execution. Again,
// this test is meant as a change detector, not necessarily a
// failure detector.
const expectedObjectsInFinalTrace = 30900;
const expectedObjectsInFinalTrace = 126539;
const timesToRunLoop = 100;
const from = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1";
const privateKey = Data.from(
Expand Down
11 changes: 11 additions & 0 deletions src/chains/ethereum/ethereum/tests/api/eth/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ describe("api", () => {
"didn't reject transaction with insufficient gas"
);
});

it("uses the correct baseFee", async () => {
const block = await provider.send("eth_getBlockByNumber", ["latest"]);
const tx = {
from,
to: contractAddress,
data: `0x${contract.contract.evm.methodIdentifiers["getBaseFee()"]}`
};
const result = await provider.send("eth_call", [tx, "latest"]);
assert.strictEqual(BigInt(result), BigInt(block.baseFeePerGas));
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract EthCall {
uint public value;

constructor() public payable {
constructor() payable {
value = 5;
}

function getBaseFee() public view virtual returns (uint256) {
return block.basefee;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

import "./NoOp.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract GetStorageAt {
uint256 public intValue1 = 123;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract Logs {
event Event(uint256 indexed first, uint256 indexed second);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

/**
* This contract does nothing and should do nothing.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract Reverts {
function invalidRevertReason() public pure {
Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum/ethereum/tests/api/evm/snapshot.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract snapshot {
uint256 public n = 42;
Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum/ethereum/tests/contracts/Debug.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract Debug {
uint public value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract DebugStorage {
uint public value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract HelloWorld {
uint256 public value;
Expand Down
27 changes: 27 additions & 0 deletions src/chains/ethereum/ethereum/tests/forking/contracts/Call.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract Call {
uint256 public value = 0;
function setValue (uint256 newValue) public returns (uint256 oldValue) {
oldValue = value;
value = newValue;
}

function setValueForAddress(address target, uint256 newValue) public returns (bytes memory oldValue) {
bytes memory data = abi.encodeWithSignature("setValue(uint256)", newValue);
(bool success, bytes memory _oldValue) = target.call(data);
require(success);
oldValue = _oldValue;
}

function setValueForAddressAddress(address target1, address target2, uint256 newValue) public returns (bytes memory oldValue) {
bytes memory data1 = abi.encodeWithSignature("setValueForAddress(address,uint256)", target2, newValue-1);
(bool success1, ) = address(this).call(data1);
require(success1);
bytes memory data = abi.encodeWithSignature("setValueForAddress(address,uint256)", target2, newValue);
(bool success, bytes memory _oldValue) = target1.call(data);
require(success);
oldValue = _oldValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma solidity ^0.8.11;

contract Forking {
// These top level `value*` variables are useful for testing state over time
Expand Down Expand Up @@ -62,7 +62,7 @@ contract Forking {
selfdestruct(payable(address(msg.sender)));
}

function getChainId() public pure returns (uint256 chainId) {
function getChainId() public view returns (uint256 chainId) {
assembly {
chainId := chainid()
}
Expand Down
48 changes: 14 additions & 34 deletions src/chains/ethereum/ethereum/tests/forking/forking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
startLocalChain,
updateRemotesAccountsBalances,
updateRemotesAccountNonces,
range
range,
encodeValue
} from "./helpers";
import compile from "../helpers/compile";
import path from "path";
Expand Down Expand Up @@ -69,7 +70,7 @@ describe("forking", function () {
wallet: { deterministic: true, totalAccounts: REMOTE_ACCOUNT_COUNT },
chain: { networkId: NETWORK_ID }
});
remoteProvider = (remoteServer.provider as unknown) as EthereumProvider;
remoteProvider = remoteServer.provider as unknown as EthereumProvider;
remoteAccounts = Object.keys(remoteProvider.getInitialAccounts());
await remoteServer.listen(PORT);
});
Expand All @@ -90,8 +91,7 @@ describe("forking", function () {
extraData: "0x0",
gasLimit: "0x0",
gasUsed: "0x0",
hash:
"0x925238ca364205c502b1771d80cd569e4200000b9aca6ded77fc8fe8f7b9e055",
hash: "0x925238ca364205c502b1771d80cd569e4200000b9aca6ded77fc8fe8f7b9e055",
logsBloom: "0x0",
miner: "0x0",
mixHash:
Expand Down Expand Up @@ -510,10 +510,6 @@ describe("forking", function () {
return provider.send("eth_sendTransaction", [tx]);
}

function encodeValue(val: number) {
return Quantity.from(val).toBuffer().toString("hex").padStart(64, "0");
}

function makeTxForSet(key: number, value: number) {
const encodedKey = encodeValue(key);
const encodedValue = encodeValue(value);
Expand Down Expand Up @@ -640,22 +636,16 @@ describe("forking", function () {
}

beforeEach("deploy contract", async () => {
({
contractAddress,
contractCode,
contractBlockNum,
methods
} = await deployContract(remoteProvider, remoteAccounts));
({ contractAddress, contractCode, contractBlockNum, methods } =
await deployContract(remoteProvider, remoteAccounts));
});

it("should fetch contract code from the remote chain via the local chain", async () => {
const { localProvider } = await startLocalChain(PORT, {
disableCache: true
});
const {
blockNumbersWithCode,
blockNumbersWithoutCode
} = await getBlockRanges(localProvider);
const { blockNumbersWithCode, blockNumbersWithoutCode } =
await getBlockRanges(localProvider);

await Promise.all(
blockNumbersWithCode.map(blockNumber =>
Expand Down Expand Up @@ -684,11 +674,8 @@ describe("forking", function () {
const { localProvider } = await startLocalChain(PORT, {
disableCache: true
});
const {
blockNum,
blockNumbersWithCode,
blockNumbersWithoutCode
} = await getBlockRanges(localProvider);
const { blockNum, blockNumbersWithCode, blockNumbersWithoutCode } =
await getBlockRanges(localProvider);

const _get = (value: string, blockNum: number) =>
get(localProvider, value, blockNum);
Expand Down Expand Up @@ -736,11 +723,8 @@ describe("forking", function () {
const { localProvider } = await startLocalChain(PORT, {
disableCache: true
});
const {
blockNum,
blockNumbersWithCode,
blockNumbersWithoutCode
} = await getBlockRanges(localProvider);
const { blockNum, blockNumbersWithCode, blockNumbersWithoutCode } =
await getBlockRanges(localProvider);

function _set(key: number, value: number) {
return set(localProvider, key, value);
Expand Down Expand Up @@ -1057,12 +1041,8 @@ describe("forking", () => {

before("deploy contract", async () => {
// deploy the contract
({
contractBlockNum,
contractAddress,
contractBlockNum,
methods
} = await deployContract(remoteProvider, remoteAccounts));
({ contractBlockNum, contractAddress, contractBlockNum, methods } =
await deployContract(remoteProvider, remoteAccounts));
});

before("fork from mainnet at contractBlockNum + 1", async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/chains/ethereum/ethereum/tests/forking/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Quantity, WEI } from "@ganache/utils";
import getProvider from "../helpers/getProvider";
import EthereumProvider from "../../src/provider";
import { EthereumProviderOptions } from "@ganache/ethereum-options";
Expand All @@ -8,6 +9,10 @@ export const logging = {
}
};

export const encodeValue = (val: number) => {
return Quantity.from(val).toBuffer().toString("hex").padStart(64, "0");
};

export const updateRemotesAccountsBalances = async (
remoteProvider: EthereumProvider,
remoteAccounts: string[]
Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum/ethereum/tests/helpers/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import solc, { CompilerInputSourceCode, CompilerInputSourceFile } from "solc";
// https://github.com/chriseth/browser-solidity/issues/167
const listeners = process.listeners("unhandledRejection");
const solcListener = listeners[listeners.length - 1];
if (solcListener && solcListener.name === "abort") {
if (solcListener && solcListener.name === "" && solcListener.length === 1) {
process.removeListener("unhandledRejection", solcListener);
} else {
throw new Error(
Expand Down

0 comments on commit eba1077

Please sign in to comment.