4
4
*/
5
5
package com .uifuture .springbootblockchain .controller ;
6
6
7
+ import com .uifuture .springbootblockchain .block .Block ;
8
+ import com .uifuture .springbootblockchain .block .Blockchain ;
9
+ import org .json .JSONObject ;
7
10
import org .springframework .stereotype .Controller ;
8
11
import org .springframework .web .bind .annotation .RequestMapping ;
9
12
13
+ import javax .servlet .http .HttpServletRequest ;
14
+ import javax .servlet .http .HttpServletResponse ;
15
+ import java .io .BufferedReader ;
10
16
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 ;
11
22
12
23
/**
13
24
* @author chenhx
@@ -23,68 +34,73 @@ public class IndexController {
23
34
* @param resp
24
35
* @throws IOException
25
36
*/
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
+ // 新建交易信息
69
84
// Blockchain blockChain = Blockchain.getInstance();
70
85
// int index = blockChain.newTransactions(jsonValues.getString("sender"), jsonValues.getString("recipient"),
71
86
// 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 {
88
104
// Blockchain blockChain = Blockchain.getInstance();
89
105
// Map<String, Object> lastBlock = blockChain.lastBlock();
90
106
// long lastProof = Long.parseLong(lastBlock.get("proof") + "");
@@ -108,6 +124,6 @@ public class IndexController {
108
124
// PrintWriter printWriter = resp.getWriter();
109
125
// printWriter.println(new JSONObject(response));
110
126
// printWriter.close();
111
- // }
127
+ }
112
128
113
- }
129
+ }
0 commit comments