cosmos-sdk/store/commitment/db.go
Aleksandr Bezobchuk 03bca7b791
feat(store/v2): Merge Feature Branch (#18150)
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: yihuang <huang@crypto.com>
2023-10-18 18:03:43 +00:00

81 lines
1.8 KiB
Go

package commitment
import (
"sync"
ics23 "github.com/cosmos/ics23/go"
"cosmossdk.io/store/v2"
)
// Database represents a state commitment store. It is designed to securely store
// and manage the most recent state information, crucial for achieving consensus.
// Each module creates its own instance of Database for managing its specific state.
type Database struct {
mu sync.Mutex
tree store.Tree
}
// NewDatabase creates a new Database instance.
func NewDatabase(tree store.Tree) *Database {
return &Database{
tree: tree,
}
}
// WriteBatch writes a batch of key-value pairs to the database.
func (db *Database) WriteBatch(cs *store.Changeset) error {
db.mu.Lock()
defer db.mu.Unlock()
return db.tree.WriteBatch(cs)
}
// WorkingHash returns the working hash of the database.
func (db *Database) WorkingHash() []byte {
db.mu.Lock()
defer db.mu.Unlock()
return db.tree.WorkingHash()
}
// LoadVersion loads the state at the given version.
func (db *Database) LoadVersion(version uint64) error {
db.mu.Lock()
defer db.mu.Unlock()
return db.tree.LoadVersion(version)
}
// Commit commits the current state to the database.
func (db *Database) Commit() ([]byte, error) {
db.mu.Lock()
defer db.mu.Unlock()
return db.tree.Commit()
}
// GetProof returns a proof for the given key and version.
func (db *Database) GetProof(version uint64, key []byte) (*ics23.CommitmentProof, error) {
db.mu.Lock()
defer db.mu.Unlock()
return db.tree.GetProof(version, key)
}
// GetLatestVersion returns the latest version of the database.
func (db *Database) GetLatestVersion() uint64 {
db.mu.Lock()
defer db.mu.Unlock()
return db.tree.GetLatestVersion()
}
// Close closes the database and releases all resources.
func (db *Database) Close() error {
db.mu.Lock()
defer db.mu.Unlock()
return db.tree.Close()
}