forked from pony-maggie/blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
217 lines (142 loc) · 4.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
)
var goblockchain = NewBlockchain()
// get ip:port/mine
func MineHandler(w http.ResponseWriter, req *http.Request) {
fmt.Println("mine handler")
type Block_ST struct {
Index int `json:index`
Message string `json:message`
Transactions []transaction `json:transactions`
Proof int `json:proof`
Previous_hash string `json:previous_hash`
}
result := Block_ST{}
var code int
code = http.StatusBadRequest
result.Message = "unsupport"
req.ParseForm()
if req.Method == "GET" {
last_block := goblockchain.last_block()
last_proof := last_block.proof
previous_hash := last_block.previous_hash
fmt.Printf("last_proof:%d\n", last_proof)
fmt.Printf("previous_hash:%s\n", previous_hash)
proof := goblockchain.proof_of_work(last_proof)
var trans_reward transaction
trans_reward.Sender = "0"
trans_reward.Recipient = "random address"
trans_reward.Amount = 1
block := goblockchain.New_Block(proof, previous_hash)
code = http.StatusOK
result.Message = "New Block Forged"
result.Index = block.index
result.Proof = block.proof
result.Transactions = block.transactions
result.Previous_hash = block.previous_hash
}
bytes, _ := json.Marshal(result)
w.WriteHeader(code)
fmt.Fprintf(w, string(bytes))
}
//post ip:port/transactions/new
func NewTransactionHandler(w http.ResponseWriter, req *http.Request) {
fmt.Println("new transactions handler")
type Transaction_ST struct {
Sender string `json:sender`
Recipient string `json:recipient`
Amount int `json:amount`
}
type ResponseJsonBean struct {
Message string `json:"message"`
}
transaction := Transaction_ST{}
result := ResponseJsonBean{}
var code int
code = http.StatusBadRequest
result.Message = "unsupport"
req.ParseForm()
b, _ := ioutil.ReadAll(req.Body)
fmt.Printf("body:%s\n", b)
if req.Method == "POST" {
err := json.Unmarshal([]byte(b), &transaction)
if err != nil {
fmt.Println(err.Error())
code = http.StatusInternalServerError
result.Message = "json unparse failed"
} else {
fmt.Printf("%+v\n", transaction)
index := goblockchain.New_Transaction(transaction.Sender, transaction.Recipient, transaction.Amount)
code = http.StatusCreated
result.Message = fmt.Sprintf("New nodes have been added to Block %d", index)
}
}
bytes, _ := json.Marshal(result)
w.WriteHeader(code)
fmt.Fprintf(w, string(bytes))
}
//get ip:port/chain
func ChainHandler(w http.ResponseWriter, req *http.Request) {
fmt.Println("chain transaction handler")
}
//post ip:port/nodes/register
func RegisterNodesHandler(w http.ResponseWriter, req *http.Request) {
fmt.Println("nodes register handler")
type Nodes_ST struct {
Nodes []string
}
type ResponseJsonBean struct {
Message string `json:"message"`
Data []string `json:"total_nodes"`
}
nodeGroup := Nodes_ST{}
result := ResponseJsonBean{}
var code int
code = http.StatusBadRequest
result.Message = "unsupport"
req.ParseForm()
b, _ := ioutil.ReadAll(req.Body)
fmt.Printf("body:%s\n", b)
if req.Method == "POST" {
err := json.Unmarshal([]byte(b), &nodeGroup)
if err != nil {
fmt.Println(err.Error())
code = http.StatusInternalServerError
result.Message = "json unparse failed"
} else {
result.Data = make([]string, 0)
fmt.Printf("%+v\n", nodeGroup)
for _, node := range nodeGroup.Nodes {
fmt.Printf("node:%s\n", node)
goblockchain.Register_Node(node)
result.Data = append(result.Data, node)
}
code = http.StatusCreated
result.Message = "New nodes have been added"
}
}
bytes, _ := json.Marshal(result)
w.WriteHeader(code)
fmt.Fprintf(w, string(bytes))
}
//get ip:port/nodes/resolve
func ConsensusHandler(w http.ResponseWriter, req *http.Request) {
fmt.Println("nodes resolve register")
}
func main() {
port := flag.String("port", "8001", "use -port <port number>")
flag.Parse()
fmt.Printf("port is:%s\n", *port)
http.HandleFunc("/mine", MineHandler)
http.HandleFunc("/transactions/new", NewTransactionHandler)
http.HandleFunc("/nodes/register", RegisterNodesHandler)
http.HandleFunc("/nodes/resolve", ConsensusHandler)
http.HandleFunc("/chain", ChainHandler)
http.ListenAndServe(fmt.Sprintf(":%s", *port), nil)
}