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
var totalTimes = 300; // total number of times to mine

// 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) {
  console.log('Here I start!');
  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();
  console.log('Call to mine took ' + Math.floor(t1 - t0) + ' milliseconds.');
  return t1 - t0;
}

// Generate data for blocks
var data = [];
for (var i = 1; i <= 1000; i++) {
  data.push(i);
}

// Mine 300 times and calculate average time for each difficulty
var averageTimes = {};
for (var d = 1; d <= 10; d++) {
  difficulty = d;
  pattern = '';
  for (var x = 0; x < difficulty; x++) {
    pattern += '0';
  }
  var totalDiffTime = 0;
  for (var t = 0; t < totalTimes; t++) {
    for (var i = 0; i < data.length; i++) {
      totalDiffTime += mine(data[i], 1, false);
    }
  }
  averageTimes[d] = totalDiffTime / totalTimes;
}

console.log(averageTimes);

备注:以上代码中,首先生成了要挖矿的数据,然后循环300次,每次循环挖矿1000个区块,并统计每种难度所消耗的平均时间。最后输出每种难度的平均时间。

代码优化说明:

  1. 标题和描述修改: 使用更吸引人、更符合搜索习惯的标题和描述,突出代码功能和价值。
  2. 关键词添加: 添加相关关键词,方便搜索引擎收录。
  3. 代码注释: 为主要函数和代码块添加注释,提高代码可读性。
  4. 双引号替换: 将代码中所有双引号替换为单引号,符合JSON格式要求。
  5. mine 函数返回值: 修改 mine 函数使其返回挖矿耗时,方便后续计算平均值。
  6. 难度循环内部更新 pattern: 在循环更改难度后,更新 pattern 以匹配新的难度值。
  7. 代码逻辑优化: 调整代码逻辑,使代码更简洁易懂。

通过以上优化,可以使代码更易于理解和维护,同时也有利于搜索引擎的收录和排名。

JavaScript区块链挖矿算法及性能分析

原文地址: https://www.cveoy.top/t/topic/f26O 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录