From a4fc90caea6809bc69f21902371b8fe6fe5a3d94 Mon Sep 17 00:00:00 2001 From: Petr Hanzl Date: Fri, 19 Apr 2024 14:07:11 +0200 Subject: [PATCH] Create read-only factory functions for databases. --- db/base_db.go | 4 ++++ db/code_db.go | 5 +++++ db/destroyed_account_db.go | 24 +++++++++--------------- db/substate_db.go | 7 ++++++- db/update_db.go | 5 +++++ 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/db/base_db.go b/db/base_db.go index bcb6304..cb488ac 100644 --- a/db/base_db.go +++ b/db/base_db.go @@ -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 { diff --git a/db/code_db.go b/db/code_db.go index 3a0a79d..1237a82 100644 --- a/db/code_db.go +++ b/db/code_db.go @@ -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 { diff --git a/db/destroyed_account_db.go b/db/destroyed_account_db.go index ccf0789..359332d 100644 --- a/db/destroyed_account_db.go +++ b/db/destroyed_account_db.go @@ -3,7 +3,6 @@ package db import ( "encoding/binary" "fmt" - "log" "github.com/syndtr/goleveldb/leveldb/opt" @@ -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 { diff --git a/db/substate_db.go b/db/substate_db.go index 2397839..4fbb41d 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -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}}} } diff --git a/db/update_db.go b/db/update_db.go index 30f5bfb..77835e4 100644 --- a/db/update_db.go +++ b/db/update_db.go @@ -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 {