badger

package
v0.43.1-access-memory-... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2025 License: AGPL-3.0 Imports: 26 Imported by: 20

Documentation

Index

Constants

View Source
const DefaultCacheSize = uint(1000)

Variables

This section is empty.

Functions

func EnsureBadgerFolder added in v0.43.0

func EnsureBadgerFolder(dataDir string) error

EnsureBadgerFolder ensures the given directory is either empty (including does not exist), or is a valid Badger folder. It returns an error if the directory exists and is not a Badger folder.

func InitAll added in v0.12.0

func InitAll(metrics module.CacheMetrics, db *badger.DB) *storage.All

func InitPublic added in v0.22.4

func InitPublic(opts badger.Options) (*badger.DB, error)

InitPublic initializes a public database by checking and setting the database type marker. If an existing, inconsistent type marker is set, this method will return an error. Once a database type marker has been set using these methods, the type cannot be changed.

func InitSecret added in v0.22.4

func InitSecret(opts badger.Options) (*badger.DB, error)

InitSecret initializes a secrets database by checking and setting the database type marker. If an existing, inconsistent type marker is set, this method will return an error. Once a database type marker has been set using these methods, the type cannot be changed.

func IsBadgerFolder added in v0.43.0

func IsBadgerFolder(dataDir string) (bool, error)

func SafeOpen added in v0.43.0

func SafeOpen(opts badger.Options) (*badger.DB, error)

SafeOpen opens a Badger database with the provided options, ensuring that the directory is a valid Badger folder. If the directory is not valid, it returns an error. This is useful to prevent accidental opening of a non-Badger (pebble) directory as a Badger database, which could wipe out the existing data.

Types

type Batch added in v0.15.1

type Batch struct {
	// contains filtered or unexported fields
}

func NewBatch added in v0.15.1

func NewBatch(db BatchBuilder) *Batch

func (*Batch) Flush added in v0.15.1

func (b *Batch) Flush() error

Flush will call the badger Batch's Flush method, in addition, it will call the callbacks added by OnSucceed

func (*Batch) GetWriter added in v0.15.1

func (b *Batch) GetWriter() *badger.WriteBatch

func (*Batch) OnSucceed added in v0.15.1

func (b *Batch) OnSucceed(callback func())

OnSucceed adds a callback to execute after the batch has been successfully flushed. useful for implementing the cache where we will only cache after the batch has been successfully flushed

type BatchBuilder added in v0.32.0

type BatchBuilder interface {
	NewWriteBatch() *badger.WriteBatch
}

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) func(*badger.Txn) (V, error)

Get will try to retrieve the resource from cache first, and then from the injected. During normal operations, the following error returns are expected:

func (*Cache[K, V]) Insert added in v0.16.1

func (c *Cache[K, V]) Insert(key K, resource V)

Insert will add a resource directly to the cache with the given ID

func (*Cache[K, V]) IsCached added in v0.31.0

func (c *Cache[K, V]) IsCached(key K) bool

IsCached returns true if the key exists in the cache. It DOES NOT check whether the key exists in the underlying data store.

func (*Cache[K, V]) PutTx added in v0.17.1

func (c *Cache[K, V]) PutTx(key K, resource V) func(*transaction.Tx) error

PutTx will return tx which adds a resource to the cache with the given ID.

Error returns: (Note actual errors depend on the specific store function used)

  • storage.ErrAlreadyExists if the key already exists in the database.
  • generic error in case of unexpected failure from the database layer or encoding failure.

func (*Cache[K, V]) Remove added in v0.16.1

func (c *Cache[K, V]) Remove(key K)

type Cleaner

type Cleaner struct {
	component.Component
	// contains filtered or unexported fields
}

Cleaner uses component.ComponentManager to implement module.Startable and module.ReadyDoneAware to run an internal goroutine which run badger value log garbage collection at a semi-regular interval. The Cleaner exists for 2 reasons:

  • Run GC frequently enough that each GC is relatively inexpensive
  • Avoid GC being synchronized across all nodes. Since in the happy path, all nodes have very similar database load patterns, without intervention they are likely to schedule GC at the same time, which can cause temporary consensus halts.

func NewCleaner

func NewCleaner(log zerolog.Logger, db *badger.DB, metrics module.CleanerMetrics, interval time.Duration) *Cleaner

NewCleaner returns a cleaner that runs the badger value log garbage collection once every `interval` duration if an interval of zero is passed in, we will not run the GC at all.

type RecoverablePrivateBeaconKeyStateMachine added in v0.39.0

type RecoverablePrivateBeaconKeyStateMachine struct {
	// contains filtered or unexported fields
}

RecoverablePrivateBeaconKeyStateMachine stores state information about in-progress and completed DKGs, including computed keys. Must be instantiated using secrets database. On the happy path, each consensus committee member takes part in the DKG, and after successfully finishing the DKG protocol it obtains a random beacon private key, which is stored in the database along with DKG state flow.DKGStateCompleted. If for any reason the DKG fails, then the private key will be nil and DKG state is set to flow.DKGStateFailure. When the epoch recovery takes place, we need to query the last valid beacon private key for the current replica and also set it for use during the Recovery Epoch, otherwise replicas won't be able to vote for blocks during the Recovery Epoch. CAUTION: This implementation heavily depends on atomic Badger transactions with interleaved reads and writes for correctness.

func NewRecoverableRandomBeaconStateMachine added in v0.39.0

func NewRecoverableRandomBeaconStateMachine(collector module.CacheMetrics, db *badger.DB, myNodeID flow.Identifier) (*RecoverablePrivateBeaconKeyStateMachine, error)

NewRecoverableRandomBeaconStateMachine returns the RecoverablePrivateBeaconKeyStateMachine implementation backed by Badger DB. No errors are expected during normal operations.

func (*RecoverablePrivateBeaconKeyStateMachine) CommitMyBeaconPrivateKey added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) CommitMyBeaconPrivateKey(epochCounter uint64, commit *flow.EpochCommit) error

CommitMyBeaconPrivateKey commits the previously inserted random beacon private key for an epoch. Effectively, this method transitions the state machine into the flow.RandomBeaconKeyCommitted state if the current state is flow.DKGStateCompleted. The caller needs to supply the flow.EpochCommit as evidence that the stored key is valid for the specified epoch. Repeated calls for the same epoch are accepted (idempotent operation), if and only if the provided EpochCommit confirms the already committed key. No errors are expected during normal operations.

func (*RecoverablePrivateBeaconKeyStateMachine) GetDKGState added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) GetDKGState(epochCounter uint64) (flow.DKGState, error)

GetDKGState retrieves the current state of the state machine for the given epoch. If an error is returned, the state is undefined meaning that state machine is in initial state Error returns:

  • storage.ErrNotFound - if there is no state stored for given epoch, meaning the state machine is in initial state.

func (*RecoverablePrivateBeaconKeyStateMachine) InsertMyBeaconPrivateKey added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) InsertMyBeaconPrivateKey(epochCounter uint64, key crypto.PrivateKey) error

InsertMyBeaconPrivateKey stores the random beacon private key for an epoch and transitions the state machine into the flow.DKGStateCompleted state.

CAUTION: these keys are stored before they are validated against the canonical key vector and may not be valid for use in signing. Use storage.SafeBeaconKeys interface to guarantee only keys safe for signing are returned. Error returns:

func (*RecoverablePrivateBeaconKeyStateMachine) IsDKGStarted added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) IsDKGStarted(epochCounter uint64) (bool, error)

IsDKGStarted checks whether the DKG has been started for the given epoch. No errors expected during normal operation.

func (*RecoverablePrivateBeaconKeyStateMachine) RetrieveMyBeaconPrivateKey added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) RetrieveMyBeaconPrivateKey(epochCounter uint64) (key crypto.PrivateKey, safe bool, err error)

RetrieveMyBeaconPrivateKey retrieves my beacon private key for the given epoch, only if my key has been confirmed valid and safe for use.

Returns:

  • (key, true, nil) if the key is present and confirmed valid
  • (nil, false, nil) if the key has been marked invalid or unavailable -> no beacon key will ever be available for the epoch in this case
  • (nil, false, storage.ErrNotFound) if the DKG has not ended
  • (nil, false, error) for any unexpected exception

func (*RecoverablePrivateBeaconKeyStateMachine) SetDKGState added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) SetDKGState(epochCounter uint64, newState flow.DKGState) error

SetDKGState performs a state transition for the Random Beacon Recoverable State Machine. Some state transitions may not be possible using this method. For instance, we might not be able to enter flow.DKGStateCompleted state directly from flow.DKGStateStarted, even if such transition is valid. The reason for this is that some states require additional data to be processed by the state machine before the transition can be made. For such cases there are dedicated methods that should be used, ex. InsertMyBeaconPrivateKey and UpsertMyBeaconPrivateKey, which allow to store the needed data and perform the transition in one atomic operation. Error returns:

func (*RecoverablePrivateBeaconKeyStateMachine) UnsafeRetrieveMyBeaconPrivateKey added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) UnsafeRetrieveMyBeaconPrivateKey(epochCounter uint64) (crypto.PrivateKey, error)

UnsafeRetrieveMyBeaconPrivateKey retrieves the random beacon private key for an epoch.

CAUTION: these keys were stored before they are validated against the canonical key vector and may not be valid for use in signing. Use storage.SafeBeaconKeys interface to guarantee only keys safe for signing are returned Error returns:

func (*RecoverablePrivateBeaconKeyStateMachine) UpsertMyBeaconPrivateKey added in v0.39.0

func (ds *RecoverablePrivateBeaconKeyStateMachine) UpsertMyBeaconPrivateKey(epochCounter uint64, key crypto.PrivateKey, commit *flow.EpochCommit) error

UpsertMyBeaconPrivateKey overwrites the random beacon private key for the epoch that recovers the protocol from Epoch Fallback Mode. The resulting state of this method call is flow.RandomBeaconKeyCommitted. State transitions are allowed if and only if the current state is not equal to flow.RandomBeaconKeyCommitted. Repeated calls for the same epoch are idempotent, if and only if the provided EpochCommit confirms the already committed key (error otherwise). No errors are expected during normal operations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL