Skip to content

Commit 7880519

Browse files
committed
处理
1 parent 793d41e commit 7880519

File tree

8 files changed

+150
-77
lines changed

8 files changed

+150
-77
lines changed

Diff for: src/main/java/com/uifuture/springbootblockchain/block/Blockchain.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public static Blockchain createBlockchain(String address) {
6666
String lastBlockHash = RocksDBUtils.getInstance().getLastBlockHash();
6767
if (StringUtils.isBlank(lastBlockHash)) {
6868
// 创建 coinBase 交易,创世奖励
69-
String genesisCoinbaseData = "The Times " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss SSS") + " Chancellor on brink of second bailout for banks";
69+
// String genesisCoinbaseData = "The Times " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss SSS") + " Chancellor on brink of second bailout for banks";
70+
String genesisCoinbaseData = "创世区块时间: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss SSS") + " 去中心化区块链的开始";
7071
Transaction coinbaseTX = Transaction.newCoinbaseTX(address, genesisCoinbaseData);
7172
Block genesisBlock = Block.newGenesisBlock(coinbaseTX);
7273
lastBlockHash = genesisBlock.getHash();
@@ -337,4 +338,4 @@ public Block next() {
337338
}
338339
}
339340

340-
}
341+
}

Diff for: src/main/java/com/uifuture/springbootblockchain/cli/CLI.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ private void send(String from, String to, int amount) throws Exception {
266266
*/
267267
private void help() {
268268
System.out.println("Usage:");
269-
System.out.println(" createwallet - Generates a new key-pair and saves it into the wallet file");
270-
System.out.println(" printaddresses - print all wallet address");
271-
System.out.println(" getbalance -address ADDRESS - Get balance of ADDRESS");
269+
System.out.println(" createwallet - 生成一个新的密钥对并将其保存到钱包文件中");
270+
System.out.println(" printaddresses - 打印所有钱包地址");
271+
System.out.println(" getbalance -address ADDRESS - 获取地址的余额");
272272
System.out.println(" createblockchain -address ADDRESS - 创建一个区块链,并向指定地址发送创世奖励");
273-
System.out.println(" printchain - Print all the blocks of the blockchain");
273+
System.out.println(" printchain - 打印区块链的所有块");
274274
System.out.println(" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO");
275275
System.exit(0);
276276
}

Diff for: src/main/java/com/uifuture/springbootblockchain/controller/IndexController.java

+77-61
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
*/
55
package com.uifuture.springbootblockchain.controller;
66

7+
import com.uifuture.springbootblockchain.block.Block;
8+
import com.uifuture.springbootblockchain.block.Blockchain;
9+
import org.json.JSONObject;
710
import org.springframework.stereotype.Controller;
811
import org.springframework.web.bind.annotation.RequestMapping;
912

13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
import java.io.BufferedReader;
1016
import java.io.IOException;
17+
import java.io.PrintWriter;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
1122

1223
/**
1324
* @author chenhx
@@ -23,68 +34,73 @@ public class IndexController {
2334
* @param resp
2435
* @throws IOException
2536
*/
26-
// @RequestMapping("chain")
27-
// public void chain(HttpServletRequest req, HttpServletResponse resp) throws IOException {
28-
// Blockchain blockChain = Blockchain.initBlockchainFromDB();
29-
// Map<String, Object> response = new HashMap<String, Object>();
30-
// response.put("chain", blockChain.getChain());
31-
// response.put("length", blockChain.getChain().size());
32-
//
33-
// JSONObject jsonResponse = new JSONObject(response);
34-
// resp.setContentType("application/json");
35-
// PrintWriter printWriter = resp.getWriter();
36-
// printWriter.println(jsonResponse);
37-
// printWriter.close();
38-
// }
39-
//
40-
// /**
41-
// * 该Servlet用于接收并处理新的交易信息
42-
// * @param req
43-
// * @param resp
44-
// * @throws IOException
45-
// */
46-
// @RequestMapping("transactions/new")
47-
// public void transactionsNew(HttpServletRequest req, HttpServletResponse resp) throws IOException {
48-
//
49-
// req.setCharacterEncoding("utf-8");
50-
// // 读取客户端传递过来的数据并转换成JSON格式
51-
// BufferedReader reader = req.getReader();
52-
// String input = null;
53-
// StringBuffer requestBody = new StringBuffer();
54-
// while ((input = reader.readLine()) != null) {
55-
// requestBody.append(input);
56-
// }
57-
// JSONObject jsonValues = new JSONObject(requestBody.toString());
58-
//
59-
// // 检查所需要的字段是否位于POST的data中
60-
// String[] required = { "sender", "recipient", "amount" };
61-
// for (String string : required) {
62-
// if (!jsonValues.has(string)) {
63-
// // 如果没有需要的字段就返回错误信息
64-
// resp.sendError(400, "Missing values");
65-
// }
66-
// }
67-
//
68-
// // 新建交易信息
37+
@RequestMapping("chain")
38+
public void chain(HttpServletRequest req, HttpServletResponse resp) throws IOException {
39+
Blockchain blockChain = Blockchain.initBlockchainFromDB();
40+
Map<String, Object> response = new HashMap<String, Object>();
41+
List<Block> blocks = new ArrayList<>();
42+
while (blockChain.getBlockchainIterator().hashNext()){
43+
blocks.add(blockChain.getBlockchainIterator().next());
44+
}
45+
response.put("blocks", blocks);
46+
response.put("length", blockChain.getAllBlockHash().size());
47+
48+
JSONObject jsonResponse = new JSONObject(response);
49+
resp.setContentType("application/json");
50+
PrintWriter printWriter = resp.getWriter();
51+
printWriter.println(jsonResponse);
52+
printWriter.close();
53+
}
54+
55+
/**
56+
* 该Servlet用于接收并处理新的交易信息
57+
* @param req
58+
* @param resp
59+
* @throws IOException
60+
*/
61+
@RequestMapping("transactions/new")
62+
public void transactionsNew(HttpServletRequest req, HttpServletResponse resp) throws IOException {
63+
64+
req.setCharacterEncoding("utf-8");
65+
// 读取客户端传递过来的数据并转换成JSON格式
66+
BufferedReader reader = req.getReader();
67+
String input = null;
68+
StringBuffer requestBody = new StringBuffer();
69+
while ((input = reader.readLine()) != null) {
70+
requestBody.append(input);
71+
}
72+
JSONObject jsonValues = new JSONObject(requestBody.toString());
73+
74+
// 检查所需要的字段是否位于POST的data中
75+
String[] required = { "sender", "recipient", "amount" };
76+
for (String string : required) {
77+
if (!jsonValues.has(string)) {
78+
// 如果没有需要的字段就返回错误信息
79+
resp.sendError(400, "Missing values");
80+
}
81+
}
82+
83+
// 新建交易信息
6984
// Blockchain blockChain = Blockchain.getInstance();
7085
// int index = blockChain.newTransactions(jsonValues.getString("sender"), jsonValues.getString("recipient"),
7186
// jsonValues.getLong("amount"));
72-
//
73-
// // 返回json格式的数据给客户端
74-
// resp.setContentType("application/json");
75-
// PrintWriter printWriter = resp.getWriter();
76-
// printWriter.println(new JSONObject().append("message", "Transaction will be added to Block " + index));
77-
// printWriter.close();
78-
// }
79-
//
80-
// /**
81-
// * 该Servlet用于运行工作算法的证明来获得下一个证明,也就是所谓的挖矿
82-
// * @param req
83-
// * @param resp
84-
// * @throws IOException
85-
// */
86-
// @RequestMapping("mine")
87-
// public void mine(HttpServletRequest req, HttpServletResponse resp) throws IOException {
87+
int index = 1;
88+
89+
// 返回json格式的数据给客户端
90+
resp.setContentType("application/json");
91+
PrintWriter printWriter = resp.getWriter();
92+
printWriter.println(new JSONObject().append("message", "Transaction will be added to Block " + index));
93+
printWriter.close();
94+
}
95+
96+
/**
97+
* 该Servlet用于运行工作算法的证明来获得下一个证明,也就是所谓的挖矿
98+
* @param req
99+
* @param resp
100+
* @throws IOException
101+
*/
102+
@RequestMapping("mine")
103+
public void mine(HttpServletRequest req, HttpServletResponse resp) throws IOException {
88104
// Blockchain blockChain = Blockchain.getInstance();
89105
// Map<String, Object> lastBlock = blockChain.lastBlock();
90106
// long lastProof = Long.parseLong(lastBlock.get("proof") + "");
@@ -108,6 +124,6 @@ public class IndexController {
108124
// PrintWriter printWriter = resp.getWriter();
109125
// printWriter.println(new JSONObject(response));
110126
// printWriter.close();
111-
// }
127+
}
112128

113-
}
129+
}

Diff for: src/main/java/com/uifuture/springbootblockchain/transaction/TXInput.java

+38-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
* @version TXInput.java, v 0.1 2018-10-15 下午 6:23
1919
*/
2020
@Data
21-
@AllArgsConstructor
22-
@NoArgsConstructor
2321
public class TXInput {
2422

2523
/**
2624
* 交易Id的hash值
2725
* 包含了它所指向的UTXO的交易的Hash值。
2826
*/
2927
private byte[] txId;
28+
private String txIdStr;
3029
/**
3130
* 交易输出索引
3231
* 定义了它所指向的UTXO在上一笔交易中交易输出数组的位置。
@@ -36,10 +35,22 @@ public class TXInput {
3635
* 签名
3736
*/
3837
private byte[] signature;
38+
private String signatureStr;
3939
/**
4040
* 公钥
4141
*/
4242
private byte[] pubKey;
43+
private String pubKeyStr;
44+
45+
public TXInput() {
46+
}
47+
48+
public TXInput(byte[] txId, int txOutputIndex, byte[] signature, byte[] pubKey) {
49+
this.txId = txId;
50+
this.txOutputIndex = txOutputIndex;
51+
this.signature = signature;
52+
this.pubKey = pubKey;
53+
}
4354

4455
/**
4556
* 检查公钥hash是否用于交易输入
@@ -52,4 +63,29 @@ public boolean usesKey(byte[] pubKeyHash) {
5263
return Arrays.equals(lockingHash, pubKeyHash);
5364
}
5465

66+
public String getTxIdStr() {
67+
if(txId!=null){
68+
txIdStr = Arrays.toString(txId);
69+
}
70+
return txIdStr;
71+
}
72+
73+
public String getSignatureStr() {
74+
if(signatureStr!=null){
75+
signatureStr = Arrays.toString(signature);
76+
}
77+
return signatureStr;
78+
}
79+
80+
public String getPubKeyStr() {
81+
if(pubKey!=null){
82+
pubKeyStr = Arrays.toString(pubKey);
83+
}
84+
return pubKeyStr;
85+
}
86+
87+
public static void main(String[] args) {
88+
byte[] pubKey = {84, 104, 101, 32, 84, 105, 109, 101, 115, 32, 50, 48, 50, 50, 45, 48, 54, 45, 48, 49, 32, 49, 55, 58, 49, 48, 58, 48, 55, 32, 51, 54, 52, 32, 67, 104, 97, 110, 99, 101, 108, 108, 111, 114, 32, 111, 110, 32, 98, 114, 105, 110, 107, 32, 111, 102, 32, 115, 101, 99, 111, 110, 100, 32, 98, 97, 105, 108, 111, 117, 116, 32, 102, 111, 114, 32, 98, 97, 110, 107, 115};
89+
System.out.println(new String(pubKey));
90+
}
5591
}

Diff for: src/main/java/com/uifuture/springbootblockchain/transaction/Transaction.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.bouncycastle.math.ec.ECPoint;
2828

2929
import java.math.BigInteger;
30+
import java.nio.charset.StandardCharsets;
3031
import java.security.KeyFactory;
3132
import java.security.PublicKey;
3233
import java.security.Security;
@@ -79,7 +80,7 @@ public static Transaction newCoinbaseTX(String to, String data) {
7980
data = String.format("Reward to '%s'", to);
8081
}
8182
// 创建交易输入
82-
TXInput txInput = new TXInput(new byte[]{}, -1, null, data.getBytes());
83+
TXInput txInput = new TXInput(new byte[]{}, -1, null, data.getBytes(StandardCharsets.UTF_8));
8384
//获取当前区块大小
8485
Integer size = RocksDBUtils.getInstance().getChainstateBucket().size();
8586
Integer multiple = size / OUT_PUT_VALUE + 1;
@@ -117,7 +118,7 @@ public static Transaction newRewardTX(String to, Blockchain blockchain) throws D
117118

118119
String data = String.format("Reward to '%s'", to);
119120
// 创建交易输入
120-
TXInput txInput = new TXInput(new byte[]{}, -1, null, data.getBytes());
121+
TXInput txInput = new TXInput(new byte[]{}, -1, null, data.getBytes(StandardCharsets.UTF_8));
121122

122123
// 创建交易输出
123124
TXOutput txOutput = TXOutput.newTXOutput(value, to);

Diff for: src/main/java/com/uifuture/springbootblockchain/wallet/WalletUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.ObjectInputStream;
2626
import java.io.ObjectOutputStream;
2727
import java.io.Serializable;
28+
import java.nio.charset.StandardCharsets;
2829
import java.util.Map;
2930
import java.util.Set;
3031

@@ -48,7 +49,7 @@ public class WalletUtils {
4849
/**
4950
* 密文
5051
*/
51-
private static final byte[] CIPHER_TEXT = "g8ZcuSjpysW60rqAQRotiD9men7bJVEz".getBytes();
52+
private static final byte[] CIPHER_TEXT = "g8ZcuSjpysW60rqAQRotiD9men7bJVEz".getBytes(StandardCharsets.UTF_8);
5253
/**
5354
* 钱包工具实例
5455
*/

Diff for: src/test/java/com/uifuture/blockchainspringboot/BlockchainTest.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ public static void main(String[] args) {
1818
// 1AycK28gqDpWJ4e6oo2oqG9Smr43K3QcTW 219999765 179999060
1919
// 15J63sETC6WuoVKZbtKxbWXsiA9W6THRAv 180000235 540000705
2020
try {
21-
String[] argss = {"createwallet"};
22-
// String[] argss = {"createblockchain", "-address", "1AycK28gqDpWJ4e6oo2oqG9Smr43K3QcTW"};
23-
//获取地址
21+
//创建钱包
22+
// String[] argss = {"createwallet"}; //18:35:21.926 [main] INFO com.uifuture.springbootblockchain.cli.CLI - wallet address : 16vdpva8tMNJXLjxafmbD7ZEwB3xoZ58AZ
23+
//创建区块链
24+
String[] argss = {"createblockchain", "-address", "16vdpva8tMNJXLjxafmbD7ZEwB3xoZ58AZ"};
25+
// 打印所有钱包地址
2426
// String[] argss = {"printaddresses"};
2527
//获取钱包余额
26-
// String[] argss = {"getbalance", "-address", "15J63sETC6WuoVKZbtKxbWXsiA9W6THRAv"};
28+
// String[] argss = {"getbalance", "-address", "164mY79vVpcPhB8mjXmCLvjTmceazLfTTF"};
29+
//交易
2730
// String[] argss = {"send", "-from", "1AycK28gqDpWJ4e6oo2oqG9Smr43K3QcTW"
2831
// , "-to", "15J63sETC6WuoVKZbtKxbWXsiA9W6THRAv", "-amount", "180000235"};
2932
//打印链
3033
// String[] argss = {"printchain"};
31-
// String[] argss = {"mining", "-address", "1AycK28gqDpWJ4e6oo2oqG9Smr43K3QcTW"};
34+
//进行挖区块
35+
// String[] argss = {"mining", "-address", "164mY79vVpcPhB8mjXmCLvjTmceazLfTTF"};
36+
//帮助
37+
// String[] argss = {"h"};
3238
CLI cli = new CLI(argss);
3339
cli.parse();
3440
} catch (Exception e) {

Diff for: target/classes/application.properties

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Redis\u914D\u7F6E start
2+
spring.redis.host=127.0.0.1
3+
spring.redis.port=6379
4+
spring.redis.password=
5+
minNumberOfActiveConnections=5
6+
maxReadIdleSeconds=120
7+
keepAlivePeriodSeconds=15
8+
pingTimeoutSeconds=5
9+
pingTTL=7
10+
autoDiscoveryPingFrequency=10
11+
leaderElectionTimeoutSeconds=5
12+
leaderRejectionTimeoutSeconds=10

0 commit comments

Comments
 (0)