This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
770 lines (609 loc) · 16.3 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"math"
"math/big"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gorilla/mux"
"github.com/spf13/pflag"
)
/***********
* Structs *
***********/
type Block struct {
Index int
Timestamp string
Data string
Hash string
PrevHash string
Nonce int
}
type ProofOfWork struct {
block *Block
target *big.Int
}
type Message struct {
Data string
}
type Hello struct {
AddrFrom string
Purpose string
}
type Addr struct {
AddrFrom string
AddrList []string
}
type GetChain struct {
AddrFrom string
}
type Chain struct {
AddrFrom string
BlockChain []Block
}
type CheckBlock struct {
AddrFrom string
Block Block
}
/*************
* Variables *
*************/
var (
nodeAddress string
httpPort int
KnownNodes []string
Blockchain []Block
mutex = &sync.Mutex{}
maxNonce = math.MaxInt64
ValidChan = make(chan bool)
jsonflag = false
broadcastflag = true
tmpls = template.Must(template.ParseFiles("web/index.html"))
hostFlag = pflag.StringP("host", "h", GetOutboundIP(), "binding host")
portFlag = pflag.IntP("port", "p", 4444, "binding port")
httpPortFlag = pflag.IntP("webport", "w", 8000, "web server port")
)
const (
targetBits = 18 // difficulty setting
printedLength = 8 // printedLength is the total prefix length of a public key associated to a chat users ID.
commandLength = 12
protocol = "tcp"
layout = "2006-01-02 15:04:05"
)
/*************
* Functions *
*************/
func main() {
go StartServer()
log.Fatal(run())
}
/*****************
* P2P Functions *
*****************/
func StartServer() {
pflag.Parse()
nodeAddress = fmt.Sprintf("%s:%d", *hostFlag, *portFlag)
fmt.Printf("You are %s\n", nodeAddress)
// add self to KnownNodes
KnownNodes = append(KnownNodes, nodeAddress)
// Node Discovery begins
if len(pflag.Args()) == 1 {
peer := pflag.Args()[0]
// send hello request to connecting peer, asking for peer's address list
fmt.Printf("Connecting to %s.\n", peer)
SendHello(peer, "neighbour")
} else if len(pflag.Args()) == 0 {
fmt.Println("No peers to connect to, generating blockchain")
StartBlockchain()
} else {
fmt.Println("Too many arguments! Select a peer.")
os.Exit(1)
}
ln, err := net.Listen(protocol, nodeAddress)
if err != nil {
log.Panic(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
log.Panic(err)
}
go HandleConnection(conn)
}
}
/* GetOutboundIP is to find ip of host for p2p discovery */
func GetOutboundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
/* HandleConnection is the main handler for any connection to the host */
func HandleConnection(conn net.Conn) {
req, err := ioutil.ReadAll(conn)
defer conn.Close()
if err != nil {
log.Panic(err)
}
command := BytesToCmd(req[:commandLength])
fmt.Printf("Received %s command\n", command)
switch command {
case "hello":
HandleHello(req)
case "addr":
HandleAddr(req)
case "getchain":
HandleGetChain(req)
case "chain":
HandleChain(req)
case "checkblock":
HandleCheckBlock(req)
default:
fmt.Println("Unknown command")
}
}
/* HandleHello handles hello requests which does node discovery.
* Neighbour is used when a new node does its first communication with a known node to request for their address list.
* Ping is used to communicate to a node in peer's address list and waits for a pong.
* Pong is used to confirm a ping to start getting a peer's chain. */
func HandleHello(request []byte) {
var buff bytes.Buffer
var payload Hello
buff.Write(request[commandLength:])
dec := gob.NewDecoder(&buff)
err := dec.Decode(&payload)
if err != nil {
log.Panic(err)
}
switch payload.Purpose {
case "neighbour":
KnownNodes = append(KnownNodes, payload.AddrFrom)
fmt.Println(KnownNodes)
SendAddr(payload.AddrFrom)
case "ping":
if !NodeIsKnown(payload.AddrFrom) {
KnownNodes = append(KnownNodes, payload.AddrFrom)
}
fmt.Println(KnownNodes)
SendHello(payload.AddrFrom, "pong")
case "pong":
if broadcastflag {
broadcastflag = false
RequestChain()
}
default:
fmt.Println("Unknown Hello Purpose")
}
}
/* HandleAddr handles Addr request which is a slice of known nodes from a peer.
* These nodes will be appended to known nodes.
* Hello ping requests will be sent to newly discovered nodes */
func HandleAddr(request []byte) {
var buff bytes.Buffer
var payload Addr
buff.Write(request[commandLength:])
dec := gob.NewDecoder(&buff)
err := dec.Decode(&payload)
if err != nil {
log.Panic(err)
}
// append nodes to KnownNodes
for _, ip := range payload.AddrList {
if !NodeIsKnown(ip) {
KnownNodes = append(KnownNodes, ip)
}
}
// send hello ping requests to newly discovered nodes
for _, node := range KnownNodes {
if node != nodeAddress {
SendHello(node, "ping")
}
}
}
/* HandleGetChain handles GetChain requests which is a request for the host's blockchain.
* host will call SendChain.*/
func HandleGetChain(request []byte) {
var buff bytes.Buffer
var payload GetChain
buff.Write(request[commandLength:])
dec := gob.NewDecoder(&buff)
err := dec.Decode(&payload)
if err != nil {
log.Panic(err)
}
SendChain(payload.AddrFrom)
}
/* HandleChain handles chain requests which compares payload blockchain and its own blockchain.
* The longer chain will be the current blockchain.
* jsonflag used to signal isValid in HandleWriteBlock to update web server. */
func HandleChain(request []byte) {
var buff bytes.Buffer
var payload Chain
buff.Write(request[commandLength:])
dec := gob.NewDecoder(&buff)
err := dec.Decode(&payload)
if err != nil {
log.Panic(err)
}
// replace BlockChain if payload chain is longer
if len(payload.BlockChain) > len(Blockchain) {
fmt.Println("Longer chain detected, replacing current chain")
Blockchain = payload.BlockChain
spew.Dump(Blockchain)
if jsonflag {
ValidChan <- true
}
} else {
fmt.Println("Discarded new chain")
if jsonflag {
ValidChan <- false
}
}
}
/* HandleCheckBlock handles CheckBlock requests which validates the sent block.
* Valid blocks will be appended to host's blockchain.
* host's chain will then be sent back to the sender. */
func HandleCheckBlock(request []byte) {
var buff bytes.Buffer
var payload CheckBlock
buff.Write(request[commandLength:])
dec := gob.NewDecoder(&buff)
err := dec.Decode(&payload)
if err != nil {
log.Panic(err)
}
mutex.Lock()
pow := NewProofOfWork(&payload.Block)
if pow.Validate() {
fmt.Println("Block is valid")
Blockchain = append(Blockchain, payload.Block)
spew.Dump(Blockchain)
} else {
fmt.Println("Block not valid")
}
mutex.Unlock()
// Send Chain to sender
SendChain(payload.AddrFrom)
}
/* SendHello sends hello requests for node discovery.
* Neighbour is used when a new node does its first communication with a known node to request for their address list.
* Ping is used to communicate to a node in peer's address list and waits for a pong.
* Pong is used to confirm a ping to start getting a peer's chain. */
func SendHello(address, purpose string) {
fmt.Printf("Sending hello %s to %s.\n", purpose, address)
data := Hello{nodeAddress, purpose}
payload := GobEncode(data)
request := append(CmdToBytes("hello"), payload...)
SendData(address, request)
}
/* SendAddr sends addr requests, sending the host's KnownNodes. */
func SendAddr(address string) {
fmt.Printf("Sending addr to %s.\n", address)
data := Addr{nodeAddress, KnownNodes}
payload := GobEncode(data)
request := append(CmdToBytes("addr"), payload...)
SendData(address, request)
}
/* SendGetChain sends getchain requests, asking the address for their blockchain. */
func SendGetChain(address string) {
fmt.Printf("Sending getchain to %s.\n", address)
data := GetChain{nodeAddress}
payload := GobEncode(data)
request := append(CmdToBytes("getchain"), payload...)
SendData(address, request)
}
/* SendChain sends chain requests consisting of host's blockchain to address. */
func SendChain(address string) {
fmt.Printf("Sending chain to %s.\n", address)
data := Chain{nodeAddress, Blockchain}
payload := GobEncode(data)
request := append(CmdToBytes("chain"), payload...)
SendData(address, request)
}
/* SendCheckBlock sends checkblock requests containing a newblock which is to be validated by address peer. */
func SendCheckBlock(address string, newBlock Block) {
fmt.Printf("Sending checkblock to %s.\n", address)
data := CheckBlock{nodeAddress, newBlock}
payload := GobEncode(data)
request := append(CmdToBytes("checkblock"), payload...)
SendData(address, request)
}
/* SendData sends data to the address.
* A connection to address is initiated. If an error occurs, address will be removed from KnownNodes. */
func SendData(address string, data []byte) {
conn, err := net.Dial(protocol, address)
if err != nil {
fmt.Printf("%s is not available\n", address)
var updatedNodes []string
for _, node := range KnownNodes {
if node != address {
updatedNodes = append(updatedNodes, node)
}
}
KnownNodes = updatedNodes
return
}
defer conn.Close()
_, err = io.Copy(conn, bytes.NewReader(data))
if err != nil {
log.Panic(err)
}
}
/* RequestChain broadcasts GetChain request to peers. */
func RequestChain() {
for _, node := range KnownNodes {
if node != nodeAddress {
SendGetChain(node)
}
}
}
func RequestCheck(newBlock Block) {
for _, address := range KnownNodes {
if address != nodeAddress{
SendCheckBlock(address, newBlock)
}
}
}
/* CmdToBytes converts a command string into a byte array of commandLength */
func CmdToBytes(cmd string) []byte {
var bytes [commandLength]byte
for i, c := range cmd {
bytes[i] = byte(c)
}
return bytes[:]
}
/* BytesToCmd converts a byte array into a command string. */
func BytesToCmd(bytes []byte) string {
var cmd []byte
for _, b := range bytes {
if b == 0x0 {
break
}
cmd = append(cmd, b)
}
return fmt.Sprintf("%s", cmd)
}
/* GobEncode converts data into a byte array */
func GobEncode(data interface{}) []byte {
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
err := enc.Encode(data)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
/* NodeIsKnown checks the ip if it is in KnownNodes. */
func NodeIsKnown(ip string) bool {
for _, node := range KnownNodes {
if node == ip {
return true
}
}
return false
}
/************************
* Blockchain Functions *
************************/
/* NewBlock generates and returns a Block */
func NewBlock(oldBlock Block, Data string) Block {
var block Block
t := time.Now()
block.Index = oldBlock.Index + 1
block.Timestamp = t.Format(layout)
block.Data = Data
block.PrevHash = oldBlock.Hash
pow := NewProofOfWork(&block)
nonce, hash := pow.RunPOW()
block.Hash = hex.EncodeToString(hash[:])
block.Nonce = nonce
return block
}
/* StartBlockchain begins the Blockchain, appending a genesis Block */
func StartBlockchain() {
t := time.Now()
genesisBlock := Block{}
genesisBlock = Block{0, t.Format(layout), "Genesis Block", "", "", 0}
pow := NewProofOfWork(&genesisBlock)
nonce, hash := pow.RunPOW()
genesisBlock.Hash = hex.EncodeToString(hash[:])
genesisBlock.Nonce = nonce
spew.Dump(genesisBlock)
mutex.Lock()
Blockchain = append(Blockchain, genesisBlock)
mutex.Unlock()
}
/* NewProofOfWork builds and returns a ProofOfWork */
func NewProofOfWork(b *Block) *ProofOfWork {
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
pow := &ProofOfWork{b, target}
return pow
}
/* prepareData formats the data into a byte array */
func (pow *ProofOfWork) prepareData(nonce int) []byte {
data := bytes.Join(
[][]byte{
[]byte(pow.block.PrevHash),
[]byte(pow.block.Data),
[]byte(pow.block.Timestamp),
IntToHex(int64(pow.block.Index)),
IntToHex(int64(targetBits)),
IntToHex(int64(nonce)),
},
[]byte{},
)
return data
}
/* RunPOW performs a proof-of-work */
func (pow *ProofOfWork) RunPOW() (int, []byte) {
var hashInt big.Int
var hash [32]byte
nonce := 0
fmt.Printf("Mining the block containing \"%s\"\n", pow.block.Data)
for nonce < maxNonce {
data := pow.prepareData(nonce)
hash = sha256.Sum256(data)
fmt.Printf("\r%x", hash)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(pow.target) == -1 {
break
} else {
nonce++
}
}
fmt.Print("\n\n")
return nonce, hash[:]
}
/* Validate validates block's Proof of Work
*
* Checks index, hash, prev hash.
*/
func (pow *ProofOfWork) Validate() bool {
var hashInt big.Int
data := pow.prepareData(pow.block.Nonce)
hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:])
prevBlock := Blockchain[len(Blockchain)-1]
if pow.block.Index != (prevBlock.Index + 1) {
return false
}
if pow.block.PrevHash != prevBlock.Hash {
return false
}
if hashInt.Cmp(pow.target) != -1 {
return false
}
return true
}
/* IntToHex converts an int64 to a byte array */
func IntToHex(num int64) []byte {
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, num)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
/************************
* Web Server Functions *
************************/
/* run will set up a http server */
func run() error {
pflag.Parse()
httpPort, _ = strconv.Atoi(fmt.Sprintf("%d", *httpPortFlag))
mux := makeMuxRouter()
log.Println(fmt.Sprintf("HTTP Server Listening on port :%d", httpPort))
s := &http.Server{
Addr: fmt.Sprintf(":%d", httpPort),
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
return err
}
return nil
}
/* Index sets up the index html page */
func Index(w http.ResponseWriter, r *http.Request) {
bytes, _ := json.MarshalIndent(Blockchain, "", " ")
data := struct {
Title string
Data string
Port string
}{
Title: "Blockchain Visualisation",
Data: strings.ReplaceAll(string(bytes), "\n", ""),
Port: strconv.Itoa(httpPort),
}
if err := tmpls.ExecuteTemplate(w, "index.html", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
/* makeMuxRouter creates and return a router handler*/
func makeMuxRouter() http.Handler {
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", handleWriteBlock).Methods("POST")
muxRouter.HandleFunc("/web/", Index)
muxRouter.PathPrefix("/web/").Handler(http.StripPrefix("/web/", http.FileServer(http.Dir("web/"))))
return muxRouter
}
/* handleWriteBlock takes the JSON payload as data input and inserts a new block */
func handleWriteBlock(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var msg Message
var isValid bool
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&msg); err != nil {
respondWithJSON(w, r, http.StatusBadRequest, r.Body)
return
}
defer r.Body.Close()
prevBlock := Blockchain[len(Blockchain)-1]
if msg.Data == "" {
respondWithJSON(w, r, http.StatusBadRequest, r.Body)
return
}
newBlock := NewBlock(prevBlock, msg.Data)
// check if there are connected peers
if len(KnownNodes) > 1 {
go RequestCheck(newBlock)
// set true to allow handleReceiveChain to send boolean to ValidChan
jsonflag = true
isValid = <-ValidChan
} else {
fmt.Println("No peers to send to, self-validating block.")
mutex.Lock()
pow := NewProofOfWork(&newBlock)
if pow.Validate() {
fmt.Println("Block is valid")
Blockchain = append(Blockchain, newBlock)
spew.Dump(Blockchain)
isValid = true
} else {
fmt.Println("Block not valid")
isValid = false
}
mutex.Unlock()
}
if isValid {
respondWithJSON(w, r, http.StatusCreated, newBlock)
}
// set to false to prevent data sent to channel
jsonflag = false
}
/* respondWithJSON writes a JSON text back to the web server */
func respondWithJSON(w http.ResponseWriter, r *http.Request, code int, payload interface{}) {
response, err := json.MarshalIndent(payload, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("HTTP 500: Internal Server Error"))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}