2018/12/04 14:01

# 认识区块链

hash是对区块信息的摘要存储，hash的好处是任意长度的信息经过hash都可以映射成固定长度的字符串，如可用sha256：

calculateHash() {
return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
}


## Block的数据结构

Block的最基本数据结构如下：

class Block {
constructor(timestamp, data, previousHash = '') {
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
//对hash的计算必须放在最后，保证所有数据赋值正确后再计算
this.hash = this.calculateHash();
}

calculateHash() {
return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
}
}


## BlockChain的数据结构

class BlockChain {
constructor() {
this.chain = [];
}
}


## 创世区块

createGenesisBlock() {
return new Block("2018-11-11 00:00:00", "Genesis block of simple chain", "");
}


class BlockChain {
constructor() {
this.chain = [this.createGenesisBlock()];
}
}


## 添加区块

class BlockChain {
getLatestBlock() {
return this.chain[this.chain.length - 1];
}

//新区块的前一个hash值是现有区块链的最后一个区块的hash值；
newBlock.previousHash = this.getLatestBlock().hash;
//重新计算新区块的hash值（因为指定了previousHash)；
newBlock.hash = newBlock.calculateHash();
//把新区块加入到链中；
this.chain.push(newBlock);
}
...
}


## 校验区块链

isChainValid() {
//遍历所有区块
for (let i = 1; i < this.chain.length; i++) {
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
//重新计算当前区块的hash值，若发现hash值对不上，说明该区块有数据被篡改，hash值未重新计算
if (currentBlock.hash !== currentBlock.calculateHash()) {
console.error("hash not equal: " + JSON.stringify(currentBlock));
return false;
}
//判断当前区块的previousHash是否真的等于前一个区块的hash，若不等，说明前一个区块被篡改，虽然hash值被重新计算正确，但是后续区块的hash值并未重新计算，导致整个链断裂
if (currentBlock.previousHash !== previousBlock.calculateHash) {
console.error("previous hash not right: " + JSON.stringify(currentBlock));
return false;
}
}
return true;
}


## Just run it

let simpleChain = new BlockChain();

console.log(JSON.stringify(simpleChain, null, 4));

console.log("is the chain valid? " + simpleChain.isChainValid());


ali-186590cc4a7f:simple-chain shanyao$node main_1.js { "chain": [ { "timestamp": "2018-11-11 00:00:00", "data": "Genesis block of simple chain", "previousHash": "", "hash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89" }, { "timestamp": "2018-11-11 00:00:01", "data": { "amount": 10 }, "previousHash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89", "hash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529" }, { "timestamp": "2018-11-11 00:00:02", "data": { "amount": 20 }, "previousHash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529", "hash": "274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34" } ] } is the chain valid? true  注意看其中的previousHash与hash，确实是当前区块的previousHash指向前一个区块的hash。 ## 篡改下试试 都说区块链不可篡改，是真的吗？让我们篡改第2个区块试试，如： let simpleChain = new BlockChain(); simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10})); simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20})); console.log("is the chain valid? " + simpleChain.isChainValid()); //将第2个区块的数据，由10改为15 simpleChain.chain[1].data = {amount: 15}; console.log("is the chain still valid? " + simpleChain.isChainValid()); console.log(JSON.stringify(simpleChain, null, 4));  结果如下： ali-186590cc4a7f:simple-chain shanyao$ node main_1.js
is the chain valid? true
hash not equal: {"timestamp":"2018-11-11 00:00:01","data":{"amount":15},"previousHash":"fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89","hash":"150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529"}
is the chain still valid? false
{
"chain": [
{
"timestamp": "2018-11-11 00:00:00",
"data": "Genesis block of simple chain",
"previousHash": "",
"hash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89"
},
{
"timestamp": "2018-11-11 00:00:01",
"data": {
"amount": 15
},
"previousHash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89",
"hash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529"
},
{
"timestamp": "2018-11-11 00:00:02",
"data": {
"amount": 20
},
"previousHash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529",
"hash": "274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34"
}
]
}



## 再篡改下试试

let simpleChain = new BlockChain();

console.log("is the chain valid? " + simpleChain.isChainValid());
//篡改后重新计算hash值
simpleChain.chain[1].data = {amount: 15};
simpleChain.chain[1].hash = simpleChain.chain[1].calculateHash();
console.log("is the chain still valid? " + simpleChain.isChainValid());
console.log(JSON.stringify(simpleChain, null, 4));


ali-186590cc4a7f:simple-chain shanyao\$ node main_1.js
is the chain valid? true
previous hash not right: {"timestamp":"2018-11-11 00:00:02","data":{"amount":20},"previousHash":"150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529","hash":"274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34"}
is the chain still valid? false
{
"chain": [
{
"timestamp": "2018-11-11 00:00:00",
"data": "Genesis block of simple chain",
"previousHash": "",
"hash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89"
},
{
"timestamp": "2018-11-11 00:00:01",
"data": {
"amount": 15
},
"previousHash": "fd56967ff621a4090ff71ce88fdd456547d1c92d2e93766b7e8791f7a5f91f89",
"hash": "74d139274fb692495b7c805dd5822faa0c5b5e6058b6beef96e87e18ab83a6b1"
},
{
"timestamp": "2018-11-11 00:00:02",
"data": {
"amount": 20
},
"previousHash": "150b196268a0152e9f0e719ac131a722472a809f49bd507965029a78c7400529",
"hash": "274a7a13ed20118e8cb745654934a7e24a4d59333ba17dfbf5d4cfe0fa8a6e34"
}
]
}



## 是真的无法篡改吗

let simpleChain = new BlockChain();

console.log("is the chain valid? " + simpleChain.isChainValid());
//篡改第2个区块
simpleChain.chain[1].data = {amount: 15};
simpleChain.chain[1].hash = simpleChain.chain[1].calculateHash();
//并把第3个区块也重新计算
simpleChain.chain[2].previousHash = simpleChain.chain[1].hash;
simpleChain.chain[2].hash = simpleChain.chain[2].calculateHash();
console.log("is the chain still valid? " + simpleChain.isChainValid());
console.log(JSON.stringify(simpleChain, null, 4

0
0 收藏

0 评论
0 收藏
0