-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
167 lines (142 loc) · 4.83 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"encoding/json"
"fmt"
"net"
"os"
"github.com/google/go-cmp/cmp"
)
// startBCServer turn on the Blockchain network server.
func startBCServer(bc *Blockchain) {
cfg := getNetworkCfg()
listener, err := net.Listen("tcp", cfg.Network.LocalNode.Address)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer listener.Close()
go closeDB(bc) //@@@ Maybe this function is not needed anymore!
Info.Println("Local Node listening on port: " + cfg.Network.LocalNode.Address)
for {
conn, err := listener.Accept()
if err != nil {
Error.Println("Error accept connection: ", err.Error())
os.Exit(1)
}
// Create new go-routines to store the new node's connection requests.
go handleReq(conn, bc)
}
}
// handleReq handles all cases of incoming message's command from any connected node.
func handleReq(conn net.Conn, bc *Blockchain) {
buf := make([]byte, 1024)
len, err := conn.Read(buf)
if err != nil {
Error.Println("Error read data: ", err.Error())
return
}
msg := new(Message)
err = json.Unmarshal(buf[:len], msg)
if err != nil {
Error.Println("Error unmarshal:", err.Error())
return
}
// msg := deserializeMsg(buf[:len])
Info.Printf("Handle command %s request from port: %s\n", msg.Cmd, conn.RemoteAddr())
switch msg.Cmd {
case CFwHashList:
handleReqFwHash(conn, bc, msg)
case CReqDepth:
handleReqDepth(conn, bc)
case CReqBlock:
handleReqBlock(conn, bc, msg)
case CReqHeader:
handleReqHeader(conn, bc, msg)
case CReqAddr:
handleReqAddr(conn, msg)
case CReqPrf:
handleReqPrf(conn, bc, msg)
case CPrintChain:
handlePrintChain(bc)
case CAddBlock:
handleAddBlock(conn, bc, []Transaction{}) // NOTE: not use anymore!
case CAddTx:
handleAddTx(conn, bc, msg)
default:
Info.Printf("Command message is invalid!\n")
}
conn.Close()
}
func handleReqPrf(conn net.Conn, bc *Blockchain, msg *Message) {
prf := msg.Data
if isValid := bc.ValidatePrf(prf); isValid {
resMsg := createMsgResPrf(isValid)
conn.Write(resMsg.Serialize())
}
Error.Print("Integrity verification given block failed!")
}
// handleReqFwHash handles request forwards hashes list to all neighbor node.
func handleReqFwHash(conn net.Conn, bc *Blockchain, msg *Message) {
Info.Printf("BlockChain detected modification. Starting synchronize chain...")
reqConnectBC(msg.Source, bc)
}
// handleReqDepth handles the request asking for the others node's depth (blockchain)
// for the synchronizing in the local node.
// Response with the message of the other node's depth.
func handleReqDepth(conn net.Conn, bc *Blockchain) {
resMsg := createMsgResDepth(bc.GetDepth())
conn.Write(resMsg.Serialize())
}
// handleReqBlock handles the request of pulling block after checking the neighbor node's depth.
// Response with the block was missing and sync it into the local node.
func handleReqBlock(conn net.Conn, bc *Blockchain, msg *Message) {
reqDepth := Bytestoi(msg.Data)
block := bc.GetBlockByDepth(reqDepth)
resMsg := createMsgResBlock(block)
conn.Write(resMsg.Serialize())
}
// handleReqHeader handles the header identical validation block between local and neighbor node.
func handleReqHeader(conn net.Conn, bc *Blockchain, msg *Message) {
neighborHeader := deserializeHeader(msg.Data)
localBlock := bc.GetBlockByDepth(neighborHeader.Depth)
result := cmp.Equal(*neighborHeader, localBlock.Header)
resMsg := createMsgResHeader(result)
conn.Write(resMsg.Serialize())
}
// handleReqAddr handles the request of fetch node's address.
func handleReqAddr(conn net.Conn, msg *Message) {
resMsg := createMsgResAddr()
conn.Write(resMsg.Serialize())
Info.Printf("Wallet address : %s", getNetworkCfg().WJson.Address)
}
// handlePrintChain handles the request of printing the chain's values in string format.
func handlePrintChain(bc *Blockchain) {
Info.Printf("%v", bc.Stringify())
}
// NOTE: now instead of adding block -> adding a blank transaction to the latest block.
// handleAddBlock handles the request of adding new block to the chain.
func handleAddBlock(conn net.Conn, bc *Blockchain, txs []Transaction) {
block := newBlock(txs, bc.GetLatestHash(), bc.GetDepth()+1)
bc.AddBlock(block)
fwHashes(bc)
}
// handleAddTx handles the request to add a transaction into a block.
func handleAddTx(conn net.Conn, bc *Blockchain, msg *Message) {
var isSuccess bool
tx := DeserializeTx(msg.Data)
Info.Printf("Receiving new transaction: %x", tx)
isSuccess = bc.VerifyTx(tx)
if isSuccess {
Info.Println("Transaction validation succeeded => Create new block!")
toAddr := getWallet().Address
Info.Printf("Indicating coinbase transaction to an address: %s", toAddr)
coinbaseTx := newCoinBaseTx(toAddr)
nBlock := newBlock([]Transaction{*tx, *coinbaseTx}, bc.GetLatestHash(), bc.GetDepth()+1)
bc.AddBlock(nBlock)
fwHashes(bc)
} else {
Info.Println("Invalid transaction!")
}
resMsg := createMsgResAddTx(isSuccess)
conn.Write(resMsg.Serialize())
}