Skip to content

Commit

Permalink
Merge branch 'main' into coherent-effectiveBalanceIncrement
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear authored Oct 31, 2024
2 parents 745c396 + 66bad3e commit e3ec723
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mod/da/pkg/store/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type BlockEvent[BeaconBlockT BeaconBlock] interface {
type IndexDB interface {
Has(index uint64, key []byte) (bool, error)
Set(index uint64, key []byte, value []byte) error

// Prune returns error if start > end
Prune(start uint64, end uint64) error
}

Expand Down
5 changes: 5 additions & 0 deletions mod/storage/pkg/deposit/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
sdkcollections "cosmossdk.io/collections"
"cosmossdk.io/core/store"
"github.com/berachain/beacon-kit/mod/storage/pkg/encoding"
"github.com/berachain/beacon-kit/mod/storage/pkg/pruner"
)

const KeyDepositPrefix = "deposit"
Expand Down Expand Up @@ -104,6 +105,10 @@ func (kv *KVStore[DepositT]) setDeposit(deposit DepositT) error {

// Prune removes the [start, end) deposits from the store.
func (kv *KVStore[DepositT]) Prune(start, end uint64) error {
if start > end {
return pruner.ErrInvalidRange
}

var ctx = context.TODO()
kv.mu.Lock()
defer kv.mu.Unlock()
Expand Down
7 changes: 7 additions & 0 deletions mod/storage/pkg/filedb/range_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func (db *RangeDB) DeleteRange(from, to uint64) error {
if !ok {
return errors.New("rangedb: delete range not supported for this db")
}
if from > to {
return pruner.ErrInvalidRange
}
for ; from < to; from++ {
path := strconv.FormatUint(from, 10) + "/"
if err := f.fs.RemoveAll(path); err != nil {
Expand All @@ -105,6 +108,10 @@ func (db *RangeDB) DeleteRange(from, to uint64) error {
// Prune removes all values in the given range [start, end) from the db.
func (db *RangeDB) Prune(start, end uint64) error {
start = max(start, db.firstNonNilIndex)
if start > end {
return pruner.ErrInvalidRange
}

if err := db.DeleteRange(start, end); err != nil {
// Resets last pruned index in case Delete somehow populates indices on
// err. This will cause the next prune operation is O(n), but next
Expand Down
11 changes: 10 additions & 1 deletion mod/storage/pkg/filedb/range_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,23 @@ func TestRangeDB_Prune(t *testing.T) {
requireExist(t, rdb, 0, 1)
},
},
{
name: "PruneWithDeleteRange-InvalidRange",
setupFunc: func(rdb *file.RangeDB) error {
return populateTestDB(rdb, 0, 50)
},
start: 7,
end: 2,
expectedError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rdb := file.NewRangeDB(newTestFDB("/tmp/testdb-2"))

if tt.setupFunc != nil {
if err := tt.setupFunc(rdb); (err != nil) != tt.expectedError {
if err := tt.setupFunc(rdb); err != nil {
t.Fatalf(
"setupFunc() error = %v, expectedError %v",
err,
Expand Down
4 changes: 4 additions & 0 deletions mod/storage/pkg/pruner/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ package pruner

import (
"context"
"errors"

"github.com/berachain/beacon-kit/mod/primitives/pkg/async"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)

var ErrInvalidRange = errors.New("range start greater than end")

// BeaconBlock is an interface for beacon blocks.
type BeaconBlock interface {
GetSlot() math.U64
Expand All @@ -48,6 +51,7 @@ type BlockEvent[BeaconBlockT BeaconBlock] interface {
// Prunable is an interface representing a store that can be pruned.
type Prunable interface {
// Prune prunes the store from [start, end).
// Returns ErrInvalidRange if start > end
Prune(start, end uint64) error
}

Expand Down

0 comments on commit e3ec723

Please sign in to comment.