change fund account function
This commit is contained in:
parent
419bfe4996
commit
5e836dfb0f
|
@ -4,6 +4,10 @@ const NetcatClient = require('netcat/client');
|
||||||
const minerStart = 'miner_start';
|
const minerStart = 'miner_start';
|
||||||
const minerStop = 'miner_stop';
|
const minerStop = 'miner_stop';
|
||||||
const getHashRate = 'miner_getHashrate';
|
const getHashRate = 'miner_getHashrate';
|
||||||
|
const getCoinbase = 'eth_coinbase';
|
||||||
|
const getBalance = 'eth_getBalance';
|
||||||
|
const newBlockFilter = 'eth_newBlockFilter';
|
||||||
|
const getChanges = 'eth_getFilterChanges';
|
||||||
|
|
||||||
class GethMiner {
|
class GethMiner {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -40,9 +44,15 @@ class GethMiner {
|
||||||
this.client.unixSocket(ipcPath)
|
this.client.unixSocket(ipcPath)
|
||||||
.enc('utf8')
|
.enc('utf8')
|
||||||
.connect()
|
.connect()
|
||||||
.on('data', function(res){
|
.on('data', function(response){
|
||||||
|
try {
|
||||||
|
response = JSON.parse(response);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (self.callback) {
|
if (self.callback) {
|
||||||
self.callback(null, res);
|
self.callback(response.error, response.result);
|
||||||
self.callback = null;
|
self.callback = null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -52,7 +62,11 @@ class GethMiner {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.sendCommand(minerStop, () => {
|
self.sendCommand(minerStop, () => {
|
||||||
self.fundAccount(function () {
|
self.fundAccount(function (err) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (this.config.mine_periodically) self.start_periodic_mining();
|
if (this.config.mine_periodically) self.start_periodic_mining();
|
||||||
if (this.config.mine_pending_txns) self.start_transaction_mining();
|
if (this.config.mine_pending_txns) self.start_transaction_mining();
|
||||||
});
|
});
|
||||||
|
@ -60,34 +74,86 @@ class GethMiner {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendCommand(method, callback) {
|
sendCommand(method, params, callback) {
|
||||||
|
if (typeof params === 'function') {
|
||||||
|
callback = params;
|
||||||
|
params = [];
|
||||||
|
}
|
||||||
if (callback) {
|
if (callback) {
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
this.client.send(JSON.stringify({"jsonrpc": "2.0", "method": method, "params": [], "id": 1}));
|
this.client.send(JSON.stringify({"jsonrpc": "2.0", "method": method, "params": params || [], "id": 1}));
|
||||||
}
|
}
|
||||||
|
|
||||||
fundAccount(cb) {
|
getCoinbase(callback) {
|
||||||
const self = this;
|
if (this.coinbase) {
|
||||||
const accountFunded = function () {
|
return callback(null, this.coinbase);
|
||||||
// TODO check https://github.com/ethereum/wiki/wiki/JSON-RPC for APIs
|
}
|
||||||
return (eth.getBalance(eth.coinbase) >= self.config.initial_ether);
|
this.sendCommand(getCoinbase, (err, response) => {
|
||||||
};
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
this.coinbase = response.result;
|
||||||
|
if (!this.coinbase) {
|
||||||
|
return callback('Failed getting coinbase account');
|
||||||
|
}
|
||||||
|
callback(null, this.coinbase);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (accountFunded()) {
|
accountFunded(callback) {
|
||||||
return cb();
|
const self = this;
|
||||||
|
self.getCoinbase((err, coinbase) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
self.sendCommand(getBalance, [coinbase], (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
callback(null, result >= self.config.initial_ether);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
watchNewBlocks() {
|
||||||
|
const self = this;
|
||||||
|
self.sendCommand(newBlockFilter, (err, filterId) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
self.sendCommand(getChanges, [filterId], (err, changes) => {
|
||||||
|
if (!changes || !changes.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.accountFunded((err, funded) => {
|
||||||
|
if (funded) {
|
||||||
|
clearTimeout(interval);
|
||||||
|
self.sendCommand(minerStop);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fundAccount(callback) {
|
||||||
|
const self = this;
|
||||||
|
|
||||||
|
self.accountFunded((err, funded) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
if (funded) {
|
||||||
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("== Funding account");
|
console.log("== Funding account");
|
||||||
this.sendCommand(minerStart);
|
this.sendCommand(minerStart);
|
||||||
|
|
||||||
const blockWatcher = web3.eth.filter("latest").watch(function () {
|
self.watchNewBlocks();
|
||||||
if (accountFunded()) {
|
|
||||||
console.log("== Account funded");
|
|
||||||
|
|
||||||
blockWatcher.stopWatching();
|
|
||||||
self.sendCommand(minerStop, cb);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,8 +209,8 @@ class GethMiner {
|
||||||
start_transaction_mining() {
|
start_transaction_mining() {
|
||||||
const self = this;
|
const self = this;
|
||||||
web3.eth.filter("pending").watch(function () {
|
web3.eth.filter("pending").watch(function () {
|
||||||
self.sendCommand(getHashRate, (err, response) => {
|
self.sendCommand(getHashRate, (err, result) => {
|
||||||
if (response.result > 0) return;
|
if (result > 0) return;
|
||||||
|
|
||||||
console.log("== Pending transactions! Looking for next block...");
|
console.log("== Pending transactions! Looking for next block...");
|
||||||
self.sendCommand(minerStart);
|
self.sendCommand(minerStart);
|
||||||
|
|
Loading…
Reference in New Issue