-
Notifications
You must be signed in to change notification settings - Fork 33
[WIP] Enrich block bodies to contain Peras certificates #1722
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
Draft
Parent:
Peras staging ground
agustinmista
wants to merge
3
commits into
peras-staging
Choose a base branch
from
peras/enrich-shelley-blocks-with-certs
base: peras-staging
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.
Draft
Changes from all commits
Commits
Show all changes
3 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
Binary file added
BIN
+1.75 KB
...s-consensus-cardano/golden/cardano/CardanoNodeToNodeVersion2/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+856 Bytes
...-consensus-cardano/golden/cardano/CardanoNodeToNodeVersion2/Header_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.75 KB
...no/golden/cardano/QueryVersion2/CardanoNodeToClientVersion12/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.75 KB
...no/golden/cardano/QueryVersion2/CardanoNodeToClientVersion13/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.75 KB
...no/golden/cardano/QueryVersion2/CardanoNodeToClientVersion14/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.75 KB
...no/golden/cardano/QueryVersion2/CardanoNodeToClientVersion15/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.75 KB
...no/golden/cardano/QueryVersion3/CardanoNodeToClientVersion16/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.75 KB
...no/golden/cardano/QueryVersion3/CardanoNodeToClientVersion17/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.75 KB
...no/golden/cardano/QueryVersion3/CardanoNodeToClientVersion18/Block_Dijkstra_WithPerasCert
Binary file not shown.
Binary file added
BIN
+1.74 KB
ouroboros-consensus-cardano/golden/cardano/disk/Block_Dijkstra_WithPerasCert
Binary file not shown.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| {-# LANGUAGE GADTs #-} | ||
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
| {-# LANGUAGE MultiParamTypeClasses #-} | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
| {-# LANGUAGE StandaloneDeriving #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
@@ -26,7 +27,10 @@ module Ouroboros.Consensus.Shelley.Ledger.Block | |
|
|
||
| -- * Shelley Compatibility | ||
| , ShelleyCompatible | ||
| , fromShelleyBlock | ||
| , toShelleyBlock | ||
| , mkShelleyBlock | ||
| , mkShelleyBlockWithPerasCert | ||
| , mkShelleyHeader | ||
|
|
||
| -- * Serialisation | ||
|
|
@@ -46,22 +50,31 @@ import Cardano.Ledger.Binary | |
| ( Annotator (..) | ||
| , DecCBOR (..) | ||
| , EncCBOR (..) | ||
| , EncCBORGroup (..) | ||
| , FullByteString (..) | ||
| , cborError | ||
| , decodeListLen | ||
| , encodeListLen | ||
| , fromPlainDecoder | ||
| , serialize | ||
| ) | ||
| import qualified Cardano.Ledger.Binary.Plain as Plain | ||
| import Cardano.Ledger.Core as SL | ||
| ( eraDecoder | ||
| ( EraBlockBody (..) | ||
| , eraDecoder | ||
| , eraProtVerLow | ||
| , toEraCBOR | ||
| ) | ||
| import qualified Cardano.Ledger.Core as SL (TranslationContext, hashBlockBody) | ||
| import qualified Cardano.Ledger.Core as SL (TranslationContext) | ||
| import Cardano.Ledger.Hashes (HASH) | ||
| import qualified Cardano.Ledger.Shelley.API as SL | ||
| import Cardano.Protocol.Crypto (Crypto) | ||
| import qualified Cardano.Protocol.TPraos.BHeader as SL | ||
| import Codec.Serialise (Serialise (..)) | ||
| import Control.Arrow (Arrow (..)) | ||
| import qualified Data.ByteString.Lazy as Lazy | ||
| import Data.Coerce (coerce) | ||
| import Data.Maybe.Strict (StrictMaybe (..)) | ||
| import Data.Typeable (Typeable) | ||
| import GHC.Generics (Generic) | ||
| import NoThunks.Class (NoThunks (..)) | ||
|
|
@@ -145,7 +158,9 @@ instance ShelleyCompatible proto era => ConvertRawHash (ShelleyBlock proto era) | |
| -- | ||
| -- This block is parametrised over both the (ledger) era and the protocol. | ||
| data ShelleyBlock proto era = ShelleyBlock | ||
| { shelleyBlockRaw :: !(SL.Block (ShelleyProtocolHeader proto) era) | ||
| { shelleyBlockHeader :: !(ShelleyProtocolHeader proto) | ||
| , shelleyBlockBody :: !(SL.BlockBody era) | ||
| , shelleyBlockPerasCert :: !(StrictMaybe (PerasCert (ShelleyBlock proto era))) | ||
| , shelleyBlockHeaderHash :: !ShelleyHash | ||
| } | ||
|
|
||
|
|
@@ -158,14 +173,46 @@ instance | |
|
|
||
| type instance HeaderHash (ShelleyBlock proto era) = ShelleyHash | ||
|
|
||
| -- | Reconstruct a Shelley ledger block from a 'ShelleyBlock'. | ||
| -- | ||
| -- TODO: we should be able to avoid this conversion in most cases | ||
| fromShelleyBlock :: ShelleyBlock proto era -> SL.Block (ShelleyProtocolHeader proto) era | ||
| fromShelleyBlock blk = SL.Block (shelleyBlockHeader blk) (shelleyBlockBody blk) | ||
|
|
||
| -- | Construct a 'ShelleyBlock' from a Shelley ledger block. | ||
| -- | ||
| -- TODO: we should be able to avoid this conversion in most cases | ||
| toShelleyBlock :: | ||
| ShelleyCompatible proto era => SL.Block (ShelleyProtocolHeader proto) era -> ShelleyBlock proto era | ||
| toShelleyBlock (SL.Block hdr body) = mkShelleyBlock hdr body | ||
|
|
||
| mkShelleyBlock :: | ||
| ShelleyCompatible proto era => | ||
| SL.Block (ShelleyProtocolHeader proto) era -> | ||
| ShelleyProtocolHeader proto -> | ||
| SL.BlockBody era -> | ||
| ShelleyBlock proto era | ||
| mkShelleyBlock raw = | ||
| mkShelleyBlock = mkShelleyBlockGeneric SNothing | ||
|
|
||
| mkShelleyBlockWithPerasCert :: | ||
| ShelleyCompatible proto era => | ||
| PerasCert (ShelleyBlock proto era) -> | ||
| ShelleyProtocolHeader proto -> | ||
| SL.BlockBody era -> | ||
| ShelleyBlock proto era | ||
| mkShelleyBlockWithPerasCert = mkShelleyBlockGeneric . SJust | ||
|
|
||
| mkShelleyBlockGeneric :: | ||
| ShelleyCompatible proto era => | ||
| StrictMaybe (PerasCert (ShelleyBlock proto era)) -> | ||
| ShelleyProtocolHeader proto -> | ||
| BlockBody era -> | ||
| ShelleyBlock proto era | ||
| mkShelleyBlockGeneric cert header body = | ||
| ShelleyBlock | ||
| { shelleyBlockRaw = raw | ||
| , shelleyBlockHeaderHash = pHeaderHash $ SL.bheader raw | ||
| { shelleyBlockHeader = header | ||
| , shelleyBlockBody = body | ||
| , shelleyBlockPerasCert = cert | ||
| , shelleyBlockHeaderHash = pHeaderHash header | ||
| } | ||
|
|
||
| class | ||
|
|
@@ -198,10 +245,10 @@ instance | |
| ShowProxy (Header (ShelleyBlock proto era)) | ||
|
|
||
| instance ShelleyCompatible proto era => GetHeader (ShelleyBlock proto era) where | ||
| getHeader (ShelleyBlock rawBlk hdrHash) = | ||
| getHeader block = | ||
| ShelleyHeader | ||
| { shelleyHeaderRaw = SL.bheader rawBlk | ||
| , shelleyHeaderHash = hdrHash | ||
| { shelleyHeaderRaw = shelleyBlockHeader block | ||
| , shelleyHeaderHash = shelleyBlockHeaderHash block | ||
| } | ||
|
|
||
| blockMatchesHeader hdr blk = | ||
|
|
@@ -210,7 +257,7 @@ instance ShelleyCompatible proto era => GetHeader (ShelleyBlock proto era) where | |
| SL.hashBlockBody blockBody == pHeaderBodyHash shelleyHdr | ||
| where | ||
| ShelleyHeader{shelleyHeaderRaw = shelleyHdr} = hdr | ||
| ShelleyBlock{shelleyBlockRaw = SL.Block _ blockBody} = blk | ||
| ShelleyBlock{shelleyBlockBody = blockBody} = blk | ||
|
|
||
| headerIsEBB = const Nothing | ||
|
|
||
|
|
@@ -288,10 +335,35 @@ instance HasNestedContent f (ShelleyBlock proto era) | |
|
|
||
| instance ShelleyCompatible proto era => EncCBOR (ShelleyBlock proto era) where | ||
| -- Don't encode the header hash, we recompute it during deserialisation | ||
| encCBOR = encCBOR . shelleyBlockRaw | ||
| encCBOR block = do | ||
| let header = shelleyBlockHeader block | ||
| let body = shelleyBlockBody block | ||
| let bodyLen = listLen body | ||
| case shelleyBlockPerasCert block of | ||
| SNothing -> | ||
| encodeListLen (1 + bodyLen) | ||
| <> encCBOR header | ||
| <> encCBORGroup body | ||
| SJust cert -> | ||
| encodeListLen (1 + bodyLen + 1) | ||
| <> encCBOR header | ||
| <> encCBORGroup body | ||
| <> encCBOR (encode cert) | ||
|
|
||
| instance ShelleyCompatible proto era => DecCBOR (Annotator (ShelleyBlock proto era)) where | ||
| decCBOR = fmap mkShelleyBlock <$> decCBOR | ||
| decCBOR = do | ||
| len <- decodeListLen | ||
| header <- decCBOR | ||
| body <- decCBOR | ||
| cert <- decMaybeCertOrFail len | ||
| pure $ mkShelleyBlockGeneric <$> cert <*> header <*> body | ||
| where | ||
| bodyLen = fromIntegral (numSegComponents @era) | ||
|
|
||
| decMaybeCertOrFail len | ||
| | len == 1 + bodyLen = pure <$> pure SNothing | ||
| | len == 1 + bodyLen + 1 = pure <$> (SJust <$> fromPlainDecoder decode) | ||
| | otherwise = cborError $ Plain.DecoderErrorCustom "ShelleyBlock" "invalid number of elements" | ||
agustinmista marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| instance ShelleyCompatible proto era => EncCBOR (Header (ShelleyBlock proto era)) where | ||
| -- Don't encode the header hash, we recompute it during deserialisation | ||
|
|
@@ -342,7 +414,7 @@ decodeShelleyHeader = eraDecoder @era $ (. Full) . runAnnotator <$> decCBOR | |
| -------------------------------------------------------------------------------} | ||
|
|
||
| instance ShelleyCompatible proto era => Condense (ShelleyBlock proto era) where | ||
| condense = show . shelleyBlockRaw | ||
| condense = show . ((shelleyBlockHeader &&& shelleyBlockBody) &&& shelleyBlockPerasCert) | ||
|
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. We might want to tweak this instance. |
||
|
|
||
| instance ShelleyCompatible proto era => Condense (Header (ShelleyBlock proto era)) where | ||
| condense = show . shelleyHeaderRaw | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
We could also just store an
SL.Block, but encode it differently. But either way, this conversion should be super cheap