ouroboros-consensus-0.1.0.0: Consensus layer for the Ouroboros blockchain protocol
Safe HaskellNone
LanguageHaskell2010

Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Synopsis

Opening the database

initLedgerDB Source #

Arguments

:: forall m blk. (IOLike m, LedgerSupportsProtocol blk, InspectLedger blk, HasCallStack) 
=> Tracer m (TraceReplayEvent blk ()) 
-> Tracer m (TraceEvent blk) 
-> SomeHasFS m 
-> (forall s. Decoder s (ExtLedgerState blk)) 
-> (forall s. Decoder s (RealPoint blk)) 
-> LedgerDbParams 
-> ExtLedgerCfg blk 
-> m (ExtLedgerState blk)

Genesis ledger state

-> StreamAPI m blk 
-> m (InitLog blk, LedgerDB' blk, Word64) 

Initialize the ledger DB from the most recent snapshot on disk

If no such snapshot can be found, use the genesis ledger DB. Returns the initialized DB as well as the block reference corresponding to the snapshot we found on disk (the latter primarily for testing/monitoring purposes).

We do not catch any exceptions thrown during streaming; should any be thrown, it is the responsibility of the ChainDB to catch these and trigger (further) validation. We only discard snapshots if

  • We cannot deserialise them, or
  • they are ahead of the chain

It is possible that the Ledger DB will not be able to roll back k blocks after initialization if the chain has been truncated (data corruption).

data InitLog blk Source #

Initialization log

The initialization log records which snapshots from disk were considered, in which order, and why some snapshots were rejected. It is primarily useful for monitoring purposes.

Constructors

InitFromGenesis

Defaulted to initialization from genesis

NOTE: Unless the blockchain is near genesis, we should see this only if data corrupted occurred.

InitFromSnapshot DiskSnapshot (Point blk)

Used a snapshot corresponding to the specified tip

InitFailure DiskSnapshot (InitFailure blk) (InitLog blk)

Initialization skipped a snapshot

We record the reason why it was skipped.

NOTE: We should only see this if data corrupted occurred.

Instances

Instances details
StandardHash blk => Eq (InitLog blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

(==) :: InitLog blk -> InitLog blk -> Bool #

(/=) :: InitLog blk -> InitLog blk -> Bool #

StandardHash blk => Show (InitLog blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

showsPrec :: Int -> InitLog blk -> ShowS #

show :: InitLog blk -> String #

showList :: [InitLog blk] -> ShowS #

Generic (InitLog blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Associated Types

type Rep (InitLog blk) :: Type -> Type #

Methods

from :: InitLog blk -> Rep (InitLog blk) x #

to :: Rep (InitLog blk) x -> InitLog blk #

type Rep (InitLog blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

data InitFailure blk Source #

Constructors

InitFailureRead ReadIncrementalErr

We failed to deserialise the snapshot

This can happen due to data corruption in the ledger DB.

InitFailureTooRecent (Point blk)

This snapshot is too recent (ahead of the tip of the chain)

Instances

Instances details
StandardHash blk => Eq (InitFailure blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

(==) :: InitFailure blk -> InitFailure blk -> Bool #

(/=) :: InitFailure blk -> InitFailure blk -> Bool #

StandardHash blk => Show (InitFailure blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

showsPrec :: Int -> InitFailure blk -> ShowS #

show :: InitFailure blk -> String #

showList :: [InitFailure blk] -> ShowS #

Generic (InitFailure blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Associated Types

type Rep (InitFailure blk) :: Type -> Type #

Methods

from :: InitFailure blk -> Rep (InitFailure blk) x #

to :: Rep (InitFailure blk) x -> InitFailure blk #

type Rep (InitFailure blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

type Rep (InitFailure blk) = D1 ('MetaData "InitFailure" "Ouroboros.Consensus.Storage.LedgerDB.OnDisk" "ouroboros-consensus-0.1.0.0-GfJNvFcM6lj2s5utKAUPEp" 'False) (C1 ('MetaCons "InitFailureRead" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ReadIncrementalErr)) :+: C1 ('MetaCons "InitFailureTooRecent" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Point blk))))

Instantiate in-memory to blk

Abstraction over the stream API

data NextBlock blk Source #

Next block returned during streaming

Constructors

NoMoreBlocks 
NextBlock blk 

data StreamAPI m blk Source #

Stream blocks from the immutable DB

When we initialize the ledger DB, we try to find a snapshot close to the tip of the immutable DB, and then stream blocks from the immutable DB to its tip to bring the ledger up to date with the tip of the immutable DB.

In CPS form to enable the use of withXYZ style iterator init functions.

Constructors

StreamAPI 

Fields

Write to disk

takeSnapshot :: forall m blk. MonadThrow m => Tracer m (TraceEvent blk) -> SomeHasFS m -> (ExtLedgerState blk -> Encoding) -> (RealPoint blk -> Encoding) -> LedgerDB' blk -> m (DiskSnapshot, Point blk) Source #

Take a snapshot of the oldest ledger state in the ledger DB

We write the oldest ledger state to disk because the intention is to only write ledger states to disk that we know to be immutable. Primarily for testing purposes, takeSnapshot returns the block reference corresponding to the snapshot that we wrote.

NOTE: This is a lower-level API that unconditionally takes a snapshot (i.e., independent from whether this snapshot corresponds to a state that is more than k back).

TODO: Should we delete the file if an error occurs during writing?

trimSnapshots :: Monad m => Tracer m (TraceEvent r) -> SomeHasFS m -> DiskPolicy -> m [DiskSnapshot] Source #

Trim the number of on disk snapshots so that at most onDiskNumSnapshots snapshots are stored on disk. The oldest snapshots are deleted.

The deleted snapshots are returned.

Low-level API (primarily exposed for testing)

data DiskSnapshot Source #

On disk snapshots are numbered monotonically

Instances

Instances details
Eq DiskSnapshot Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Ord DiskSnapshot Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Show DiskSnapshot Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Generic DiskSnapshot Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Associated Types

type Rep DiskSnapshot :: Type -> Type #

type Rep DiskSnapshot Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

type Rep DiskSnapshot = D1 ('MetaData "DiskSnapshot" "Ouroboros.Consensus.Storage.LedgerDB.OnDisk" "ouroboros-consensus-0.1.0.0-GfJNvFcM6lj2s5utKAUPEp" 'True) (C1 ('MetaCons "DiskSnapshot" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)))

deleteSnapshot :: HasCallStack => SomeHasFS m -> DiskSnapshot -> m () Source #

Delete snapshot from disk

Trace events

data TraceEvent blk Source #

Constructors

InvalidSnapshot DiskSnapshot (InitFailure blk)

An on disk snapshot was skipped because it was invalid.

TookSnapshot DiskSnapshot (Point blk)

A snapshot was written to disk.

DeletedSnapshot DiskSnapshot

An old or invalid on-disk snapshot was deleted

Instances

Instances details
StandardHash blk => Eq (TraceEvent blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

(==) :: TraceEvent blk -> TraceEvent blk -> Bool #

(/=) :: TraceEvent blk -> TraceEvent blk -> Bool #

StandardHash blk => Show (TraceEvent blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

showsPrec :: Int -> TraceEvent blk -> ShowS #

show :: TraceEvent blk -> String #

showList :: [TraceEvent blk] -> ShowS #

Generic (TraceEvent blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Associated Types

type Rep (TraceEvent blk) :: Type -> Type #

Methods

from :: TraceEvent blk -> Rep (TraceEvent blk) x #

to :: Rep (TraceEvent blk) x -> TraceEvent blk #

type Rep (TraceEvent blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

data TraceReplayEvent blk replayTo Source #

Events traced while replaying blocks against the ledger to bring it up to date w.r.t. the tip of the ImmutableDB during initialisation. As this process takes a while, we trace events to inform higher layers of our progress.

The replayTo parameter is meant to be filled in by a higher layer, i.e., the ChainDB.

Constructors

ReplayFromGenesis replayTo

There were no LedgerDB snapshots on disk, so we're replaying all blocks starting from Genesis against the initial ledger.

The replayTo parameter corresponds to the block at the tip of the ImmutableDB, i.e., the last block to replay.

ReplayFromSnapshot DiskSnapshot (Point blk) replayTo

There was a LedgerDB snapshot on disk corresponding to the given tip. We're replaying more recent blocks against it.

The replayTo parameter corresponds to the block at the tip of the ImmutableDB, i.e., the last block to replay.

ReplayedBlock (RealPoint blk) [LedgerEvent blk] replayTo

We replayed the given block (reference) on the genesis snapshot during the initialisation of the LedgerDB.

The blockInfo parameter corresponds replayed block and the replayTo parameter corresponds to the block at the tip of the ImmutableDB, i.e., the last block to replay.

Instances

Instances details
Functor (TraceReplayEvent blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

fmap :: (a -> b) -> TraceReplayEvent blk a -> TraceReplayEvent blk b #

(<$) :: a -> TraceReplayEvent blk b -> TraceReplayEvent blk a #

Foldable (TraceReplayEvent blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

fold :: Monoid m => TraceReplayEvent blk m -> m #

foldMap :: Monoid m => (a -> m) -> TraceReplayEvent blk a -> m #

foldMap' :: Monoid m => (a -> m) -> TraceReplayEvent blk a -> m #

foldr :: (a -> b -> b) -> b -> TraceReplayEvent blk a -> b #

foldr' :: (a -> b -> b) -> b -> TraceReplayEvent blk a -> b #

foldl :: (b -> a -> b) -> b -> TraceReplayEvent blk a -> b #

foldl' :: (b -> a -> b) -> b -> TraceReplayEvent blk a -> b #

foldr1 :: (a -> a -> a) -> TraceReplayEvent blk a -> a #

foldl1 :: (a -> a -> a) -> TraceReplayEvent blk a -> a #

toList :: TraceReplayEvent blk a -> [a] #

null :: TraceReplayEvent blk a -> Bool #

length :: TraceReplayEvent blk a -> Int #

elem :: Eq a => a -> TraceReplayEvent blk a -> Bool #

maximum :: Ord a => TraceReplayEvent blk a -> a #

minimum :: Ord a => TraceReplayEvent blk a -> a #

sum :: Num a => TraceReplayEvent blk a -> a #

product :: Num a => TraceReplayEvent blk a -> a #

Traversable (TraceReplayEvent blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

traverse :: Applicative f => (a -> f b) -> TraceReplayEvent blk a -> f (TraceReplayEvent blk b) #

sequenceA :: Applicative f => TraceReplayEvent blk (f a) -> f (TraceReplayEvent blk a) #

mapM :: Monad m => (a -> m b) -> TraceReplayEvent blk a -> m (TraceReplayEvent blk b) #

sequence :: Monad m => TraceReplayEvent blk (m a) -> m (TraceReplayEvent blk a) #

(StandardHash blk, InspectLedger blk, Eq replayTo) => Eq (TraceReplayEvent blk replayTo) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

(==) :: TraceReplayEvent blk replayTo -> TraceReplayEvent blk replayTo -> Bool #

(/=) :: TraceReplayEvent blk replayTo -> TraceReplayEvent blk replayTo -> Bool #

(StandardHash blk, InspectLedger blk, Show replayTo) => Show (TraceReplayEvent blk replayTo) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Methods

showsPrec :: Int -> TraceReplayEvent blk replayTo -> ShowS #

show :: TraceReplayEvent blk replayTo -> String #

showList :: [TraceReplayEvent blk replayTo] -> ShowS #

Generic (TraceReplayEvent blk replayTo) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk

Associated Types

type Rep (TraceReplayEvent blk replayTo) :: Type -> Type #

Methods

from :: TraceReplayEvent blk replayTo -> Rep (TraceReplayEvent blk replayTo) x #

to :: Rep (TraceReplayEvent blk replayTo) x -> TraceReplayEvent blk replayTo #

type Rep (TraceReplayEvent blk replayTo) Source # 
Instance details

Defined in Ouroboros.Consensus.Storage.LedgerDB.OnDisk