forked from ethereum/go-ethereum
-
Couldn't load subscription status.
- Fork 5
feat: triedb.Config support for arbitrary backend implementations
#70
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74dc312
make backends configurable
7789060
refactor: reduce delta from `main`
ARR4N 2f63ee1
test: `triedb.Config.DBOverride` and routing
ARR4N 4404e70
refactor: override backend immediately after `db` instantiation
ARR4N 7a62eb6
refactor: use "DB" instead of "backend" in names
ARR4N File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright 2024 the libevm authors. | ||
| // | ||
| // The libevm additions to go-ethereum are free software: you can redistribute | ||
| // them and/or modify them under the terms of the GNU Lesser General Public License | ||
| // as published by the Free Software Foundation, either version 3 of the License, | ||
| // or (at your option) any later version. | ||
| // | ||
| // The libevm additions are distributed in the hope that they will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
| // General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with the go-ethereum library. If not, see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| package triedb | ||
ARR4N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "github.com/ava-labs/libevm/common" | ||
| "github.com/ava-labs/libevm/ethdb" | ||
| "github.com/ava-labs/libevm/log" | ||
| "github.com/ava-labs/libevm/trie/triestate" | ||
| "github.com/ava-labs/libevm/triedb/database" | ||
| "github.com/ava-labs/libevm/triedb/hashdb" | ||
| "github.com/ava-labs/libevm/triedb/pathdb" | ||
| ) | ||
|
|
||
| // Backend defines the intersection of methods shared by [hashdb.Database] and | ||
| // [pathdb.Database]. | ||
| type Backend 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]. | ||
| // | ||
| // [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 BackendOverride is an arbitrary implementation of a [Database] backend. It | ||
| // MUST be either a [HashBackend] or a [PathBackend]. | ||
| type BackendOverride interface { | ||
| Backend | ||
| ReaderProvider | ||
| } | ||
|
|
||
| func (db *Database) overrideBackend(diskdb ethdb.Database, config *Config) { | ||
| if config.DBOverride == nil { | ||
| return | ||
| } | ||
| if config.HashDB != nil || config.PathDB != nil { | ||
| log.Crit("Database override provided when 'hash' or 'path' mode are configured") | ||
| } | ||
| db.backend.Close() //nolint:gosec // geth defaults to hashdb instances, which always return nil from Close() | ||
|
|
||
| db.backend = config.DBOverride(diskdb, config) | ||
| switch db.backend.(type) { | ||
| case HashBackend: | ||
| case PathBackend: | ||
| default: | ||
| log.Crit("Database override is neither hash- nor path-based") | ||
| } | ||
| } | ||
|
|
||
| var ( | ||
| // If either of these break then the respective interface SHOULD be updated. | ||
| _ HashBackend = (*hashdb.Database)(nil) | ||
| _ PathBackend = (*pathdb.Database)(nil) | ||
| ) | ||
|
|
||
| // A HashBackend mirrors the functionality of a [hashdb.Database]. | ||
| type HashBackend interface { | ||
| Backend | ||
|
|
||
| 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 | ||
|
|
||
| Recover(root common.Hash, loader triestate.TrieLoader) error | ||
| Recoverable(root common.Hash) bool | ||
| Disable() error | ||
| Enable(root common.Hash) error | ||
| Journal(root common.Hash) error | ||
| SetBufferSize(size int) error | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2024 the libevm authors. | ||
| // | ||
| // The libevm additions to go-ethereum are free software: you can redistribute | ||
| // them and/or modify them under the terms of the GNU Lesser General Public License | ||
| // as published by the Free Software Foundation, either version 3 of the License, | ||
| // or (at your option) any later version. | ||
| // | ||
| // The libevm additions are distributed in the hope that they will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
| // General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with the go-ethereum library. If not, see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| package triedb | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/ava-labs/libevm/common" | ||
| "github.com/ava-labs/libevm/ethdb" | ||
| "github.com/ava-labs/libevm/triedb/database" | ||
| ) | ||
|
|
||
| func TestDBOverride(t *testing.T) { | ||
| config := &Config{ | ||
| DBOverride: func(d ethdb.Database, c *Config) BackendOverride { | ||
| return override{} | ||
| }, | ||
| } | ||
|
|
||
| db := NewDatabase(nil, config) | ||
| got, err := db.Reader(common.Hash{}) | ||
| require.NoError(t, err) | ||
| if _, ok := got.(reader); !ok { | ||
| t.Errorf("with non-nil %T.DBOverride, %T.Reader() got concrete type %T; want %T", config, db, got, reader{}) | ||
| } | ||
| } | ||
|
|
||
| type override struct { | ||
| PathBackend | ||
| } | ||
|
|
||
| type reader struct { | ||
| database.Reader | ||
| } | ||
|
|
||
| func (override) Reader(common.Hash) (database.Reader, error) { | ||
| return reader{}, nil | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.