-
Notifications
You must be signed in to change notification settings - Fork 720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally support lightweight checkpointing #6048
Open
amesgen
wants to merge
2
commits into
master
Choose a base branch
from
amesgen/lightweight-checkpointing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
-- | Configuration for lightweight checkpointing. | ||
module Cardano.Node.Protocol.Checkpoints | ||
( CheckpointsReadError(..) | ||
, readCheckpointsMap | ||
) where | ||
|
||
import Cardano.Api | ||
|
||
import qualified Cardano.Crypto.Hash.Class as Crypto | ||
import Cardano.Ledger.Crypto (StandardCrypto) | ||
import Cardano.Node.Types | ||
import Ouroboros.Consensus.Block | ||
import Ouroboros.Consensus.Cardano | ||
import Ouroboros.Consensus.Config (CheckpointsMap (..), emptyCheckpointsMap) | ||
|
||
import Control.Exception (IOException) | ||
import Control.Monad (forM, unless, when) | ||
import qualified Data.Aeson as Aeson | ||
import qualified Data.Aeson.Types as Aeson | ||
import qualified Data.ByteString as BS | ||
import qualified Data.ByteString.Base16 as B16 | ||
import Data.Foldable (forM_) | ||
import qualified Data.Map.Strict as Map | ||
import Data.Set (Set) | ||
import qualified Data.Set as Set | ||
import qualified Data.Text as Text | ||
import qualified Data.Text.Encoding as Text | ||
|
||
data CheckpointsReadError = | ||
CheckpointsReadFileError !FilePath !IOException | ||
| CheckpointsHashMismatch | ||
!FilePath | ||
-- | Actual | ||
!CheckpointsHash | ||
-- | Expected | ||
!CheckpointsHash | ||
| CheckpointsDecodeError !FilePath !String | ||
deriving Show | ||
|
||
instance Error CheckpointsReadError where | ||
prettyError (CheckpointsReadFileError fp err) = | ||
"There was an error reading the checkpoints file: " | ||
<> pshow fp <> " Error: " <> pshow err | ||
|
||
prettyError (CheckpointsHashMismatch fp actual expected) = | ||
"Hash mismatch for checkpoints file " <> pshow fp <> ": " | ||
<> "the actual hash is " <> pshow actual <> ", but the expected " | ||
<> "hash given in the node configuration file is " <> pshow expected | ||
|
||
prettyError (CheckpointsDecodeError fp err) = | ||
"There was an error parsing the checkpoints file: " | ||
<> pshow fp <> " Error: " <> pshow err | ||
|
||
readCheckpointsMap | ||
:: NodeCheckpointsConfiguration | ||
-> ExceptT CheckpointsReadError IO (CheckpointsMap (CardanoBlock StandardCrypto)) | ||
readCheckpointsMap NodeCheckpointsConfiguration { | ||
npcCheckpointsFile, | ||
npcCheckpointsFileHash = mExpectedHash | ||
} = case npcCheckpointsFile of | ||
Nothing -> pure emptyCheckpointsMap | ||
Just (CheckpointsFile file) -> do | ||
content <- | ||
handleIOExceptT (CheckpointsReadFileError file) $ BS.readFile file | ||
|
||
let actualHash = CheckpointsHash $ Crypto.hashWith id content | ||
forM_ mExpectedHash $ \expectedHash -> | ||
when (actualHash /= expectedHash) $ | ||
throwError (CheckpointsHashMismatch file actualHash expectedHash) | ||
|
||
WrapCheckpointsMap checkpointsMap <- | ||
firstExceptT (CheckpointsDecodeError file) $ hoistEither $ | ||
Aeson.eitherDecodeStrict' content | ||
pure checkpointsMap | ||
|
||
newtype WrapCheckpointsMap = | ||
WrapCheckpointsMap (CheckpointsMap (CardanoBlock StandardCrypto)) | ||
|
||
instance Aeson.FromJSON WrapCheckpointsMap where | ||
parseJSON = Aeson.withObject "CheckpointsMap" $ \o -> do | ||
checkpointList :: [Aeson.Object] <- o Aeson..: "checkpoints" | ||
|
||
checkpoints :: [(BlockNo, HeaderHash (CardanoBlock StandardCrypto))] <- | ||
forM checkpointList $ \c -> do | ||
bno <- c Aeson..: "blockNo" | ||
hash <- parseCardanoHash =<< c Aeson..: "hash" | ||
pure (bno, hash) | ||
|
||
let duplicates :: Set BlockNo | ||
duplicates = | ||
Map.keysSet $ Map.filter (> 1) $ Map.fromListWith (+) $ | ||
(\(bno, _) -> (bno, 1 :: Int)) <$> checkpoints | ||
unless (Set.null duplicates) $ | ||
fail $ "Duplicate checkpoints for block numbers " | ||
<> show (Set.toList duplicates) | ||
|
||
pure $ WrapCheckpointsMap $ CheckpointsMap $ Map.fromList checkpoints | ||
where | ||
parseCardanoHash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe some genesis hash config field parser already has this logic somewhere? |
||
:: Aeson.Value | ||
-> Aeson.Parser (HeaderHash (CardanoBlock StandardCrypto)) | ||
parseCardanoHash = Aeson.withText "CheckpointHash" $ \t -> | ||
case B16.decode $ Text.encodeUtf8 t of | ||
Right h -> do | ||
when (BS.length h /= fromIntegral (hashSize p)) $ | ||
fail $ "Invalid hash size for " <> Text.unpack t | ||
pure $ fromRawHash p h | ||
Left e -> | ||
fail $ "Invalid base16 for " <> Text.unpack t <> ": " <> e | ||
where | ||
p = Proxy @(CardanoBlock StandardCrypto) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd consider a
{blockNoHashPairs: [[3, "abcdef"], [100, "32cdef"]]}
format, to avoid the obnoxious per-entry repetition of the field names. But I don't have a lot of experience regarding the widely-expected UX for this sort of thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah 👍, I have no strong opinions here; one tiny advantage of the current format is that it is impossible to forgot that the numbers are referring to block numbers (and not e.g. slot numbers), even when looking at individual checkpoints only; but that is not a major thing.