Skip to content

Instantly share code, notes, and snippets.

@kilpatty
Created April 25, 2019 17:03
Show Gist options
  • Save kilpatty/0148cc09c1f7cc96454147a1589cfd28 to your computer and use it in GitHub Desktop.
Save kilpatty/0148cc09c1f7cc96454147a1589cfd28 to your computer and use it in GitHub Desktop.
const common = require('hsd/lib/mining/common');
function toDifficulty(bits) {
let shift = (bits >>> 24) & 0xff;
let diff = 0x0000ffff / (bits & 0x00ffffff);
while (shift < 29) {
diff *= 256.0;
shift++;
}
while (shift > 29) {
diff /= 256.0;
shift--;
}
return diff;
}
const bits = 489684992;
const bitcoinBits = 0x172c4e11;
//Get Difficulty from toDifficulty
let difficultyA = toDifficulty(bits);
// console.log(difficultyA); //0.02083301544189453
//Get Difficulty from mining functions
let target = common.getTarget(bits);
let difficultyB = common.getDifficulty(target);
// console.log(difficultyB); //0
//Attempt difficulty with current Bitcoin bits number.
let diffBitcoinA = toDifficulty(bitcoinBits);
// console.log(diffBitcoinA);
let bitcoinTarget = common.getTarget(bitcoinBits);
let diffBitcoinB = common.getDifficulty(bitcoinTarget);
// console.log(diffBitcoinB);
//common.getDifficulty function edited to remove the Math.floor
const DIFF = 0x00000000ffff0000000000000000000000000000000000000000000000000000;
function getDifficultyRevised(target) {
const d = DIFF;
const n = common.double256(target);
if (n === 0)
return d;
return d / n;
};
let difficultyC = getDifficultyRevised(target);
let diffBitcoinC = getDifficultyRevised(bitcoinTarget);
//Handshake Difficulty Test
//Using rpc method toDifficulty
console.log(difficultyA); //0.02083301544189453
//Using current common.getDifficulty in mining package.
console.log(difficultyB); //0
//Using revised getDifficulty with no Math.floor
console.log(difficultyC); //0.02083301544189453
console.log(diffBitcoinA); //6353030562983.983
console.log(diffBitcoinB); //6353030562983
console.log(diffBitcoinC); //6353030562983.983
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment