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

ethdb: refactor Database interface #30693

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
8 changes: 4 additions & 4 deletions core/rawdb/accessors_indexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ var newTestHasher = blocktest.NewHasher
func TestLookupStorage(t *testing.T) {
tests := []struct {
name string
writeTxLookupEntriesByBlock func(ethdb.Writer, *types.Block)
writeTxLookupEntriesByBlock func(ethdb.KeyValueWriter, *types.Block)
}{
{
"DatabaseV6",
func(db ethdb.Writer, block *types.Block) {
func(db ethdb.KeyValueWriter, block *types.Block) {
WriteTxLookupEntriesByBlock(db, block)
},
},
{
"DatabaseV4-V5",
func(db ethdb.Writer, block *types.Block) {
func(db ethdb.KeyValueWriter, block *types.Block) {
for _, tx := range block.Transactions() {
db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes())
}
},
},
{
"DatabaseV3",
func(db ethdb.Writer, block *types.Block) {
func(db ethdb.KeyValueWriter, block *types.Block) {
for index, tx := range block.Transactions() {
entry := LegacyTxLookupEntry{
BlockHash: block.Hash(),
Expand Down
11 changes: 9 additions & 2 deletions core/rawdb/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ const freezerTableSize = 2 * 1000 * 1000 * 1000
// - The append-only nature ensures that disk writes are minimized.
// - The in-order data ensures that disk reads are always optimized.
type Freezer struct {
frozen atomic.Uint64 // Number of items already frozen
tail atomic.Uint64 // Number of the first stored item in the freezer
datadir string
frozen atomic.Uint64 // Number of items already frozen
tail atomic.Uint64 // Number of the first stored item in the freezer

// This lock synchronizes writers and the truncate operation, as well as
// the "atomic" (batched) read operations.
Expand Down Expand Up @@ -109,6 +110,7 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
}
// Open all the supported data tables
freezer := &Freezer{
datadir: datadir,
readonly: readonly,
tables: make(map[string]*freezerTable),
instanceLock: lock,
Expand Down Expand Up @@ -172,6 +174,11 @@ func (f *Freezer) Close() error {
return nil
}

// AncientDatadir returns the path of the ancient store.
func (f *Freezer) AncientDatadir() (string, error) {
return f.datadir, nil
}

// HasAncient returns an indicator whether the specified ancient data exists
// in the freezer.
func (f *Freezer) HasAncient(kind string, number uint64) (bool, error) {
Expand Down
6 changes: 6 additions & 0 deletions core/rawdb/freezer_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,9 @@ func (f *MemoryFreezer) Reset() error {
f.items, f.tail = 0, 0
return nil
}

// AncientDatadir returns the path of the ancient store.
// Since the memory freezer is ephemeral, an empty string is returned.
func (f *MemoryFreezer) AncientDatadir() (string, error) {
return "", nil
}
8 changes: 8 additions & 0 deletions core/rawdb/freezer_resettable.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ func (f *resettableFreezer) Sync() error {
return f.freezer.Sync()
}

// AncientDatadir returns the path of the ancient store.
func (f *resettableFreezer) AncientDatadir() (string, error) {
f.lock.RLock()
defer f.lock.RUnlock()

return f.freezer.AncientDatadir()
}

// cleanup removes the directory located in the specified path
// has the name with deletion marker suffix.
func cleanup(path string) error {
Expand Down
25 changes: 3 additions & 22 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,12 @@ type Reader interface {
AncientReader
}

// Writer contains the methods required to write data to both key-value as well as
// immutable ancient data.
type Writer interface {
KeyValueWriter
KeyValueRangeDeleter
AncientWriter
}

// Stater contains the methods required to retrieve states from both key-value as well as
// immutable ancient data.
type Stater interface {
KeyValueStater
AncientStater
}

// AncientStore contains all the methods required to allow handling different
// ancient data stores backing immutable data store.
type AncientStore interface {
AncientReader
AncientWriter
AncientStater
io.Closer
}

Expand All @@ -196,11 +182,6 @@ type ResettableAncientStore interface {
// Database contains all the methods required by the high level database to not
// only access the key-value data store but also the ancient chain store.
type Database interface {
Reader
Writer
Batcher
Iteratee
Stater
Compacter
io.Closer
KeyValueStore
AncientStore
}