Skip to content

Commit

Permalink
Refactored code to use locks when accessing shared data and removed d…
Browse files Browse the repository at this point in the history
…uplicate pins from the list.
  • Loading branch information
nathansenn committed Aug 21, 2023
1 parent ebe6fea commit caa7406
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
12 changes: 9 additions & 3 deletions Rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ func RunProofs() error {
//fmt.Println("Running proofs")
//fmt.Println("length of localdata.PeerNames: " + strconv.Itoa(len(localdata.PeerNames)))
//fmt.Println("Length of ThreeSpeakVideos: " + strconv.Itoa(len(localdata.ThreeSpeakVideos)))
for _, cid := range localdata.ThreeSpeakVideos {
localdata.Lock.Lock()
threeSpeak := localdata.ThreeSpeakVideos
localdata.Lock.Unlock()
for _, cid := range threeSpeak {
fmt.Println("Running proofs for CID: " + cid)
for _, peer := range localdata.PeerNames {
//fmt.Println("Running proofs for peer: " + peer)
localdata.Lock.Lock()
peerNames := localdata.PeerNames
localdata.Lock.Unlock()
for _, peer := range peerNames {
fmt.Println("Running proof for peer: " + peer)
isPinnedInDB := ipfs.IsPinnedInDB(cid)
if isPinnedInDB == true {
//fmt.Println("Running proofs for peer: " + peer)
Expand Down
25 changes: 22 additions & 3 deletions messaging/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ func HandleRequestProof(req Request) {
CID := req.CID
hash := req.Hash
if ipfs.IsPinned(CID) == true {
fmt.Println("Sending proof of access to validation node")
validationHash := validation.CreatProofHash(hash, CID)
SendProof(req, validationHash, hash, localdata.NodeName)
} else {
fmt.Println("Sending proof of access to validation node")
SendProof(req, hash, req.Seed, localdata.NodeName)
}

Expand Down Expand Up @@ -474,12 +474,31 @@ func SyncNode(req Request) {
log.Println("Pins data:", req.Pins)
return
}
// Lock to safely read from shared data
localdata.Lock.Lock()
allPins := localdata.PeerCids[req.User]
localdata.Lock.Unlock()
for _, value := range pins {
allPins = append(allPins, value)

// Create a map to use as a set for unique values
uniquePins := make(map[string]struct{})

// Populate the map with the existing pins
for _, pin := range allPins {
uniquePins[pin] = struct{}{}
}

// Add new pins to the map, automatically removing duplicates
for _, pin := range pins {
uniquePins[pin] = struct{}{}
}

// Convert the map keys back into a slice
allPins = make([]string, 0, len(uniquePins))
for pin := range uniquePins {
allPins = append(allPins, pin)
}

// Lock to safely write to shared data
localdata.Lock.Lock()
localdata.PeerCids[req.User] = allPins
localdata.PeerSyncSeed[req.Seed] = localdata.PeerSyncSeed[req.Seed] + 1
Expand Down

0 comments on commit caa7406

Please sign in to comment.