Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 50 additions & 28 deletions public/javascripts/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,48 @@
var difficultyMajor = 4;

// 0-15, maximum (decimal) value of the hex digit after the front
// 15 means any hex character is allowed next
// 7 means next bit must be 0 (because 0x7=0111),
// (so the bit-strength is doubled)
// 0 means only 0x0 can be next
// (equivalent to one more difficultyMajor)
var difficultyMinor = 15;

var maximumNonce = 8; // limit the nonce so we don't mine too long
var maximumNonce = 8;
var pattern = '';
for (var x=0; x<difficultyMajor; x++) {
pattern += '0'; // every additional required 0
maximumNonce *= 16; // takes 16x longer to mine
pattern += '0';
maximumNonce *= 16;
}
// at this point in the setup, difficultyMajor=4
// yields pattern '0000' and maximumNonce 8*16^4=524288

// add one more hex-char for the minor difficulty
pattern += difficultyMinor.toString(16);
var patternLen = pattern.length; // == difficultyMajor+1

if (difficultyMinor == 0) { maximumNonce *= 16; } // 0000 require 4 more 0 bits
else if (difficultyMinor == 1) { maximumNonce *= 8; } // 0001 require 3 more 0 bits
else if (difficultyMinor <= 3) { maximumNonce *= 4; } // 0011 require 2 more 0 bits
else if (difficultyMinor <= 7) { maximumNonce *= 2; } // 0111 require 1 more 0 bit
// else don't bother increasing maximumNonce, it already started with 8x padding
var patternLen = pattern.length;

if (difficultyMinor == 0) { maximumNonce *= 16; }
else if (difficultyMinor == 1) { maximumNonce *= 8; }
else if (difficultyMinor <= 3) { maximumNonce *= 4; }
else if (difficultyMinor <= 7) { maximumNonce *= 2; }


/////////////////////////
// functions
/////////////////////////

function sha256(block, chain) {
// calculate a SHA256 hash of the contents of the block
return CryptoJS.SHA256(getText(block, chain));
}

function updateState(block, chain) {
// set the well background red or green for this block
if ($('#block'+block+'chain'+chain+'hash').val().substr(0, patternLen) <= pattern) {
$('#block'+block+'chain'+chain+'well').removeClass('well-error').addClass('well-success');
}
else {
$('#block'+block+'chain'+chain+'well').removeClass('well-success').addClass('well-error');
$('#block'+block+'chain'+chain+'well').removeClass('well-error').addClass('well-success');
} else {
$('#block'+block+'chain'+chain+'well').removeClass('well-success').addClass('well-error');
}
}

function updateHash(block, chain) {
// update the SHA256 hash value for this block
$('#block'+block+'chain'+chain+'hash').val(sha256(block, chain));
updateState(block, chain);
}

function updateChain(block, chain) {
// update all blocks walking the chain from this block to the end
for (var x = block; x <= 5; x++) {
for (var x = block; x <= 7; x++) { // Increased from 5 to 7 blocks
if (x > 1) {
$('#block'+x+'chain'+chain+'previous').val($('#block'+(x-1).toString()+'chain'+chain+'hash').val());
}
Expand All @@ -75,11 +62,46 @@ function mine(block, chain, isChain) {
if ($('#block'+block+'chain'+chain+'hash').val().substr(0, patternLen) <= pattern) {
if (isChain) {
updateChain(block, chain);
}
else {
} else {
updateState(block, chain);
}
break;
}
}
}

/////////////////////////
// Add Two New Peers (Peer D and Peer E)
/////////////////////////

var peers = ['A', 'B', 'C', 'D', 'E'];

peers.forEach(peer => {
for (var block = 1; block <= 7; block++) {
$(`#block${block}chain${peer}`).on('input', () => updateChain(block, peer));
}
});

/////////////////////////
// HTML for New Peers and Blocks
/////////////////////////

function addNewPeersAndBlocks() {
peers.slice(3).forEach(peer => {
for (let block = 6; block <= 7; block++) {
$('#blockchain').append(`
<div id="block${block}chain${peer}" class="block">
<input id="block${block}chain${peer}previous" type="text" readonly />
<input id="block${block}chain${peer}data" type="text" placeholder="Data" />
<input id="block${block}chain${peer}nonce" type="number" value="0" />
<input id="block${block}chain${peer}hash" type="text" readonly />
<button onclick="mine(${block}, '${peer}', true)">Mine</button>
</div>
`);
}
});
}

$(document).ready(() => {
addNewPeersAndBlocks();
});