negentropy: fix varint encoding.

This commit is contained in:
fiatjaf
2026-04-07 17:20:10 -03:00
parent 637412fd38
commit d5b54a1c91
3 changed files with 14 additions and 21 deletions
+8 -16
View File
@@ -66,13 +66,13 @@ func (bw *BoundWriter) WriteTimestamp(w *bytes.Buffer, timestamp nostr.Timestamp
bw.lastTimestampOut = timestamp bw.lastTimestampOut = timestamp
// add 1 to prevent zeroes from being read as infinites // add 1 to prevent zeroes from being read as infinites
WriteVarInt(w, int(delta+1)) WriteVarInt(w, uint64(delta)+1)
return return
} }
func (bw *BoundWriter) WriteBound(w *bytes.Buffer, bound Bound) { func (bw *BoundWriter) WriteBound(w *bytes.Buffer, bound Bound) {
bw.WriteTimestamp(w, bound.Timestamp) bw.WriteTimestamp(w, bound.Timestamp)
WriteVarInt(w, len(bound.IDPrefix)) WriteVarInt(w, uint64(len(bound.IDPrefix)))
w.Write(bound.IDPrefix) w.Write(bound.IDPrefix)
} }
@@ -111,33 +111,25 @@ func ReadVarInt(reader *bytes.Reader) (int, error) {
return res, nil return res, nil
} }
func WriteVarInt(w *bytes.Buffer, n int) { func WriteVarInt(w *bytes.Buffer, n uint64) {
if n == 0 { if n == 0 {
w.WriteByte(0) w.WriteByte(0)
return return
} }
w.Write(EncodeVarInt(n)) var buf [10]byte
} idx := 9
func EncodeVarInt(n int) []byte {
if n == 0 {
return []byte{0}
}
result := make([]byte, 8)
idx := 7
for n != 0 { for n != 0 {
result[idx] = byte(n & 0x7F) buf[idx] = byte(n & 0x7F)
n >>= 7 n >>= 7
idx-- idx--
} }
result = result[idx+1:] result := buf[idx+1:]
for i := 0; i < len(result)-1; i++ { for i := 0; i < len(result)-1; i++ {
result[i] |= 0x80 result[i] |= 0x80
} }
return result w.Write(result)
} }
+2 -2
View File
@@ -230,7 +230,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader) ([]byte, error) {
finishSkip() finishSkip()
responseIds := make([]byte, 0, 32*100) responseIds := make([]byte, 0, 32*100)
responses := 0 var responses uint64 = 0
endBound := currBound endBound := currBound
@@ -284,7 +284,7 @@ func (n *Negentropy) SplitRange(lower, upper int, upperBound Bound, output *byte
// we just send the full ids here // we just send the full ids here
n.WriteBound(output, upperBound) n.WriteBound(output, upperBound)
output.WriteByte(byte(IdListMode)) output.WriteByte(byte(IdListMode))
WriteVarInt(output, numElems) WriteVarInt(output, uint64(numElems))
for _, item := range n.storage.Range(lower, upper) { for _, item := range n.storage.Range(lower, upper) {
output.Write(item.ID[:]) output.Write(item.ID[:])
+4 -3
View File
@@ -1,6 +1,7 @@
package storage package storage
import ( import (
"bytes"
"crypto/sha256" "crypto/sha256"
"encoding/binary" "encoding/binary"
@@ -41,8 +42,8 @@ func (acc *Accumulator) AddBytes(other []byte) {
} }
func (acc *Accumulator) GetFingerprint(n int) [negentropy.FingerprintSize]byte { func (acc *Accumulator) GetFingerprint(n int) [negentropy.FingerprintSize]byte {
input := acc.Buf[:32] input := bytes.NewBuffer(acc.Buf[:32])
input = append(input, negentropy.EncodeVarInt(n)...) negentropy.WriteVarInt(input, uint64(n))
hash := sha256.Sum256(input) hash := sha256.Sum256(input.Bytes())
return [negentropy.FingerprintSize]byte(hash[:negentropy.FingerprintSize]) return [negentropy.FingerprintSize]byte(hash[:negentropy.FingerprintSize])
} }