ethdb: refactor Database interface (#30693)

This commit is contained in:
Marius van der Wijden 2024-10-29 09:32:40 +01:00 committed by GitHub
parent 7180d26530
commit 236147bf70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 30 additions and 28 deletions

@ -35,17 +35,17 @@ var newTestHasher = blocktest.NewHasher
func TestLookupStorage(t *testing.T) { func TestLookupStorage(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
writeTxLookupEntriesByBlock func(ethdb.Writer, *types.Block) writeTxLookupEntriesByBlock func(ethdb.KeyValueWriter, *types.Block)
}{ }{
{ {
"DatabaseV6", "DatabaseV6",
func(db ethdb.Writer, block *types.Block) { func(db ethdb.KeyValueWriter, block *types.Block) {
WriteTxLookupEntriesByBlock(db, block) WriteTxLookupEntriesByBlock(db, block)
}, },
}, },
{ {
"DatabaseV4-V5", "DatabaseV4-V5",
func(db ethdb.Writer, block *types.Block) { func(db ethdb.KeyValueWriter, block *types.Block) {
for _, tx := range block.Transactions() { for _, tx := range block.Transactions() {
db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()) db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes())
} }
@ -53,7 +53,7 @@ func TestLookupStorage(t *testing.T) {
}, },
{ {
"DatabaseV3", "DatabaseV3",
func(db ethdb.Writer, block *types.Block) { func(db ethdb.KeyValueWriter, block *types.Block) {
for index, tx := range block.Transactions() { for index, tx := range block.Transactions() {
entry := LegacyTxLookupEntry{ entry := LegacyTxLookupEntry{
BlockHash: block.Hash(), BlockHash: block.Hash(),

@ -58,8 +58,9 @@ const freezerTableSize = 2 * 1000 * 1000 * 1000
// - The append-only nature ensures that disk writes are minimized. // - The append-only nature ensures that disk writes are minimized.
// - The in-order data ensures that disk reads are always optimized. // - The in-order data ensures that disk reads are always optimized.
type Freezer struct { type Freezer struct {
frozen atomic.Uint64 // Number of items already frozen datadir string
tail atomic.Uint64 // Number of the first stored item in the freezer 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 // This lock synchronizes writers and the truncate operation, as well as
// the "atomic" (batched) read operations. // the "atomic" (batched) read operations.
@ -109,6 +110,7 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
} }
// Open all the supported data tables // Open all the supported data tables
freezer := &Freezer{ freezer := &Freezer{
datadir: datadir,
readonly: readonly, readonly: readonly,
tables: make(map[string]*freezerTable), tables: make(map[string]*freezerTable),
instanceLock: lock, instanceLock: lock,
@ -172,6 +174,11 @@ func (f *Freezer) Close() error {
return nil 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 // HasAncient returns an indicator whether the specified ancient data exists
// in the freezer. // in the freezer.
func (f *Freezer) HasAncient(kind string, number uint64) (bool, error) { func (f *Freezer) HasAncient(kind string, number uint64) (bool, error) {

@ -419,3 +419,9 @@ func (f *MemoryFreezer) Reset() error {
f.items, f.tail = 0, 0 f.items, f.tail = 0, 0
return nil 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
}

@ -202,6 +202,14 @@ func (f *resettableFreezer) Sync() error {
return f.freezer.Sync() 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 // cleanup removes the directory located in the specified path
// has the name with deletion marker suffix. // has the name with deletion marker suffix.
func cleanup(path string) error { func cleanup(path string) error {

@ -162,26 +162,12 @@ type Reader interface {
AncientReader 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 // AncientStore contains all the methods required to allow handling different
// ancient data stores backing immutable data store. // ancient data stores backing immutable data store.
type AncientStore interface { type AncientStore interface {
AncientReader AncientReader
AncientWriter AncientWriter
AncientStater
io.Closer io.Closer
} }
@ -196,11 +182,6 @@ type ResettableAncientStore interface {
// Database contains all the methods required by the high level database to not // 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. // only access the key-value data store but also the ancient chain store.
type Database interface { type Database interface {
Reader KeyValueStore
Writer AncientStore
Batcher
Iteratee
Stater
Compacter
io.Closer
} }