forked from Jeiwan/blockchain_go
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.go
147 lines (124 loc) · 3.78 KB
/
server.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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"net"
"os"
"github.com/google/go-cmp/cmp"
)
func startServer(bc *Blockchain) {
config = getConfig()
l, err := net.Listen("tcp", config.Nw.LocalNode.Address)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer l.Close()
Info.Println("Node listening on " + config.Nw.LocalNode.Address)
for {
conn, err := l.Accept()
if err != nil {
Error.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go handleRequest(conn, bc)
}
}
func handleRequest(conn net.Conn, bc *Blockchain) {
buf := make([]byte, 1024)
length, err := conn.Read(buf)
if err != nil {
Error.Println("Error reading:", err.Error())
return
}
m := new(Message)
err = json.Unmarshal(buf[:length], m)
if err != nil {
Error.Println("Error unmarshal:", err.Error())
return
}
Info.Printf("Handle command %s request from : %s\n", m.Cmd, conn.RemoteAddr())
switch m.Cmd {
case CmdReqBestHeight:
handleReqBestHeight(conn, bc)
case CmdReqBlock:
handleReqBlock(conn, bc, m)
case CmdPrintBlockchain:
handlePrintBlockchain(bc)
case CmdSpreadHashList:
handleSpreadHashList(conn, bc, m)
case CmdReqHeaderValidation:
handleReqHeaderValidation(conn, bc, m)
case CmdReqAddress:
handleReqAddress(conn, m)
case CmdReqPrintAllAddressInfo:
handleReqPrintAllAddressInfo(conn, bc, m)
case CmdReqAddTransaction:
handleReqAddTransaction(conn, bc, m)
default:
Info.Printf("Message command invalid\n")
}
conn.Close()
}
func handleReqBestHeight(conn net.Conn, bc *Blockchain) {
responseMs := createMsReponseBestHeight(bc.getBestHeight())
conn.Write(responseMs.serialize())
}
func handleReqBlock(conn net.Conn, bc *Blockchain, m *Message) {
requestBlockHeight := bytesToInt(m.Data)
block := bc.getBlockByHeight(requestBlockHeight)
responseMs := createMsResponseBlock(block)
conn.Write(responseMs.serialize())
}
func handlePrintBlockchain(bc *Blockchain) {
Info.Printf("\n%s", bc)
}
func handleReqAddTransaction(conn net.Conn, bc *Blockchain, m *Message) {
var isSuccess bool
tx := deserializeTransaction(m.Data)
Info.Printf("Receive tx : %s", tx)
isSuccess = bc.verifyTransaction(tx)
if isSuccess == true {
Info.Printf("Transaction is valid. Create new block.")
Info.Printf("Append coinbase transaction to address %s.", getWallet().Address)
coinbaseTx := newCoinbaseTx(getWallet().Address)
newBlock := newBlock([]Transaction{*tx, *coinbaseTx}, bc.getTopBlockHash(), bc.getBestHeight()+1)
bc.addBlock(newBlock)
spreadHashList(bc)
} else {
Info.Printf("Transaction is invalid. Nothing happened.")
}
responseMs := createMsResponseAddTransaction(isSuccess)
conn.Write(responseMs.serialize())
}
func handleSpreadHashList(conn net.Conn, bc *Blockchain, m *Message) {
Info.Printf("Blockchain's change detected. Start sync.")
sendRequestBc(m.Source, bc)
}
func handleReqHeaderValidation(conn net.Conn, bc *Blockchain, m *Message) {
oppHeader := deserializeHeader(m.Data)
myBlock := bc.getBlockByHeight(oppHeader.Height)
result := cmp.Equal(*oppHeader, myBlock.Header)
responseMs := createMsResponseHeaderValidation(result)
conn.Write(responseMs.serialize())
}
func handleReqAddress(conn net.Conn, m *Message) {
responseMs := createMsResponseAddress()
conn.Write(responseMs.serialize())
Info.Printf("My Address : %s", getConfig().SWallet.Address)
}
func handleReqPrintAllAddressInfo(conn net.Conn, bc *Blockchain, m *Message) {
UTXOSet := UTXOSet{bc}
UTXOSet.Reindex()
addressInfos := UTXOSet.getAllAddressInfo()
Info.Print(" All address information ")
Info.Printf("| Address | Value |")
for pubKeyHashAsStr, val := range addressInfos {
pubKeyHash, err := hex.DecodeString(pubKeyHashAsStr)
if err != nil {
Error.Fatal(err.Error())
}
Info.Printf("| %s | %5d |", generateAddress(pubKeyHash), val)
}
}