forked from dongyuanxin/node-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
188 lines (160 loc) · 4.33 KB
/
main.js
File metadata and controls
188 lines (160 loc) · 4.33 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const CryptoJS = require('crypto-js')
/**
* 区块信息的结构化定义
*/
class Block {
/**
* 构造函数
* @param {Number} index
* @param {String} previousHash
* @param {Number} timestamp
* @param {*} data
* @param {String} hash
*/
constructor(index, previousHash, timestamp, data, hash) {
this.index = index
this.previousHash = previousHash + ''
this.timestamp = timestamp
this.data = data
this.hash = hash + ''
}
}
/**
* 区块链的结构信息定义
*/
class BlockChain {
constructor() {
this.blocks = [this.getGenesisBlock()]
}
/**
* 创建区块链起源块, 此块是硬编码
*/
getGenesisBlock() {
return new Block(0, '0', 1552801194452, 'genesis block', '810f9e854ade9bb8730d776ea02622b65c02b82ffa163ecfe4cb151a14412ed4')
}
/**
* 根据信息计算hash值
*/
calcuteHash(index, previousHash, timestamp, data) {
return CryptoJS.SHA256(index + previousHash + timestamp + data) + ''
}
/**
* 得到区块链中最后一个块节点
*/
getLatestBlock() {
return this.blocks[this.blocks.length - 1]
}
/**
* 计算当前链表的下一个区块
* @param {*} blockData
*/
generateNextBlock(blockData) {
const previousBlock = this.getLatestBlock()
const nextIndex = previousBlock.index + 1
const nextTimeStamp = new Date().getTime()
const nextHash = this.calcuteHash(nextIndex, previousBlock.hash, nextTimeStamp, blockData)
return new Block(nextIndex, previousBlock.hash, nextTimeStamp, blockData, nextHash)
}
/**
* 判断新加入的块是否合法
* @param {Block} newBlock
* @param {Block} previousBlock
*/
isValidNewBlock(newBlock, previousBlock) {
if(
!(newBlock instanceof Block) ||
!(previousBlock instanceof Block)
) {
return false
}
// 判断index
if(newBlock.index !== previousBlock.index + 1) {
return false
}
// 判断hash值
if(newBlock.previousHash !== previousBlock.hash) {
return false
}
// 计算新块的hash值是否符合规则
if(this.calcuteHash(newBlock.index, newBlock.previousHash, newBlock.timestamp, newBlock.data) !== newBlock.hash) {
return false
}
return true
}
/**
* 向区块链添加新节点
* @param {Block} newBlock
*/
addBlock(newBlock) {
if(this.isValidNewBlock(newBlock, this.getLatestBlock())) {
this.blocks.push(newBlock)
return true
}
return false
}
/**
* 判断新插入的区块链是否合法而且可以覆盖原来的节点
* @param {Array} newChain
*/
isValidNewChain(newChain) {
if(Array.isArray(newChain) === false || newChain.length === 0) {
return false
}
let newChainLength = newChain.length,
firstBlock = newChain[0]
// 硬编码的起源块不能改变
if(firstBlock.index === 0) {
return false
}
// 移植新的链的长度 <= 现有链的长度
// 新的链不可信
if(newChainLength + firstBlock.index <= this.blocks.length) {
return false
}
// 下面检查新的链能否移植
// 以及新的链的每个节点是否符合规则
if(!this.isValidNewBlock(firstBlock, this.blocks[firstBlock.index - 1])) {
return false
}
for(let i = 1; i < newChainLength; ++i) {
if(!this.isValidNewBlock(newChain[i], newChain[i - 1])) {
return false
}
}
return true
}
/**
* 插入新链表
* @param {Array} newChain
*/
addChain(newChain) {
if(this.isValidNewChain(newChain)) {
const index = newChain[0].index
this.blocks.splice(index)
this.blocks = this.blocks.concat(newChain)
return true
}
return false
}
}
module.exports = (() => {
const blockChain = new BlockChain()
const getLength = () => blockChain.blocks.length
const generateNextBlock = (data) => blockChain.generateNextBlock(data)
const addBlock = (newBlock) => blockChain.addBlock(newBlock)
const addChain = (newChain) => blockChain.addChain(newChain)
const getBlock = (index) => blockChain.blocks[index]
const checkBlock = (newBlock) => {
const previousBlock = blockChain.getLatestBlock()
return blockChain.isValidNewBlock(newBlock, previousBlock)
}
return {
getLength,
generateNextBlock,
addBlock,
addChain,
getBlock,
checkBlock,
Block
}
})()