-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_v2.go
326 lines (268 loc) · 7.09 KB
/
main_v2.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
// TODO: Signatures and transactions
// TODO: Merkle Tree
// TODO: Prrof of Work
// TODO: Listen for network transactions
// TODO: Replacement of chain refreshes proof of work mining
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io"
"log"
"net"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/davecgh/go-spew/spew" // pretty print slices
"github.com/gorilla/mux"
"github.com/joho/godotenv" // read .env
)
type Block struct {
Index int
Timestamp string
BPM int
Hash string
PrevHash string
}
type Message struct {
BPM int
}
var Blockchain []Block
var bcServer chan []Block //bsServer (a.k.a Blockchain Server) is a channel that handles incoming concurrent Blockchains (which will be broadcast to the other nodes)
var mutex = &sync.Mutex{}
/*
Generates the hash of a block
*/
func calculateHash(block Block) string {
// Convert data to string
record := string(block.Index) + block.Timestamp + string(block.BPM) + block.PrevHash
// Create new sha256 instance
h := sha256.New()
// Cast string to bytes and write to hashing algorithm as input
h.Write([]byte(record))
// Convert input into 256 byte output
hashed := h.Sum(nil)
// Convert output into hex string
return hex.EncodeToString(hashed)
}
/*
Generates a new block using the previous block
*/
func generateBlock(previousBlock Block, BPM int) (Block, error) {
// Create a new block with 0 values
var newBlock Block
// Get the time of creating the new block
t := time.Now()
// Set values of the new block
newBlock.Index = previousBlock.Index + 1
newBlock.Timestamp = t.String()
newBlock.BPM = BPM
newBlock.PrevHash = previousBlock.Hash
// Using previously set values, get the hash of the new block
newBlock.Hash = calculateHash(newBlock)
// Done!
return newBlock, nil
}
/*
Determines if a block is valid given the previous block
*/
func isBlockValid(newBlock, oldBlock Block) bool {
if oldBlock.Index+1 != newBlock.Index {
return false
}
if oldBlock.Hash != newBlock.PrevHash {
return false
}
if calculateHash(newBlock) != newBlock.Hash {
return false
}
return true
}
/*
Sets the current chain to the longest chain
*/
func replaceChain(newBlocks []Block) {
if len(newBlocks) > len(Blockchain) {
Blockchain = newBlocks
}
}
/*
Returms an object that conforms to http.Handler interface
*/
func makeMuxRouter() http.Handler {
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", handleGetBlockchain).Methods("GET")
muxRouter.HandleFunc("/", handleWriteBlock).Methods("POST")
return muxRouter
}
/*
Read from the entire blockchain
*/
func handleGetBlockchain(w http.ResponseWriter, r *http.Request) {
bytes, err := json.MarshalIndent(Blockchain, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
io.WriteString(w, string(bytes))
}
/*
Write a block to the blockchain
*/
func handleWriteBlock(w http.ResponseWriter, r *http.Request) {
var m Message
// Create a json decoder using request Body text
decoder := json.NewDecoder(r.Body)
// Determine if there is an error with the request body
// Attempt to decode into 'm' Message
if err := decoder.Decode(&m); err != nil {
respondWithJSON(w, r, http.StatusBadRequest, r.Body)
return
}
defer r.Body.Close() // The request is OK. Data placed into 'm' Message
// Create a new block from the HTTP request
newBlock, err := generateBlock(Blockchain[len(Blockchain)-1], m.BPM)
if err != nil {
respondWithJSON(w, r, http.StatusInternalServerError, m)
return
}
// Determine if the block is valid
if isBlockValid(newBlock, Blockchain[len(Blockchain)-1]) {
// Append to current blockchain
newBlockchain := append(Blockchain, newBlock)
// Attempt to replace the chain (if a longer one already exists then don't do it)
replaceChain(newBlockchain)
// Pretty print latest Blockchain
spew.Dump(Blockchain)
}
respondWithJSON(w, r, http.StatusCreated, newBlock)
}
/*
Send out actual message to requester
*/
func respondWithJSON(w http.ResponseWriter, r *http.Request, code int, payload interface{}) {
response, err := json.MarshalIndent(payload, "", " ")
// Failed to convert payload into string
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("HTTP 500: Internal Server Error"))
return
}
// Write the code and the reponse
w.WriteHeader(code)
w.Write(response)
}
func run() error {
// Create a router
mux := makeMuxRouter()
httpAddr := os.Getenv("ADDR")
log.Println("Listening on port ", httpAddr)
// Get a pointer to the server (ensure you are modifying the same server and not making copies)
s := &http.Server{
Addr: ":" + httpAddr,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
return err
}
return nil
}
/*
Handles TCP Connections
*/
func handleTCPConn(conn net.Conn) {
// Make sure to close the connection once the function finishes
defer conn.Close()
println("New connection established")
// Scan input
scanner := bufio.NewScanner(conn)
// Generate blocks in a goroutine
go func() {
for scanner.Scan() {
// Ask for BPM from the user
io.WriteString(conn, "Enter a new BPM:")
// Convert text to number
bpm, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Printf("%v is not a number: %v", scanner.Text(), err)
continue
}
// Create a new block with the data
newBlock, err := generateBlock(Blockchain[len(Blockchain)-1], bpm)
if err != nil {
log.Println(err)
continue
}
// Check for validity
if isBlockValid(newBlock, Blockchain[len(Blockchain)-1]) {
newBlockchain := append(Blockchain, newBlock)
replaceChain(newBlockchain)
}
// Put the new blockchain in the channel (bcServer)
bcServer <- Blockchain
}
}()
// Simulate Receiving Broadcast (Client)
go func() {
for {
// Every 30 seconds, print out the current blockchain
time.Sleep(30 * time.Second)
mutex.Lock()
output, err := json.Marshal(Blockchain)
if err != nil {
log.Fatal(err)
}
mutex.Unlock()
io.WriteString(conn, "\n"+string(output)+"\n")
}
}()
// Print out recieved blockchains (Server)
for _ = range bcServer {
println("Server recieved: ")
spew.Dump(Blockchain)
}
println("Connection closed")
}
/*
Listens for TCP requests on Port #XXXX
Calls handleTCPConn() when a request is recieved
*/
func startTCPServer() {
// Listen for TCP packets at Port #XXXX
server, err := net.Listen("tcp", ":"+os.Getenv("ADDR"))
if err != nil {
log.Fatal(err)
}
defer server.Close()
// Create connection once we hear a request
// Infinite loop blocks defer server.Close()
for {
conn, err := server.Accept()
if err != nil {
log.Fatal(err)
}
go handleTCPConn(conn)
}
}
func main() {
// Attempt to read environment variables
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
// Make (channel of blockchains)
bcServer = make(chan []Block)
// Create a genesis block
t := time.Now()
genesisBlock := Block{0, t.String(), 0, "", ""}
spew.Dump(genesisBlock)
Blockchain = append(Blockchain, genesisBlock)
startTCPServer()
}