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

Commit

Permalink
Add gasLimit and gasPrice to README, fix gasPrice
Browse files Browse the repository at this point in the history
Prior to this change, the `gasPrice` set in the `options` object wasn't
respected, and neither gasPrice nor gasLimit were documented in our
README.md. This change also adds tests for these options.

fixes #314
  • Loading branch information
benjamincburns committed Aug 13, 2018
1 parent c35f6a5 commit 45e88ff
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Both `.provider()` and `.server()` take a single object which allows you to spec
* `"vmErrorsOnRPCResponse"`: Whether to report runtime errors from EVM code as RPC errors. This is `true` by default to replicate the error reporting behavior of previous versions of ganache.
* `"hdPath"`: The hierarchical deterministic path to use when generating accounts. Default: "m/44'/60'/0'/0/"
* `"allowUnlimitedContractSize"`: Allows unlimited contract sizes while debugging. By setting this to `true`, the check within the EVM for contract size limit of 2KB (see [EIP-170](https://git.io/vxZkK)) is bypassed. Setting this to `true` **will** cause `ganache-core` to behave differently than production environments. (default: `false`; **ONLY** set to `true` during debugging).
* `"gasPrice"`: Sets the default gas price for transactions if not otherwise specified. Must be specified as a hex string in wei. Defaults to `"0x77359400"`, or 2 gwei.
* `"gasLimit"`: Sets the block gas limit. Must be specified as a hex string. Defaults to `"0x6691b7"`.

# IMPLEMENTED METHODS

Expand Down
4 changes: 3 additions & 1 deletion lib/statemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ StateManager = function(options, provider) {

const defaultOptions = {
total_accounts: 10,
gasPrice: '0x4A817C800',
gasPrice: '0x77359400', //2 gwei
default_balance_ether: 100,
unlocked_accounts: [],
hdPath: "m/44'/60'/0'/0/"
Expand Down Expand Up @@ -325,6 +325,8 @@ StateManager.prototype.queueTransaction = function(method, tx_params, block_numb

if (tx_params.gasPrice != null) {
rawTx.gasPrice = utils.addHexPrefix(tx_params.gasPrice);
} else {
rawTx.gasPrice = this.gasPriceVal;
}

if (tx_params.to != null) {
Expand Down
6 changes: 4 additions & 2 deletions test/bad_input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Web3 = require('web3');
var Ganache = require("../index.js");
var assert = require('assert');
var assert = require('assert-match');
var regex = require('assert-match/matchers').regex;

var tests = function(web3) {
var accounts;
Expand Down Expand Up @@ -134,7 +135,8 @@ var tests = function(web3) {
// Note that if using the Ganache as a provider, err will be non-null when there's
// an error. However, when using it as a server it won't be. In both cases, however,
// result.error should be set with the same error message. We'll check for that.
assert(result.error.message.indexOf("sender doesn't have enough funds to send tx. The upfront cost is: 324518553658426726783156021576256 and the sender's account only has: 99999999999463087088") >= 0);
assert.deepEqual(result.error.message, regex(/sender doesn't have enough funds to send tx. The upfront cost is: \d+ and the sender's account only has: \d+/),
`Unexpected error message. Got ${result.error.message}.`);

request.params[0].value = "0x5";
provider.send(request, done)
Expand Down
42 changes: 42 additions & 0 deletions test/helpers/contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs')
const solc = require('solc')
const path = require('path')

async function compileAndDeploy(contractPath, contractName, web3) {
let contractFilename = path.basename(contractPath)

let source = fs.readFileSync(contractPath, "utf8");

let result = solc.compile({sources: {[contractFilename]: source}}, 1);

let bytecode = "0x" + result.contracts[`${contractFilename}:${contractName}`].bytecode;
let abi = JSON.parse(result.contracts[`${contractFilename}:${contractName}`].interface);

console.log('good')
let contract = new web3.eth.Contract(abi);

let accounts = await web3.eth.getAccounts()
let block = await web3.eth.getBlock("latest");
let gasLimit = block.gasLimit;


let instance = await contract.deploy({data: bytecode}).send({from: accounts[0], gas: gasLimit})

// TODO: ugly workaround - not sure why this is necessary.
if (!instance._requestManager.provider) {
instance._requestManager.setProvider(web3.eth._provider);
}

return {
source,
bytecode,
abi,
contract,
instance
}
}

exports = module.exports = {
compileAndDeploy
}

30 changes: 30 additions & 0 deletions test/options/gasLimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Web3 = require('web3');
const assert = require('assert');
const Ganache = require("../../index.js");
const path = require("path");

const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat'

describe('options:gasLimit', function() {
let options = {mnemonic}
let provider = null
let web3 = null
let accounts = []

before ('setup web3', async function() {
provider = new Ganache.provider(options)
web3 = new Web3(provider)
})

before('get accounts', async function() {
accounts = await web3.eth.getAccounts()
})

it('should respect the assigned gasLimit', async function() {
let assignedGasLimit = provider.engine.manager.state.blockchain.blockGasLimit;
let block = await web3.eth.getBlock('latest')
assert.deepEqual(block.gasLimit, assignedGasLimit)
})
});


49 changes: 49 additions & 0 deletions test/options/gasPrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const to = require('../../lib/utils/to');
const Web3 = require('web3');
const assert = require('assert');
const Ganache = require("../../index");
const path = require("path");

const compileAndDeploy = require ('../helpers/contracts').compileAndDeploy

const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat'

describe('options:gasLimit', function() {
let options = {mnemonic}
let provider = null
let web3 = null
let accounts = []
let contractArtifact = {}
let instance = null

before ('setup web3', async function() {
provider = new Ganache.provider(options)
web3 = new Web3(provider)
})

before('get accounts', async function() {
accounts = await web3.eth.getAccounts()
})

before("compile source", async function() {
this.timeout(10000)
let contractName = 'Example'
contractArtifact = await compileAndDeploy(path.join(__dirname, '..', `${contractName}.sol`), contractName, web3)
instance = contractArtifact.instance
})

it('should respect the default gasPrice', async function() {
let assignedGasPrice = provider.engine.manager.state.gasPriceVal;

let receipt = await instance.methods.setValue('0x10').send({from: accounts[0], gas: 3141592})

let transactionHash = receipt.transactionHash;
let tx = await web3.eth.getTransaction(transactionHash)
let gasPrice = tx.gasPrice

assert.deepEqual(to.hex(gasPrice), to.hex(assignedGasPrice))
})

});


4 changes: 2 additions & 2 deletions test/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ var tests = function(web3) {
});

describe("eth_gasPrice", function() {
it("should return gas price of 0.02 szabo", function(done) {
it("should return gas price of 2 gwei", function(done) {
web3.eth.getGasPrice(function(err, result) {
if (err) return done(err);

assert.equal(to.hexWithZeroPadding(result), to.hexWithZeroPadding(20000000000));
assert.equal(to.hexWithZeroPadding(result), to.hexWithZeroPadding(2000000000));
done();
});
});
Expand Down

0 comments on commit 45e88ff

Please sign in to comment.