为什么你给我的下列代码提示我 return CryptoJSSHA256getTextblock chain;这句话有问题 缺少getTEXT定义怎么修改extends layoutblock content container-fluid h1 #__Blockchain rowrow-horizon - var blocks = - for var i =
在给出的代码中,确实缺少了getText函数的定义,导致了提示错误。要修复这个问题,你需要在代码中添加getText函数的定义。在给出的代码中,getText函数的定义和实现已经在脚本标签中给出了,所以你只需要将这部分代码移动到blockchain.js文件中即可。
修改后的blockchain.js文件如下所示:
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 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 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 someFunction took " + Math.floor(t1 - t0) + " milliseconds.");
}
// Generate block data
for (var i = 1; i <= 1000; i++) {
$('#block' + i.toString() + 'chain1data').val(i);
}
// Mine 300 times and calculate average time
var totalTime = 0;
for (var j = 1; j <= 300; j++) {
var t0 = performance.now();
for (var k = 1; k <= 1000; k++) {
mine(k, 1, false);
}
var t1 = performance.now();
totalTime += (t1 - t0);
}
var averageTime = totalTime / 300;
console.log("Average time: " + averageTime + " milliseconds.");
在这个修改后的代码中,getText函数的定义已经添加到了blockchain.js文件中,可以正常使用了
原文地址: https://www.cveoy.top/t/topic/hyyR 著作权归作者所有。请勿转载和采集!