fixed bugs

This commit is contained in:
Matthew Little 2014-01-06 16:54:48 -05:00
parent ca02d2c11b
commit 3b7c7dff29
4 changed files with 50 additions and 55 deletions

View File

@ -46,5 +46,6 @@ BTC: 1KRotMnQpxu3sePQnsVLRy3EraRFYfJQFR
License
-------
node-stratum is released under the GNU General Public License v2
Released under the GNU General Public License v2
http://www.gnu.org/licenses/gpl-2.0.html

View File

@ -10,6 +10,7 @@ function DaemonInterface(options){
//private members
var _this = this;
this.options = options;
(function init(){
isOnline(function(online){
@ -38,7 +39,7 @@ function DaemonInterface(options){
}
function isOnline(callback){
this.cmd('getinfo', [], function(error, result){
cmd('getinfo', [], function(error, result){
if (error)
callback(false);
else
@ -46,24 +47,7 @@ function DaemonInterface(options){
});
}
//public members
this.isOnline = isOnline;
this.start = function(){
var cmdArgs = [
'-rpcport=' + this.options.port,
'-rpcuser=' + this.options.user,
'-rpcpassword=' + this.options.password,
'-blocknotify=' + this.options.blocknotify
];
var child = cp.spawn(this.options.bin, cmdArgs, { detached: true, stdio: [ 'ignore', 'ignore', 'ignore' ] });
child.unref();
console.log('started daemon');
};
this.cmd = function(method, params, callback){
function cmd(method, params, callback){
var requestJson = JSON.stringify({
id: Date.now() + Math.floor(Math.random() * 10),
@ -73,9 +57,9 @@ function DaemonInterface(options){
var options = {
hostname: 'localhost',
port: this.options.port,
port: _this.options.port,
method: 'POST',
auth: this.options.user + ':' + this.options.password,
auth: _this.options.user + ':' + _this.options.password,
headers: {
'Content-Length': requestJson.length
}
@ -103,6 +87,22 @@ function DaemonInterface(options){
req.end(requestJson);
}
//public members
this.isOnline = isOnline;
this.cmd = cmd;
this.start = function(){
var cmdArgs = [
'-rpcport=' + _this.options.port,
'-rpcuser=' + _this.options.user,
'-rpcpassword=' + _this.options.password,
'-blocknotify=' + _this.options.blocknotify
];
var child = cp.spawn(_this.options.bin, cmdArgs, { detached: true, stdio: [ 'ignore', 'ignore', 'ignore' ] });
child.unref();
console.log('started daemon');
};
}
DaemonInterface.prototype.__proto__ = events.EventEmitter.prototype;

48
pool.js
View File

@ -1,4 +1,5 @@
var net = require('net');
var events = require('events');
var bignum = require('bignum');
@ -10,47 +11,50 @@ var coinbase = require('./coinbase.js');
exports.pool = function pool(coin){
var pool = module.exports = function pool(coin){
coin.jobManager = new jobManager({
var _this = this;
this.jobManager = new jobManager({
algorithm: coin.options.algorithm,
address: coin.options.address
});
coin.jobManager.on('newBlock', function(blockTemplate){
coin.stratumServer.broadcastMiningJobs(blockTemplate.getJobParams());
this.jobManager.on('newBlock', function(blockTemplate){
_this.stratumServer.broadcastMiningJobs(blockTemplate.getJobParams());
});
coin.daemon = new daemon.interface(coin.options.daemon);
coin.daemon.on('online', function(){
coin.daemon.cmd(
this.daemon = new daemon.interface(coin.options.daemon);
this.daemon.on('online', function(){
this.cmd(
'getblocktemplate',
[{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}],
function(error, response){
coin.jobManager.newTemplate(response.result);
console.log(coin.jobManager.currentJob.getJobParams());
_this.jobManager.newTemplate(response.result);
console.log(_this.jobManager.currentJob.getJobParams());
}
);
}).on('startFailed', function(){
console.log('Failed to start daemon for ' + coin.name);
});
console.log('Failed to start daemon for ' + coin.name);
});
coin.stratumServer = new stratum.Server({
this.stratumServer = new stratum.Server({
port: 3333
});
coin.stratumServer.on('client', function(client){
this.stratumServer.on('client', function(client){
client.on('subscription', function(params, result){
var extraNonce = coin.jobManager.extraNonceCounter.next();
var extraNonce2Size = coinbase.extranonce_size - coin.jobManager.extraNonceCounter.size();
var extraNonce = _this.jobManager.extraNonceCounter.next();
var extraNonce2Size = coinbase.extranonce_size - _this.jobManager.extraNonceCounter.size();
result(extraNonce, extraNonce2Size);
client.sendDifficulty(1);
client.sendMiningJob(coin.jobManager.currentJob.getJobParams());
this.sendDifficulty(1);
this.sendMiningJob(_this.jobManager.currentJob.getJobParams());
}).on('authorize', function(params, result){
result(true);
}).on('submit', function(params, result){
result(true);
}).on('submit', function(params, result){
result(true);
});
result(true);
});
});
};
};
pool.prototype.__proto__ = events.EventEmitter.prototype;

12
util.js
View File

@ -62,7 +62,7 @@ exports.uint256BufferFromHash = function(hex){
var fromHex = new Buffer(hex, 'hex');
if (buffer.length != 32){
if (fromHex.length != 32){
var empty = new Buffer(32);
empty.fill(0);
fromHex.copy(empty);
@ -77,16 +77,6 @@ exports.hexFromReversedBuffer = function(buffer){
};
exports.randomId = function(){
var text = "";
var possible = "abcdef0123456789";
for (var i = 0; i < 32; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
exports.uint256_from_compact = function(c){
var nbytes = (c >> 24) & 0xFF;
v = (c & 0xFFFFFF) << (8 * (nbytes - 3))