example code for new structure
This commit is contained in:
parent
6ad310b872
commit
2b5aefd7b4
139
example/init.js
139
example/init.js
@ -1,90 +1,49 @@
|
|||||||
var net = require('net');
|
var fs = require('fs');
|
||||||
var fs = require('fs');
|
|
||||||
var path = require('path');
|
var Stratum = require('../index.js'); //require('node-stratum')
|
||||||
var pool = require('../index.js');
|
|
||||||
var ShareManager = require('./shareManager.js').ShareManager;
|
|
||||||
|
|
||||||
var logRef = console.log;
|
var timeLog = function(text, poolName){
|
||||||
console.log = function(s){
|
var desc = poolName ? '[' + poolName + '] ' : '';
|
||||||
var time = new Date().toISOString();
|
var time = new Date().toISOString();
|
||||||
logRef(time + ': ' + s);
|
console.log(time + ': ' + desc + text);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var config = JSON.parse(fs.readFileSync("config.json"));
|
||||||
var config = JSON.parse(fs.readFileSync("config.json"));
|
|
||||||
|
|
||||||
|
var stratum = new Stratum(config);
|
||||||
function Coin(options){
|
stratum.on('log', function(logText){
|
||||||
this.options = options;
|
timeLog(logText);
|
||||||
}
|
});
|
||||||
Coin.prototype = {};
|
|
||||||
|
|
||||||
var coins = [];
|
|
||||||
|
fs.readdirSync('coins').forEach(function(file){
|
||||||
var confFolder = 'coins';
|
|
||||||
|
var coinOptions = JSON.parse(fs.readFileSync('coins/' + file, {encoding: 'utf8'}));
|
||||||
|
|
||||||
var authorizeFN = function (ip, workerName, password, cback) {
|
var authorizeFN = function (ip, workerName, password, callback) {
|
||||||
// Default implementation just returns true
|
// Default implementation just returns true
|
||||||
console.log("Athorize ["+ip+"] "+workerName+":"+password);
|
timeLog(coinOptions.name, "Authorize ["+ip+"] "+workerName+":"+password);
|
||||||
cback(
|
callback({
|
||||||
null, // error
|
error: null,
|
||||||
true, // authorized?
|
authorized: true,
|
||||||
false, // should disconnect user?
|
disconnect: false
|
||||||
16 // difficulty
|
});
|
||||||
);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
fs.readdir(confFolder, function(err, files){
|
var pool = stratum.createPool(coinOptions, authorizeFN);
|
||||||
if (err) throw err;
|
pool.on('share', function(isValid, data){
|
||||||
files.forEach(function(file){
|
if (isValid)
|
||||||
var filePath = confFolder + '/' + file;
|
timeLog(coinOptions.name, "A new Valid share from " + data.client.workerName + " has arrived! - " + data.headerHex);
|
||||||
if (path.extname(filePath) !== '.json') return;
|
else
|
||||||
fs.readFile(filePath, {encoding: 'utf8'}, function(err, data){
|
timeLog(coinOptions.name, "Invalid share form " + data.client.workerName + " ErrorCode: " + data.errorCode + " ErrorDescription: " + data.errorDescription);
|
||||||
if (err) throw err;
|
}).on('log', function(logText){
|
||||||
var coinJson = JSON.parse(data)
|
timeLog(coinOptions.name, logText);
|
||||||
var coin = new Coin(coinJson);
|
});
|
||||||
console.log('Starting pool for ' + coin.options.name);
|
|
||||||
|
});
|
||||||
coin.pool = new pool(coin, authorizeFN );
|
|
||||||
var shareManager = new ShareManager(coin.pool);
|
|
||||||
|
|
||||||
coins.push(coin);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
if (config.blockNotifyListener.enabled){
|
|
||||||
console.log("Block listener is enabled, starting server on port " + config.blockNotifyListener.port);
|
|
||||||
var blockNotifyServer = net.createServer(function(c) {
|
|
||||||
console.log('server connected');
|
|
||||||
var data = '';
|
|
||||||
c.on('data', function(d){
|
|
||||||
console.log('got blocknotify data');
|
|
||||||
data += d;
|
|
||||||
if (data.slice(-1) === '\n'){
|
|
||||||
c.end();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
c.on('end', function() {
|
|
||||||
console.log(data);
|
|
||||||
|
|
||||||
var message = JSON.parse(data);
|
|
||||||
if (message.password === config.blockNotifyListener.password){
|
|
||||||
coins.forEach(function(coin){
|
|
||||||
if (coin.options.symbol === message.coin){
|
|
||||||
coin.pool.processBlockNotify(message.blockHash);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('server disconnected');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
blockNotifyServer.listen(config.blockNotifyListener.port, function() {
|
|
||||||
console.log('Block notify listener server started on port ' + config.blockNotifyListener.port)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user