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

Read-only database #75

Merged
merged 1 commit into from
Apr 22, 2024
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
4 changes: 4 additions & 0 deletions db/base_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func MakeDefaultBaseDBFromBaseDB(db BaseDB) BaseDB {
return &baseDB{backend: db.getBackend()}
}

// NewReadOnlyBaseDB creates a new instance of read-only BaseDB.
func NewReadOnlyBaseDB(path string) (BaseDB, error) {
return newBaseDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}
func newBaseDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*baseDB, error) {
b, err := leveldb.OpenFile(path, o)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions db/code_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func MakeDefaultCodeDBFromBaseDB(db BaseDB) CodeDB {
return &codeDB{&baseDB{backend: db.getBackend()}}
}

// NewReadOnlyCodeDB creates a new instance of read-only CodeDB.
func NewReadOnlyCodeDB(path string) (CodeDB, error) {
return newCodeDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}

func newCodeDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*codeDB, error) {
base, err := newBaseDB(path, o, wo, ro)
if err != nil {
Expand Down
24 changes: 9 additions & 15 deletions db/destroyed_account_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package db
import (
"encoding/binary"
"fmt"
"log"

"github.com/syndtr/goleveldb/leveldb/opt"

Expand All @@ -15,29 +14,24 @@ type DestroyedAccountDB struct {
backend BaseDB
}

func NewDestroyedAccountDB(backend BaseDB) *DestroyedAccountDB {
return &DestroyedAccountDB{backend: backend}
}

func OpenDestroyedAccountDB(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return openDestroyedAccountDB(destroyedAccountDir, &opt.Options{ReadOnly: false}, nil, nil)
func NewDefaultDestroyedAccountDB(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return newDestroyedAccountDB(destroyedAccountDir, nil, nil, nil)
}

func OpenDestroyedAccountDBReadOnly(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return openDestroyedAccountDB(destroyedAccountDir, &opt.Options{ReadOnly: true}, nil, nil)
func MakeDefaultDestroyedAccountDBFromBaseDB(backend BaseDB) *DestroyedAccountDB {
return &DestroyedAccountDB{backend: backend}
}

func MakeDestroyedAccountDBFromBaseDB(db BaseDB) *DestroyedAccountDB {
return &DestroyedAccountDB{db}
func NewReadOnlyDestroyedAccountDB(destroyedAccountDir string) (*DestroyedAccountDB, error) {
return newDestroyedAccountDB(destroyedAccountDir, &opt.Options{ReadOnly: true}, nil, nil)
}

func openDestroyedAccountDB(destroyedAccountDir string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*DestroyedAccountDB, error) {
log.Println("substate: OpenDestroyedAccountDB")
func newDestroyedAccountDB(destroyedAccountDir string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*DestroyedAccountDB, error) {
backend, err := newBaseDB(destroyedAccountDir, o, wo, ro)
if err != nil {
return nil, fmt.Errorf("error opening deletion-db %s: %v", destroyedAccountDir, err)
return nil, fmt.Errorf("error opening deletion-db %s: %w", destroyedAccountDir, err)
}
return NewDestroyedAccountDB(backend), nil
return MakeDefaultDestroyedAccountDBFromBaseDB(backend), nil
}

func (db *DestroyedAccountDB) Close() error {
Expand Down
7 changes: 6 additions & 1 deletion db/substate_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func MakeDefaultSubstateDBFromBaseDB(db BaseDB) SubstateDB {
return &substateDB{&codeDB{&baseDB{backend: db.getBackend()}}}
}

func MakeSubstateDb(db *leveldb.DB, wo *opt.WriteOptions, ro *opt.ReadOptions) SubstateDB {
// NewReadOnlySubstateDB creates a new instance of read-only SubstateDB.
func NewReadOnlySubstateDB(path string) (SubstateDB, error) {
return newSubstateDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}

func MakeSubstateDB(db *leveldb.DB, wo *opt.WriteOptions, ro *opt.ReadOptions) SubstateDB {
return &substateDB{&codeDB{&baseDB{backend: db, wo: wo, ro: ro}}}
}

Expand Down
5 changes: 5 additions & 0 deletions db/update_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func MakeDefaultUpdateDBFromBaseDB(db BaseDB) UpdateDB {
return &updateDB{&codeDB{&baseDB{backend: db.getBackend()}}}
}

// NewReadOnlyUpdateDB creates a new instance of read-only UpdateDB.
func NewReadOnlyUpdateDB(path string) (UpdateDB, error) {
return newUpdateDB(path, &opt.Options{ReadOnly: true}, nil, nil)
}

func newUpdateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*updateDB, error) {
base, err := newCodeDB(path, o, wo, ro)
if err != nil {
Expand Down
Loading