diff --git a/portalnetwork/storage/content_storage.go b/portalnetwork/storage/content_storage.go index a5d0d2b26d847..075b4a6bf4b6f 100644 --- a/portalnetwork/storage/content_storage.go +++ b/portalnetwork/storage/content_storage.go @@ -13,7 +13,6 @@ var MaxDistance = uint256.MustFromHex("0xfffffffffffffffffffffffffffffffffffffff type ContentType byte -var RadisuKey = []byte("radius") var SizeKey = []byte("size") type ContentKey struct { diff --git a/portalnetwork/storage/ethpepple/storage.go b/portalnetwork/storage/ethpepple/storage.go index ac1f662412b29..ae7d269aba1ab 100644 --- a/portalnetwork/storage/ethpepple/storage.go +++ b/portalnetwork/storage/ethpepple/storage.go @@ -1,6 +1,7 @@ package ethpepple import ( + "bytes" "encoding/binary" "runtime" "sync/atomic" @@ -139,28 +140,32 @@ func NewPeppleStorage(config PeppleStorageConfig) (storage.ContentStorage, error writeOptions: &pebble.WriteOptions{Sync: false}, } cs.radius.Store(storage.MaxDistance) - radius, _, err := cs.db.Get(storage.RadisuKey) + + val, _, err := cs.db.Get(storage.SizeKey) if err != nil && err != pebble.ErrNotFound { return nil, err } if err == nil { + size := binary.BigEndian.Uint64(val) + // init stage, no need to use lock + cs.size = size + } + + iter, err := cs.db.NewIter(nil) + if err != nil { + return nil, err + } + defer iter.Close() + if iter.Last() && iter.Valid() { + distance := iter.Key() dis := uint256.NewInt(0) - err = dis.UnmarshalSSZ(radius) + err = dis.UnmarshalSSZ(distance) if err != nil { return nil, err } cs.radius.Store(dis) } - val, _, err := cs.db.Get(storage.SizeKey) - if err != nil && err != pebble.ErrNotFound { - return nil, err - } - if err == nil { - size := binary.BigEndian.Uint64(val) - // init stage, no need to use lock - cs.size = size - } cs.sizeChan <- struct{}{} go cs.saveCapacity() return cs, nil @@ -256,12 +261,14 @@ func (c *ContentStorage) prune() error { batch := c.db.NewBatch() for iter.Last(); iter.Valid(); iter.Prev() { + if bytes.Equal(iter.Key(), storage.SizeKey) { + continue + } if curentSize < expectSize { batch.Delete(iter.Key(), nil) curentSize += uint64(len(iter.Key())) + uint64(len(iter.Value())) } else { distance := iter.Key() - c.db.Set(storage.RadisuKey, distance, c.writeOptions) dis := uint256.NewInt(0) err = dis.UnmarshalSSZ(distance) if err != nil {