embark-area-51/lib/cmds/blockchain/miner.js

325 lines
7.7 KiB
JavaScript
Raw Normal View History

2018-06-27 18:14:58 +00:00
const async = require('async');
const NetcatClient = require('netcat/client');
//Constants
const minerStart = 'miner_start';
const minerStop = 'miner_stop';
const getHashRate = 'miner_getHashrate';
2018-06-27 14:17:31 +00:00
const getCoinbase = 'eth_coinbase';
const getBalance = 'eth_getBalance';
const newBlockFilter = 'eth_newBlockFilter';
2018-06-27 18:14:58 +00:00
const pendingBlockFilter = 'eth_newPendingTransactionFilter';
2018-06-27 14:17:31 +00:00
const getChanges = 'eth_getFilterChanges';
2018-06-27 18:14:58 +00:00
const getBlockCount = 'eth_getBlockTransactionCountByNumber';
class GethMiner {
constructor() {
const self = this;
// TODO: Find a way to load mining config from YML.
// In the meantime, just set an empty config object
this.config = {};
2018-06-27 18:14:58 +00:00
self.interval = null;
self.callback = null;
2018-06-28 13:17:19 +00:00
self.started = null;
2018-06-27 18:14:58 +00:00
self.commandQueue = async.queue((task, callback) => {
self.callback = callback;
self.client.send(JSON.stringify({"jsonrpc": "2.0", "method": task.method, "params": task.params || [], "id": 1}));
}, 1);
const defaults = {
interval_ms: 15000,
initial_ether: 15000000000000000000,
mine_pending_txns: true,
mine_periodically: false,
mine_normally: false,
threads: 1
};
for (let key in defaults) {
if (this.config[key] === undefined) {
this.config[key] = defaults[key];
}
}
const isWin = process.platform === "win32";
let ipcPath;
if (isWin) {
ipcPath = '\\\\.\\pipe\\geth.ipc';
} else {
ipcPath = this.datadir + '/geth.ipc';
}
this.client = new NetcatClient();
this.client.unixSocket(ipcPath)
.enc('utf8')
.connect()
2018-06-27 18:14:58 +00:00
.on('data', (response) => {
2018-06-27 14:17:31 +00:00
try {
response = JSON.parse(response);
} catch (e) {
console.error(e);
return;
}
if (self.callback) {
2018-06-27 14:17:31 +00:00
self.callback(response.error, response.result);
}
});
if (this.config.mine_normally) {
2018-06-28 13:17:19 +00:00
this.startMiner();
return;
}
2018-06-28 13:17:19 +00:00
self.stopMiner(() => {
2018-06-27 14:17:31 +00:00
self.fundAccount(function (err) {
if (err) {
console.error(err);
return;
}
2018-06-27 18:14:58 +00:00
if (self.config.mine_periodically) self.start_periodic_mining();
if (self.config.mine_pending_txns) self.start_transaction_mining();
});
});
}
2018-06-27 14:17:31 +00:00
sendCommand(method, params, callback) {
if (typeof params === 'function') {
callback = params;
params = [];
}
2018-06-27 18:14:58 +00:00
if (!callback) {
2018-06-28 13:17:19 +00:00
callback = function () {
};
}
2018-06-27 18:14:58 +00:00
this.commandQueue.push({method, params: params || []}, callback);
}
2018-06-28 13:17:19 +00:00
startMiner(callback) {
if (this.started) {
if (callback) {
callback();
}
return;
}
this.started = true;
this.sendCommand(minerStart, callback);
}
stopMiner(callback) {
if (!this.started) {
if (callback) {
callback();
}
return;
}
this.started = false;
this.sendCommand(minerStop, callback);
}
2018-06-27 14:17:31 +00:00
getCoinbase(callback) {
if (this.coinbase) {
return callback(null, this.coinbase);
}
2018-06-27 18:14:58 +00:00
this.sendCommand(getCoinbase, (err, result) => {
2018-06-27 14:17:31 +00:00
if (err) {
return callback(err);
}
2018-06-27 18:14:58 +00:00
this.coinbase = result;
2018-06-27 14:17:31 +00:00
if (!this.coinbase) {
return callback('Failed getting coinbase account');
}
callback(null, this.coinbase);
});
}
accountFunded(callback) {
const self = this;
self.getCoinbase((err, coinbase) => {
if (err) {
return callback(err);
}
2018-06-27 18:14:58 +00:00
self.sendCommand(getBalance, [coinbase, 'latest'], (err, result) => {
2018-06-27 14:17:31 +00:00
if (err) {
return callback(err);
}
2018-06-28 13:17:19 +00:00
callback(null, parseInt(result, 16) >= self.config.initial_ether);
2018-06-27 14:17:31 +00:00
});
});
}
2018-06-28 13:17:19 +00:00
watchBlocks(filterCommand, callback, delay) {
2018-06-27 14:17:31 +00:00
const self = this;
2018-06-27 18:14:58 +00:00
self.sendCommand(filterCommand, (err, filterId) => {
2018-06-27 14:17:31 +00:00
if (err) {
2018-06-28 13:17:19 +00:00
return callback(err);
2018-06-27 14:17:31 +00:00
}
2018-06-27 18:14:58 +00:00
self.interval = setInterval(() => {
2018-06-27 14:17:31 +00:00
self.sendCommand(getChanges, [filterId], (err, changes) => {
2018-06-27 18:14:58 +00:00
if (err) {
console.error(err);
return;
}
2018-06-27 14:17:31 +00:00
if (!changes || !changes.length) {
return;
}
2018-06-27 18:14:58 +00:00
callback(null, changes);
2018-06-27 14:17:31 +00:00
});
2018-06-28 13:17:19 +00:00
}, delay || 1000);
2018-06-27 14:17:31 +00:00
});
}
2018-06-27 18:14:58 +00:00
mineUntilFunded(callback) {
const self = this;
2018-06-28 13:17:19 +00:00
this.startMiner();
2018-06-27 18:14:58 +00:00
self.watchBlocks(newBlockFilter, (err) => {
if (err) {
console.error(err);
return;
}
self.accountFunded((err, funded) => {
if (funded) {
clearTimeout(self.interval);
2018-06-28 13:17:19 +00:00
self.stopMiner();
2018-06-27 18:14:58 +00:00
callback();
}
});
});
}
2018-06-27 14:17:31 +00:00
fundAccount(callback) {
const self = this;
2018-06-27 14:17:31 +00:00
self.accountFunded((err, funded) => {
if (err) {
return callback(err);
}
2018-06-27 14:17:31 +00:00
if (funded) {
return callback();
}
console.log("== Funding account");
2018-06-27 18:14:58 +00:00
self.mineUntilFunded(callback);
});
}
2018-06-27 18:14:58 +00:00
pendingTransactions(callback) {
const self = this;
self.sendCommand(getBlockCount, ['pending'], (err, hexCount) => {
if (err) {
return callback(err);
}
callback(null, parseInt(hexCount, 16));
});
}
start_periodic_mining() {
const self = this;
2018-06-27 18:14:58 +00:00
const WAIT = 'wait';
let last_mined_ms = Date.now();
let timeout_set = false;
2018-06-27 18:14:58 +00:00
let next_block_in_ms;
2018-06-28 13:17:19 +00:00
self.startMiner();
2018-06-27 18:14:58 +00:00
self.watchBlocks(newBlockFilter, (err) => {
if (err) {
console.error(err);
return;
}
2018-06-27 18:14:58 +00:00
if (timeout_set) {
return;
}
2018-06-27 18:14:58 +00:00
async.waterfall([
function checkPendingTransactions(next) {
if (!self.config.mine_pending_txns) {
return next();
}
self.pendingTransactions((err, count) => {
if (err) {
return next(err);
}
if (count) {
return next(WAIT);
}
next();
});
},
function stopMiner(next) {
timeout_set = true;
const now = Date.now();
const ms_since_block = now - last_mined_ms;
last_mined_ms = now;
if (ms_since_block > self.config.interval_ms) {
next_block_in_ms = 0;
} else {
next_block_in_ms = (self.config.interval_ms - ms_since_block);
}
2018-06-28 13:17:19 +00:00
self.stopMiner();
2018-06-27 18:14:58 +00:00
console.log("== Looking for next block in " + next_block_in_ms + "ms");
next();
},
function startAfterTimeout(next) {
setTimeout(function () {
console.log("== Looking for next block");
timeout_set = false;
2018-06-28 13:17:19 +00:00
self.startMiner();
2018-06-27 18:14:58 +00:00
next();
}, next_block_in_ms);
}
], (err) => {
if (err === WAIT) {
return;
}
if (err) {
console.error(err);
}
});
});
}
start_transaction_mining() {
const self = this;
2018-06-28 13:17:19 +00:00
const pendingTrasactionsMessage = "== Pending transactions! Looking for next block...";
2018-06-27 18:14:58 +00:00
self.watchBlocks(pendingBlockFilter, (err) => {
if (err) {
console.error(err);
return;
}
2018-06-27 14:17:31 +00:00
self.sendCommand(getHashRate, (err, result) => {
if (result > 0) return;
2018-06-28 13:17:19 +00:00
console.log(pendingTrasactionsMessage);
self.startMiner();
});
2018-06-28 13:17:19 +00:00
}, 2000);
2018-06-27 18:14:58 +00:00
if (self.config.mine_periodically) return;
2018-06-27 18:14:58 +00:00
self.watchBlocks(newBlockFilter, (err) => {
if (err) {
console.error(err);
return;
}
2018-06-27 18:14:58 +00:00
self.pendingTransactions((err, count) => {
if (err) {
console.error(err);
return;
}
if (!count) {
console.log("== No transactions left. Stopping miner...");
2018-06-28 13:17:19 +00:00
self.stopMiner();
} else {
console.log(pendingTrasactionsMessage);
self.startMiner();
2018-06-27 18:14:58 +00:00
}
});
2018-06-28 13:17:19 +00:00
}, 2000);
}
}
module.exports = GethMiner;