Change mining script to mine periodically and use filters instead of polling.

This commit is contained in:
Ryan Casey 2015-08-21 09:49:18 -07:00
parent 94a85339c4
commit d23eec1081
1 changed files with 99 additions and 24 deletions

View File

@ -1,16 +1,49 @@
var miner_var; // Adapted from Iuri Matias' Embark framework
// https://github.com/iurimatias/embark-framework
// Modified by ryepdx to mine at regular intervals.
(function() {
var main = function () {
/* TODO: Find a way to load mining config from YML.
if (admin.miner === undefined) { if (!loadScript("config.js")) {
miner_var = miner; console.log("== config.js not found");
} }
else {
miner_var = admin.miner;
}
miner_var.setEtherbase(web3.eth.accounts[0]); if (typeof(config) === "undefined") {
config = {};
console.log("== config is undefined, proceeding with defaults");
}
In the meantime, just set an empty config object.
*/
config = {}
defaults = {
interval_ms: 15000,
mine_pending_txns: true,
mine_periodically: true,
mine_normally: false,
threads: 1
}
for (key in defaults) {
if (config[key] === undefined) {
config[key] = defaults[key];
}
}
var miner_obj = (admin.miner === undefined) ? miner : admin.miner;
if (config.mine_normally) {
miner_obj.start(config.threads);
return;
}
miner_obj.stop();
if (config.mine_periodically) start_periodic_mining(config, miner_obj);
if (config.mine_pending_txns) start_transaction_mining(config, miner_obj);
};
setInterval(function() {
var minimalAmount = (web3.eth.getBalance(web3.eth.coinbase) >= 15000000000000000000);
var pendingTransactions = function() { var pendingTransactions = function() {
if (web3.eth.pendingTransactions === undefined || web3.eth.pendingTransactions === null) { if (web3.eth.pendingTransactions === undefined || web3.eth.pendingTransactions === null) {
return txpool.status.pending || txpool.status.queued; return txpool.status.pending || txpool.status.queued;
@ -21,19 +54,61 @@ setInterval(function() {
else { else {
return web3.eth.pendingTransactions.length > 0 || web3.eth.getBlock('pending').transactions.length > 0; return web3.eth.pendingTransactions.length > 0 || web3.eth.getBlock('pending').transactions.length > 0;
} }
} };
if(!web3.eth.mining && (!minimalAmount || pendingTransactions())) { var start_periodic_mining = function (config, miner_obj) {
if (!minimalAmount) { console.log("=== minimal ether amount not reached yet") } var last_mined_ms = Date.now();
if (pendingTransactions()) { console.log("=== there are pending transactions") } var timeout_set = false;
console.log("=== start mining");
miner_var.start(); miner_obj.start(config.threads);
} web3.eth.filter("latest").watch(function () {
else if (web3.eth.mining && minimalAmount && !pendingTransactions()) { if ((config.mine_pending_txns && pendingTransactions()) || timeout_set) {
if (minimalAmount) { console.log("=== minimal ether amount reached") } return;
if (!pendingTransactions()) { console.log("=== no pending transactions") } }
console.log("=== stop mining");
miner_var.stop(); timeout_set = true;
}
}, 1000) var now = Date.now();
var ms_since_block = now - last_mined_ms;
last_mined_ms = now;
var next_block_in_ms;
if (ms_since_block > config.interval_ms) {
next_block_in_ms = 0;
} else {
next_block_in_ms = (config.interval_ms - ms_since_block);
}
miner_obj.stop();
console.log("== Looking for next block in " + next_block_in_ms + "ms");
setTimeout(function () {
console.log("== Looking for next block");
timeout_set = false;
miner_obj.start(config.threads);
}, next_block_in_ms);
});
};
var start_transaction_mining = function (config, miner_obj) {
web3.eth.filter("pending").watch(function () {
if (miner_obj.hashrate > 0) return;
console.log("== Pending transactions! Looking for next block...");
miner_obj.start(config.threads);
});
if (config.mine_periodically) return;
web3.eth.filter("latest").watch(function () {
if (!pendingTransactions()) {
console.log("== No transactions left. Stopping miner...");
miner_obj.stop();
}
});
};
main();
})();