区块链学习:nodejs 实现一个基本区块链
区块链是分布式数据存储、点对点传输、共识机制、加密算法等计算机技术的新型应用模式。所谓共识机制是区块链系统中实现不同节点之间建立信任、获取权益的数学算法 。
区块链(Blockchain)是比特币的一个重要概念,它本质上是一个去中心化的数据库,同时作为比特币的底层技术。区块链是一串使用密码学方法相关联产生的数据块,每一个数据块中包含了一次比特币网络交易的信息,用于验证其信息的有效性(防伪)和生成下一个区块。
下面是我学习区块链基本原理写的代码片段:
Git 地址 https://gitee.com/wolfx/nodejs-blockchain/blob/master/blockchain.js
js
const CryptoJS = require("crypto-js");
class Block {
/**
* @param {*} index
* first block:0
* @param {*} previousHash
* explicity determines the previous block
* @param {*} timestamp
* @param {*} data
* anything that the finder of the block wants to include in the blockchain
* @param {*} hash
* taken from the content of the block
*/
constructor(index, previousHash, timestamp, data, hash) {
this.index = index;
this.previousHash = previousHash.toString();
this.timestamp = timestamp;
this.data = data;
this.hash = hash.toString();
}
}
/**
* 散列单元
* @param {*} index
* @param {*} previousHash
* explicity determines the previous block
* @param {*} timestamp
* @param {*} data
* anything that the finder of the block wants to include in the blockchain
*/
const calculateHash = (index, previousHash, timestamp, data) => {
return CryptoJS.SHA256(index + previousHash + timestamp + data).toString();
};
/**
* 计算散列
* @param {*} block
*/
const calculateHashForBlock = (block) => {
return calculateHash(block.index, block.previousHash, block.timestamp, block.data);
};
const getGenesisBlock = () => {
return new Block(
0,
"0",
1465154705,
"my genesis block!!",
"816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7"
);
};
// 创世纪块
const blockchain = [getGenesisBlock()];
const getLatestBlock = () => blockchain[blockchain.length - 1];
/**
* 产生单元
* @param {*} blockData
*/
const generateNextBlock = (blockData) => {
const previousBlock = getLatestBlock();
const nextIndex = previousBlock.index + 1;
const nextTimestamp = new Date().getTime() / 1000;
const nextHash = calculateHash(nextIndex, previousBlock.hash, nextTimestamp, blockData);
return new Block(nextIndex, previousBlock.hash, nextTimestamp, blockData, nextHash);
};
/**
* 确认块的完整性
* @param {*} newBlock
* @param {*} previousBlock
*/
const isValidNewBlock = (newBlock, previousBlock) => {
if (previousBlock.index + 1 !== newBlock.index) {
console.log("invalid index");
return false;
} else if (previousBlock.hash !== newBlock.previousHash) {
console.log("invalid previoushash");
return false;
} else if (calculateHashForBlock(newBlock) !== newBlock.hash) {
console.log(typeof newBlock.hash + " " + typeof calculateHashForBlock(newBlock));
console.log("invalid hash: " + calculateHashForBlock(newBlock) + " " + newBlock.hash);
return false;
}
return true;
};
/**
*
* @param {*} newBlock
*/
const addBlock = (newBlock) => {
if (isValidNewBlock(newBlock, getLatestBlock())) {
blockchain.push(newBlock);
}
};
console.log(blockchain);
addBlock(generateNextBlock("测试"));
console.log("-----------");
console.log(blockchain);