在 NodeJS 区块链中,这段代码是否创建了一个基本的点对点区块链来检查前一个块的哈希值?
In a NodeJS Blockchain, does this code create a basic peer to peer blockchain that checks the previous block's hash?
// Import modules
const SHA512 = require("crypto-js/sha512");
var readline = require('readline-sync');
//Transactions are important so miners don't continuously mine blocks
and spam the blockchain (proof of work)
class Transaction {
constructor(fromAddress, toAddress, amount){
this.fromAddress = fromAddress;
this.toAddress = toAddress;
this.amount = amount;
}
}
class Block {
constructor(timestamp, transactions, previousHash = '') {
this.previousHash = previousHash;
this.timestamp = timestamp;
this.transactions = transactions;
this.hash = this.calculateHash();
this.nonce = 0;
}
calculateHash() {
return SHA512(this.previousHash + this.timestamp +
JSON.stringify(this.transactions) + this.nonce).toString();
}
mineBlock(difficulty) {
while (this.hash.substring(0, difficulty) !== Array(difficulty
+ 1).join("0")) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log("BLOCK MINED: " + this.hash);
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.difficulty = 3;
this.pendingTransactions = [];
this.miningReward = 10;
}
createGenesisBlock() {
return new Block(Date.now(), [], "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
getAllBlocks() {
return this.chain;
}
minePendingTransactions(miningRewardAddress){
let block = new Block(Date.now(), this.pendingTransactions,
this.getLatestBlock().hash);
block.mineBlock(this.difficulty);
console.log('Block successfully mined!');
this.chain.push(block);
this.pendingTransactions = [
new Transaction(address1, miningRewardAddress,
this.miningReward)
];
}
createTransaction(transaction){
this.pendingTransactions.push(transaction);
}
getBalanceOfAddress(address){
let balance = 0;
for(const block of this.chain){
for(const trans of block.transactions){
if(trans.fromAddress === address){
balance -= trans.amount;
}
if(trans.toAddress === address){
balance += trans.amount;
}
}
}
return balance;
}
isChainValid() {
for (let i = 1; i < this.chain.length; i++){
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
if (currentBlock.hash !== currentBlock.calculateHash()) {
return false;
}
if (currentBlock.previousHash !== previousBlock.hash) {
return false;
}
}
return true;
}
}
let brewcrypt = new Blockchain();
console.log('\n\n Latest block is:', brewcrypt.getLatestBlock());
console.log('\n-----------------------------');
var address1 = readline.question('\n\nEnter your blockchain address - ');
var address2 = readline.question('\nEnter the address you are sending to - ');
brewcrypt.createTransaction(new Transaction(address1, address2, 10));
if(address2 == address1) {
console.log('You cannot send transactions to yourself...');
return process.exit();
}
var blockinput = readline.question('Enter amounts of transaction blocks to be mined - ');
for(i = 0; i <= blockinput; i++) {
console.log('\n Starting the miner...');
brewcrypt.minePendingTransactions(address2);
console.log('\nBalance of second address is', brewcrypt.getBalanceOfAddress(address2));
console.log('\n Is chain valid?: ', brewcrypt.isChainValid());
}
console.log('\n\n Blockchain is:', brewcrypt.getAllBlocks());
console.log('\n-----------------------------');
所以我正在学习区块链,我想知道这个(非常基本的)区块链是否被视为点对点?只是看看这些块如何使用哈希检查先前的块,以及这是否会被认为是一种真正基本形式的点对点?如果没有,你能向我解释一下什么是点对点区块链吗?
这是不是点对点但是区块链。区块链通常用于 网络 节点,因此要使这个简单的区块链点对点,您只需创建一个系统,无论何时创建新块,它都会中继到所有节点在那个网络中。此外,无论何时创建交易,都应将其中继到网络中的所有节点,以便它可以包含在下一个块中。
// Import modules
const SHA512 = require("crypto-js/sha512");
var readline = require('readline-sync');
//Transactions are important so miners don't continuously mine blocks
and spam the blockchain (proof of work)
class Transaction {
constructor(fromAddress, toAddress, amount){
this.fromAddress = fromAddress;
this.toAddress = toAddress;
this.amount = amount;
}
}
class Block {
constructor(timestamp, transactions, previousHash = '') {
this.previousHash = previousHash;
this.timestamp = timestamp;
this.transactions = transactions;
this.hash = this.calculateHash();
this.nonce = 0;
}
calculateHash() {
return SHA512(this.previousHash + this.timestamp +
JSON.stringify(this.transactions) + this.nonce).toString();
}
mineBlock(difficulty) {
while (this.hash.substring(0, difficulty) !== Array(difficulty
+ 1).join("0")) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log("BLOCK MINED: " + this.hash);
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.difficulty = 3;
this.pendingTransactions = [];
this.miningReward = 10;
}
createGenesisBlock() {
return new Block(Date.now(), [], "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
getAllBlocks() {
return this.chain;
}
minePendingTransactions(miningRewardAddress){
let block = new Block(Date.now(), this.pendingTransactions,
this.getLatestBlock().hash);
block.mineBlock(this.difficulty);
console.log('Block successfully mined!');
this.chain.push(block);
this.pendingTransactions = [
new Transaction(address1, miningRewardAddress,
this.miningReward)
];
}
createTransaction(transaction){
this.pendingTransactions.push(transaction);
}
getBalanceOfAddress(address){
let balance = 0;
for(const block of this.chain){
for(const trans of block.transactions){
if(trans.fromAddress === address){
balance -= trans.amount;
}
if(trans.toAddress === address){
balance += trans.amount;
}
}
}
return balance;
}
isChainValid() {
for (let i = 1; i < this.chain.length; i++){
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
if (currentBlock.hash !== currentBlock.calculateHash()) {
return false;
}
if (currentBlock.previousHash !== previousBlock.hash) {
return false;
}
}
return true;
}
}
let brewcrypt = new Blockchain();
console.log('\n\n Latest block is:', brewcrypt.getLatestBlock());
console.log('\n-----------------------------');
var address1 = readline.question('\n\nEnter your blockchain address - ');
var address2 = readline.question('\nEnter the address you are sending to - ');
brewcrypt.createTransaction(new Transaction(address1, address2, 10));
if(address2 == address1) {
console.log('You cannot send transactions to yourself...');
return process.exit();
}
var blockinput = readline.question('Enter amounts of transaction blocks to be mined - ');
for(i = 0; i <= blockinput; i++) {
console.log('\n Starting the miner...');
brewcrypt.minePendingTransactions(address2);
console.log('\nBalance of second address is', brewcrypt.getBalanceOfAddress(address2));
console.log('\n Is chain valid?: ', brewcrypt.isChainValid());
}
console.log('\n\n Blockchain is:', brewcrypt.getAllBlocks());
console.log('\n-----------------------------');
所以我正在学习区块链,我想知道这个(非常基本的)区块链是否被视为点对点?只是看看这些块如何使用哈希检查先前的块,以及这是否会被认为是一种真正基本形式的点对点?如果没有,你能向我解释一下什么是点对点区块链吗?
这是不是点对点但是区块链。区块链通常用于 网络 节点,因此要使这个简单的区块链点对点,您只需创建一个系统,无论何时创建新块,它都会中继到所有节点在那个网络中。此外,无论何时创建交易,都应将其中继到网络中的所有节点,以便它可以包含在下一个块中。