Skip to content

Commit

Permalink
nip60: prevent duplicate token entries after doing some operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 30, 2025
1 parent 37db1c9 commit 78a9411
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions nip60/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"iter"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -152,12 +153,20 @@ func loadStash(

// get all pending stuff and assign them to this, then delete the pending stuff
for _, he := range wl.pendingHistory[wallet.Identifier] {
wallet.History = append(wallet.History, he)
if !slices.ContainsFunc(wallet.History, func(c HistoryEntry) bool {
return c.event.ID == he.event.ID
}) {
wallet.History = append(wallet.History, he)
}
}
delete(wl.pendingHistory, wallet.Identifier)
wallet.tokensMu.Lock()
for _, token := range wl.pendingTokens[wallet.Identifier] {
wallet.Tokens = append(wallet.Tokens, token)
if !slices.ContainsFunc(wallet.Tokens, func(c Token) bool {
return c.event.ID == token.event.ID
}) {
wallet.Tokens = append(wallet.Tokens, token)
}
}
delete(wl.pendingTokens, wallet.Identifier)
wallet.tokensMu.Unlock()
Expand Down Expand Up @@ -188,7 +197,11 @@ func loadStash(

if wallet, ok := wl.wallets[spl[2]]; ok {
wallet.tokensMu.Lock()
wallet.Tokens = append(wallet.Tokens, token)
if !slices.ContainsFunc(wallet.Tokens, func(c Token) bool {
return c.event.ID == token.event.ID
}) {
wallet.Tokens = append(wallet.Tokens, token)
}
wallet.tokensMu.Unlock()
} else {
wl.pendingTokens[spl[2]] = append(wl.pendingTokens[spl[2]], token)
Expand Down Expand Up @@ -227,7 +240,11 @@ func loadStash(
}

if wallet, ok := wl.wallets[spl[2]]; ok {
wallet.History = append(wallet.History, he)
if !slices.ContainsFunc(wallet.History, func(c HistoryEntry) bool {
return c.event.ID == he.event.ID
}) {
wallet.History = append(wallet.History, he)
}
} else {
wl.pendingHistory[spl[2]] = append(wl.pendingHistory[spl[2]], he)
}
Expand Down

0 comments on commit 78a9411

Please # to comment.