JavaScript区块链模拟:自动挖矿及性能分析
extends layout
block content
.container-fluid
h1 #{__('Blockchain')}
.row.row-horizon
- var blocks = []
- for (var i = 1; i <= 1000; i++) {
- blocks.push({block: i, chain: 1, nonce: i, previous: '0000000000000000000000000000000000000000000000000000000000000000'})
- }
each block in blocks
.col-xs-7
include includes/block
script.
$(function() {
var miningTimes = []; // 用于存储每次挖矿的时间
for (var i = 1; i <= 300; i++) {
var startTime = performance.now();
for (var j = 1; j <= 1000; j++) {
setup(j, 1);
mine(j, 1, false);
}
var endTime = performance.now();
miningTimes.push(endTime - startTime);
console.log('第 ' + i + ' 次挖矿耗时:' + (endTime - startTime) + ' 毫秒');
}
// 计算平均挖矿时间
var totalMiningTime = 0;
for (var i = 0; i < miningTimes.length; i++) {
totalMiningTime += miningTimes[i];
}
var averageMiningTime = totalMiningTime / miningTimes.length;
console.log('平均挖矿时间:' + averageMiningTime + ' 毫秒');
});
function getText(block, chain) {
return $('#block'+block+'chain'+chain+'number').val() +
$('#block'+block+'chain'+chain+'nonce').val() +
$('#block'+block+'chain'+chain+'data').val() +
$('#block'+block+'chain'+chain+'previous').val();
}
function setup(block, chain) {
updateHash(block, chain);
$('#block'+block+'chain'+chain+'mineButton').click(function(e) {
e.preventDefault();
var l = Ladda.create(this);
l.start();
setTimeout(function() {
mine(block, chain, true);
l.stop();
}, 250); // give UI time to update
});
}
var difficulty = 5; // number of zeros required at front of hash
var maximumNonce = 50000000; // limit the nonce to this so we don't mine too long
// NOTE: Because there are 16 possible characters in a hex value, each time you increment
// the difficulty by one you make the puzzle 16 times harder. In my testing, a difficulty
// of 6 requires a maximumNonce well over 500,000,000.
/////////////////////////
// global variable setup
/////////////////////////
var pattern = '';
for (var x = 0; x < difficulty; x++) {
pattern += '0';
}
/////////////////////////
// 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, difficulty) === pattern) {
$('#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++) {
if (x > 1) {
$('#block' + x + 'chain' + chain + 'previous').val($('#block' + (x - 1).toString() + 'chain' + chain + 'hash').val());
}
updateHash(x, chain);
}
}
function mine(block, chain, isChain) {
var t0 = performance.now();
for (var x = 0; x <= maximumNonce; x++) {
$('#block' + block + 'chain' + chain + 'nonce').val(x);
$('#block' + block + 'chain' + chain + 'hash').val(sha256(block, chain));
if ($('#block' + block + 'chain' + chain + 'hash').val().substr(0, difficulty) === pattern) {
if (isChain) {
updateChain(block, chain);
}
else {
updateState(block, chain);
}
break;
}
}
var t1 = performance.now();
}
原文地址: https://www.cveoy.top/t/topic/f28m 著作权归作者所有。请勿转载和采集!