Skip to content

Commit

Permalink
Difficulty WTF?
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed May 11, 2014
1 parent 148f652 commit b2e3601
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{

"startingTarget": 200000000,
"difficulty": 5,

"poolPort": 5555,

Expand Down
31 changes: 24 additions & 7 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ var cryptoNight = multiHashing['cryptonight'];

var reserveSize = 4;

var diff1 = 0xffffffff;
var diff1 = bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 16);

var connectedMiners = {};


//console.log(new Buffer('33333303', 'hex').readUInt32LE(0));


var rpc = function(host, port, method, params, callback){

Expand Down Expand Up @@ -90,15 +88,15 @@ function Miner(id, login, pass){
this.login = login;
this.pass = pass;
this.heartbeat();
this.target = config.startingTarget;
this.difficulty = config.difficulty;
}
Miner.prototype = {
heartbeat: function(){
this.lastBeat = Date.now();
},
getTargetHex: function(){
var buff = new Buffer(4);
buff.writeUInt32LE(config.startingTarget, 0);
var buff = diff1.div(config.difficulty).toBuffer().slice(0, 4);
this.target = buff.readUInt32LE(0);
var hex = buff.toString('hex');
return hex;
}
Expand Down Expand Up @@ -171,14 +169,33 @@ function processShare(miner, nonce, resultHash){
return false;
}

var hashNum = bignum.fromBuffer(hash, {size: 'auto'});
var hashDiff = diff1.div(hashNum);

if (hashDiff.ge(CurrentJob.difficulty)){
console.log('Block found!');
rpcDaemon('submitblock', [hashHex], function(error, result){
if (error){
console.log('error submitting block ' + JSON.stringify(error));
return;
}
console.log('Block submitted successfully ' + JSON.stringify(result));
});
}

/*if (hashDiff.lt(miner.difficulty)){
console.log('Rejected low difficulty share of ' + hashDiff.toString());
return false;
}*/

var hashTarget = hash.readUInt32LE(hash.length - 4);
var percent = (miner.target / hashTarget * 100).toFixed(2);

if (hashTarget > miner.target){
console.log('high target share ' + percent);
return false;
}
console.log('Accepted share at ' + percent + '% of target (' + miner.target + '/' + hashTarget + ')');
console.log('Accepted share at difficulty ' + hashDiff.toString() + ' - ' + percent + '% of target (' + miner.target + '/' + hashTarget + ')');

return true;
}
Expand Down

0 comments on commit b2e3601

Please sign in to comment.