基于Java 实现区块链的工作量证明机制
1. 区块加入随机值
public class Block {
private Object data;
private String preHash;
private String hash;
private Long nonce;
public Block(Object data, String preHash) {
this.data = data;
this.preHash = preHash;
this.hash = this.computeHash();
this.nonce = 1L;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getPreHash() {
return preHash;
}
public void setPreHash(String preHash) {
this.preHash = preHash;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public Long getNonce() {
return nonce;
}
public void setNonce(Long nonce) {
this.nonce = nonce;
}
public String computeHash() {
return new Encrypt().SHA256(this.data + this.preHash + this.nonce);
}
public String getAnswer(Integer difficulty) {
String answer = "";
for (int i = 0; i < difficulty; i++) {
answer +="0";
}
return answer;
}
public void mine(Integer difficulty) {
while (true) {
this.hash = this.computeHash();
if (! this.hash.substring(0,difficulty).equals(this.getAnswer(difficulty))) {
this.nonce ++ ;
this.hash =this.computeHash();
} else {
break;
}
}
System.out.println("挖矿结束:"+ this.hash);
}
@Override
public String toString() {
return "Block{" +
"data=" + data +
", preHash='" + preHash + '\'' +
", hash='" + hash + '\'' +
", nonce=" + nonce +
'}';
}
public static void main(String[] args) {
Block block = new Block("转账10元","1234");
System.out.println(block);
}
}
2. 链对象添加 工作量难度系数
import java.util.ArrayList;
import java.util.List;
public class Chain {
private List<Block> chain = new ArrayList<>();
private Integer difficulty;
public Chain() {
chain.add(bigBang());
this.difficulty = 5;
}
public Chain(List<Block> chain) {
chain.add(bigBang());
this.chain.addAll(chain);
}
public Block bigBang() {
return new Block("祖先","");
}
public Block getLatestBlock() {
return getChain().get(this.chain.size()-1);
}
public void addBlockToChain(Block newBlock) {
newBlock.setPreHash(this.getLatestBlock().computeHash());
newBlock.setHash(newBlock.computeHash());
newBlock.mine(this.difficulty);
this.chain.add(newBlock);
}
public List<Block> getChain() {
return chain;
}
public void setChain(List<Block> chain) {
this.chain = chain;
}
public Boolean validateBlockChain() {
if (this.chain.size() == 1) {
if (! this.chain.get(0).getHash().equalsIgnoreCase(this.chain.get(0).computeHash())) {
return false;
}
return true;
} else {
for (int i = 1; i <= this.chain.size() -1; i++) {
Block blockValidate = this.chain.get(i);
if (!blockValidate.getHash().equalsIgnoreCase(blockValidate.computeHash())) {
System.out.println("数据被篡改!");
return false;
}
Block previousBlock = this.chain.get(i-1);
if (!blockValidate.getPreHash().equalsIgnoreCase(previousBlock.getHash())) {
System.out.println("前后区块链接断裂!");
return false;
}
}
return true;
}
}
@Override
public String toString() {
return "Chain{" +
"chain=" + chain +
'}';
}
public static void main(String[] args) {
Chain chain = new Chain();
Block block1 = new Block("转账10元","1234");
chain.addBlockToChain(block1);
Block block2 = new Block("转账20元","1234");
chain.addBlockToChain(block2);
System.out.println(chain);
chain.getChain().get(1).setData("转账30元");
chain.getChain().get(1).mine(5);
System.out.println(chain);
System.out.println(chain.validateBlockChain());
}
}