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

refactor: use "DB" instead of "backend" in names #71

Merged
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
20 changes: 10 additions & 10 deletions triedb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Config struct {
HashDB *hashdb.Config // Configs for hash-based scheme
PathDB *pathdb.Config // Configs for experimental path-based scheme

DBOverride BackendConstructor // Injects an arbitrary backend implementation
DBOverride DBConstructor // Injects an arbitrary backend-database implementation
}

// HashDefaults represents a config for using hash-based scheme with
Expand Down Expand Up @@ -228,7 +228,7 @@ func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) {
//
// It's only supported by hash-based database and will return an error for others.
func (db *Database) Cap(limit common.StorageSize) error {
hdb, ok := db.backend.(HashBackend)
hdb, ok := db.backend.(HashDB)
if !ok {
return errors.New("not supported")
}
Expand All @@ -244,7 +244,7 @@ func (db *Database) Cap(limit common.StorageSize) error {
//
// It's only supported by hash-based database and will return an error for others.
func (db *Database) Reference(root common.Hash, parent common.Hash) error {
hdb, ok := db.backend.(HashBackend)
hdb, ok := db.backend.(HashDB)
if !ok {
return errors.New("not supported")
}
Expand All @@ -255,7 +255,7 @@ func (db *Database) Reference(root common.Hash, parent common.Hash) error {
// Dereference removes an existing reference from a root node. It's only
// supported by hash-based database and will return an error for others.
func (db *Database) Dereference(root common.Hash) error {
hdb, ok := db.backend.(HashBackend)
hdb, ok := db.backend.(HashDB)
if !ok {
return errors.New("not supported")
}
Expand All @@ -268,7 +268,7 @@ func (db *Database) Dereference(root common.Hash) error {
// corresponding trie histories are existent. It's only supported by path-based
// database and will return an error for others.
func (db *Database) Recover(target common.Hash) error {
pdb, ok := db.backend.(PathBackend)
pdb, ok := db.backend.(PathDB)
if !ok {
return errors.New("not supported")
}
Expand All @@ -286,7 +286,7 @@ func (db *Database) Recover(target common.Hash) error {
// recovered. It's only supported by path-based database and will return an
// error for others.
func (db *Database) Recoverable(root common.Hash) (bool, error) {
pdb, ok := db.backend.(PathBackend)
pdb, ok := db.backend.(PathDB)
if !ok {
return false, errors.New("not supported")
}
Expand All @@ -299,7 +299,7 @@ func (db *Database) Recoverable(root common.Hash) (bool, error) {
//
// It's only supported by path-based database and will return an error for others.
func (db *Database) Disable() error {
pdb, ok := db.backend.(PathBackend)
pdb, ok := db.backend.(PathDB)
if !ok {
return errors.New("not supported")
}
Expand All @@ -309,7 +309,7 @@ func (db *Database) Disable() error {
// Enable activates database and resets the state tree with the provided persistent
// state root once the state sync is finished.
func (db *Database) Enable(root common.Hash) error {
pdb, ok := db.backend.(PathBackend)
pdb, ok := db.backend.(PathDB)
if !ok {
return errors.New("not supported")
}
Expand All @@ -321,7 +321,7 @@ func (db *Database) Enable(root common.Hash) error {
// flattening everything down (bad for reorgs). It's only supported by path-based
// database and will return an error for others.
func (db *Database) Journal(root common.Hash) error {
pdb, ok := db.backend.(PathBackend)
pdb, ok := db.backend.(PathDB)
if !ok {
return errors.New("not supported")
}
Expand All @@ -332,7 +332,7 @@ func (db *Database) Journal(root common.Hash) error {
// It's only supported by path-based database and will return an error for
// others.
func (db *Database) SetBufferSize(size int) error {
pdb, ok := db.backend.(PathBackend)
pdb, ok := db.backend.(PathDB)
if !ok {
return errors.New("not supported")
}
Expand Down
41 changes: 21 additions & 20 deletions triedb/database.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,28 @@ import (
"github.com/ava-labs/libevm/triedb/pathdb"
)

// Backend defines the intersection of methods shared by [hashdb.Database] and
// [pathdb.Database].
type Backend backend
// BackendDB defines the intersection of methods shared by [hashdb.Database] and
// [pathdb.Database]. It is defined to export an otherwise internal type used by
// the non-libevm geth implementation.
type BackendDB backend

// A ReaderProvider exposes its underlying Reader as an interface. Both
// [hashdb.Database] and [pathdb.Database] return concrete types so Go's lack of
// support for [covariant types] means that this method can't be defined on
// [Backend].
// [BackendDB].
//
// [covariant types]: https://go.dev/doc/faq#covariant_types
type ReaderProvider interface {
Reader(common.Hash) (database.Reader, error)
}

// A BackendConstructor constructs alternative backend implementations.
type BackendConstructor func(ethdb.Database, *Config) BackendOverride
// A DBConstructor constructs alternative backend-database implementations.
type DBConstructor func(ethdb.Database, *Config) DBOverride

// A BackendOverride is an arbitrary implementation of a [Database] backend. It
// MUST be either a [HashBackend] or a [PathBackend].
type BackendOverride interface {
Backend
// A DBOverride is an arbitrary implementation of a [Database] backend. It MUST
// be either a [HashDB] or a [PathDB].
type DBOverride interface {
BackendDB
ReaderProvider
}

Expand All @@ -60,8 +61,8 @@ func (db *Database) overrideBackend(diskdb ethdb.Database, config *Config) bool

db.backend = config.DBOverride(diskdb, config)
switch db.backend.(type) {
case HashBackend:
case PathBackend:
case HashDB:
case PathDB:
default:
log.Crit("Database override is neither hash- nor path-based")
}
Expand All @@ -70,22 +71,22 @@ func (db *Database) overrideBackend(diskdb ethdb.Database, config *Config) bool

var (
// If either of these break then the respective interface SHOULD be updated.
_ HashBackend = (*hashdb.Database)(nil)
_ PathBackend = (*pathdb.Database)(nil)
_ HashDB = (*hashdb.Database)(nil)
_ PathDB = (*pathdb.Database)(nil)
)

// A HashBackend mirrors the functionality of a [hashdb.Database].
type HashBackend interface {
Backend
// A HashDB mirrors the functionality of a [hashdb.Database].
type HashDB interface {
BackendDB

Cap(limit common.StorageSize) error
Reference(root common.Hash, parent common.Hash)
Dereference(root common.Hash)
}

// A PathBackend mirrors the functionality of a [pathdb.Database].
type PathBackend interface {
Backend
// A PathDB mirrors the functionality of a [pathdb.Database].
type PathDB interface {
BackendDB

Recover(root common.Hash, loader triestate.TrieLoader) error
Recoverable(root common.Hash) bool
Expand Down
4 changes: 2 additions & 2 deletions triedb/database.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func TestDBOverride(t *testing.T) {
config := &Config{
DBOverride: func(d ethdb.Database, c *Config) BackendOverride {
DBOverride: func(d ethdb.Database, c *Config) DBOverride {
return override{}
},
}
Expand All @@ -42,7 +42,7 @@ func TestDBOverride(t *testing.T) {
}

type override struct {
PathBackend
PathDB
}

type reader struct {
Expand Down