ouroboros-consensus-0.18.0.0: Consensus layer for the Ouroboros blockchain protocol
Safe HaskellSafe-Inferred
LanguageHaskell2010

Ouroboros.Consensus.Storage.ImmutableDB.Impl.State

Synopsis

State types

data ImmutableDBEnv m blk Source #

The environment used by the immutable database.

Constructors

∀ h.Eq h ⇒ ImmutableDBEnv 

Fields

data InternalState m blk h Source #

Constructors

DbClosed 
DbOpen !(OpenState m blk h) 

Instances

Instances details
Generic (InternalState m blk h) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.ImmutableDB.Impl.State

Associated Types

type Rep (InternalState m blk h) ∷ TypeType #

Methods

fromInternalState m blk h → Rep (InternalState m blk h) x #

toRep (InternalState m blk h) x → InternalState m blk h #

StandardHash blk ⇒ NoThunks (InternalState m blk h) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.ImmutableDB.Impl.State

type Rep (InternalState m blk h) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.ImmutableDB.Impl.State

type Rep (InternalState m blk h) = D1 ('MetaData "InternalState" "Ouroboros.Consensus.Storage.ImmutableDB.Impl.State" "ouroboros-consensus-0.18.0.0-inplace" 'False) (C1 ('MetaCons "DbClosed" 'PrefixI 'False) (U1TypeType) :+: C1 ('MetaCons "DbOpen" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (OpenState m blk h))))

data OpenState m blk h Source #

Internal state when the database is open.

Constructors

OpenState 

Fields

Instances

Instances details
Generic (OpenState m blk h) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.ImmutableDB.Impl.State

Associated Types

type Rep (OpenState m blk h) ∷ TypeType #

Methods

fromOpenState m blk h → Rep (OpenState m blk h) x #

toRep (OpenState m blk h) x → OpenState m blk h #

StandardHash blk ⇒ NoThunks (OpenState m blk h) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.ImmutableDB.Impl.State

Methods

noThunksContextOpenState m blk h → IO (Maybe ThunkInfo) Source #

wNoThunksContextOpenState m blk h → IO (Maybe ThunkInfo) Source #

showTypeOfProxy (OpenState m blk h) → String Source #

type Rep (OpenState m blk h) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.ImmutableDB.Impl.State

type Rep (OpenState m blk h) = D1 ('MetaData "OpenState" "Ouroboros.Consensus.Storage.ImmutableDB.Impl.State" "ouroboros-consensus-0.18.0.0-inplace" 'False) (C1 ('MetaCons "OpenState" 'PrefixI 'True) (((S1 ('MetaSel ('Just "currentChunk") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ChunkNo) :*: S1 ('MetaSel ('Just "currentChunkOffset") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 BlockOffset)) :*: (S1 ('MetaSel ('Just "currentSecondaryOffset") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 SecondaryOffset) :*: S1 ('MetaSel ('Just "currentChunkHandle") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Handle h)))) :*: ((S1 ('MetaSel ('Just "currentPrimaryHandle") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Handle h)) :*: S1 ('MetaSel ('Just "currentSecondaryHandle") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Handle h))) :*: (S1 ('MetaSel ('Just "currentTip") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (WithOrigin (Tip blk))) :*: S1 ('MetaSel ('Just "currentIndex") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Index m blk h))))))

State helpers

type ModifyOpenState m blk h = StateT (OpenState m blk h) (WithTempRegistry (OpenState m blk h) m) Source #

Shorthand

cleanUpMonad m ⇒ HasFS m h → OpenState m blk h → m () Source #

Clean up the OpenState: closeOpenHandles + close the index (i.e., shut down its background thread)

closeOpenHandlesMonad m ⇒ HasFS m h → OpenState m blk h → m () Source #

Close the handles in the OpenState.

Idempotent, as closing a handle is idempotent.

getOpenState ∷ ∀ m blk. (HasCallStack, IOLike m, StandardHash blk, Typeable blk) ⇒ ImmutableDBEnv m blk → STM m (SomePair (HasFS m) (OpenState m blk)) Source #

Get the OpenState of the given database, throw a ClosedDBError in case it is closed.

NOTE: Since the OpenState is parameterized over a type parameter h of handles, which is not visible from the type of the ImmutableDBEnv, we return a SomePair here that returns the open state along with a HasFS instance for the same type parameter h. Note that it would be impossible to use an existing HasFS instance already in scope otherwise, since the h parameters would not be known to match.

mkOpenState ∷ ∀ m blk h. (HasCallStack, IOLike m, Eq h) ⇒ HasFS m h → Index m blk h → ChunkNoWithOrigin (Tip blk) → AllowExistingWithTempRegistry (OpenState m blk h) m (OpenState m blk h) Source #

Create the internal open state for the given chunk.

modifyOpenState ∷ ∀ m blk a. (HasCallStack, IOLike m, StandardHash blk, Typeable blk) ⇒ ImmutableDBEnv m blk → (∀ h. Eq h ⇒ HasFS m h → ModifyOpenState m blk h a) → m a Source #

Modify the internal state of an open database.

In case the database is closed, a ClosedDBError is thrown.

In case an ImmutableDBError is thrown, the database is closed to prevent further appending to a database in a potentially inconsistent state.

The action is run in the ModifyOpenState monad, which is a StateT transformer (of the OpenState) over the WithTempRegistry monad. This monad can be used to allocate resources in that will be transferred to the returned OpenState that is safely stored in the ImmutableDBEnv. This approach makes sure that no resources are leaked when an exception is thrown while running the action modifying the state.

TODO: update this comment Note: This takes the TMVar, then runs the action (which might be in IO), and then puts the TMVar back, just like modifyMVar does. Consequently, it has the same gotchas that modifyMVar does; the effects are observable and it is susceptible to deadlock.

withOpenState ∷ ∀ m blk r. (HasCallStack, IOLike m, StandardHash blk, Typeable blk) ⇒ ImmutableDBEnv m blk → (∀ h. HasFS m h → OpenState m blk h → m r) → m r Source #

Perform an action that accesses the internal state of an open database.

In case the database is closed, a ClosedDBError is thrown.

In case an ImmutableDBError is thrown while the action is being run, the database is closed to prevent further appending to a database in a potentially inconsistent state.