Skip to content

Commit 277d907

Browse files
committed
Added websocket connection parameter to messaging functions to prevent panic: runtime error: invalid memory address or nil pointer dereference
1 parent e1a1a6f commit 277d907

File tree

7 files changed

+30
-38
lines changed

7 files changed

+30
-38
lines changed

api/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func handleMessaging(c *gin.Context) {
222222
fmt.Println("Error encoding JSON:", err)
223223
return
224224
}
225-
go messaging.HandleMessage(string(jsonData))
225+
go messaging.HandleMessage(string(jsonData), ws)
226226
}
227227
}
228228

api/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func performPingPong(ctx context.Context, salt string, name string, conn *websoc
7777
if messaging.Ping[salt] {
7878
return nil
7979
}
80-
messaging.SendPing(salt, name)
80+
messaging.SendPing(salt, name, conn)
8181
attempts++
8282
if attempts > maxPingAttempts {
8383
return sendWsResponseAndHandleError("Connection Error", "Could not connect to peer, try again", "0", conn, errors.New("connection error"))

connection/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func StartWsClient(name string) {
129129
isConnected = false
130130
continue
131131
}
132-
go messaging.HandleMessage(string(message))
132+
go messaging.HandleMessage(string(message), localdata.WsValidators[name])
133133
//fmt.Println("Client recv: ", string(message))
134134
} else {
135135
log.Println("Connection is not established.")

messaging/cids.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313
)
1414

15-
func RequestCIDS(req Request) {
15+
func RequestCIDS(req Request, ws *websocket.Conn) {
1616
fmt.Println("Requesting CIDS")
1717
data := map[string]string{
1818
"type": "RequestCIDS",
@@ -27,7 +27,6 @@ func RequestCIDS(req Request) {
2727
if localdata.WsPeers[req.User] == req.User && localdata.NodeType == 1 {
2828
WsMutex.Lock()
2929
fmt.Println("Locking wsMutex")
30-
ws := localdata.WsClients[req.User]
3130
ws.WriteMessage(websocket.TextMessage, jsonData)
3231
fmt.Println("Sent RequestCIDS to client")
3332
WsMutex.Unlock()
@@ -40,7 +39,7 @@ func RequestCIDS(req Request) {
4039
}
4140

4241
}
43-
func SendCIDS(name string) {
42+
func SendCIDS(name string, ws *websocket.Conn) {
4443
allPins, _ := ipfs.Shell.Pins()
4544
fmt.Println("Fetched pins")
4645
NewPins := make([]string, 0)
@@ -76,8 +75,7 @@ func SendCIDS(name string) {
7675
}
7776

7877
if localdata.UseWS == true && localdata.NodeType == 2 {
79-
ws := localdata.WsValidators[name]
80-
fmt.Println("Sending CIDS to validation node")
78+
fmt.Println("Sending CIDS to validation node", name)
8179
WsMutex.Lock()
8280
ws.WriteJSON(data)
8381
WsMutex.Unlock()

messaging/messaging.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type PinMap map[string]PinType
5050

5151
// HandleMessage
5252
// This is the function that handles the messages from the pubsub
53-
func HandleMessage(message string) {
53+
func HandleMessage(message string, ws *websocket.Conn) {
5454
fmt.Println("Message received:", message)
5555
// JSON decode message
5656
req := Request{}
@@ -70,7 +70,7 @@ func HandleMessage(message string) {
7070
fmt.Println("User: " + req.User)
7171
fmt.Println("CID: " + req.CID)
7272
fmt.Println("Pins: " + req.Pins)
73-
go HandleProofOfAccess(req)
73+
go HandleProofOfAccess(req, ws)
7474
}
7575

7676
}
@@ -79,7 +79,7 @@ func HandleMessage(message string) {
7979
if nodeType == 2 {
8080
if req.Type == TypeRequestProof {
8181
fmt.Println("Request for proof received")
82-
go HandleRequestProof(req)
82+
go HandleRequestProof(req, ws)
8383
}
8484
if req.Type == TypePingPongPong {
8585
validatorName := req.User
@@ -95,25 +95,25 @@ func HandleMessage(message string) {
9595
}
9696
if req.Type == TypePingPongPing {
9797
fmt.Println("PingPongPing received")
98-
PingPongPong(req, req.Hash, req.User)
98+
PingPongPong(req, ws)
9999
nodeStatus := localdata.NodesStatus[req.User]
100100
nodes := Nodes[req.User]
101101
fmt.Println("Node Status: " + nodeStatus)
102102
fmt.Println("Nodes: " + fmt.Sprint(nodes))
103103
if nodeType == 1 && !nodes && nodeStatus != "Synced" {
104104
fmt.Println("syncing: " + req.User)
105-
go SyncNode(req)
105+
go SyncNode(req, ws)
106106
}
107107

108108
}
109109
if req.Type == "RequestCIDS" {
110110
fmt.Println("RequestCIDS received")
111-
go SendCIDS(req.User)
111+
go SendCIDS(req.User, ws)
112112

113113
}
114114
if req.Type == "SendCIDS" {
115115
fmt.Println("SendCIDS received")
116-
go SyncNode(req)
116+
go SyncNode(req, ws)
117117

118118
}
119119
if req.Type == "Syncing" {
@@ -148,14 +148,15 @@ func PubsubHandler(ctx context.Context) {
148148
log.Error("Error reading from pubsub: ", err)
149149
continue
150150
}
151-
HandleMessage(msg)
151+
var ws *websocket.Conn
152+
HandleMessage(msg, ws)
152153
}
153154
}
154155
} else {
155156
time.Sleep(1 * time.Second)
156157
}
157158
}
158-
func SendPing(hash string, user string) {
159+
func SendPing(hash string, user string, ws *websocket.Conn) {
159160
fmt.Println("Sending Ping")
160161
data := map[string]string{
161162
"type": TypePingPongPing,
@@ -169,7 +170,6 @@ func SendPing(hash string, user string) {
169170
}
170171
localdata.PingTime[user] = time.Now()
171172
if localdata.WsPeers[user] == user && localdata.NodeType == 1 {
172-
ws := localdata.WsClients[user]
173173
WsMutex.Lock()
174174
ws.WriteMessage(websocket.TextMessage, jsonData)
175175
WsMutex.Unlock()
@@ -181,11 +181,11 @@ func SendPing(hash string, user string) {
181181
pubsub.Publish(string(jsonData), user)
182182
}
183183
}
184-
func PingPongPong(req Request, hash string, user string) {
184+
func PingPongPong(req Request, ws *websocket.Conn) {
185185
fmt.Println("Sending PingPongPong")
186186
data := map[string]string{
187187
"type": TypePingPongPong,
188-
"hash": hash,
188+
"hash": req.Hash,
189189
"user": localdata.GetNodeName(),
190190
}
191191
jsonData, err := json.Marshal(data)
@@ -194,7 +194,6 @@ func PingPongPong(req Request, hash string, user string) {
194194
return
195195
}
196196
if localdata.WsPeers[req.User] == req.User && localdata.NodeType == 1 {
197-
ws := localdata.WsClients[req.User]
198197
fmt.Println("Sending PingPongPong to client")
199198
localdata.Lock.Lock()
200199
localdata.PeerLastActive[req.User] = time.Now()
@@ -209,6 +208,6 @@ func PingPongPong(req Request, hash string, user string) {
209208
localdata.WsValidators[req.User].WriteMessage(websocket.TextMessage, jsonData)
210209
WsMutex.Unlock()
211210
} else {
212-
pubsub.Publish(string(jsonData), user)
211+
pubsub.Publish(string(jsonData), req.User)
213212
}
214213
}

messaging/proofs.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ import (
1414

1515
// HandleRequestProof
1616
// This is the function that handles the request for proof from the validation node
17-
func HandleRequestProof(req Request) {
17+
func HandleRequestProof(req Request, ws *websocket.Conn) {
1818
CID := req.CID
1919
hash := req.Hash
2020
if ipfs.IsPinnedInDB(CID) == true {
2121
fmt.Println("Sending proof of access to validation node")
2222
validationHash := validation.CreatProofHash(hash, CID)
23-
SendProof(req, validationHash, hash, localdata.NodeName)
23+
SendProof(req, validationHash, hash, localdata.NodeName, ws)
2424
} else {
2525
fmt.Println("Pin not found")
26-
SendProof(req, "NA", hash, localdata.NodeName)
26+
SendProof(req, "NA", hash, localdata.NodeName, ws)
2727
}
2828
}
2929

3030
// HandleProofOfAccess
3131
// This is the function that handles the proof of access response from the storage node
32-
func HandleProofOfAccess(req Request) {
32+
func HandleProofOfAccess(req Request, ws *websocket.Conn) {
3333
fmt.Println("Handling proof of access response from storage node")
3434
// Get the start time from the seed
3535
start := localdata.GetTime(req.Seed)
@@ -82,12 +82,12 @@ func HandleProofOfAccess(req Request) {
8282

8383
// SendProof
8484
// This is the function that sends the proof of access to the validation node
85-
func SendProof(req Request, hash string, seed string, user string) {
85+
func SendProof(req Request, validationHash string, salt string, user string, ws *websocket.Conn) {
8686
fmt.Println("Sending proof of access to validation node")
8787
data := map[string]string{
8888
"type": TypeProofOfAccess,
89-
"hash": hash,
90-
"seed": seed,
89+
"hash": validationHash,
90+
"seed": salt,
9191
"user": user,
9292
}
9393
jsonData, err := json.Marshal(data)
@@ -98,12 +98,10 @@ func SendProof(req Request, hash string, seed string, user string) {
9898
wsPeers := localdata.WsPeers[req.User]
9999
nodeType := localdata.NodeType
100100
if wsPeers == req.User && nodeType == 1 {
101-
ws := localdata.WsClients[req.User]
102101
WsMutex.Lock()
103102
ws.WriteMessage(websocket.TextMessage, jsonData)
104103
WsMutex.Unlock()
105104
} else if localdata.UseWS == true && localdata.NodeType == 2 {
106-
ws := localdata.WsValidators[req.User]
107105
WsMutex.Lock()
108106
ws.WriteMessage(websocket.TextMessage, jsonData)
109107
WsMutex.Unlock()

messaging/syncing.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"proofofaccess/pubsub"
99
)
1010

11-
func SendSyncing(req Request) {
11+
func SendSyncing(req Request, ws *websocket.Conn) {
1212
localdata.Synced = true
1313
data := map[string]string{
1414
"type": "Syncing",
@@ -21,20 +21,18 @@ func SendSyncing(req Request) {
2121
return
2222
}
2323
if localdata.WsPeers[req.User] == req.User && localdata.NodeType == 1 {
24-
ws := localdata.WsClients[req.User]
2524
WsMutex.Lock()
2625
ws.WriteMessage(websocket.TextMessage, jsonData)
2726
WsMutex.Unlock()
2827
} else if localdata.UseWS == true && localdata.NodeType == 2 {
29-
ws := localdata.WsClients[req.User]
3028
WsMutex.Lock()
3129
ws.WriteMessage(websocket.TextMessage, jsonData)
3230
WsMutex.Unlock()
3331
} else {
3432
pubsub.Publish(string(jsonData), req.User)
3533
}
3634
}
37-
func SendSynced(req Request) {
35+
func SendSynced(req Request, ws *websocket.Conn) {
3836
data := map[string]string{
3937
"type": "Synced",
4038
"user": localdata.GetNodeName(),
@@ -48,7 +46,6 @@ func SendSynced(req Request) {
4846
if localdata.WsPeers[req.User] == req.User && localdata.NodeType == 1 {
4947
fmt.Println("Sending Synced to " + req.User)
5048
WsMutex.Lock()
51-
ws := localdata.WsClients[req.User]
5249
ws.WriteMessage(websocket.TextMessage, jsonData)
5350
WsMutex.Unlock()
5451
} else if localdata.UseWS == true && localdata.NodeType == 2 {
@@ -73,15 +70,15 @@ func ReceiveSynced(req Request) {
7370
localdata.Lock.Unlock()
7471
fmt.Println("Synced with " + req.User)
7572
}
76-
func SyncNode(req Request) {
73+
func SyncNode(req Request, ws *websocket.Conn) {
7774
fmt.Println("Syncing with " + req.User)
7875
peerName := req.User
7976
localdata.Lock.Lock()
8077
Nodes[req.User] = true
8178
localdata.PeerNames = localdata.RemoveDuplicates(append(localdata.PeerNames, peerName))
8279
localdata.NodesStatus[req.User] = "Synced"
8380
localdata.Lock.Unlock()
84-
SendSynced(req)
81+
SendSynced(req, ws)
8582
fmt.Println("Synced with " + req.User)
8683
Nodes[req.User] = false
8784
}

0 commit comments

Comments
 (0)