Skip to content

Commit

Permalink
test: triedb.Config.DBOverride and routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Nov 13, 2024
1 parent 7789060 commit 2f63ee1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
11 changes: 7 additions & 4 deletions triedb/database.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ type ReaderProvider interface {
Reader(common.Hash) (database.Reader, error)
}

// A BackendConstructor constructs alternative backend implements. The returned
// type MUST be either a [HashBackend] or a [PathBackend].
type BackendConstructor func(ethdb.Database, *Config) interface {
// 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
}
Expand All @@ -54,7 +57,7 @@ func (db *Database) overrideBackend(diskdb ethdb.Database, config *Config) {
if config.HashDB != nil || config.PathDB != nil {
log.Crit("Database override provided when 'hash' or 'path' mode are configured")
}
db.backend.Close() //nolint:gsec // geth defaults to hashdb instances, which always return nil from Close()
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) {
Expand Down
54 changes: 54 additions & 0 deletions triedb/database.libevm_test.go
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
}

0 comments on commit 2f63ee1

Please sign in to comment.