Skip to content

Commit caa7406

Browse files
committed
Refactored code to use locks when accessing shared data and removed duplicate pins from the list.
1 parent ebe6fea commit caa7406

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

Rewards/rewards.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ func RunProofs() error {
7272
//fmt.Println("Running proofs")
7373
//fmt.Println("length of localdata.PeerNames: " + strconv.Itoa(len(localdata.PeerNames)))
7474
//fmt.Println("Length of ThreeSpeakVideos: " + strconv.Itoa(len(localdata.ThreeSpeakVideos)))
75-
for _, cid := range localdata.ThreeSpeakVideos {
75+
localdata.Lock.Lock()
76+
threeSpeak := localdata.ThreeSpeakVideos
77+
localdata.Lock.Unlock()
78+
for _, cid := range threeSpeak {
7679
fmt.Println("Running proofs for CID: " + cid)
77-
for _, peer := range localdata.PeerNames {
78-
//fmt.Println("Running proofs for peer: " + peer)
80+
localdata.Lock.Lock()
81+
peerNames := localdata.PeerNames
82+
localdata.Lock.Unlock()
83+
for _, peer := range peerNames {
84+
fmt.Println("Running proof for peer: " + peer)
7985
isPinnedInDB := ipfs.IsPinnedInDB(cid)
8086
if isPinnedInDB == true {
8187
//fmt.Println("Running proofs for peer: " + peer)

messaging/messaging.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ func HandleRequestProof(req Request) {
161161
CID := req.CID
162162
hash := req.Hash
163163
if ipfs.IsPinned(CID) == true {
164+
fmt.Println("Sending proof of access to validation node")
164165
validationHash := validation.CreatProofHash(hash, CID)
165166
SendProof(req, validationHash, hash, localdata.NodeName)
166167
} else {
167-
fmt.Println("Sending proof of access to validation node")
168168
SendProof(req, hash, req.Seed, localdata.NodeName)
169169
}
170170

@@ -474,12 +474,31 @@ func SyncNode(req Request) {
474474
log.Println("Pins data:", req.Pins)
475475
return
476476
}
477+
// Lock to safely read from shared data
477478
localdata.Lock.Lock()
478479
allPins := localdata.PeerCids[req.User]
479480
localdata.Lock.Unlock()
480-
for _, value := range pins {
481-
allPins = append(allPins, value)
481+
482+
// Create a map to use as a set for unique values
483+
uniquePins := make(map[string]struct{})
484+
485+
// Populate the map with the existing pins
486+
for _, pin := range allPins {
487+
uniquePins[pin] = struct{}{}
482488
}
489+
490+
// Add new pins to the map, automatically removing duplicates
491+
for _, pin := range pins {
492+
uniquePins[pin] = struct{}{}
493+
}
494+
495+
// Convert the map keys back into a slice
496+
allPins = make([]string, 0, len(uniquePins))
497+
for pin := range uniquePins {
498+
allPins = append(allPins, pin)
499+
}
500+
501+
// Lock to safely write to shared data
483502
localdata.Lock.Lock()
484503
localdata.PeerCids[req.User] = allPins
485504
localdata.PeerSyncSeed[req.Seed] = localdata.PeerSyncSeed[req.Seed] + 1

0 commit comments

Comments
 (0)