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

feat: add _chainIdRpc option #629

Merged
merged 3 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function Provider(options) {

const defaultOptions = {
_chainId: 1,
_chainIdRpc: 1337,
vmErrorsOnRPCResponse: true,
verbose: false,
asyncRequestProcessing: false,
Expand Down
2 changes: 1 addition & 1 deletion lib/subproviders/geth_api_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/contracts/chainId/ChainId.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma solidity ^0.6.0;

contract ChainId {
function getChainId() pure external returns (uint256) {
uint256 id;
assembly {
id := chainid()
}
return id;
}
}
52 changes: 52 additions & 0 deletions test/local/chainId.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});