From c7ad27ec06384934a3e8089dfcc765c3f158b64c Mon Sep 17 00:00:00 2001 From: David Murdoch Date: Fri, 11 Sep 2020 12:51:14 -0400 Subject: [PATCH 1/3] add test for customized `eth_chainId` RPC value via `_chainIdRpc` option --- test/contracts/chainId/ChainId.sol | 11 +++++++ test/local/chainId.js | 52 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 test/contracts/chainId/ChainId.sol create mode 100644 test/local/chainId.js diff --git a/test/contracts/chainId/ChainId.sol b/test/contracts/chainId/ChainId.sol new file mode 100644 index 0000000000..195b87c3c6 --- /dev/null +++ b/test/contracts/chainId/ChainId.sol @@ -0,0 +1,11 @@ +pragma solidity ^0.6.0; + +contract ChainId { + function getChainId() pure external returns (uint256) { + uint256 id; + assembly { + id := chainid() + } + return id; + } +} diff --git a/test/local/chainId.js b/test/local/chainId.js new file mode 100644 index 0000000000..e49f43507b --- /dev/null +++ b/test/local/chainId.js @@ -0,0 +1,52 @@ +const assert = require("assert"); +const initializeTestProvider = require("../helpers/web3/initializeTestProvider"); +const { compile } = require("../helpers/contract/compileAndDeploy"); + +describe.only("Chain Id option", function() { + const contract = {}; + + before("compile contract", async function() { + this.timeout(10000); + const contractSubdirectory = "chainId"; + const contractFilename = "ChainId"; + const subcontractFiles = []; + const { abi, bytecode } = await compile(contractFilename, subcontractFiles, contractSubdirectory, "istanbul"); + contract.abi = abi; + contract.bytecode = bytecode; + }); + + describe("Allow Unlimited Contract Size", function() { + let context; + + before("Setup provider to allow unlimited contract size", async function() { + const ganacheOptions = { + _chainId: 1, + _chainIdRpc: 1 + }; + + context = await initializeTestProvider(ganacheOptions); + }); + + before("deploy contract", async function() { + const chainIdContract = new context.web3.eth.Contract(contract.abi); + contract.deployed = await chainIdContract + .deploy({ + data: contract.bytecode + }) + .send({ + from: context.accounts[0], + gas: 3141592 + }); + }); + + it("chainid opcode should match options", async function() { + const chainId = await contract.deployed.methods.getChainId().call(); + assert.strictEqual(chainId, "1"); + }); + + it("chain id rpc should match options", async function() { + const chainId = await context.web3.eth.getChainId(); + assert.strictEqual(chainId, 1); + }); + }); +}); From afb14b17757e349d8c20503db1c558e5c37e9905 Mon Sep 17 00:00:00 2001 From: David Murdoch Date: Fri, 11 Sep 2020 12:51:51 -0400 Subject: [PATCH 2/3] add temp option to customize `eth_chainId` RPC value --- lib/provider.js | 1 + lib/subproviders/geth_api_double.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/provider.js b/lib/provider.js index 66423e18e3..05882b9302 100644 --- a/lib/provider.js +++ b/lib/provider.js @@ -54,6 +54,7 @@ function Provider(options) { const defaultOptions = { _chainId: 1, + _chainIdRpc: 1337, vmErrorsOnRPCResponse: true, verbose: false, asyncRequestProcessing: false, diff --git a/lib/subproviders/geth_api_double.js b/lib/subproviders/geth_api_double.js index 1e4dfb3f3e..936cd8ba36 100644 --- a/lib/subproviders/geth_api_double.js +++ b/lib/subproviders/geth_api_double.js @@ -149,7 +149,7 @@ GethApiDouble.prototype.eth_blockNumber = function(callback) { }; GethApiDouble.prototype.eth_chainId = function(callback) { - callback(null, to.hex(1337)); + callback(null, to.hex(this.options._chainIdRpc)); }; GethApiDouble.prototype.eth_coinbase = function(callback) { From e9c100cb723b496fb49abd47917a9368a1f65d00 Mon Sep 17 00:00:00 2001 From: David Murdoch Date: Fri, 11 Sep 2020 12:56:29 -0400 Subject: [PATCH 3/3] add `_chainId` and `_chainIdRpc` options to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 18455bc5fd..44c0fadaac 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ Both `.provider()` and `.server()` take a single object which allows you to spec * `"fork_block_number"`: `string` or `number` - Block number the provider should fork from, when the `fork` option is specified. If the `fork` option is specified as a string including the `@` sign and a block number, the block number in the `fork` parameter takes precedence. - `"forkCacheSize"`: `number` - The maximum size, in bytes, of the in-memory cache for queries on a chain fork. Defaults to `1_073_741_824` bytes (1 gigabyte). You can set this to `0` to disable caching (not recommended), or to `-1` for unlimited (will be limited by your node/browser process). * `"network_id"`: Specify the network id ganache-core will use to identify itself (defaults to the current time or the network id of the forked blockchain if configured) +* `"_chainId"`: **(temporary option until v3)** Specify the chain's chainId. For legacy reasons, this does NOT affect the `eth_chainId` RPC response! Defaults to `1` +* `"_chainIdRpc"`: **(temporary option until v3)** Specify the `eth_chainId` RPC response value. For legacy reasons, this does NOT affect the chain's `chainid`! Defaults to `1337` * `"time"`: `Date` - Date that the first block should start. Use this feature, along with the `evm_increaseTime` method to test time-dependent code. * `"locked"`: `boolean` - whether or not accounts are locked by default. * `"unlocked_accounts"`: `Array` - array of addresses or address indexes specifying which accounts should be unlocked.