Skip to content

Commit 2f63ee1

Browse files
committed
test: triedb.Config.DBOverride and routing
1 parent 7789060 commit 2f63ee1

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

triedb/database.libevm.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ type ReaderProvider interface {
4040
Reader(common.Hash) (database.Reader, error)
4141
}
4242

43-
// A BackendConstructor constructs alternative backend implements. The returned
44-
// type MUST be either a [HashBackend] or a [PathBackend].
45-
type BackendConstructor func(ethdb.Database, *Config) interface {
43+
// A BackendConstructor constructs alternative backend implementations.
44+
type BackendConstructor func(ethdb.Database, *Config) BackendOverride
45+
46+
// A BackendOverride is an arbitrary implementation of a [Database] backend. It
47+
// MUST be either a [HashBackend] or a [PathBackend].
48+
type BackendOverride interface {
4649
Backend
4750
ReaderProvider
4851
}
@@ -54,7 +57,7 @@ func (db *Database) overrideBackend(diskdb ethdb.Database, config *Config) {
5457
if config.HashDB != nil || config.PathDB != nil {
5558
log.Crit("Database override provided when 'hash' or 'path' mode are configured")
5659
}
57-
db.backend.Close() //nolint:gsec // geth defaults to hashdb instances, which always return nil from Close()
60+
db.backend.Close() //nolint:gosec // geth defaults to hashdb instances, which always return nil from Close()
5861

5962
db.backend = config.DBOverride(diskdb, config)
6063
switch db.backend.(type) {

triedb/database.libevm_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package triedb
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/ava-labs/libevm/common"
25+
"github.com/ava-labs/libevm/ethdb"
26+
"github.com/ava-labs/libevm/triedb/database"
27+
)
28+
29+
func TestDBOverride(t *testing.T) {
30+
config := &Config{
31+
DBOverride: func(d ethdb.Database, c *Config) BackendOverride {
32+
return override{}
33+
},
34+
}
35+
36+
db := NewDatabase(nil, config)
37+
got, err := db.Reader(common.Hash{})
38+
require.NoError(t, err)
39+
if _, ok := got.(reader); !ok {
40+
t.Errorf("with non-nil %T.DBOverride, %T.Reader() got concrete type %T; want %T", config, db, got, reader{})
41+
}
42+
}
43+
44+
type override struct {
45+
PathBackend
46+
}
47+
48+
type reader struct {
49+
database.Reader
50+
}
51+
52+
func (override) Reader(common.Hash) (database.Reader, error) {
53+
return reader{}, nil
54+
}

0 commit comments

Comments
 (0)