Skip to content

Commit

Permalink
fix: the root VKey and the 0-block VKey must be different values
Browse files Browse the repository at this point in the history
  • Loading branch information
gritzko committed Sep 4, 2024
1 parent 3dceb66 commit ec64992
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
5 changes: 2 additions & 3 deletions chotki.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ func (cho *Chotki) Disconnect(addr string) error {
}

func (cho *Chotki) VersionVector() (vv rdx.VV, err error) {
key0 := VKey(rdx.ID0)
val, clo, err := cho.db.Get(key0)
val, clo, err := cho.db.Get(VKey0)
if err == nil {
vv = make(rdx.VV)
err = vv.PutTLV(val)
Expand Down Expand Up @@ -571,7 +570,7 @@ func (cho *Chotki) DumpVV(writer io.Writer) {
}
i := cho.db.NewIter(&io)
defer i.Close()
for i.SeekGE(VKey(rdx.ID0)); i.Valid(); i.Next() {
for i.SeekGE(VKey0); i.Valid(); i.Next() {
id := rdx.IDFromBytes(i.Key()[1:])
vv := make(rdx.VV)
_ = vv.PutTLV(i.Value())
Expand Down
6 changes: 4 additions & 2 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ func OKeyIdRdt(key []byte) (id rdx.ID, rdt byte) {
return
}

var VKey0 = []byte{'V', 0, 0, 0, 0, 0, 0, 0, 0, 'V'}

func VKey(id rdx.ID) (key []byte) {
var ret = [16]byte{'V'}
block := id & ^SyncBlockMask
block := id | SyncBlockMask
key = binary.BigEndian.AppendUint64(ret[:1], uint64(block))
key = append(key, 'V')
return
Expand All @@ -42,7 +44,7 @@ func VKeyId(key []byte) rdx.ID {
if len(key) != LidLKeyLen {
return rdx.BadId
}
return rdx.IDFromBytes(key[1:])
return rdx.IDFromBytes(key[1:]) & ^SyncBlockMask
}

// A class contains a number of fields. Each Field has
Expand Down
4 changes: 2 additions & 2 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (cho *Chotki) UpdateVTree(id, ref rdx.ID, pb *pebble.Batch) (err error) {
v := protocol.Record('V', id.ZipBytes())
err = pb.Merge(VKey(ref), v, &pebbleWriteOptions)
if err == nil {
err = pb.Merge(VKey(rdx.ID0), v, &pebbleWriteOptions)
err = pb.Merge(VKey0, v, &pebbleWriteOptions)
}
return
}
Expand All @@ -35,7 +35,7 @@ func (cho *Chotki) ApplyH(id, ref rdx.ID, body []byte, batch *pebble.Batch) (err
_, rest := protocol.Take('M', body)
var vbody []byte
vbody, _ = protocol.Take('V', rest)
err = batch.Merge(VKey(rdx.ID0), vbody, &pebbleWriteOptions)
err = batch.Merge(VKey0, vbody, &pebbleWriteOptions)
return
}

Expand Down
8 changes: 4 additions & 4 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ func (sync *Syncer) FeedHandshake() (vv protocol.Records, err error) {
UpperBound: []byte{'P'},
})

key0 := VKey(rdx.ID0)
ok := sync.vvit.SeekGE(key0)
if !ok || 0 != bytes.Compare(sync.vvit.Key(), key0) {
ok := sync.vvit.SeekGE(VKey0)
if !ok || 0 != bytes.Compare(sync.vvit.Key(), VKey0) {
return nil, rdx.ErrBadV0Record
}
sync.hostvv = make(rdx.VV)
Expand Down Expand Up @@ -235,7 +234,8 @@ func (sync *Syncer) FeedBlockDiff() (diff protocol.Records, err error) {
return protocol.Records{}, nil
}
block := VKeyId(sync.vvit.Key()).ZeroOff()
sync.ffit.SeekGE(OKey(rdx.ID0, 0))
key := OKey(block, 0)
sync.ffit.SeekGE(key)
bmark, parcel := protocol.OpenHeader(nil, 'D')
parcel = append(parcel, protocol.Record('T', sync.snaplast.ZipBytes())...)
parcel = append(parcel, protocol.Record('R', block.ZipBytes())...)
Expand Down

0 comments on commit ec64992

Please sign in to comment.