negentropy: fix the two bugs @hoytech found.

This commit is contained in:
fiatjaf
2024-09-20 22:41:49 -03:00
parent 4d63672462
commit 7fd28f6db0

View File

@@ -3,7 +3,6 @@ package negentropy
import ( import (
"fmt" "fmt"
"math" "math"
"slices"
"strings" "strings"
"unsafe" "unsafe"
@@ -159,12 +158,12 @@ func (n *Negentropy) reconcileAux(reader *StringHexReader) (string, error) {
} }
// what they have // what they have
theirItems := make([]string, 0, numIds) theirItems := make(map[string]struct{}, numIds)
for i := 0; i < numIds; i++ { for i := 0; i < numIds; i++ {
if id, err := reader.ReadString(64); err != nil { if id, err := reader.ReadString(64); err != nil {
return "", fmt.Errorf("failed to read id (#%d/%d) in list: %w", i, numIds, err) return "", fmt.Errorf("failed to read id (#%d/%d) in list: %w", i, numIds, err)
} else { } else {
theirItems = append(theirItems, id) theirItems[id] = struct{}{}
} }
} }
@@ -172,9 +171,9 @@ func (n *Negentropy) reconcileAux(reader *StringHexReader) (string, error) {
for _, item := range n.storage.Range(lower, upper) { for _, item := range n.storage.Range(lower, upper) {
id := item.ID id := item.ID
if idx, theyHave := slices.BinarySearch(theirItems, id); theyHave { if _, theyHave := theirItems[id]; theyHave {
// if we have and they have, ignore // if we have and they have, ignore
theirItems[idx] = "" delete(theirItems, id)
} else { } else {
// if we have and they don't, notify client // if we have and they don't, notify client
if n.isClient { if n.isClient {
@@ -185,11 +184,9 @@ func (n *Negentropy) reconcileAux(reader *StringHexReader) (string, error) {
if n.isClient { if n.isClient {
// notify client of what they have and we don't // 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 // 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 // client got list of ids, it's done, skip
@@ -227,7 +224,7 @@ func (n *Negentropy) reconcileAux(reader *StringHexReader) (string, error) {
return "", fmt.Errorf("unexpected mode %d", mode) 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 // frame size limit exceeded, handle by encoding a boundary and fingerprint for the remaining range
remainingFingerprint := n.storage.Fingerprint(upper, n.storage.Size()) remainingFingerprint := n.storage.Fingerprint(upper, n.storage.Size())
n.writeBound(fullOutput, InfiniteBound) n.writeBound(fullOutput, InfiniteBound)