Skip to content

Commit

Permalink
negentropy: fix the two bugs @hoytech found.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 21, 2024
1 parent 4d63672 commit 7fd28f6
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions nip77/negentropy/negentropy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package negentropy
import (
"fmt"
"math"
"slices"
"strings"
"unsafe"

Expand Down Expand Up @@ -159,22 +158,22 @@ func (n *Negentropy) reconcileAux(reader *StringHexReader) (string, error) {
}

// what they have
theirItems := make([]string, 0, numIds)
theirItems := make(map[string]struct{}, numIds)
for i := 0; i < numIds; i++ {
if id, err := reader.ReadString(64); err != nil {
return "", fmt.Errorf("failed to read id (#%d/%d) in list: %w", i, numIds, err)
} else {
theirItems = append(theirItems, id)
theirItems[id] = struct{}{}
}
}

// what we have
for _, item := range n.storage.Range(lower, upper) {
id := item.ID

if idx, theyHave := slices.BinarySearch(theirItems, id); theyHave {
if _, theyHave := theirItems[id]; theyHave {
// if we have and they have, ignore
theirItems[idx] = ""
delete(theirItems, id)
} else {
// if we have and they don't, notify client
if n.isClient {
Expand All @@ -185,11 +184,9 @@ func (n *Negentropy) reconcileAux(reader *StringHexReader) (string, error) {

if n.isClient {
// notify client of what they have and we don't
for _, id := range theirItems {
for id := range theirItems {
// skip empty strings here because those were marked to be excluded as such in the previous step
if id != "" {
n.HaveNots <- id
}
n.HaveNots <- id
}

// client got list of ids, it's done, skip
Expand Down Expand Up @@ -227,7 +224,7 @@ func (n *Negentropy) reconcileAux(reader *StringHexReader) (string, error) {
return "", fmt.Errorf("unexpected mode %d", mode)
}

if n.frameSizeLimit-200 <= fullOutput.Len()/2+partialOutput.Len()/2 {
if n.frameSizeLimit-200 < fullOutput.Len()/2+partialOutput.Len()/2 {
// frame size limit exceeded, handle by encoding a boundary and fingerprint for the remaining range
remainingFingerprint := n.storage.Fingerprint(upper, n.storage.Size())
n.writeBound(fullOutput, InfiniteBound)
Expand Down

0 comments on commit 7fd28f6

Please sign in to comment.