-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgethdev.js
More file actions
71 lines (58 loc) · 1.5 KB
/
gethdev.js
File metadata and controls
71 lines (58 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* global web3 */
// https://github.com/amacneil/gethdev
var minHeight = 0;
// minimum etherbase balance we should mine
var MIN_BALANCE = web3.toWei(web3.toBigNumber(100), 'ether');
// number of threads to mine with
var THREADS = 1;
// number of blocks to mine on top of new transactions before stopping
var CONFIRMATIONS = 5;
function log(str) {
console.log('[gethdev] ' + str);
}
/**
* Start the miner if it is not already running
*/
function startMiner() {
if (!web3.eth.mining) {
log('Starting miner');
web3.miner.start(THREADS);
}
}
/**
* Stop the miner if it is running
*/
function stopMiner() {
if (web3.eth.mining) {
log('Stopping miner');
web3.miner.stop();
}
}
/**
* Start or stop the miner if necessary
*/
function checkStatus() {
// if etherbase balance is too low, start mining
if (web3.eth.getBalance(web3.eth.coinbase).lessThan(MIN_BALANCE)) {
startMiner();
return;
}
// if there are any pending transactions, start mining
if (web3.eth.getBlock('pending').transactions.length > 0) {
minHeight = web3.eth.blockNumber + CONFIRMATIONS;
startMiner();
return;
}
if (web3.eth.blockNumber > minHeight) {
// nothing to do, pause mining for now
stopMiner();
}
}
// create the first account (with blank password) if necessary
if (web3.personal.listAccounts.length === 0) {
log('Creating etherbase account');
web3.personal.newAccount('');
}
web3.eth.filter('latest', checkStatus);
web3.eth.filter('pending', checkStatus);
startMiner();