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

Commit

Permalink
Make threads argument to mining_start optional, fixes #383
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns committed Jan 17, 2018
1 parent 102a64a commit 2b0b887
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
26 changes: 23 additions & 3 deletions lib/subproviders/geth_api_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,26 @@ GethApiDouble.prototype.handleRequest = function(payload, next, end) {
args.push(end);

// avoid crash by checking to make sure that we haven't specified too many arguments
if (args.length > method.length) {
var errorMessage = `Method '${payload.method}' requires a maximum of ${method.length - 1} arguments. `;
if (
args.length > method.length
|| (method.minLength !== undefined && args.length < method.minLength)
|| (method.minLength === undefined && args.length < method.length)
){

var errorMessage = `Incorrect number of arguments. Method '${payload.method}' requires `;
if (method.minLength) {
errorMessage += `between ${method.minLength - 1} and ${method.length - 1} arguments. `;
} else {
errorMessage += `exactly ${method.length - 1} arguments. `;
}

if (addedBlockParam) {
errorMessage += 'Including the implicit block argument, r';
} else {
// new sentence, capitalize it.
errorMessage += 'R';
}
errorMessage += `equest specified ${args.length} arguments: ${JSON.stringify(args)}.`
errorMessage += `equest specified ${args.length - 1} arguments: ${JSON.stringify(args)}.`

return end(new Error(errorMessage));
}
Expand Down Expand Up @@ -354,11 +366,19 @@ GethApiDouble.prototype.net_version = function(callback) {
};

GethApiDouble.prototype.miner_start = function(threads, callback) {
if (!callback && typeof threads === 'function') {
callback = threads;
threads = null;
}

this.state.startMining(function(err) {
callback(err, true);
});
};

// indicate that `miner_start` only requires one argument (the callback)
GethApiDouble.prototype.miner_start.minLength = 1;

GethApiDouble.prototype.miner_stop = function(callback) {
this.state.stopMining(function(err) {
callback(err, true);
Expand Down
30 changes: 30 additions & 0 deletions test/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,36 @@ var tests = function(web3) {
})
})
})

it("should treat the threads argument as optional", function(done){
web3.currentProvider.send({
id: new Date().getTime(),
jsonrpc: "2.0",
method: "miner_stop",
}, function(err,result){
web3.currentProvider.send({
id: new Date().getTime(),
jsonrpc: "2.0",
method: "miner_start",
params: []
}, function(err,result){
var tx_data = {}
tx_data.to = accounts[1];
tx_data.from = accounts[0];
tx_data.value = 0x1;

web3.eth.sendTransaction(tx_data, function(err, tx) {
if (err) return done(err);
//Check the receipt
web3.eth.getTransactionReceipt(tx, function(err, receipt) {
if (err) return done(err);
assert.notEqual(receipt, null); //i.e. receipt exists, so transaction was mined
done();
});
});
})
})
})
});

describe("web3_sha3", function() {
Expand Down
2 changes: 1 addition & 1 deletion test/stability.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("stability", function(done) {
// 10 oughtta do it!
]
}).catch(err => {
assert.deepEqual(err.message, regex(/Method 'evm_mine' requires a maximum of \d+ arguments/));
assert.deepEqual(err.message, regex(/Method \'evm_mine\' requires exactly \d+ arguments/));
});// nothing to check from here, if the promise rejects, test fails
})

Expand Down

0 comments on commit 2b0b887

Please sign in to comment.