-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ChainDB: pass new block to ledger validation
In `ChainDB.addBlock`, we receive a new block and use it to try switch to a longer chain. This will require applying the block to the ledger, which requires reading the block from disk and parsing it again. This is right in the critical path of (bulk) chain sync. Since we have the block in memory, we can avoid the redundant read and pass it to the validation code directly in the form of a `BlockCache`. We can later use this cache for caching more/other blocks. Note that when switching to a fork or when we can extend the new chain with more blocks after the new block, we'll still have to read blocks from disk in order to validate them, but not the new block.
- Loading branch information
Showing
6 changed files
with
74 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
ouroboros-consensus/src/Ouroboros/Storage/ChainDB/Impl/BlockCache.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
-- | Cache blocks in memory | ||
-- | ||
-- Intended for qualified import. | ||
-- | ||
-- > import Ouroboros.Storage.ChainDB.Impl.BlockCache (BlockCache) | ||
-- > import qualified Ouroboros.Storage.ChainDB.Impl.BlockCache as BlockCache | ||
module Ouroboros.Storage.ChainDB.Impl.BlockCache | ||
( BlockCache -- opaque | ||
, empty | ||
, singleton | ||
, cacheBlock | ||
, lookup | ||
, toHeaderOrBlock | ||
) where | ||
|
||
import Prelude hiding (lookup) | ||
|
||
import Data.Map (Map) | ||
import qualified Data.Map as Map | ||
|
||
import Ouroboros.Network.Block (HasHeader (..), HeaderHash) | ||
|
||
import Ouroboros.Consensus.Block (Header, headerHash) | ||
|
||
|
||
newtype BlockCache blk = BlockCache (Map (HeaderHash blk) blk) | ||
|
||
empty :: BlockCache blk | ||
empty = BlockCache Map.empty | ||
|
||
singleton :: HasHeader blk => blk -> BlockCache blk | ||
singleton blk = cacheBlock blk empty | ||
|
||
cacheBlock :: HasHeader blk => blk -> BlockCache blk -> BlockCache blk | ||
cacheBlock blk (BlockCache cache) = BlockCache (Map.insert (blockHash blk) blk cache) | ||
|
||
lookup :: HasHeader blk => HeaderHash blk -> BlockCache blk -> Maybe blk | ||
lookup hash (BlockCache cache) = Map.lookup hash cache | ||
|
||
toHeaderOrBlock | ||
:: (HasHeader blk, HasHeader (Header blk)) | ||
=> Header blk -> BlockCache blk -> Either (Header blk) blk | ||
toHeaderOrBlock hdr blockCache | ||
| Just blk <- lookup (headerHash hdr) blockCache | ||
= Right blk | ||
| otherwise | ||
= Left hdr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters