如图是一个虚拟货币的钱包截图,每一个人的钱包,就是一个hash值
主要为 账号 和剩余金额
增加交易
BlockChain blockChain = BlockChain.getInstance();
int index = blockChain.newTransactions(jsonValues.getString("sender"), jsonValues.getString("recipient"),
jsonValues.getLong("amount"));
A 给 B 转账 100 元,拼接到最新的区块上
创建新的区块
// 给工作量证明的节点提供奖励,发送者为 "0" 表明是新挖出的币
String uuid = (String) this.getServletContext().getAttribute("uuid");
blockChain.newTransactions("0", uuid, 1);
// 构建新的区块
Map<String, Object> newBlock = blockChain.newBlock(proof, null);
Map<String, Object> response = new HashMap<String, Object>();
response.put("message", "New Block Forged");
response.put("index", newBlock.get("index"));
response.put("transactions", newBlock.get("transactions"));
response.put("proof", newBlock.get("proof"));
response.put("previous_hash", newBlock.get("previous_hash"));
最后一个方法,查询当前区块链上所有区块和交易记录
BlockChain blockChain = BlockChain.getInstance();
Map<String, Object> response = new HashMap<String, Object>();
response.put("chain", blockChain.getChain());
response.put("length", blockChain.getChain().size());
JSONObject jsonResponse = new JSONObject(response)
一致性(共识)
一个基本的区块链可以接受交易和挖矿,区块链系统应该是分布式的。既然是分布式的,那么我们究竟拿什么保证所有节点有同样的链呢?这就是一致性问题,我们要想在网络上有多个节点,就必须实现一个一致性的算法。
注册节点
在实现一致性算法之前,我们需要找到一种方式让一个节点知道它相邻的节点。每个节点都需要保存一份包含网络中其它节点的记录。
/**
* 注册节点
*
*/
public void registerNode(String address) throws MalformedURLException {
URL url = new URL(address);
String node = url.getHost() + ":" + (url.getPort() == -1 ? url.getDefaultPort() : url.getPort());
nodes.add(node);
}
/**
* 检查是否是有效链,遍历每个区块验证hash和proof,来确定一个给定的区块链是否有效
*
* @param chain
* @return
*/
public boolean validChain(List<Map<String, Object>> chain) {
Map<String, Object> lastBlock = chain.get(0);
int currentIndex = 1;
while (currentIndex < chain.size()) {
Map<String, Object> block = chain.get(currentIndex);
System.out.println(lastBlock.toString());
System.out.println(block.toString());
System.out.println("\n-------------------------\n");
// 检查block的hash是否正确
if (!block.get("previous_hash").equals(hash(lastBlock))) {
return false;
}
lastBlock = block;
currentIndex++;
}
return true;
}
/**
* 共识算法解决冲突,使用网络中最长的链. 遍历所有的邻居节点,并用上一个方法检查链的有效性, 如果发现有效更长链,就替换掉自己的链
*
* @return 如果链被取代返回true, 否则返回false
* @throws IOException
*/
public boolean resolveConflicts() throws IOException {
Set<String> neighbours = this.nodes;
List<Map<String, Object>> newChain = null;
// 寻找最长的区块链
long maxLength = this.chain.size();
// 获取并验证网络中的所有节点的区块链
for (String node : neighbours) {
URL url = new URL("http://" + node + "/chain");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() == 200) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "utf-8"));
StringBuffer responseData = new StringBuffer();
String response = null;
while ((response = bufferedReader.readLine()) != null) {
responseData.append(response);
}
bufferedReader.close();
JSONObject jsonData = new JSONObject(bufferedReader.toString());
long length = jsonData.getLong("length");
List<Map<String, Object>> chain = (List) jsonData.getJSONArray("chain").toList();
// 检查长度是否长,链是否有效
if (length > maxLength && validChain(chain)) {
maxLength = length;
newChain = chain;
}
}
}
// 如果发现一个新的有效链比我们的长,就替换当前的链
if (newChain != null) {
this.chain = newChain;
return true;
}
return false;
}
网友评论