-
Notifications
You must be signed in to change notification settings - Fork 9
/
myblockchain.js
121 lines (104 loc) · 2.97 KB
/
myblockchain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 从node.js的crypto-js库中导入SHA256函数
const SHA256 = require('crypto-js/sha256');
// 定义当前日期
var dt = new Date();
var timestamp = dt.toString();
/**
* 构造区块对象
*/
class Block {
/**
* 区块对象默认构造函数
* @param index [区块索引值]
* @param timestamp [时间戳]
* @param data [区块交易数据]
* @param previousHash [之前区块的哈希]
*/
constructor(index, timestamp, data, previousHash) {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash();
this.nonce = 0;
}
/**
* 计算区块哈希
* @return [返回区块的哈希值]
*/
calculateHash() {
return SHA256(this.index + this.previousHash + this.timestamp + this.data + this.nonce).toString();
}
/**
* 挖矿
* @param difficulty [挖矿的难度值]
*/
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.createGenesis()];
this.difficulty = 4;
}
/**
* 创建创世区块
* @return [返回创世区块]
*/
createGenesis() {
return new Block(0, timestamp, "Genesis block", "0");
}
/**
* 获取最新区块
* @return [返回最新的区块]
*/
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
/**
* 增加新区块
* @param newBlock [新区块]
*/
addBlock(newBlock){
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.mineBlock(this.difficulty);
this.chain.push(newBlock);
}
/**
* 检查区块链的有效性
* @return [true代表区块没有被篡改,false代表区块被篡改]
*/
checkValid() {
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 testChain = new Blockchain();
console.log("Mining block...");
testChain.addBlock(new Block(1, timestamp, "This is block 1"));
console.log("Mining block...");
testChain.addBlock(new Block(2, timestamp, "This is block 2"));
console.log(JSON.stringify(testChain, null, 4));
//检查区块链数据是否被篡改
console.log("Is blockchain valid?" + testChain.checkValid().toString());