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. 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) { 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); + }); + }); +});