Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: generic test suite for AncientStore #23707

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ type nofreezedb struct {
ethdb.KeyValueStore
}

// HasAncient returns an error as we don't have a backing chain freezer.
func (db *nofreezedb) HasAncient(kind string, number uint64) (bool, error) {
return false, errNotSupported
}

// Ancient returns an error as we don't have a backing chain freezer.
func (db *nofreezedb) Ancient(kind string, number uint64) ([]byte, error) {
return nil, errNotSupported
Expand Down
23 changes: 3 additions & 20 deletions core/rawdb/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ var (
// mutations are disallowed.
errReadOnly = errors.New("read only")

// errUnknownTable is returned if the user attempts to read from a table that is
// not tracked by the freezer.
errUnknownTable = errors.New("unknown table")

// errOutOrderInsertion is returned if the user attempts to inject out-of-order
// binary blobs into the freezer.
errOutOrderInsertion = errors.New("the append operation is out-order")

// errSymlinkDatadir is returned if the ancient directory specified by user
// is a symbolic link.
errSymlinkDatadir = errors.New("symbolic link datadir is not supported")
Expand Down Expand Up @@ -193,21 +185,12 @@ func (f *freezer) Close() error {
return nil
}

// HasAncient returns an indicator whether the specified ancient data exists
// in the freezer.
func (f *freezer) HasAncient(kind string, number uint64) (bool, error) {
if table := f.tables[kind]; table != nil {
return table.has(number), nil
}
return false, nil
}

// Ancient retrieves an ancient binary blob from the append-only immutable files.
func (f *freezer) Ancient(kind string, number uint64) ([]byte, error) {
if table := f.tables[kind]; table != nil {
return table.Retrieve(number)
}
return nil, errUnknownTable
return nil, ethdb.ErrAncientUnknownKind
}

// AncientRange retrieves multiple items in sequence, starting from the index 'start'.
Expand All @@ -219,7 +202,7 @@ func (f *freezer) AncientRange(kind string, start, count, maxBytes uint64) ([][]
if table := f.tables[kind]; table != nil {
return table.RetrieveItems(start, count, maxBytes)
}
return nil, errUnknownTable
return nil, ethdb.ErrAncientUnknownKind
}

// Ancients returns the length of the frozen items.
Expand All @@ -242,7 +225,7 @@ func (f *freezer) AncientSize(kind string) (uint64, error) {
if table := f.tables[kind]; table != nil {
return table.size()
}
return 0, errUnknownTable
return 0, ethdb.ErrAncientUnknownKind
}

// ReadAncients runs the given read operation while ensuring that no writes take place
Expand Down
5 changes: 3 additions & 2 deletions core/rawdb/freezer_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync/atomic"

"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
"github.com/golang/snappy"
)
Expand Down Expand Up @@ -116,7 +117,7 @@ func (batch *freezerTableBatch) reset() {
// existing data.
func (batch *freezerTableBatch) Append(item uint64, data interface{}) error {
if item != batch.curItem {
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
return fmt.Errorf("%w: have %d want %d", ethdb.ErrAncientOutOrderInsertion, item, batch.curItem)
}

// Encode the item.
Expand All @@ -136,7 +137,7 @@ func (batch *freezerTableBatch) Append(item uint64, data interface{}) error {
// existing data.
func (batch *freezerTableBatch) AppendRaw(item uint64, blob []byte) error {
if item != batch.curItem {
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
return fmt.Errorf("%w: have %d want %d", ethdb.ErrAncientOutOrderInsertion, item, batch.curItem)
}

encItem := blob
Expand Down
7 changes: 2 additions & 5 deletions core/rawdb/freezer_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sync/atomic"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/golang/snappy"
Expand All @@ -38,10 +39,6 @@ var (
// freezer table after it has already been closed.
errClosed = errors.New("closed")

// errOutOfBounds is returned if the item requested is not contained within the
// freezer table.
errOutOfBounds = errors.New("out of bounds")

// errNotSupported is returned if the database doesn't support the required operation.
errNotSupported = errors.New("this operation is not supported")
)
Expand Down Expand Up @@ -735,7 +732,7 @@ func (t *freezerTable) retrieveItems(start, count, maxBytes uint64) ([]byte, []i
// Ensure the start is written, not deleted from the tail, and that the
// caller actually wants something
if items <= start || hidden > start || count == 0 {
return nil, nil, errOutOfBounds
return nil, nil, ethdb.ErrAncientOutOfBounds
}
if start+count > items {
count = items - start
Expand Down
61 changes: 31 additions & 30 deletions core/rawdb/freezer_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/metrics"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestFreezerBasics(t *testing.T) {
}
// Check that we cannot read too far
_, err = f.Retrieve(uint64(255))
if err != errOutOfBounds {
if err != ethdb.ErrAncientOutOfBounds {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -603,10 +604,10 @@ func TestFreezerOffset(t *testing.T) {
require.NoError(t, batch.commit())

checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds,
1: errOutOfBounds,
2: errOutOfBounds,
3: errOutOfBounds,
0: ethdb.ErrAncientOutOfBounds,
1: ethdb.ErrAncientOutOfBounds,
2: ethdb.ErrAncientOutOfBounds,
3: ethdb.ErrAncientOutOfBounds,
})
checkRetrieve(t, f, map[uint64][]byte{
4: getChunk(20, 0xbb),
Expand Down Expand Up @@ -651,11 +652,11 @@ func TestFreezerOffset(t *testing.T) {
t.Log(f.dumpIndexString(0, 100))

checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds,
1: errOutOfBounds,
2: errOutOfBounds,
3: errOutOfBounds,
999999: errOutOfBounds,
0: ethdb.ErrAncientOutOfBounds,
1: ethdb.ErrAncientOutOfBounds,
2: ethdb.ErrAncientOutOfBounds,
3: ethdb.ErrAncientOutOfBounds,
999999: ethdb.ErrAncientOutOfBounds,
})
checkRetrieve(t, f, map[uint64][]byte{
1000000: getChunk(20, 0xbb),
Expand Down Expand Up @@ -703,7 +704,7 @@ func TestTruncateTail(t *testing.T) {
f.truncateTail(1)
fmt.Println(f.dumpIndexString(0, 1000))
checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds,
0: ethdb.ErrAncientOutOfBounds,
})
checkRetrieve(t, f, map[uint64][]byte{
1: getChunk(20, 0xEE),
Expand All @@ -721,7 +722,7 @@ func TestTruncateTail(t *testing.T) {
t.Fatal(err)
}
checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds,
0: ethdb.ErrAncientOutOfBounds,
})
checkRetrieve(t, f, map[uint64][]byte{
1: getChunk(20, 0xEE),
Expand All @@ -735,8 +736,8 @@ func TestTruncateTail(t *testing.T) {
// truncate two elements( item 0, item 1 ), the file 0 should be deleted
f.truncateTail(2)
checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds,
1: errOutOfBounds,
0: ethdb.ErrAncientOutOfBounds,
1: ethdb.ErrAncientOutOfBounds,
})
checkRetrieve(t, f, map[uint64][]byte{
2: getChunk(20, 0xdd),
Expand All @@ -755,8 +756,8 @@ func TestTruncateTail(t *testing.T) {
defer f.Close()

checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds,
1: errOutOfBounds,
0: ethdb.ErrAncientOutOfBounds,
1: ethdb.ErrAncientOutOfBounds,
})
checkRetrieve(t, f, map[uint64][]byte{
2: getChunk(20, 0xdd),
Expand All @@ -769,13 +770,13 @@ func TestTruncateTail(t *testing.T) {
// truncate all, the entire freezer should be deleted
f.truncateTail(7)
checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds,
1: errOutOfBounds,
2: errOutOfBounds,
3: errOutOfBounds,
4: errOutOfBounds,
5: errOutOfBounds,
6: errOutOfBounds,
0: ethdb.ErrAncientOutOfBounds,
1: ethdb.ErrAncientOutOfBounds,
2: ethdb.ErrAncientOutOfBounds,
3: ethdb.ErrAncientOutOfBounds,
4: ethdb.ErrAncientOutOfBounds,
5: ethdb.ErrAncientOutOfBounds,
6: ethdb.ErrAncientOutOfBounds,
})
}

Expand Down Expand Up @@ -806,13 +807,13 @@ func TestTruncateHead(t *testing.T) {
// NewHead is required to be 3, the entire table should be truncated
f.truncateHead(4)
checkRetrieveError(t, f, map[uint64]error{
0: errOutOfBounds, // Deleted by tail
1: errOutOfBounds, // Deleted by tail
2: errOutOfBounds, // Deleted by tail
3: errOutOfBounds, // Deleted by tail
4: errOutOfBounds, // Deleted by Head
5: errOutOfBounds, // Deleted by Head
6: errOutOfBounds, // Deleted by Head
0: ethdb.ErrAncientOutOfBounds, // Deleted by tail
1: ethdb.ErrAncientOutOfBounds, // Deleted by tail
2: ethdb.ErrAncientOutOfBounds, // Deleted by tail
3: ethdb.ErrAncientOutOfBounds, // Deleted by tail
4: ethdb.ErrAncientOutOfBounds, // Deleted by Head
5: ethdb.ErrAncientOutOfBounds, // Deleted by Head
6: ethdb.ErrAncientOutOfBounds, // Deleted by Head
})

// Append new items
Expand Down
Loading